state-tree
Advanced tools
Comparing version 0.1.13 to 0.1.14
{ | ||
"name": "state-tree", | ||
"version": "0.1.13", | ||
"version": "0.1.14", | ||
"description": "A state tree that handles reference updates and lets you flush a description of changes", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -133,6 +133,8 @@ # state-tree (EXPERIMENTAL) | ||
tree.set('foo', 'bar2'); | ||
// You can also use arrays for the path | ||
tree.set(['foo'], 'bar2'); | ||
tree.set('foo', 'bar2'); | ||
tree.merge('foo', {something: 'cool'}); | ||
tree.import({foo: 'bar', deeply: {nested: 'foo'}}); // Deep merging | ||
tree.unset('foo'); | ||
@@ -144,2 +146,3 @@ tree.push('list', 'something'); | ||
tree.splice('list', 0, 1, 'newValue'); | ||
tree.concat('list', ['something']); | ||
``` | ||
@@ -146,0 +149,0 @@ |
@@ -217,2 +217,56 @@ var subscribers = []; | ||
}, | ||
concat: function () { | ||
var args = [].slice.call(arguments); | ||
var path = args.shift(); | ||
var pathArray = typeof path === 'string' ? path.split('.') : path.slice(); | ||
var key = pathArray.pop(); | ||
var host = getByPath(pathArray, state); | ||
host[key] = host[key].concat.apply(host[key], args.map(function (arg) { | ||
return setReferences(arg, pathArray.slice().concat(key, [[host[key], arg]])); | ||
})); | ||
updateChanges(host, key); | ||
}, | ||
import: function (value) { | ||
function deepmerge(target, src) { | ||
var array = Array.isArray(src); | ||
var dst = array && [] || {}; | ||
if (array) { | ||
target = target || []; | ||
dst = src.slice(); | ||
src.forEach(function(e, i) { | ||
if (typeof dst[i] === 'undefined') { | ||
dst[i] = e; | ||
} else if (typeof e === 'object') { | ||
dst[i] = deepmerge(target[i], e); | ||
} | ||
}); | ||
} else { | ||
if (target && typeof target === 'object') { | ||
Object.keys(target).forEach(function (key) { | ||
dst[key] = target[key]; | ||
}) | ||
} | ||
Object.keys(src).forEach(function (key) { | ||
if (typeof src[key] !== 'object' || !src[key]) { | ||
dst[key] = src[key]; | ||
} else { | ||
if (!target[key]) { | ||
dst[key] = src[key]; | ||
} else { | ||
dst[key] = deepmerge(target[key], src[key]); | ||
} | ||
} | ||
}); | ||
} | ||
return dst; | ||
}; | ||
cleanReferences(state, state, []); | ||
state = deepmerge(state, value); | ||
Object.keys(state).forEach(function (key) { | ||
state[key] = setReferences(state[key], [key]); | ||
}); | ||
}, | ||
subscribe: function (cb) { | ||
@@ -219,0 +273,0 @@ subscribers.push(cb); |
36577
20
903
259