mixme
A library for recursive merging of Javascript objects
Last updated 6 years ago by david .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ gnpm install mixme 
SYNC missed versions from official npm registry.

Node.js mixme

Build Status

Merge multiple object recursively, with TypeScript support. The last object takes precedence over the previous ones. Only objects are merged. Arrays are overwritten.

  • Zero dependencies
  • Small size
  • Pure functions
  • ESM and CommonJS support

API

Function merge(...data)

The API is minimalist, pass as many literal objects as you wish, they will all be merged. This function is immutable, the source objects won't be altered.

import { merge } from 'mixme'

const target = merge({a: '1'}, {b: '2'});
// target is {a: '1', b: '2'}

Function mutate(...data)

Use the mutate function to enrich an object. The first argument will be mutated:

import { mutate } from 'mixme'

const source = {a: '1'};
const target = mutate(source, {b: '2'});
target.c = '3';
// source and target are both {a: '1', b: '2', c: '3'}

Function clone(data)

It is possible to clone a literal object by simply calling mixme with this object as the first argument. Use the clone function in case you wish to clone any type of argument including arrays:

import { clone } from 'mixme'

const target = clone(['a', 'b'])
// target is now a copy of source

Function is_object_literal(object)

Use the is_object_literal function to ensure an object is literate.

import { is_object_literal } from 'mixme'

// {} is literate
is_object_literal({})

// error is not literate
is_object_literal(new Error('Catch me'))

// Array is not literate
is_object_literal([])

Function snake_case(object)

Clone a object and convert its properties into snake case.

import { snake_case } from 'mixme'

snake_case({aA: '1', bB: cC: '2'})
// Return {a_a: '1', b_b: c_c: '2'}

Function compare(item_1, item_2)

Compare two items and return true if their values match.

import { compare } from 'mixme'

compare([{a: 1}], [{a: 1}])
// Return true

compare({a: 1}, {a: 2})
// Return false

Example

Create a new object from two objects:

import { merge } from 'mixme'

const obj1 = { a_key: 'a value', b_key: 'b value'}
const obj2 = { b_key: 'new b value'}
const result = merge(obj1, obj2)

assert.eql(result.b_key, 'new b value')

Merge an existing object with a second one:

import { mutate } from 'mixme'

const obj1 = { a_key: 'a value', b_key: 'b value'};
const obj2 = { b_key: 'new b value'};
const result = mutate(obj1, obj2)

assert.eql(result, obj1)
assert.eql(obj1.b_key, 'new b value')

Testing

Clone the repo, install the development dependencies and run the tests:

git clone http://github.com/wdavidw/node-mixme.git .
npm install
npm run test

Developers

To automatically generate a new version:

yarn run release
git push --follow-tags origin master

Package publication is handled by the CI/CD with GitHub action.

Note:

  • On release, both the publish and test workflows run in parallel. Not very happy about it but I haven't found a better way.
  • yarn does not call the "postrelease" script and npm fails if the package-lock.json file is present and git ignored.

Contributors

This package is developed by Adaltas.

Current Tags

  • 0.5.9                                ...           latest (2 years ago)

18 Versions

  • 0.5.9                                ...           2 years ago
  • 0.5.8                                ...           2 years ago
  • 0.5.6                                ...           2 years ago
  • 0.5.5                                ...           2 years ago
  • 0.5.4                                ...           3 years ago
  • 0.5.3                                ...           3 years ago
  • 0.5.2                                ...           3 years ago
  • 0.5.1                                ...           4 years ago
  • 0.5.0                                ...           4 years ago
  • 0.4.0                                ...           4 years ago
  • 0.3.5                                ...           5 years ago
  • 0.3.3                                ...           5 years ago
  • 0.3.2                                ...           6 years ago
  • 0.3.1                                ...           6 years ago
  • 0.3.0                                ...           6 years ago
  • 0.2.0                                ...           6 years ago
  • 0.1.0                                ...           6 years ago
  • 0.0.1                                ...           7 years ago
Maintainers (1)
Downloads
Today 1
This Week 1
This Month 1
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (7)
Dependents (1)

Copyright 2013 - present © cnpmjs.org | Home |