Comparing version 0.0.4 to 0.0.5
@@ -44,2 +44,13 @@ (function (global, factory) { | ||
function deepMerge(a, b) { | ||
props(b).forEach(function (k) { | ||
if (typeof a[k] === 'object' && typeof b[k] === 'object') { | ||
a[k] = a[k].deepMerge(b[k]); | ||
} | ||
else { | ||
a[k] = b[k]; | ||
} | ||
}); | ||
} | ||
function thaw(source, constructor) { | ||
@@ -73,2 +84,6 @@ return merge(new (constructor || source.constructor), source); | ||
FrozenObject.prototype.deepMerge = updater(function (obj) { | ||
deepMerge(this, obj); | ||
}); | ||
FrozenObject.prototype.thaw = function () { | ||
@@ -75,0 +90,0 @@ return thaw(this, Object); |
{ | ||
"name": "copykitten", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "Tiny immutable JSON data structures", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -43,3 +43,4 @@ # copykitten.js | ||
* [remove(key)](#FrozenObject_remove) | ||
* [merge(props)](#FrozenObject_props) | ||
* [merge(props)](#FrozenObject_merge) | ||
* [deepMerge(props)](#FrozenObject_deepMerge) | ||
* [update(f)](#FrozenObject_update) | ||
@@ -156,2 +157,17 @@ * [thaw()](#FrozenObject_thaw) | ||
<a name="FrozenObject_deepMerge"></a> | ||
#### deepMerge(props) | ||
Like [merge(props)](#FrozenObject_merge) but will recursively merge | ||
nested objects. | ||
```javascript | ||
var a = copykitten.toImmutable({animal: {name: 'Kitten'}}); | ||
var b = a.deepMerge({animal: {status: 'hungry'}}); | ||
// a is now {animal: {name: 'Kitten'}} | ||
// b is now {animal: {name: 'Kitten', status: 'hungry'}} | ||
``` | ||
<a name="FrozenObject_update"></a> | ||
@@ -158,0 +174,0 @@ |
@@ -166,2 +166,41 @@ var assert = (typeof chai === 'undefined') ? | ||
test('deepMerge', function () { | ||
"use strict"; | ||
var obj = copykitten.toImmutable({ | ||
title: 'test', | ||
meta: { | ||
author: { | ||
name: 'foo', | ||
role: 'user' | ||
} | ||
} | ||
}); | ||
var obj2 = obj.deepMerge({ | ||
meta: { | ||
author: { | ||
id: 123, | ||
role: 'admin' | ||
}, | ||
date: 'today' | ||
}, | ||
other: { | ||
something: 'else' | ||
} | ||
}); | ||
assert.deepEqual(JSON.parse(JSON.stringify(obj2)), { | ||
title: 'test', | ||
meta: { | ||
author: { | ||
id: 123, | ||
role: 'admin', | ||
name: 'foo' | ||
}, | ||
date: 'today' | ||
}, | ||
other: { | ||
something: 'else' | ||
} | ||
}); | ||
}); | ||
test('JSON.stringify', function () { | ||
@@ -168,0 +207,0 @@ var obj = copykitten.toImmutable({ |
399380
11202
484