This is yet another library which provides operations over JavaScript objects, like deep compare and deep merge.
npm install @cleverbrush/deep
import deep from '@cleverbrush/deep';
// or import functions individually
import { deepEqual, deepExtend } from '@cleverbrush/deep';
Takes two objects as parameters, compares it recursively and returns true
if they are equal or false
in other case.
import { deepEqual } from '@cleverbrush/deep';
const equal1 = deepEqual({ a: { b: 1 } }, { a: { b: 1 } });
// equal1 === true
const equal2 = deepEqual({ a: { b: 1 } }, { a: { b: 20 } });
// equal2 === false
const equal3 = deepEqual({ a: { b: 1, c: 2 } }, { a: { b: 20 } });
// equal3 === false
Works similary to Object.extend
, but respects the deep structure of objects.
import { deepExtend } from '@cleverbrush/deep';
deepExtend(
{},
{
a: 'something',
name: {
first: 'Ivan'
}
},
{
name: {
last: 'Ivanov'
}
}
);
will return the following object:
{
a: 'something',
name: {
first: 'Ivan',
last: 'Ivanov'
}
}
Flattens an object to a single level. Accepts an optional separator as a second parameter.
If no separator is provided, the default separator is .
.
Supports only objects with string keys. Supports nested objects of any depth.
Throws an error if the object contains circular references.
import { deepFlatten } from '@cleverbrush/deep';
deepFlatten({
a: {
b: 1,
c: 2
},
d: 3
});
// => { 'a.b': 1, 'a.c': 2, d: 3 }
deepFlatten(
{
a: {
b: 1,
c: 2
},
d: {
e: {
f: 3
}
}
},
'-'
);
// => { 'a-b': 1, 'a-c': 2, 'd-e-f': 3 }
Generated using TypeDoc