can-key-tree
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -165,1 +165,46 @@ var QUnit = require('steal-qunit'); | ||
} | ||
QUnit.test(".getNode and .get", function(){ | ||
var keyTree = new KeyTree([Object, Object, Array]); | ||
function handler1(){} | ||
function handler2(){} | ||
keyTree.add(["click","li", handler1] ); | ||
keyTree.add(["click","li", handler2] ); | ||
keyTree.add(["click","span", handler2] ); | ||
QUnit.deepEqual( keyTree.getNode(["click"]), { | ||
li: [handler1, handler2], | ||
span: [handler2] | ||
}, ".getNode works"); | ||
QUnit.deepEqual( keyTree.get(["click"]), [handler1, handler2, handler2], ".get works"); | ||
}); | ||
QUnit.test("lifecycle callbacks", function(){ | ||
var calls = 0; | ||
var keyTree = new KeyTree([Object, Object, Array],{ | ||
onFirst: function(){ | ||
QUnit.equal(calls, 1, "called when the first node is added"); | ||
}, | ||
onEmpty: function(){ | ||
QUnit.equal(calls, 3, "called when all nodes are removed"); | ||
} | ||
}); | ||
function handler1(){} | ||
function handler2(){} | ||
calls++; | ||
keyTree.add(["click","li", handler1] ); | ||
calls++; | ||
keyTree.add(["click","li", handler2] ); | ||
calls++; | ||
keyTree.delete([]); | ||
}); |
@@ -9,3 +9,5 @@ var reflect = require("can-reflect"); | ||
var KeyTree = function(treeStructure){ | ||
var KeyTree = function(treeStructure, callbacks){ | ||
this.callbacks = callbacks || {}; | ||
this.treeStructure = treeStructure; | ||
@@ -23,2 +25,3 @@ var FirstConstructor = treeStructure[0]; | ||
var place = this.root; | ||
var rootWasEmpty = reflect.size( this.root ) === 0; | ||
for(var i = 0; i < keys.length - 1 ; i++) { | ||
@@ -45,4 +48,35 @@ var key = keys[i]; | ||
} | ||
if(rootWasEmpty && this.callbacks.onFirst) { | ||
this.callbacks.onFirst.call(this); | ||
} | ||
}; | ||
function getDeep(item, items, depth, maxDepth) { | ||
if(maxDepth === depth) { | ||
if(reflect.isMoreListLikeThanMapLike(item)) { | ||
// remove each item | ||
reflect.addValues(items, reflect.toArray(item)); | ||
} else { | ||
// only containers supported at the end | ||
throw new Error("can-key-tree: Map types are not supported yet."); | ||
} | ||
} else { | ||
reflect.each(item, function(value){ | ||
getDeep(value, items, depth+1, maxDepth); | ||
}); | ||
} | ||
} | ||
KeyTree.prototype.get = function(keys) { | ||
var place = this.getNode(keys); | ||
if(this.treeStructure.length === keys.length) { | ||
return place; | ||
} else { | ||
// recurse deep | ||
var Type = this.treeStructure[this.treeStructure.length - 1]; | ||
var items = new Type(); | ||
getDeep(place, items, keys.length, this.treeStructure.length - 1); | ||
return items; | ||
} | ||
}; | ||
KeyTree.prototype.getNode = function(keys) { | ||
var place = this.root; | ||
@@ -103,2 +137,4 @@ for(var i = 0; i < keys.length; i++) { | ||
} | ||
} else if(!keys.length){ | ||
clear(place, 0, this.treeStructure.length - 1); | ||
} else { | ||
@@ -115,5 +151,8 @@ // we need to recursively clear the node's values | ||
} else { | ||
return true; | ||
break; | ||
} | ||
} | ||
if( this.callbacks.onEmpty && reflect.size( this.root ) === 0 ) { | ||
this.callbacks.onEmpty.call(this); | ||
} | ||
return true; | ||
@@ -120,0 +159,0 @@ }; |
@@ -1,2 +0,2 @@ | ||
/*can-key-tree@0.0.0#can-key-tree*/ | ||
/*can-key-tree@0.0.1#can-key-tree*/ | ||
define([ | ||
@@ -12,3 +12,4 @@ 'require', | ||
} | ||
var KeyTree = function (treeStructure) { | ||
var KeyTree = function (treeStructure, callbacks) { | ||
this.callbacks = callbacks || {}; | ||
this.treeStructure = treeStructure; | ||
@@ -24,2 +25,3 @@ var FirstConstructor = treeStructure[0]; | ||
var place = this.root; | ||
var rootWasEmpty = reflect.size(this.root) === 0; | ||
for (var i = 0; i < keys.length - 1; i++) { | ||
@@ -44,4 +46,31 @@ var key = keys[i]; | ||
} | ||
if (rootWasEmpty && this.callbacks.onFirst) { | ||
this.callbacks.onFirst.call(this); | ||
} | ||
}; | ||
function getDeep(item, items, depth, maxDepth) { | ||
if (maxDepth === depth) { | ||
if (reflect.isMoreListLikeThanMapLike(item)) { | ||
reflect.addValues(items, reflect.toArray(item)); | ||
} else { | ||
throw new Error('can-key-tree: Map types are not supported yet.'); | ||
} | ||
} else { | ||
reflect.each(item, function (value) { | ||
getDeep(value, items, depth + 1, maxDepth); | ||
}); | ||
} | ||
} | ||
KeyTree.prototype.get = function (keys) { | ||
var place = this.getNode(keys); | ||
if (this.treeStructure.length === keys.length) { | ||
return place; | ||
} else { | ||
var Type = this.treeStructure[this.treeStructure.length - 1]; | ||
var items = new Type(); | ||
getDeep(place, items, keys.length, this.treeStructure.length - 1); | ||
return items; | ||
} | ||
}; | ||
KeyTree.prototype.getNode = function (keys) { | ||
var place = this.root; | ||
@@ -92,2 +121,4 @@ for (var i = 0; i < keys.length; i++) { | ||
} | ||
} else if (!keys.length) { | ||
clear(place, 0, this.treeStructure.length - 1); | ||
} else { | ||
@@ -102,5 +133,8 @@ clear(reflect.getKeyValue(place, lastKey), keys.length, this.treeStructure.length - 1); | ||
} else { | ||
return true; | ||
break; | ||
} | ||
} | ||
if (this.callbacks.onEmpty && reflect.size(this.root) === 0) { | ||
this.callbacks.onEmpty.call(this); | ||
} | ||
return true; | ||
@@ -107,0 +141,0 @@ }; |
@@ -1,2 +0,2 @@ | ||
/*can-key-tree@0.0.0#can-key-tree*/ | ||
/*can-key-tree@0.0.1#can-key-tree*/ | ||
var reflect = require('can-reflect'); | ||
@@ -6,3 +6,4 @@ function isBuiltInPrototype(obj) { | ||
} | ||
var KeyTree = function (treeStructure) { | ||
var KeyTree = function (treeStructure, callbacks) { | ||
this.callbacks = callbacks || {}; | ||
this.treeStructure = treeStructure; | ||
@@ -18,2 +19,3 @@ var FirstConstructor = treeStructure[0]; | ||
var place = this.root; | ||
var rootWasEmpty = reflect.size(this.root) === 0; | ||
for (var i = 0; i < keys.length - 1; i++) { | ||
@@ -38,4 +40,31 @@ var key = keys[i]; | ||
} | ||
if (rootWasEmpty && this.callbacks.onFirst) { | ||
this.callbacks.onFirst.call(this); | ||
} | ||
}; | ||
function getDeep(item, items, depth, maxDepth) { | ||
if (maxDepth === depth) { | ||
if (reflect.isMoreListLikeThanMapLike(item)) { | ||
reflect.addValues(items, reflect.toArray(item)); | ||
} else { | ||
throw new Error('can-key-tree: Map types are not supported yet.'); | ||
} | ||
} else { | ||
reflect.each(item, function (value) { | ||
getDeep(value, items, depth + 1, maxDepth); | ||
}); | ||
} | ||
} | ||
KeyTree.prototype.get = function (keys) { | ||
var place = this.getNode(keys); | ||
if (this.treeStructure.length === keys.length) { | ||
return place; | ||
} else { | ||
var Type = this.treeStructure[this.treeStructure.length - 1]; | ||
var items = new Type(); | ||
getDeep(place, items, keys.length, this.treeStructure.length - 1); | ||
return items; | ||
} | ||
}; | ||
KeyTree.prototype.getNode = function (keys) { | ||
var place = this.root; | ||
@@ -86,2 +115,4 @@ for (var i = 0; i < keys.length; i++) { | ||
} | ||
} else if (!keys.length) { | ||
clear(place, 0, this.treeStructure.length - 1); | ||
} else { | ||
@@ -96,5 +127,8 @@ clear(reflect.getKeyValue(place, lastKey), keys.length, this.treeStructure.length - 1); | ||
} else { | ||
return true; | ||
break; | ||
} | ||
} | ||
if (this.callbacks.onEmpty && reflect.size(this.root) === 0) { | ||
this.callbacks.onEmpty.call(this); | ||
} | ||
return true; | ||
@@ -101,0 +135,0 @@ }; |
@@ -130,3 +130,3 @@ /*[global-shim-start]*/ | ||
/*can-key-tree@0.0.0#can-key-tree*/ | ||
/*can-key-tree@0.0.1#can-key-tree*/ | ||
define('can-key-tree', [ | ||
@@ -142,3 +142,4 @@ 'require', | ||
} | ||
var KeyTree = function (treeStructure) { | ||
var KeyTree = function (treeStructure, callbacks) { | ||
this.callbacks = callbacks || {}; | ||
this.treeStructure = treeStructure; | ||
@@ -154,2 +155,3 @@ var FirstConstructor = treeStructure[0]; | ||
var place = this.root; | ||
var rootWasEmpty = reflect.size(this.root) === 0; | ||
for (var i = 0; i < keys.length - 1; i++) { | ||
@@ -174,4 +176,31 @@ var key = keys[i]; | ||
} | ||
if (rootWasEmpty && this.callbacks.onFirst) { | ||
this.callbacks.onFirst.call(this); | ||
} | ||
}; | ||
function getDeep(item, items, depth, maxDepth) { | ||
if (maxDepth === depth) { | ||
if (reflect.isMoreListLikeThanMapLike(item)) { | ||
reflect.addValues(items, reflect.toArray(item)); | ||
} else { | ||
throw new Error('can-key-tree: Map types are not supported yet.'); | ||
} | ||
} else { | ||
reflect.each(item, function (value) { | ||
getDeep(value, items, depth + 1, maxDepth); | ||
}); | ||
} | ||
} | ||
KeyTree.prototype.get = function (keys) { | ||
var place = this.getNode(keys); | ||
if (this.treeStructure.length === keys.length) { | ||
return place; | ||
} else { | ||
var Type = this.treeStructure[this.treeStructure.length - 1]; | ||
var items = new Type(); | ||
getDeep(place, items, keys.length, this.treeStructure.length - 1); | ||
return items; | ||
} | ||
}; | ||
KeyTree.prototype.getNode = function (keys) { | ||
var place = this.root; | ||
@@ -222,2 +251,4 @@ for (var i = 0; i < keys.length; i++) { | ||
} | ||
} else if (!keys.length) { | ||
clear(place, 0, this.treeStructure.length - 1); | ||
} else { | ||
@@ -232,5 +263,8 @@ clear(reflect.getKeyValue(place, lastKey), keys.length, this.treeStructure.length - 1); | ||
} else { | ||
return true; | ||
break; | ||
} | ||
} | ||
if (this.callbacks.onEmpty && reflect.size(this.root) === 0) { | ||
this.callbacks.onEmpty.call(this); | ||
} | ||
return true; | ||
@@ -237,0 +271,0 @@ }; |
{ | ||
"name": "can-key-tree", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Add and remove items to a tree", | ||
@@ -5,0 +5,0 @@ "homepage": "", |
@@ -22,3 +22,3 @@ # can-key-tree | ||
```js | ||
var keyTree = new KeyTree([Object,Object,Array]) | ||
var keyTree = new KeyTree([Object,Object,Array],{onFirst, onEmpty}) | ||
``` | ||
@@ -25,0 +25,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
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
43870
922