1.5.0 โ€ข Published 8 months ago

smob v1.5.0

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

SMOB ๐Ÿงช

npm version main codecov Known Vulnerabilities semantic-release: angular

A zero dependency library to safe merge objects and arrays with customizable behavior.

Table of Contents

Installation

npm install smob --save

Usage

import { merge } from "smob";

const output = merge(...sources);

The following merge options are set by default:

  • array: true Merge object array properties.
  • arrayDistinct: false Remove duplicates, when merging array elements.
  • arrayPriority: left (options.priority) The source aka leftmost array has by default the highest priority.
  • clone: false Deep clone input sources.
  • inPlace: false Merge sources in place.
  • priority: left The source aka leftmost object has by default the highest priority.

The merge behaviour can be changed by creating a custom merger.

Arguments

  • sources (any[] | Record<string, any>)[]: The source arrays/objects.
import { merge } from 'smob';

merge({ a: 1 }, { b: 2 }, { c: 3 });
// { a: 1, b: 2, c: 3 }

merge(['foo'], ['bar']);
// ['foo', 'bar']

Merger

A custom merger can simply be created by using the createMerger method.

Array

import { createMerger } from 'smob';

const merge = createMerger({ array: false });

merge({ a: [1,2,3] }, { a: [4,5,6] });
// { a: [1,2,3] }

ArrayDistinct

import { createMerger } from 'smob';

const merge = createMerger({ arrayDistinct: true });

merge({ a: [1,2,3] }, { a: [3,4,5] });
// { a: [1,2,3,4,5] }

Priority

import { createMerger } from 'smob';

const merge = createMerger({ priority: 'right' });

merge({ a: 1 }, { a: 2 }, { a: 3 })
// { a: 3 }

Strategy

import { createMerger } from 'smob';

const merge = createMerger({
    strategy: (target, key, value) => {
        if (
            typeof target[key] === 'number' &&
            typeof value === 'number'
        ) {
            target[key] += value;
            return target;
        }
    }
});

merge({ a: 1 }, { a: 2 }, { a: 3 });
// { a: 6 }

A returned value indicates that the strategy has been applied.

Utils

distinctArray

import { distinctArray } from 'smob';

distnctArray(['foo', 'bar', 'foo']);
// ['foo', 'bar']

The function also removes non-primitive elements that are identical by value or reference.

Objects

import { distinctArray } from 'smob';

distinctArray([{ foo: 'bar' }, { foo: 'bar' }]);
// [{ foo: 'bar' }]

Arrays

import { distinctArray } from 'smob';

distinctArray([['foo', 'bar'], ['foo', 'bar']]);
// [['foo', 'bar']]

isEqual

Checks if two (non-primitive) elements are identical by value or reference.

import { isEqual } from 'smob';

isEqual({foo: 'bar'}, {foo: 'bar'});
// true

isEqual(['foo', 'bar'], ['foo', 'bar']);
// true

License

Made with ๐Ÿ’š

Published under MIT License.

@everything-registry/sub-chunk-2786component-library-starter-packroutuprollup-plugin-confuserrenu-packsedanbosoksweetalert2-denazificationvitron@purnasatria/directus-extension-document-interfacelasagnajsmdf-readeridx-uplotilingomangudinlagirajinmaster-imagespileuleuyanteapatepangdeuireact-component-library-starter-packnuxtpaginationsrapiqredis-extensiontehmusimhujansetiltypeorm-extension@authelion/common@authelion/server-core@authelion/vue@authup/client-web-config@authup/client-web-nuxt@authup/common@authup/config@authup/kit@authup/server@authup/server-api@authup/server-common@authup/server-core@authup/server-core-app@authup/server-database@authup/server-http@authup/server-kit@authup/vue2@baolong281/gsplat@cheap-pets/rollup-plugin-css@cheap-pets/rollup-plugin-swc@aabelmann/ui-layervalidup@hapic/harbor@hapic/vault@hoang_ng/directus-extension-dependent-fields@krenaldi/learnstorybook-design-system-template@max_alieksieiev/react-pdf-viewer-root@jwfl/compose@ilingo/fs@skedulr/nebula-ui-library@superiortech/music-tempo@dnpm-dip/core@cristianps1988/ds-template@routup/swagger@routup/body@routup/core@rollup/plugin-terser@rollup/plugin-swcaction-docker-jsamqp-extensionanakwadonteaanaklanangteaanakketiga@waspeer/config@validup/adapter-validator@vue-layout/utils@vue-layout/hyperscript@tomw2w/my-nuxt-layer@trapi/swaggerconfinitydesign-system-trial-milyasbpadarshan-ui-testdocker-scandirectus-extension-layout-boilerplatedirectus-extension-delete-users-with-fileselectron-adapterbraid-extension-component-libraryauthupgerimismalamsenin
1.5.0

8 months ago

1.2.0

1 year ago

1.4.1

1 year ago

1.4.0

1 year ago

1.3.0

1 year ago

1.3.0-alpha.2

1 year ago

1.3.0-alpha.1

1 year ago

1.1.1

2 years ago

0.1.0

2 years ago

1.1.0

2 years ago

1.0.0

2 years ago

0.0.5

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago