A lightweight (~1kb) set of basic operations defined on a plain js data structures (Object, Array, Map, Set). Provides two sets of operations for your choice: mutable or immutable.
npm install @sadbox/structure-ops
yarn add @sadbox/structure-ops
mutable
import { get } from '@sadbox/structure-ops';
const a = { b: 0 };
get(a, 'b'); // 0immutable
import { set } from '@sadbox/structure-ops/immutable';
const a = { b: 0 };
const b = set(a, 'b', 1); // { b: 1 }
console.log(a); // { b: 0 }
console.log(a === b); // falseCreates a new copy of the collection.
Arguments
- collection (
Object|Array|Map|Set) - The collection to copy.
Returns
- (
Object|Array|Map|Set) - New copy of the collection.
Example
const a = { b: 0 };
copy(a); // { b: 0 }Gets the value of the collection property.
Arguments
- collection (
Object|Array|Map|Set) - The original collection. - key (
*) - Key of the property to get. - [defaultValue] (
*) - The value returned if property does not exists.
Returns
- (
*) - Resolved value.
Example
const a = { b: 0 };
get(a, 'b'); // 0
get(a, 'c', 'ups'); // upsGets the value at the path of the collection.
Arguments
- collection (
Object|Array|Map|Set) - The original collection. - path (
*|Array<*>) - Path of the property to get. - [defaultValue] (
*) - The value returned if property does not exists.
Returns
- (
*) - Resolved value.
Example
const a = { b: { c: [0] } };
getIn(a, ['b', 'c', 0]); // 0
getIn(a, 'b.c[0]'); // 0
getIn(a, 'b.c.0'); // 0Check if key is a direct property of the collection.
Arguments
- collection (
Object|Array|Map|Set) - The original collection. - key (
*) - Key of the property to check.
Returns
- (
boolean) - Returnstrueif property exists, elsefalse.
Example
const a = { b: 0 };
has(a, 'b'); // true
has(a, 'c'); // falseCheck if path is a direct property of the collection.
Arguments
- collection (
Object|Array|Map|Set) - The original collection. - path (
*|Array<*>) - Path of the property to check.
Returns
- (
boolean) - Returnstrueif property exists, elsefalse.
Example
const a = { b: { c: [0] } };
hasIn(a, 'b.c[0]'); // trueMerges the original collection with sources.
Arguments
- collection (
Object|Array|Map|Set) - The original collection. - sources (
Object|Array|Map|Set) - One or more collections to merge in.
Returns
- (
Object|Array|Map|Set) - Mutablemergemutates and returns the source collection. Immutablemergereturns a new collection.
Example
const a = { b: 0, c: 2 };
merge(a, { b: 1 }, { d: 3 }); // { b: 1, c: 2, d: 3 }Merges the property at path of the original collection with sources.
Arguments
- collection (
Object|Array|Map|Set) - The original collection. - path (
*,Array<*>) - Path of the property to merge. - sources (
Object|Array|Map|Set) - One or more collections to merge in.
Returns
- (
Object|Array|Map|Set) - MutablemergeInmutates and returns the source collection. ImmutablemergeInreturns a new collection.
Example
const a = { b: { d: 0 } };
mergeIn(a, 'b', { c: 1 }); // { b: { d: 0, c: 1 } }Deeply merges the original collection with sources.
Arguments
- collection (
Object|Array|Map|Set) - The original collection. - sources (
Object|Array|Map|Set) - One or more collections to merge in.
Returns
- (
Object|Array|Map|Set) - MutablemergeInmutates and returns the source collection. ImmutablemergeInreturns a new collection.
Example
const a = { a: [{ b: 1 }] };
const b = { a: [{ c: 2 }] };
mergeDeep(a, b); // { a: [{ b: 1, c: 2 }] }Removes the property from the collection.
Arguments
- collection (
Object|Array|Map) - The original collection. - key (
*) - Key of the property to remove.
Returns
- (
Object|Array|Map) - Mutableremovemutates and returns the source collection. Immutableremovereturns a new collection.
Example
const a = { b: 0, c: 1 };
remove(a, 'b'); // { c: 1 }Removes the property at path from the collection.
Arguments
- collection (
Object|Array|Map) - The original collection. - path (
*|Array<*>) - Path of the property to remove.
Returns
- (
Object|Array|Map) - MutableremoveInmutates and returns the source collection. ImmutableremoveInreturns a new collection.
Example
const a = { b: { c: 0 } };
removeIn(a, 'b.c'); // { b: {} }Sets the value of property of the collection.
Arguments
- collection (
Object|Array|Map) - The original collection. - key (
*) - Key of the property to set. - value (
*) - The value to set.
Returns
- (
Object|Array|Map) - Mutablesetmutates and returns the source collection. Immutablesetreturns a new collection.
Example
const a = {};
set(a, 'b', 0); // { b: 0 }Sets the value at path of the collection.
Arguments
- collection (
Object|Array|Map) - The original collection. - path (
*,Array<*>) - Path of the property to set. - value (
*) - The value to set.
Returns
- (
Object|Array|Map) - MutablesetInmutates and returns the source collection. ImmutablesetInreturns a new collection.
Example
const a = {};
setIn(a, 'b.c', 0); // { b: { c: 0 } }Updates the value of property of the collection.
Arguments
- collection (
Object|Array|Map) - The original collection. - key (
*) - Key of the property to update. - updater (
Function<(value:*):*>) - The updater function that receive an old value and returns a new value.
Returns
- (
Object|Array|Map) - Mutableupdatemutates and returns the source collection. Immutableupdatereturns a new collection.
Example
const a = { b: [1, 2, 3] };
const updater = value => value.map(v => v ** 2);
update(a, 'b', updater); // { b: [1, 4, 9] }Updates the value of property at path of the collection.
Arguments
- collection (
Object|Array|Map) - The original collection. - path (
*|Array<*>) - Path of the property. - updater (
Function<(value:*):*>) - The updater function that receive an old value and returns a new value.
Returns
- (
Object|Array|Map) - MutableupdateInmutates and returns the source collection. ImmutableupdateInreturns a new collection.
Example
const a = { b: { c: 0 } };
const updater = value => [value];
updateIn(a, 'b.c', updater); // { b: c: [0] }