object-breacher
TypeScript icon, indicating that this package has built-in type declarations

1.4.2 • Public • Published

Object Breacher

Almost the fastest library to get and set nested values in objects using a path.

Benchmarks

Benchmarked against:
  • lodash
  • @hapi/hoek
  • dot-prop
  • object-path
  • nested-property

Benchmarks

Checkout the script used to do this benchmark test

Install:

NPM:
npm install object-breacher
Yarn:
yarn add object-breacher

USAGE

Import:

import { get, set } from 'object-breacher';
// or
const { get, set } = require('object-breacher');

Get

Access nested value.

Params:

  • obj: Object
  • path: String | Array
  • options?: TGetOptions
    • defaultValue?: String
    • pathSeparator?: String
/* This can be any object */
let routes = {
  app: {
    root: '/app',
    home: {
      test: '/home/test',
    },
  },
};

const homeUrl = get(routes, 'app.home.test');

const homeUrl = get(routes, 'app#home#test', {
  defaultValue: '/';
  pathSeparator: '#';
});

Set

Change nested values dynamically.

Params:

  • obj: Object
  • path: String | Array
  • value: any
  • options?: TSetOptions
    • pathSeparator?: String
set(routes, 'app.home.test', 'YOUR VALUE HERE');

/* With Options */
set(routes, 'app/home/settings', '/settings', {
  pathSeparator: '/',
});

/* Not supplying an object will create a new object with the keys and value specified */
set('app.home.test', 'worked!'); // {app: {home: {test: 'worked!'}}}

/* Value can be of any type */
set(routes, 'wink.face', ['😉', ';-)']);

set(routes, 'auth', {
  login: {
    index: '/auth/login',
    facebook: '/auth/login/facebook',
  },
});

Package Sidebar

Install

npm i object-breacher

Weekly Downloads

0

Version

1.4.2

License

MIT

Unpacked Size

31.7 kB

Total Files

17

Last publish

Collaborators

  • a-maged