Comparing version 0.1.2 to 0.2.0
@@ -8,5 +8,7 @@ var lib = require('./lib/deap'); | ||
extend: lib.extend, | ||
update: lib.update, | ||
merge: lib.merge, | ||
extendShallow: lib.extendShallow, | ||
updateShallow: lib.updateShallow, | ||
mergeShallow: lib.mergeShallow | ||
}); |
@@ -8,2 +8,4 @@ var typeOf = require('./typeof'), | ||
extendShallow: extend, | ||
update: deepUpdate, | ||
updateShallow: update, | ||
merge: deepMerge, | ||
@@ -49,3 +51,3 @@ mergeShallow: merge | ||
function merge(a, b /*, [b2..n] */) { | ||
function update(a, b /*, [b2..n] */) { | ||
slice.call(arguments, 1).forEach(function(b) { | ||
@@ -59,3 +61,3 @@ Object.keys(b).forEach(function(p) { | ||
function deepMerge(a, b /*, [b2..n] */) { | ||
function deepUpdate(a, b /*, [b2..n] */) { | ||
slice.call(arguments, 1).forEach(function(b) { | ||
@@ -70,3 +72,3 @@ var ap, bp, ta, tb; | ||
if(tb === 'object' && ta === 'object') | ||
deepMerge(ap, bp); | ||
deepUpdate(ap, bp); | ||
else if(tb === 'array' && ta === 'array') { | ||
@@ -82,1 +84,27 @@ ap.length = 0; | ||
} | ||
function merge(a, b /*, [b2..n] */) { | ||
slice.call(arguments, 1).forEach(function(b) { | ||
Object.keys(b).forEach(function(p) { | ||
if(!a.hasOwnProperty(p)) a[p] = b[p]; | ||
}); | ||
}); | ||
return a; | ||
} | ||
function deepMerge(a, b /*, [b2..n] */) { | ||
slice.call(arguments, 1).forEach(function(b) { | ||
var ap, bp, ta, tb; | ||
Object.keys(b).forEach(function(p) { | ||
ap = a[p]; | ||
bp = b[p]; | ||
ta = typeOf(ap); | ||
tb = typeOf(bp); | ||
if(tb === 'object' && ta === 'object') | ||
deepMerge(ap, bp); | ||
else if(!a.hasOwnProperty(p)) | ||
a[p] = clone(bp); | ||
}); | ||
}); | ||
return a; | ||
} |
{ | ||
"name": "deap", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"description": "extend and merge objects, deep or shallow", | ||
@@ -41,3 +41,3 @@ "main": "index.js", | ||
"browsers": [ | ||
"ie/8..latest", | ||
"ie/9..latest", | ||
"firefox/19..latest", | ||
@@ -47,3 +47,4 @@ "chrome/25..latest", | ||
"iphone/6", | ||
"ipad/6" | ||
"ipad/6", | ||
"android-browser/latest" | ||
], | ||
@@ -50,0 +51,0 @@ "harness": "mocha-tdd", |
@@ -0,1 +1,3 @@ | ||
[![browser support](https://ci.testling.com/selfcontained/deap.png)](http://ci.testling.com/selfcontained/deap) | ||
[![Build Status](https://secure.travis-ci.org/selfcontained/deap.png?branch=master)](http://travis-ci.org/selfcontained/deap) | ||
@@ -19,2 +21,6 @@ | ||
### browser usage | ||
**deap** assumes es5, so we recommend using an es5 shim for older browsers. [Browserify](https://github.com/substack/node-browserify) is also recommended as a means to use this module client-side, but other module loaders for browsers will work with **deap** as well if you shim it. | ||
### available functions | ||
@@ -24,4 +30,6 @@ | ||
+ deap.merge() - **deep merge** | ||
+ deap.update() - **deep update** | ||
+ deap.extendShallow() - **shallow extend** | ||
+ deap.mergeShallow() - **shallow merge** | ||
+ deap.updateShallow() - **shallow update** | ||
+ deap.clone() - **deep clone** | ||
@@ -46,3 +54,3 @@ | ||
Deep merge. Fill an object's existing properties from another object. | ||
Deep merge. Copy properties from one object to another, not replacing existing properties. | ||
@@ -52,4 +60,16 @@ Takes *n* number of arguments, modifies the first argument and returns it. | ||
```javascript | ||
var a = { name: 'Joe', address: { number: 1234 }; | ||
deap.merge(a, { name: 'Jack', age: 26, phone: '555-555-5555', address: { number: 4321, street: 'University Blvd' }); | ||
// returns: a => { name: 'Joe', age: 26, phone: '555-555-5555', address: { number: 1234, street: 'University Blvd' }} | ||
``` | ||
### deap.update() | ||
Deep update. Fill an object's existing properties from another object. | ||
Takes *n* number of arguments, modifies the first argument and returns it. | ||
```javascript | ||
var a = { name: 'Joe', phone: '' }; | ||
deap.merge(a, { age: 26, phone: '555-555-5555' }); // returns: a => { name: 'Joe', phone: '555-555-5555' } | ||
deap.update(a, { age: 26, phone: '555-555-5555' }); // returns: a => { name: 'Joe', phone: '555-555-5555' } | ||
``` | ||
@@ -68,3 +88,6 @@ | ||
deap.merge(); //shallow merge | ||
deap.update(); //shallow update | ||
deap.clone(); // deep clone | ||
``` | ||
...the end |
@@ -8,3 +8,4 @@ var lib = require('./lib/deap'); | ||
extend: lib.extendShallow, | ||
update: lib.updateShallow, | ||
merge: lib.mergeShallow | ||
}); |
@@ -27,2 +27,12 @@ var assert = require('chai').assert, | ||
it('should have update defined', function() { | ||
assert.isFunction(deap.update); | ||
assert.deepEqual(deap.update, lib.update); | ||
}); | ||
it('should have updateShallow defined', function() { | ||
assert.isFunction(deap.updateShallow); | ||
assert.deepEqual(deap.updateShallow, lib.updateShallow); | ||
}); | ||
it('should have merge defined', function() { | ||
@@ -29,0 +39,0 @@ assert.isFunction(deap.merge); |
@@ -7,6 +7,7 @@ var lib = require('../lib/deap'), | ||
it('should not merge anything into an empty object', function() { | ||
var result = shallowMerge({}, { foo: 'bar' }); | ||
it('should merge everything into an empty object', function() { | ||
var a = { foo: 'bar' }, | ||
result = shallowMerge({}, a); | ||
assert.deepEqual(result, {}); | ||
assert.deepEqual(result, a); | ||
}); | ||
@@ -23,3 +24,3 @@ | ||
it('should replace existing values only', function() { | ||
it('should not replace existing values', function() { | ||
var a = { burp: 'adurp' }, | ||
@@ -30,5 +31,4 @@ b = { burp: 'zing', grr: 'arghh' }; | ||
assert.deepEqual(result, a); | ||
assert.equal(a.burp, b.burp); | ||
assert.isUndefined(a.grr); | ||
assert.deepEqual(result, { burp: 'adurp', grr: 'arghh' }); | ||
assert.equal(result.burp, a.burp); | ||
}); | ||
@@ -52,9 +52,10 @@ | ||
var a = { foo: 'bar', deep: { foo: 'bar', baz: 'buzz' }}, | ||
b = { deep: { foo: 'beep' } }; | ||
b = { foo: 'bop', deep: { foo: 'beep', biz: 'baz' } }; | ||
var result = deepMerge(a, b); | ||
assert.equal(result.foo, a.foo); | ||
assert.equal(result.deep.foo, b.deep.foo); | ||
assert.equal(result.deep.baz, a.deep.baz); | ||
assert.equal(result.foo, 'bar'); | ||
assert.equal(result.deep.foo, 'bar'); | ||
assert.equal(result.deep.baz, 'buzz'); | ||
assert.equal(result.deep.biz, 'baz'); | ||
}); | ||
@@ -64,5 +65,5 @@ | ||
var a = { foo: 'bar', deep: { hi: 'hello', deeper: { foo: 'bar', baz: 'buzz' }}}, | ||
b = { deep: { deeper: { foo: 'beep' } } }; | ||
b = { foo: 'baz', deep: { hi: 'bye', bye: 'hi', deeper: { foo: 'beep', bop: 'boop' } } }; | ||
var result = deepMerge(a, b); | ||
var result = deepMerge({}, a, b); | ||
@@ -72,5 +73,7 @@ assert.equal(result.foo, a.foo); | ||
assert.equal(result.deep.hi, a.deep.hi); | ||
assert.equal(result.deep.bye, b.deep.bye); | ||
assert.isObject(result.deep.deeper); | ||
assert.equal(result.deep.deeper.foo, b.deep.deeper.foo); | ||
assert.equal(result.deep.deeper.foo, a.deep.deeper.foo); | ||
assert.equal(result.deep.deeper.baz, a.deep.deeper.baz); | ||
assert.equal(result.deep.deeper.bop, b.deep.deeper.bop); | ||
}); | ||
@@ -83,35 +86,31 @@ | ||
var result = deepMerge(a, b, c); | ||
var result = deepMerge({}, a, b, c); | ||
assert.deepEqual(result, { | ||
foo: b.foo, | ||
boo: c.boo, | ||
poo: a.poo | ||
foo: a.foo, | ||
boo: a.boo, | ||
poo: a.poo, | ||
zoo: b.zoo, | ||
two: c.two | ||
}); | ||
}); | ||
it('should not merge properties that are not on the first argument', function() { | ||
var a = { foo: 'bar', deep: { deeper: { foo: 'bar' } } }, | ||
b = { boo: 'far', deep: { hi: 'hello', deeper: { foo: 'beep', baz: 'buzz' } } }; | ||
it('should not preserve nested object references', function() { | ||
var a = { foo: 'bar' }, | ||
nested = { grr: 'argh' }, | ||
newFoo = { burp: nested }, | ||
b = { foo: newFoo, foo2: newFoo }; | ||
var result = deepMerge(a, b); | ||
assert.isUndefined(result.boo); | ||
assert.isObject(result.deep); | ||
assert.isUndefined(result.deep.hi); | ||
assert.isObject(result.deep.deeper); | ||
assert.isUndefined(result.deep.deeper.baz); | ||
assert.equal(result.deep.deeper.foo, b.deep.deeper.foo); | ||
assert.equal(a.foo, 'bar'); | ||
assert.deepEqual(a.foo2.burp, b.foo2.burp); | ||
assert.notStrictEqual(a.foo2.burp, nested); | ||
}); | ||
it('should not preserve nested object references', function() { | ||
it('should not override a string with an object', function() { | ||
var a = { foo: 'bar' }, | ||
nested = { grr: 'argh' }, | ||
newFoo = { burp: nested }, | ||
b = { foo: newFoo }; | ||
b = { foo: { biz: 'baz' } }; | ||
var result = deepMerge(a, b); | ||
assert.deepEqual(a.foo.burp, b.foo.burp); | ||
assert.notStrictEqual(a.foo.burp, nested); | ||
assert.deepEqual(a, { foo: 'bar' }); | ||
}); | ||
@@ -126,5 +125,4 @@ | ||
assert.deepEqual(result.nested, b.nested); | ||
assert.deepEqual(result.nested, a.nested); | ||
assert.notStrictEqual(result.nested, b.nested); | ||
assert.strictEqual(result.nested, a.nested); | ||
assert.strictEqual(result.nested, deep); | ||
@@ -138,5 +136,5 @@ }); | ||
var result = deepMerge(a, b); | ||
var result = deepMerge({}, a, b); | ||
assert.deepEqual(result.nested, b.nested); | ||
assert.deepEqual(result.nested, a.nested); | ||
assert.notStrictEqual(result.nested[0], deeper); | ||
@@ -143,0 +141,0 @@ }); |
@@ -11,2 +11,3 @@ var assert = require('chai').assert, | ||
assert.isFunction(shallow.extend); | ||
assert.isFunction(shallow.update); | ||
assert.isFunction(shallow.merge); | ||
@@ -16,5 +17,6 @@ assert.isFunction(shallow.clone); | ||
it('should be shallow functions', function() { | ||
it('should have shallow functions', function() { | ||
assert.equal(shallow, lib.extendShallow); | ||
assert.equal(shallow.extend, lib.extendShallow); | ||
assert.equal(shallow.update, lib.updateShallow); | ||
assert.equal(shallow.merge, lib.mergeShallow); | ||
@@ -21,0 +23,0 @@ assert.equal(shallow.clone, lib.clone); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20198
13
499
89