bubble-gum-tools
Work with nested objects is easy with a bubble-gum-tool.
Install
You can install bubble-gum-tools using npm.
npm install --save bubble-gum-tools
Or you can also get a modularized package per each method.
API Reference
Modules
Typedefs
- actionCallback ⇒
*
Callback at the end of the loop
bubble-gum-tools
Example
const bubbleGumTools = ; const nestedObj = root: foo: 'bar' ; // getconst foo = bubbleGumTools;console; // => 'bar' // hasconst existsFoo = bubbleGumTools;console; // => true // setbubbleGumTools;console; // => { root: [{ foo: 'newBar' }] } // sliceconst sObject = bubbleGumTools;console; // => { newFoo: 'newBar' } // createconst cObject = bubbleGumTools;console; // => { 'other-root': [{ 'other-foo': 'other-bar' }] } // gotoconst resultGOTO = bubbleGumToolscObject;console; // => { 'other-foo': 'other-bar' }
- bubble-gum-tools
- .create(path, initValue) ⇒
Object
|Array
- .goto(path, fn) ⇒
function
- .get(target, path, [defaultValue]) ⇒
*
- .has(target, path, [isStrict]) ⇒
Boolean
- .set(target, path, valueToSet)
- .slice(target, config) ⇒
Object
- .create(path, initValue) ⇒
Object
| Array
bubble-gum-tools.create(path, initValue) ⇒ It creates a new object or an initialized array depending on the input path
Kind: static method of bubble-gum-tools
Returns: Object
| Array
- output - The new array or new object with the input path structure
Param | Type | Description |
---|---|---|
path | Array |
Input path with the structure |
initValue | * |
Initial value for the end of the input path structure |
Example
const create = create; // create nested arrays const nestedArray = ; console; // => [ [ , , [ 'bar' ] ] ] // create nested objects const nestedObject = ; console; // => { root: { foo: { bar: 'bar' } } } // no defined value const noDefaultVal = ; console; // => undefined // create both const mixed = ; console; // => [ { nested: { key: 'value' } } ]
function
bubble-gum-tools.goto(path, fn) ⇒ It receives a input path and a callback(actionCallback), It returns the function _goto, the _goto function receives a target object or target array, when the _goto is called, this navigates the target object or target array using the input path, when it reaches the end of the path, _goto executs the callback and returns the result
Kind: static method of bubble-gum-tools
Param | Type | Description |
---|---|---|
path | Array |
Path to property |
fn | actionCallback |
Callback with the action that will be called at the end of the path |
Example
const goto = goto; const target = root: foo: 'bar' ; target; const result = target;console; // => bar-foo
*
bubble-gum-tools.get(target, path, [defaultValue]) ⇒ It gets a property from a nested object or a nested array using an array path
Kind: static method of bubble-gum-tools
Returns: *
- propertyValue
Param | Type | Description |
---|---|---|
target | Object | Array |
Target object or target array |
path | Array |
Path to property |
[defaultValue] | * |
Value to be returned in case the property does not exist |
Example
const get = get; const target = root: foo: 'bar' arr: 'baz' ; // Working with nested objects const bar = ; console; // => 'bar' // Working with nested arrays const baz = ; console; // => 'baz' // Set a default const defaultVal = ; console; // => 'default'
Boolean
bubble-gum-tools.has(target, path, [isStrict]) ⇒ It checks if the property exists in a nested object or a nested array using an array path
Kind: static method of bubble-gum-tools
Returns: Boolean
- exists - Returns if the property exists
Param | Type | Default | Description |
---|---|---|---|
target | Object | Array |
Target object or target array | |
path | Array |
Path to property | |
[isStrict] | Boolean |
false |
is strict |
Example
const has = has; const target1 = root: foo: 'bar' arr: 'baz' ; const existsBar = ; console; // => true const existsBaz = ; console; // => true const noExists = ; console; // => false
isStrict = false
const has = has; const target = root: zero: 0 null: null empty: '' false: false ; const isNotStrict = false; const noStrictZero = ; console; // => false const noStrictNull = ; console; // => false const noStrictEmpty = ; console; // => false const noStrictFalse = ; console; // => false
isStrict = true
const has = has; const target = root: zero: 0 null: null empty: '' false: false ; const isStrict = true; const strictZero = ; console; // => true const strictEmpty = ; console; // => true const strictNull = ; console; // => false const strictFalse = ; console; // => false
bubble-gum-tools.set(target, path, valueToSet)
It sets a new value in a nested object or a nested array using an array path, if the path does not exist create this
Kind: static method of bubble-gum-tools
Param | Type | Description |
---|---|---|
target | Object | Array |
Target object or target array |
path | Array |
Path to property |
valueToSet | * |
Value to set in target |
Example
const set = set; const target = root: foo: 'bar' arr: 'baz' ; ;console; // => 'newbar' ;console; // => 'newbaz' ;console; // => 'foo2' ;console; // => 'newbaz2'
Object
bubble-gum-tools.slice(target, config) ⇒ It slices a object or an array generating a new object or a new array
Kind: static method of bubble-gum-tools
Returns: Object
- slicedObject - New object or new array with sliced values
Param | Type | Description |
---|---|---|
target | Object | Array |
Target object or target array |
config | Array.Object |
Array with the configuration of the slice |
config[].path | Array |
Path to the property to be sliced |
config[].newPath | Array |
Path to sets a new value in the slicedObject, if this is not defined, this will have the same value as the config[].path |
Example
const slice = slice; const target = root: foo: 'bar' arr: 'baz'; const sliced1 = ;console; // => { root: { foo: 'bar' } } const sliced2 = ;console; // => { bar: 'bar' } const sliced3 = ;console; // => { '0': 'bar' } const sliced4 = ;console; // => { baz: 'baz', bar: 'bar' }
*
actionCallback ⇒ Callback at the end of the loop
Kind: global typedef
Param | Type | Description |
---|---|---|
options | Object |
Values in the end of the path |
options.indexPath | Number |
Current index in the array path |
options.target | Object | Array |
Target object or target array |
[options.current] | * |
Current value in target object |
[options.key] | * |
Current value in the path |
[options.previous] | * |
Previous value in target object |
TODOS
- Add method to compare objects in depth
- Add method to clone objects in depth
License
MIT @ Nicolas Quiceno