Work with nested objects is easy with a bubble-gum-tool.
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.
- actionCallback ⇒
*
Callback at the end of the loop
Example
const bubbleGumTools = require('bubble-gum-tools');
const nestedObj = {
root: [{
foo: 'bar',
}],
};
// get
const foo = bubbleGumTools.get(nestedObj, ['root', 0, 'foo']);
console.log(foo); // => 'bar'
// has
const existsFoo = bubbleGumTools.has(nestedObj, ['root', 0, 'foo']);
console.log(existsFoo); // => true
// set
bubbleGumTools.set(nestedObj, ['root', 0, 'foo'], 'newBar');
console.log(nestedObj); // => { root: [{ foo: 'newBar' }] }
// slice
const sObject = bubbleGumTools.slice(nestedObj, [{
path: ['root', 0, 'foo'],
newPath: ['newFoo'],
}]);
console.log(sObject); // => { newFoo: 'newBar' }
// create
const cObject = bubbleGumTools.create(['other-root', 0, 'other-foo'], 'other-bar');
console.log(cObject); // => { 'other-root': [{ 'other-foo': 'other-bar' }] }
// goto
const resultGOTO = bubbleGumTools.goto(['other-root', 0, 'other-foo'], ({ current, key }) => ({
[key]: current,
}))(cObject);
console.log(resultGOTO); // => { '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) ⇒
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 = require('bubble-gum-tools').create;
// create nested arrays
const nestedArray = create([0, 2, 0], 'bar');
console.log(nestedArray); // => [ [ , , [ 'bar' ] ] ]
// create nested objects
const nestedObject = create(['root', 'foo', 'bar'], 'bar');
console.log(nestedObject); // => { root: { foo: { bar: 'bar' } } }
// no defined value
const noDefaultVal = get(target, ['no', 'defined']);
console.log(noDefaultVal); // => undefined
// create both
const mixed = create([0, 'nested', 'key'], 'value');
console.log(mixed); // => [ { nested: { key: 'value' } } ]
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 = require('bubble-gum-tools').goto;
const target = {
root: {
foo: 'bar',
},
};
goto(['root', 'foo'], (result) => {
const {
indexPath,
previous,
target,
current,
key,
} = result;
console.log(indexPath); // => 1
console.log(previous); // => { foo: 'bar' }
console.log(target); // => { root: { foo: 'bar' }, arr: [ [ [Object] ] ] }
console.log(current); // => bar
console.log(key); // => foo
})(target);
const result = goto(['root', 'foo'], ({current, key}) => (current + '-' + key))(target);
console.log(result); // => bar-foo
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 = require('bubble-gum-tools').get;
const target = {
root: {
foo: 'bar',
},
arr: [[
['baz'],
]],
};
// Working with nested objects
const bar = get(target, ['root', 'foo']);
console.log(bar); // => 'bar'
// Working with nested arrays
const baz = get(target, ['arr', 0, 0, 0]);
console.log(baz); // => 'baz'
// Set a default
const defaultVal = get(target, ['no', 'defined'], 'default');
console.log(defaultVal); // => 'default'
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 = require('bubble-gum-tools').has;
const target1 = {
root: {
foo: 'bar',
},
arr: [[
['baz'],
]],
};
const existsBar = has(target1, ['root', 'foo']);
console.log(existsBar); // => true
const existsBaz = has(target1, ['arr', 0, 0, 0]);
console.log(existsBaz); // => true
const noExists = has(target1, ['no', 'defined']);
console.log(noExists); // => false
isStrict = false
const has = require('bubble-gum-tools').has;
const target = {
root: {
zero: 0,
null: null,
empty: '',
false: false,
},
};
const isNotStrict = false;
const noStrictZero = has(target, ['root', 'zero'], isNotStrict);
console.log(noStrictZero); // => false
const noStrictNull = has(target, ['root', 'null'], isNotStrict);
console.log(noStrictNull); // => false
const noStrictEmpty = has(target, ['root', 'empty'], isNotStrict);
console.log(noStrictEmpty); // => false
const noStrictFalse = has(target, ['root', 'false'], isNotStrict);
console.log(noStrictFalse); // => false
isStrict = true
const has = require('bubble-gum-tools').has;
const target = {
root: {
zero: 0,
null: null,
empty: '',
false: false,
},
};
const isStrict = true;
const strictZero = has(target, ['root', 'zero'], isStrict);
console.log(strictZero); // => true
const strictEmpty = has(target, ['root', 'empty'], isStrict);
console.log(strictEmpty); // => true
const strictNull = has(target, ['root', 'null'], isStrict);
console.log(strictNull); // => false
const strictFalse = has(target, ['root', 'false'], isStrict);
console.log(strictFalse); // => false
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 = require('bubble-gum-tools').set;
const target = {
root: {
foo: 'bar',
},
arr: [[
['baz'],
]],
};
set(target, ['root', 'foo'], 'newbar');
console.log(target.root.foo); // => 'newbar'
set(target, ['arr', 0, 0, 0], 'newbaz');
console.log(target.arr[0][0][0]); // => 'newbaz'
set(target, ['root', 'foo2'], 'foo2');
console.log(target.root.foo2); // => 'foo2'
set(target, ['arr', 0, 0, 1], 'newbaz2');
console.log(target.arr[0][0][1]); // => 'newbaz2'
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 = require('bubble-gum-tools').slice;
const target = {
root: { foo: 'bar' },
arr: [[['baz']]],
};
const sliced1 = slice(target, [{
path: ['root', 'foo']
}]);
console.log(sliced1); // => { root: { foo: 'bar' } }
const sliced2 = slice(target, [{
path: ['root', 'foo'],
newPath: ['bar'],
}]);
console.log(sliced2); // => { bar: 'bar' }
const sliced3 = slice(target, [{
path: ['root', 'foo'],
newPath: [0],
}]);
console.log(sliced3); // => { '0': 'bar' }
const sliced4 = slice(target, [{
path: ['arr', 0, 0, 0],
newPath: ['baz'],
}, {
path: ['root', 'foo'],
newPath: ['bar'],
}]);
console.log(sliced4); // => { baz: 'baz', bar: 'bar' }
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 |
- Add method to compare objects in depth
- Add method to clone objects in depth
MIT @ Nicolas Quiceno