Skip to content

Commit

Permalink
empty keyword or key should return origin tree
Browse files Browse the repository at this point in the history
  • Loading branch information
Jogis committed Jan 5, 2016
1 parent a222eee commit 081c292
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tree.io",
"version": "1.2.2",
"version": "1.2.3",
"description": "JS Tree shim for browser or nodejs enhance",
"main": "lib/index.js",
"files": [
Expand Down
12 changes: 11 additions & 1 deletion src/__test__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,23 @@ describe('Tree class', function(){
//filter child node
expectTree.children[0].children.splice(1, 1);
expectTree.children.splice(1, 2);
let filteredTree = tree.keyWordFilter('name', '1.1');
let filteredTree = tree.keyWordFilter('name', '1.1', true);
should(filteredTree).eql(expectTree);

//orgin tree data shouldn't be modified
let treeData = tree.getTree(true);
should(originTreeData).eql(treeData)
})

it('empty keyword or key should reutrn origin tree', function(){
let tree = new Tree(nestedTree);
let originTreeData = tree.getTree(true);
let filteredTree = tree.keyWordFilter('', '1.1', true);
should(filteredTree).eql(originTreeData);
filteredTree = tree.keyWordFilter('name', '', true);
should(filteredTree).eql(originTreeData);
})

it('constructor error detect', function(){
(function(){
new Tree(233);
Expand Down
8 changes: 6 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,17 @@ class Tree {
// }
// }, 'inOrder')
}
keyWordFilter(key, keyWord){
keyWordFilter(key, keyWord, flag){
flag = _.isBoolean(flag)?flag:false;
if(!key || !keyWord){
return this.getTree(flag);
}
return this.filter((tree, node) => {
if(node[key] === keyWord || (node[this.childrenPropName] && node[this.childrenPropName].length!=0)){
return true;
}
return false;
}, true)
}, flag)
}
}
module.exports = Tree;

0 comments on commit 081c292

Please sign in to comment.