Comparing version 1.0.3 to 1.1.0-immutable
{ | ||
"name": "baobab", | ||
"main": "build/baobab.min.js", | ||
"version": "1.0.3", | ||
"version": "1.1.0-immutable", | ||
"homepage": "https://github.com/Yomguithereal/baobab", | ||
@@ -6,0 +6,0 @@ "author": { |
# Changelog | ||
## v1.0.3 | ||
* Exposing `Cursor` and `Facet` classes for type checking (**@charlieschwabacher**). | ||
* Fixing `type.Object`. | ||
* Fixing root updates. | ||
## v1.0.2 | ||
@@ -10,0 +4,0 @@ |
@@ -17,2 +17,5 @@ /** | ||
// Should the tree's data be immutable? | ||
immutable: false, | ||
// Validation specifications | ||
@@ -19,0 +22,0 @@ validate: null, |
@@ -14,3 +14,3 @@ /** | ||
Object.defineProperty(Baobab, 'version', { | ||
value: '1.0.3' | ||
value: '1.1.0-immutable' | ||
}); | ||
@@ -17,0 +17,0 @@ |
{ | ||
"name": "baobab", | ||
"version": "1.0.3", | ||
"version": "1.1.0-immutable", | ||
"description": "JavaScript persistent data tree with cursors.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -48,6 +48,10 @@ /** | ||
// Properties | ||
this.data = helpers.deepClone(initialData); | ||
this.data = initialData; | ||
this.root = this.select([]); | ||
this.facets = {}; | ||
// Immutable tree? | ||
if (this.options.immutable) | ||
helpers.deepFreeze(this.data); | ||
// Boostrapping root cursor's methods | ||
@@ -54,0 +58,0 @@ function bootstrap(name) { |
@@ -58,3 +58,3 @@ /** | ||
// Cloning function | ||
function clone(deep, item) { | ||
function cloner(deep, item) { | ||
if (!item || | ||
@@ -104,29 +104,46 @@ typeof item !== 'object' || | ||
// Shallow & deep cloning functions | ||
var shallowClone = clone.bind(null, false), | ||
deepClone = clone.bind(null, true); | ||
var shallowClone = cloner.bind(null, false), | ||
deepClone = cloner.bind(null, true); | ||
// Freezing function | ||
var freeze = Object.freeze || Function.prototype; | ||
function deepFreeze(o) { | ||
function freezer(deep, o) { | ||
if (typeof o !== 'object') | ||
return; | ||
var p, | ||
k; | ||
Object.freeze(o); | ||
freeze(o); | ||
if (!deep) | ||
return; | ||
for (k in o) { | ||
p = o[k]; | ||
if (Array.isArray(o)) { | ||
if (!o.hasOwnProperty(k) || | ||
typeof p !== 'object' || | ||
Object.isFrozen(p)) | ||
continue; | ||
// Iterating through the elements | ||
var i, | ||
l; | ||
deepFreeze(p); | ||
for (i = 0, l = o.length; i < l; i++) | ||
deepFreeze(o[i]); | ||
} | ||
else { | ||
var p, | ||
k; | ||
for (k in o) { | ||
p = o[k]; | ||
if (!p || | ||
!o.hasOwnProperty(k) || | ||
typeof p !== 'object' || | ||
Object.isFrozen(p)) | ||
continue; | ||
deepFreeze(p); | ||
} | ||
} | ||
} | ||
// Shallow & deep freezing function | ||
var freeze = Object.freeze ? freezer.bind(null, false) : Function.prototype, | ||
deepFreeze = Object.freeze ? freezer.bind(null, true) : Function.prototype; | ||
// Simplistic composition | ||
@@ -385,2 +402,3 @@ function compose(fn1, fn2) { | ||
before: before, | ||
freeze: freeze, | ||
deepClone: deepClone, | ||
@@ -387,0 +405,0 @@ deepFreeze: deepFreeze, |
@@ -41,8 +41,2 @@ /** | ||
// If nested object does not exist, we create it | ||
if (type.Primitive(o[lastKey])) | ||
o[lastKey] = {}; | ||
else | ||
o[lastKey] = helpers.shallowClone(o[lastKey]); | ||
// Are we at leaf level? | ||
@@ -69,2 +63,4 @@ var leafLevel = Object.keys(spec).some(function(k) { | ||
if (opts.immutable) | ||
helpers.freeze(parent[olderKey]); | ||
break; | ||
@@ -130,5 +126,20 @@ } | ||
} | ||
// Deep freezing the new value? | ||
if (opts.immutable) | ||
helpers.deepFreeze(o); | ||
} | ||
} | ||
else { | ||
// If nested object does not exist, we create it | ||
if (type.Primitive(o[lastKey])) | ||
o[lastKey] = {}; | ||
else | ||
o[lastKey] = helpers.shallowClone(o[lastKey]); | ||
// Should we freeze the parent? | ||
if (opts.immutable) | ||
helpers.freeze(o); | ||
for (k in spec) { | ||
@@ -135,0 +146,0 @@ |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
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
61402
15
1301
1