Installations
npm install utils-merge2
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
4.2.3
NPM Version
2.14.7
Score
69.5
Supply Chain
98.1
Quality
72.1
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (89.81%)
Makefile (10.19%)
Developer
kgryte
Download Statistics
Total Downloads
111,032
Last Day
42
Last Week
113
Last Month
914
Last Year
17,803
GitHub Statistics
3 Stars
63 Commits
3 Watching
1 Branches
1 Contributors
Bundle Size
9.97 kB
Minified
3.09 kB
Minified + Gzipped
Package Meta Information
Latest Version
2.0.0
Package Id
utils-merge2@2.0.0
Size
6.44 kB
NPM Version
2.14.7
Node Version
4.2.3
Total Downloads
Cumulative downloads
Total Downloads
111,032
Last day
133.3%
42
Compared to previous day
Last week
18.9%
113
Compared to previous week
Last month
-72.1%
914
Compared to previous month
Last year
39.4%
17,803
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Merge
Merge and extend objects.
Installation
1$ npm install utils-merge2
Usage
1var merge = require( 'utils-merge2' );
merge( target, source1[, source2[,...,sourceN]] )
Merges and extends a target object
.
1var target = { 2 'a': 'beep' 3}; 4var source = { 5 'a': 'boop', 6 'b': 'bap' 7}; 8 9var out = merge( target, source ); 10/* returns 11 { 12 'a': 'boop', 13 'b': 'bap' 14 } 15*/
The function
supports merging multiple source objects
.
1var target = { 2 'a': 'beep' 3}; 4var source1 = { 5 'b': 'boop' 6}; 7var source2 = { 8 'c': 'cat' 9}; 10 11var out = merge( target, source1, source2 ); 12/* returns 13 { 14 'a': 'beep', 15 'b': 'boop', 16 'c': 'cat' 17 } 18*/
merge.factory( options )
Returns a custom merge function
for merging and extending objects
.
1var factory = require( 'utils-merge2' ).factory; 2 3var opts = { 4 'level': 100, 5 'copy': true, 6 'override': true, 7 'extend': true 8}; 9 10var merge = factory( opts );
The function
accepts the following options
:
- level: limits the merge depth. The default merge strategy is a deep (recursive) merge. Default:
level = +infinity
. - copy:
boolean
indicating whether to deep copy merged values. Deep copying prevents shared references and sourceobject
mutation. Default:true
. - override: defines the merge strategy. If
true
, sourceobject
values will always override targetobject
values. Iffalse
, source values never override target values (useful for adding, but not overwriting, properties). To define a custom merge strategy, provide afunction
. Default:true
. - extend:
boolean
indicating whether new properties can be added to the targetobject
. Iffalse
, only shared properties are merged. Default:true
.
The default merge is a deep (recursive) merge.
1var target = { 2 'a': { 3 'b': { 4 'c': 5 5 }, 6 'd': 'beep' 7 } 8}; 9var source = { 10 'a': { 11 'b': { 12 'c': 10 13 } 14 } 15}; 16 17var out = merge( target, source ); 18/* returns 19 { 20 'a': { 21 'b': { 22 'c': 10 23 }, 24 'd': 'beep' 25 } 26 } 27*/
To limit the merge depth, set the level
option.
1var merge = factory({ 2 'level': 2 3}); 4 5var target = { 6 '1': { 7 'a': 'beep', 8 '2': { 9 '3': null, 10 'b': [ 5, 6, 7 ] 11 } 12 } 13}; 14 15var source = { 16 '1': { 17 'b': 'boop', 18 '2': { 19 '3': [ 1, 2, 3 ] 20 } 21 } 22}; 23 24var out = merge( target, source ); 25/* returns 26 { 27 '1': { 28 'a': 'beep', 29 'b': 'boop', 30 '2': { 31 '3': [ 1, 2, 3 ] 32 } 33 } 34 } 35*/
By default, merged values are deep copied.
1var target = { 2 'a': null 3}; 4var source = { 5 'a': { 6 'b': [ 1, 2, 3 ] 7 } 8}; 9 10var out = merge( target, source ); 11 12console.log( out.a.b === source.a.b ); 13// returns false
To allow shared references, set the copy
option to false
.
1var merge = factory({ 2 'copy': false 3}); 4 5var target = {}; 6 7var source = { 8 'a': [ 1, 2, 3 ] 9}; 10 11var out = merge( target, source ); 12 13var bool = ( out.a === source.a ); 14// returns true
To prevent existing properties from being overridden, set the override
option to false
.
1var merge = factory({ 2 'override': false 3}); 4 5var target = { 6 'a': 'beep', 7 'b': 'boop' 8}; 9 10var source = { 11 'a': null, 12 'c': 'bop' 13}; 14 15var out = merge( target, source ); 16/* returns 17 { 18 'a': 'beep', 19 'b': 'boop', 20 'c': 'bop' 21 } 22*/
Alternatively, to define a custom merge strategy, set the override
option to a function
.
1function strategy( a, b, key ) { 2 // a => target value 3 // b => source value 4 // key => object key 5 if ( key === 'a' ) { 6 return b; 7 } 8 if ( key === 'b' ) { 9 return a; 10 } 11 return 'bebop'; 12} 13 14var merge = factory({ 15 'override': strategy 16}); 17 18var target = { 19 'a': 'beep', 20 'b': 'boop', 21 'c': 1234 22}; 23 24var source = { 25 'a': null, 26 'b': {}, 27 'c': 'bop' 28}; 29 30var out = merge( target, source ); 31/* returns 32 { 33 'a': null, 34 'b': 'boop', 35 'c': 'bebop' 36 } 37*/
To prevent non-existent properties from being added to the target object
, set the extend
option to false
.
1var merge = factory({ 2 'extend': false 3}); 4 5var target = { 6 'a': 'beep', 7 'b': 'boop' 8}; 9 10var source = { 11 'b': 'hello', 12 'c': 'world' 13}; 14 15var out = merge( target, source ); 16/* returns 17 { 18 'a': 'beep', 19 'b': 'hello' 20 } 21*/
Notes
-
The target
object
is mutated.1var target = { 2 'a': 'beep' 3}; 4var source = { 5 'b': 'boop' 6}; 7 8var out = merge( target, source ); 9 10console.log( out === target ); 11// returns true 12 13console.log( target.b ); 14// returns 'boop'
To return a new
object
, provide an emptyobject
as the first argument.1var target = { 2 'a': 'beep' 3}; 4var source = { 5 'b': 'boop' 6}; 7 8var out = merge( {}, target, source ); 9 10console.log( out === target ); 11// returns false
-
Only plain JavaScript
objects
are merged and extended. The following values/types are either deep copied or assigned:Boolean
String
Number
Date
RegExp
Array
Int8Array
Uint8Array
Uint8ClampedArray
Init16Array
Uint16Array
Int32Array
Uint32Array
Float32Array
Float64Array
Buffer
(Node.js)Set
Map
Error
URIError
ReferenceError
SyntaxError
RangeError
-
Support for deep merging class instances is inherently fragile.
-
Number
,String
, orBoolean
objects are merged as primitives. -
Functions are not deep copied.
Examples
1var merge = require( 'utils-merge2' ); 2 3var target; 4var source; 5var out; 6 7target = { 8 'a': 'beep', 9 'b': 'boop', 10 'c': { 11 'c1': 'woot', 12 'c2': false, 13 'c3': { 14 'c3a': [ 1, 2 ], 15 'c3b': null 16 } 17 }, 18 'd': [ 1, 2, 3 ] 19}; 20 21source = { 22 'b': Math.PI, 23 'c': { 24 'c1': 'bap', 25 'c3': { 26 'c3b': 5, 27 'c3c': 'bop' 28 }, 29 'c4': 1337, 30 'c5': new Date() 31 }, 32 'd': [ 4, 5, 6 ], 33 'e': true 34}; 35 36out = merge( {}, target, source ); 37 38console.dir( out ); 39/* returns 40 { 41 'a': 'beep', 42 'b': 3.141592653589793, 43 'c': { 44 'c1': 'bap', 45 'c2': false, 46 'c3': { 47 'c3a': [ 1, 2 ], 48 'c3b': 5, 49 'c3c': 'bop' 50 }, 51 'c4': 1337, 52 'c5': <Date> 53 }, 54 'd': [ 4, 5, 6 ], 55 'e': true 56 } 57*/
To run the example code from the top-level application directory,
1$ node ./examples/index.js
Tests
Unit
This repository uses tape for unit tests. To run the tests, execute the following command in the top-level application directory:
1$ make test
All new feature development should have corresponding unit tests to validate correct functionality.
Test Coverage
This repository uses Istanbul as its code coverage tool. To generate a test coverage report, execute the following command in the top-level application directory:
1$ make test-cov
Istanbul creates a ./reports/coverage
directory. To access an HTML version of the report,
1$ make view-cov
Browser Support
This repository uses Testling for browser testing. To run the tests in a (headless) local web browser, execute the following command in the top-level application directory:
1$ make test-browsers
To view the tests in a local web browser,
1$ make view-browser-tests
License
Copyright
Copyright © 2015-2016. Athan Reines.
![Empty State](/_next/static/media/empty.e5fae2e5.png)
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 0/3 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 28 are checked with a SAST tool
Score
3
/10
Last Scanned on 2025-01-27
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More