GithubHelp home page GithubHelp logo

jonschlinkert / merge-deep Goto Github PK

View Code? Open in Web Editor NEW
111.0 4.0 33.0 34 KB

Recursively merge values in a JavaScript object.

Home Page: https://github.com/jonschlinkert

License: MIT License

JavaScript 100.00%
merge objects recursive properties values assign extend mixin deep javascript

merge-deep's Introduction

merge-deep NPM version NPM monthly downloads NPM total downloads Linux Build Status

Recursively merge values in a javascript object.

Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your ❤️ and support.

Install

Install with npm:

$ npm install --save merge-deep

Based on mout's implementation of merge

Usage

var merge = require('merge-deep');

merge({a: {b: {c: 'c', d: 'd'}}}, {a: {b: {e: 'e', f: 'f'}}});
//=> { a: { b: { c: 'c', d: 'd', e: 'e', f: 'f' } } }

About

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Running Tests

Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:

$ npm install && npm test
Building docs

(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)

To generate the readme, run the following command:

$ npm install -g verbose/verb#dev verb-generate-readme && verb

Related projects

You might also be interested in these projects:

  • assign-deep: Deeply assign the values of all enumerable-own-properties and symbols from one or more source objects… more | homepage
  • defaults-deep: Like extend but recursively copies only the missing properties/values to the target object. | homepage
  • extend-shallow: Extend an object with the properties of additional objects. node.js/javascript util. | homepage
  • merge-deep: Recursively merge values in a javascript object. | homepage
  • mixin-deep: Deeply mix the properties of objects into the first object. Like merge-deep, but doesn't clone… more | homepage
  • omit-deep: Recursively omit the specified key or keys from an object. | homepage

Contributors

Commits Contributor
32 jonschlinkert
8 doowb

Author

Jon Schlinkert

License

Copyright © 2021, Jon Schlinkert. Released under the MIT License.


This file was generated by verb-generate-readme, v0.8.0, on January 11, 2021.

merge-deep's People

Contributors

doowb avatar jonschlinkert avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

merge-deep's Issues

Misuse of CommonJS

From webpack:

WARNING in ./node_modules/merge-deep/utils.js
7:34-41 Critical dependency: require function is used in a way in which dependencies cannot be statically extracted
 @ ./node_modules/merge-deep/utils.js
 @ ./node_modules/merge-deep/index.js

security contact

Hi,

The GitHub Security Lab is attempting to reach a security contact for this project. Please drop us a line at [email protected] with reference GHSL-2020-160.

Kind regards,
Bas Alberts
GitHub Security Lab

webpack support

I got the error:

 WARNING in ./node_modules/merge-deep/utils.js
    7:34-41 Critical dependency: require function is used in a way in which dependencies cannot be statically extracted
     @ ./node_modules/merge-deep/utils.js
     @ ./node_modules/merge-deep/index.js

Symbols are ignored

Symbols can be used as property keys, but they are ignored.

const merge = require('merge-deep');

const mySymbol = Symbol('mySymbol');

const x = { value: 42, [mySymbol]: 'hello' };

console.log(x);
// { value: 42, [Symbol(mySymbol)]: 'hello' }

const y = { other: 33 };
const z = merge(x, y);

console.log(z);
// { value: 42, other: 33 }
// expected: { value: 42, [Symbol(mySymbol)]: 'hello', other: 33 }

notUnion property for arrays

Can specify which array fields will be replaced (instead of concatenated with union). Few lines in code and all tests working.

module.exports = function mergeDeep(orig, objects, notUnionParams) {

if (isObject(val) || Array.isArray(val)) {
     merge(target, val, notUnionParams);
}
function merge(target, obj, notUnionParams) {
var notUnion = false;
if((notUnionParams !== undefined) && (notUnionParams.length > 0) && ((notUnionParams.indexOf(key) > -1) || (notUnionParams == key))){
     notUnion = true;
}
if (isObject(newVal) && isObject(oldVal)) {
     target[key] = merge(newVal, oldVal, notUnionParams);
} else if (Array.isArray(newVal)) {
     if(notUnion){
          target[key] = union([], newVal);  
     } else {
          target[key] = union([], newVal, oldVal);
     }
} else {
     target[key] = clone(oldVal);
}

And the test

it('should correctly use not union property', function() {
    var obj1 = {a: {
      "a": [{
        1: 1,2: 2
      }]
    }};
    var obj2 = {a: {
      "a": [{
        1: 2,2: 3
      }]
    }};

    var actual = merge(obj1, obj2, ["a"]);
    console.log(actual.a);
    assert.deepEqual(actual.a, { a: [{ 1:1,2:2 }] });
});

doesn't browserify

The use of lazy-cache breaks browserify's ability to build this module. Would you accept pull requests to drop lazy-cache from merge-deep and clone-deep?

Does Array values concat instead of be replaced?

I'm not sure, if this a bug or a feature. Look:

import merge from 'merge-deep';

const object1 = { a: '1', b: '2', c: [1, 2, 3] };
const object2 = { a: undefined, b: null, c: null }; // 'c' property might be anything else, like '1', or undefined, or an empty Array, doesn't matter

const mergedObject = merge(object1, object2);
console.log(mergedObject);
=> { a: undefined, b: null, c: [1, 2, 3] } // So, the last property is not changed.

If 'c' in the object2 was 'abc', we'll get

=> { a: undefined, b: null, c: ['abc', 1, 2, 3] }

It looks like Array type values concat, instead of be replaced with a new value. But I need a tool to replace values while merging.

Should I use assign-deep instead? Cause the last one does it well.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.