@cleverbrush/deep
TypeScript icon, indicating that this package has built-in type declarations

1.1.7 • Public • Published

Deep operations on objects

This is yet another library which provides operations over JavaScript objects, like deep compare and deep merge.

Installation

npm install @cleverbrush/deep

Usage

import deep from '@cleverbrush/deep';
// or import functions individually
import { deepEqual, deepExtend } from '@cleverbrush/deep';

Functions

deepEqual

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

deepExtend

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'
        }
    }

deepFlatten

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 }

Package Sidebar

Install

npm i @cleverbrush/deep

Weekly Downloads

1

Version

1.1.7

License

BSD 3-Clause

Unpacked Size

12.7 kB

Total Files

12

Last publish

Collaborators

  • andrew_zol