Comparing version 0.3.1 to 0.3.2
{ | ||
"name": "baobab", | ||
"main": "build/baobab.min.js", | ||
"version": "0.3.1", | ||
"version": "0.3.2", | ||
"homepage": "https://github.com/Yomguithereal/baobab", | ||
@@ -6,0 +6,0 @@ "author": { |
@@ -12,3 +12,3 @@ /** | ||
Object.defineProperty(Baobab, 'version', { | ||
value: '0.3.1' | ||
value: '0.3.2' | ||
}); | ||
@@ -15,0 +15,0 @@ |
{ | ||
"name": "baobab", | ||
"version": "0.3.1", | ||
"version": "0.3.2", | ||
"description": "JavaScript data tree with cursors.", | ||
@@ -12,3 +12,3 @@ "main": "index.js", | ||
"async": "~0.9.0", | ||
"browserify": "^8.0.3", | ||
"browserify": "^9.0.3", | ||
"gulp": "^3.8.10", | ||
@@ -28,3 +28,4 @@ "gulp-header": "^1.2.2", | ||
"test": "gulp test", | ||
"build": "gulp build" | ||
"build": "gulp build", | ||
"lint": "gulp lint" | ||
}, | ||
@@ -31,0 +32,0 @@ "repository": { |
@@ -13,5 +13,5 @@ /** | ||
merge = require('./merge.js'), | ||
types = require('./typology.js'), | ||
mixins = require('./mixins.js'), | ||
defaults = require('../defaults.json'); | ||
defaults = require('../defaults.json'), | ||
type = require('./type.js'); | ||
@@ -27,3 +27,3 @@ /** | ||
if (!types.check(initialData, 'object|array')) | ||
if (!type.Object(initialData) && !type.Array(initialData)) | ||
throw Error('Baobab: invalid data.'); | ||
@@ -46,3 +46,3 @@ | ||
this.typology = this.options.typology ? | ||
(types.check(this.options.typology, 'typology') ? | ||
(this.options.typology instanceof Typology ? | ||
this.options.typology : | ||
@@ -79,3 +79,3 @@ new Typology(this.options.typology)) : | ||
if (!types.check(spec, 'object')) | ||
if (!type.Object(spec)) | ||
throw Error('Baobab.update: wrong specification.'); | ||
@@ -96,4 +96,5 @@ | ||
this._willUpdate = true; | ||
helpers.later(function() { | ||
self.commit(); | ||
process.nextTick(function() { | ||
if (self._willUpdate) | ||
self.commit(); | ||
}); | ||
@@ -190,12 +191,10 @@ } | ||
if (!types.check(path, 'path')) | ||
if (!type.Path(path)) | ||
throw Error('Baobab.select: invalid path.'); | ||
// Casting to array | ||
path = (types.get(path) !== 'array') ? [path] : path; | ||
path = !type.Array(path) ? [path] : path; | ||
// Complex path? | ||
var complex = path.some(function(step) { | ||
return types.check(step, 'complexStep'); | ||
}); | ||
var complex = type.ComplexPath(path); | ||
@@ -231,7 +230,7 @@ var solvedPath; | ||
if (!types.check(path, 'path')) | ||
if (!type.Path(path)) | ||
throw Error('Baobab.get: invalid path.'); | ||
return helpers.getIn( | ||
this.data, types.check(path, 'string|number') ? [path] : path | ||
this.data, type.String(path) || type.Number(path) ? [path] : path | ||
); | ||
@@ -281,8 +280,9 @@ }; | ||
/** | ||
* Type definition | ||
*/ | ||
types.add('baobab', function(v) { | ||
return v instanceof Baobab; | ||
}); | ||
Baobab.prototype.release = function() { | ||
this.unbindAll(); | ||
delete this.data; | ||
delete this._futureUpdate; | ||
delete this._history; | ||
delete this._registeredCursors; | ||
}; | ||
@@ -289,0 +289,0 @@ /** |
@@ -8,4 +8,4 @@ /** | ||
var EventEmitter = require('emmett'), | ||
types = require('./typology.js'), | ||
helpers = require('./helpers.js'); | ||
helpers = require('./helpers.js'), | ||
type = require('./type.js'); | ||
@@ -37,3 +37,3 @@ /** | ||
if (!types.check(first, 'cursor')) | ||
if (!type.Cursor(first)) | ||
throw Error('baobab.Combination: argument should be a cursor.'); | ||
@@ -93,36 +93,24 @@ | ||
*/ | ||
Combination.prototype.or = function(cursor) { | ||
function makeOperator(operator) { | ||
Combination.prototype[operator] = function(cursor) { | ||
// Safeguard | ||
if (!types.check(cursor, 'cursor')) | ||
throw Error('baobab.Combination.or: argument should be a cursor.'); | ||
// Safeguard | ||
if (!type.Cursor(cursor)) | ||
throw Error('baobab.Combination.' + operator + ': argument should be a cursor.'); | ||
if (~this.cursors.indexOf(cursor)) | ||
throw Error('baobab.Combination.or: cursor already in combination.'); | ||
if (~this.cursors.indexOf(cursor)) | ||
throw Error('baobab.Combination.' + operator + ': cursor already in combination.'); | ||
this.cursors.push(cursor); | ||
this.operators.push('or'); | ||
this.updates.length++; | ||
bindCursor(this, cursor); | ||
this.cursors.push(cursor); | ||
this.operators.push(operator); | ||
this.updates.length++; | ||
bindCursor(this, cursor); | ||
return this; | ||
}; | ||
return this; | ||
}; | ||
} | ||
Combination.prototype.and = function(cursor) { | ||
makeOperator('or'); | ||
makeOperator('and'); | ||
// Safeguard | ||
if (!types.check(cursor, 'cursor')) | ||
throw Error('baobab.Combination.and: argument should be a cursor.'); | ||
if (~this.cursors.indexOf(cursor)) | ||
throw Error('baobab.Combination.and: cursor already in combination.'); | ||
this.cursors.push(cursor); | ||
this.operators.push('and'); | ||
this.updates.length++; | ||
bindCursor(this, cursor); | ||
return this; | ||
}; | ||
Combination.prototype.release = function() { | ||
@@ -129,0 +117,0 @@ |
@@ -11,3 +11,3 @@ /** | ||
helpers = require('./helpers.js'), | ||
types = require('./typology.js'); | ||
type = require('./type.js'); | ||
@@ -62,3 +62,3 @@ /** | ||
// If path is not relevant to us, we break | ||
if (p !== self.solvedPath[j]) | ||
if (p !== '' + self.solvedPath[j]) | ||
break; | ||
@@ -120,3 +120,3 @@ | ||
Cursor.prototype.isLeaf = function() { | ||
return types.check(this.reference(), 'primitive'); | ||
return type.Primitive(this.reference()); | ||
}; | ||
@@ -135,3 +135,3 @@ | ||
if (!types.check(path, 'path')) | ||
if (!type.Path(path)) | ||
throw Error('baobab.Cursor.select: invalid path.'); | ||
@@ -207,3 +207,3 @@ return this.root.select(this.path.concat(path)); | ||
if (types.check(path, 'step')) | ||
if (type.Step(path)) | ||
return this.root.get(this.solvedPath.concat(path)); | ||
@@ -218,3 +218,3 @@ else | ||
if (types.check(path, 'step')) | ||
if (type.Step(path)) | ||
return this.root.reference(this.solvedPath.concat(path)); | ||
@@ -229,3 +229,3 @@ else | ||
if (types.check(path, 'step')) | ||
if (type.Step(path)) | ||
return this.root.clone(this.solvedPath.concat(path)); | ||
@@ -289,6 +289,6 @@ else | ||
Cursor.prototype.merge = function(o) { | ||
if (!types.check(o, 'object')) | ||
if (!type.Object(o)) | ||
throw Error('baobab.Cursor.merge: trying to merge a non-object.'); | ||
if (!types.check(this.reference(), 'object')) | ||
if (!type.Object(this.reference())) | ||
throw Error('baobab.Cursor.merge: trying to merge into a non-object.'); | ||
@@ -330,8 +330,5 @@ | ||
/** | ||
* Type definition | ||
*/ | ||
types.add('cursor', function(v) { | ||
return v instanceof Cursor; | ||
}); | ||
type.Cursor = function (value) { | ||
return value instanceof Cursor; | ||
}; | ||
@@ -338,0 +335,0 @@ /** |
@@ -7,3 +7,3 @@ /** | ||
*/ | ||
var types = require('typology'); | ||
var type = require('./type.js'); | ||
@@ -32,11 +32,11 @@ // Make a real array of an array-like object | ||
// Array | ||
if (types.get(item) === 'array') | ||
if (type.Array(item)) | ||
return item.slice(0); | ||
// Date | ||
if (types.get(item) === 'date') | ||
if (type.Date(item)) | ||
return new Date(item.getTime()); | ||
// Object | ||
if (types.get(item) === 'object') { | ||
if (type.Object(item)) { | ||
var k, o = {}; | ||
@@ -57,3 +57,3 @@ for (k in item) | ||
// Array | ||
if (types.get(item) === 'array') { | ||
if (type.Array(item)) { | ||
var i, l, a = []; | ||
@@ -66,7 +66,7 @@ for (i = 0, l = item.length; i < l; i++) | ||
// Date | ||
if (types.get(item) === 'date') | ||
if (type.Date(item)) | ||
return new Date(item.getTime()); | ||
// Object | ||
if (types.get(item) === 'object') { | ||
if (type.Object(item)) { | ||
var k, o = {}; | ||
@@ -113,6 +113,6 @@ for (k in item) | ||
for (k in spec) { | ||
if (types.get(spec[k]) === 'object') { | ||
if (type.Object(spec[k])) { | ||
ok = ok && compare(object[k]); | ||
} | ||
else if (types.get(spec[k]) === 'array') { | ||
else if (type.Array(spec[k])) { | ||
ok = ok && !!~spec[k].indexOf(object[k]); | ||
@@ -136,3 +136,3 @@ } | ||
function indexByComparison(object, spec) { | ||
return indexOf(object, function(e) { | ||
return index(object, function(e) { | ||
return compare(e, spec); | ||
@@ -155,3 +155,3 @@ }); | ||
if (typeof path[i] === 'function') { | ||
if (types.get(c) !== 'array') | ||
if (!type.Array(c)) | ||
return; | ||
@@ -162,3 +162,3 @@ | ||
else if (typeof path[i] === 'object') { | ||
if (types.get(c) !== 'array') | ||
if (!type.Array(c)) | ||
return; | ||
@@ -189,3 +189,3 @@ | ||
if (typeof path[i] === 'function') { | ||
if (types.get(c) !== 'array') | ||
if (!type.Array(c)) | ||
return; | ||
@@ -198,6 +198,6 @@ | ||
else if (typeof path[i] === 'object') { | ||
if (types.get(c) !== 'array') | ||
if (!type.Array(c)) | ||
return; | ||
idx = index(indexByComparison(c, path[i])); | ||
idx = indexByComparison(c, path[i]); | ||
solvedPath.push(idx); | ||
@@ -241,9 +241,2 @@ c = c[idx]; | ||
// Delay execution until next tick or frame | ||
var later = (typeof window === 'undefined') ? | ||
process.nextTick : | ||
('requestAnimationFrame' in window) ? | ||
window.requestAnimationFrame.bind(window) : | ||
function(fn) {setTimeout(fn, 0);}; | ||
module.exports = { | ||
@@ -257,5 +250,4 @@ arrayOf: arrayOf, | ||
inherits: inherits, | ||
later: later, | ||
pathObject: pathObject, | ||
solvePath: solvePath | ||
}; |
@@ -7,4 +7,4 @@ /** | ||
*/ | ||
var types = require('typology'), | ||
helpers = require('./helpers.js'); | ||
var helpers = require('./helpers.js'), | ||
type = require('./type.js'); | ||
@@ -16,15 +16,2 @@ // Helpers | ||
function hasOneOf(o, keys) { | ||
for (var i = 0, l = keys.length; i < l; i++) | ||
if (hasKey(o, keys[i])) | ||
return true; | ||
return false; | ||
} | ||
function hasCommand(o) { | ||
return Object.keys(o).some(function(k) { | ||
return k.charAt(0) === '$'; | ||
}); | ||
} | ||
function conflict(a, b, key) { | ||
@@ -84,7 +71,7 @@ return hasKey(a, key) && hasKey(b, key); | ||
if (current && types.check(next, 'object')) { | ||
if (current && type.Object(next)) { | ||
// $push conflict | ||
if (conflict(current, next, '$push')) { | ||
if (types.check(current.$push, 'array')) | ||
if (type.Array(current.$push)) | ||
current.$push = current.$push.concat(next.$push); | ||
@@ -97,3 +84,3 @@ else | ||
else if (conflict(current, next, '$unshift')) { | ||
if (types.check(next.$unshift, 'array')) | ||
if (type.Array(next.$unshift)) | ||
current.$unshift = next.$unshift.concat(current.$unshift); | ||
@@ -100,0 +87,0 @@ else |
@@ -7,4 +7,4 @@ /** | ||
*/ | ||
var types = require('./typology.js'), | ||
Combination = require('./combination.js'); | ||
var Combination = require('./combination.js'), | ||
type = require('./type.js'); | ||
@@ -21,5 +21,8 @@ module.exports = { | ||
this.tree = baobab; | ||
this.__type = null; | ||
// Is there any cursors to create? | ||
if (!this.cursor && !this.cursors) | ||
return {}; | ||
// Is there conflicting definitions? | ||
if (this.cursor && this.cursors) | ||
@@ -30,2 +33,5 @@ throw Error('baobab.mixin: you cannot have both ' + | ||
// Type | ||
this.__type = null; | ||
// Making update handler | ||
@@ -37,6 +43,6 @@ this.__updateHandler = (function() { | ||
if (this.cursor) { | ||
if (!types.check(this.cursor, 'string|number|array|cursor')) | ||
if (!type.MixinCursor(this.cursor)) | ||
throw Error('baobab.mixin.cursor: invalid data (cursor, string or array).'); | ||
if (!types.check(this.cursor, 'cursor')) | ||
if (!type.Cursor(this.cursor)) | ||
this.cursor = baobab.select(this.cursor); | ||
@@ -50,8 +56,8 @@ | ||
else if (this.cursors) { | ||
if (!types.check(this.cursors, 'object|array')) | ||
if (['object', 'array'].indexOf(type(this.cursors)) === -1) | ||
throw Error('baobab.mixin.cursor: invalid data (object or array).'); | ||
if (types.check(this.cursors, 'array')) { | ||
if (type.Array(this.cursors)) { | ||
this.cursors = this.cursors.map(function(path) { | ||
return types.check(path, 'cursor') ? path : baobab.select(path); | ||
return type.Cursor(path) ? path : baobab.select(path); | ||
}); | ||
@@ -68,3 +74,3 @@ | ||
for (var k in this.cursors) { | ||
if (!types.check(this.cursors[k], 'cursor')) | ||
if (!type.Cursor(this.cursors[k])) | ||
this.cursors[k] = baobab.select(this.cursors[k]); | ||
@@ -87,3 +93,4 @@ } | ||
if (this.__type === 'single') { | ||
this.cursor.on('update', this.__updateHandler); | ||
this.__combination = new Combination('or', [this.cursor]); | ||
this.__combination.on('update', this.__updateHandler); | ||
} | ||
@@ -105,8 +112,4 @@ else if (this.__type === 'array') { | ||
componentWillUnmount: function() { | ||
if (this.__type === 'single') { | ||
this.cursor.off('update', this.__updateHandler); | ||
} | ||
else { | ||
if (this.__combination) | ||
this.__combination.release(); | ||
} | ||
} | ||
@@ -113,0 +116,0 @@ }].concat(baobab.options.mixins) |
@@ -8,4 +8,4 @@ /** | ||
*/ | ||
var types = require('./typology.js'), | ||
helpers = require('./helpers.js'); | ||
var helpers = require('./helpers.js'), | ||
type = require('./type.js'); | ||
@@ -57,6 +57,6 @@ var COMMANDS = {}; | ||
case '$push': | ||
if (!types.check(o, 'array')) | ||
if (!type.Array(o)) | ||
throw makeError(path, 'using command $push to a non array'); | ||
if (!types.check(v, 'array')) | ||
if (!type.Array(v)) | ||
o.push(v); | ||
@@ -67,6 +67,6 @@ else | ||
case '$unshift': | ||
if (!types.check(o, 'array')) | ||
if (!type.Array(o)) | ||
throw makeError(path, 'using command $unshift to a non array'); | ||
if (!types.check(v, 'array')) | ||
if (!type.Array(v)) | ||
o.unshift(v); | ||
@@ -101,3 +101,3 @@ else | ||
if (!types.check(o[k], 'object')) | ||
if (!type.Object(o[k])) | ||
throw makeError(path.concat(k), 'using command $merge on a non-object'); | ||
@@ -115,3 +115,3 @@ | ||
if (!types.check(o[k], 'array')) | ||
if (!type.Array(o[k])) | ||
throw makeError(path.concat(k), 'using command $push to a non array'); | ||
@@ -123,3 +123,3 @@ o[k] = o[k].concat(v); | ||
if (!types.check(o[k], 'array')) | ||
if (!type.Array(o[k])) | ||
throw makeError(path.concat(k), 'using command $unshift to a non array'); | ||
@@ -126,0 +126,0 @@ o[k] = (v instanceof Array ? v : [v]).concat(o[k]); |
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 README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
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
1275
37112
13
1
0