Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

smob

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

smob

Zero dependency library to safe merge objects.

  • 1.3.0-alpha.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.2M
decreased by-18.15%
Maintainers
1
Weekly downloads
 
Created
Source

SMOB 🧪

npm version main codecov Known Vulnerabilities semantic-release: angular

Zero dependency library to safe merge objects.

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:

  • priority: left The source aka leftmost array/object has by default the highest priority.
  • array: true Merge object array properties.
  • distinct: false Remove duplicates, when merging array elements.

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

console.log(merge({ a: 1 }, { b: 2 }, { c: 3 }));
// => { a: 1, b: 2, c: 3 }

Merger

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

Priority

import { createMerger } from 'smob';

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

console.log(merge({ a: 1 }, { a: 2 }, { a: 3 }));
// => { a: 3 }

Array

import { createMerger } from 'smob';

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

console.log(merge({ a: [1,2,3] }, { a: [4,5,6] }));
// => { a: [1,2,3] }

Distinct

import { createMerger } from 'smob';

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

console.log(merge({ a: [1,2,3] }, { a: [3,4,5] }));
// => { a: [1,2,3,4,5] }

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;
        }
    }
});

console.log(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.

Keywords

FAQs

Package last updated on 28 May 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc