Skip to content

Commit

Permalink
adds an isEmpty method for #11
Browse files Browse the repository at this point in the history
  • Loading branch information
justinbmeyer committed Jun 19, 2018
1 parent 1ac0232 commit d6017d9
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 9 deletions.
25 changes: 25 additions & 0 deletions can-key-tree-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,28 @@ QUnit.test("delete can get path of nodes deleted", function(){
});
QUnit.deepEqual(KEYS, [keys[0]], "got nodes that were deleted([key])");
});


QUnit.test('isEmpty', function () {
var keyTree = new KeyTree( [Object, Object, Array] );

function handler1 () {}
function handler2 () {}

QUnit.equal( keyTree.isEmpty(), true, "empty" );

keyTree.add( ["click", "li", handler1] );
keyTree.add( ["click", "li", handler2] );
QUnit.equal( keyTree.isEmpty(), false, "2" );

QUnit.deepEqual( keyTree.get( ["click", "li"] ), [handler1, handler2] );

keyTree.delete( ["click", "li", handler1] );
QUnit.equal( keyTree.isEmpty(), false, "1" );

keyTree.delete( ["click", "li", handler1] );
QUnit.equal( keyTree.isEmpty(), false, "empty" );

keyTree.delete( ["click", "li", handler2] );
QUnit.equal( keyTree.isEmpty(), true, "empty" );
});
29 changes: 20 additions & 9 deletions can-key-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,16 @@ function clearDeep ( node, keys, maxDepth, deleteHandler ) {
// ## KeyTree
// Creates an instance of the KeyTree.
var KeyTree = function ( treeStructure, callbacks ) {
this.callbacks = callbacks || {};

this.treeStructure = treeStructure;
var FirstConstructor = treeStructure[0];
if ( reflect.isConstructorLike( FirstConstructor ) ) {
this.root = new FirstConstructor();
} else {
this.root = FirstConstructor;
}
this.callbacks = callbacks || {};
this.treeStructure = treeStructure;
// An extra bit of state held for performance
this.empty = true;
};

// ## Methods
Expand All @@ -101,7 +102,7 @@ reflect.assign(KeyTree.prototype,{
var place = this.root;

// Record if the root was empty so we know to call `onFirst`.
var rootWasEmpty = reflect.size( this.root ) === 0;
var rootWasEmpty = this.empty === true;

// For each key, try to get the corresponding childNode.
for ( var i = 0; i < keys.length - 1; i++ ) {
Expand All @@ -128,8 +129,12 @@ reflect.assign(KeyTree.prototype,{
}

// Callback `onFirst` if appropriate.
if ( rootWasEmpty && this.callbacks.onFirst ) {
this.callbacks.onFirst.call( this );
if ( rootWasEmpty ) {
this.empty = false;
if(this.callbacks.onFirst) {
this.callbacks.onFirst.call( this );
}

}

return this;
Expand Down Expand Up @@ -229,16 +234,22 @@ reflect.assign(KeyTree.prototype,{
}
}
// Call `onEmpty` if the tree is now empty.
if ( this.callbacks.onEmpty && reflect.size( this.root ) === 0 ) {
this.callbacks.onEmpty.call( this );
if ( reflect.size( this.root ) === 0 ) {
this.empty = true;
if(this.callbacks.onEmpty) {
this.callbacks.onEmpty.call( this );
}
}
return true;
},
// ### size
// Recursively count the number of leaf values.
size: function () {
return getDeepSize( this.root, this.treeStructure.length - 1 );
}
},
isEmpty: function(){
return this.empty;
}
});

module.exports = KeyTree;
20 changes: 20 additions & 0 deletions doc/isEmpty.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
@function can-key-tree.prototype.isEmpty isEmpty
@parent can-key-tree.prototype

@signature `keyTree.isEmpty()`

Returns if the keyTree is empty.

```js
var keyTree = new KeyTree( [Object, Object, Array] );

function handler1 () {}
function handler2 () {}
keyTree.isEmpty(); //-> true

keyTree.add( ["click", "li", handler1] );
keyTree.add( ["click", "li", handler2] );
keyTree.isEmpty(); //-> false
```

@return {Boolean} Returns `true` if the keyTree is empty, `false` if otherwise.

0 comments on commit d6017d9

Please sign in to comment.