New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

un-flatten-tree

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

un-flatten-tree - npm Package Compare versions

Comparing version

to
0.1.1

.travis.yml

8

package.json
{
"name": "un-flatten-tree",
"version": "0.1.0",
"version": "0.1.1",
"description": "Functions for converting trees to lists and vice versa.",

@@ -11,3 +11,4 @@ "main": "un-flatten-tree.js",

"test": "mocha --reporter spec",
"prepublish": "uglifyjs -c -m -o un-flatten-tree.min.js un-flatten-tree.js"
"prepublish": "uglifyjs -c -m -o un-flatten-tree.min.js un-flatten-tree.js",
"cover": "node_modules/istanbul/lib/cli.js cover node_modules/mocha/bin/_mocha -- -R spec test/*"
},

@@ -31,2 +32,5 @@ "repository": {

"chai": "^3.5.0",
"coveralls": "^2.11.8",
"istanbul": "^0.4.2",
"lodash.map": "^4.2.1",
"mocha": "^2.4.5",

@@ -33,0 +37,0 @@ "uglify-js": "^2.6.2"

'use strict';
var expect = require('chai').expect;
var map = require('lodash.map');
var uft = require('../un-flatten-tree');

@@ -86,6 +87,8 @@

expect(uft.flatten(
tree,
function (node) { return node.i; }
)).to.eql(list);
expect(
uft.flatten(
tree,
function (node) { return node.i; }
)
).to.eql(list);
});

@@ -120,12 +123,14 @@

expect(uft.flatten(
tree,
function (node) { return node.items; },
function (node, parentNode) {
return {
id: node.id,
pid: parentNode !== undefined ? parentNode.id : null
};
}
)).to.eql(list);
expect(
uft.flatten(
tree,
function (node) { return node.items; },
function (node, parentNode) {
return {
id: node.id,
pid: parentNode !== undefined ? parentNode.id : null
};
}
)
).to.eql(list);
});

@@ -149,7 +154,9 @@

expect(uft.flatten(
tree,
function (node) { return node.getChildren(); },
function (node) { return node.getName(); }
)).to.eql(list);
expect(
uft.flatten(
tree,
function (node) { return node.getChildren(); },
function (node) { return node.getName(); }
)
).to.eql(list);
});

@@ -180,14 +187,16 @@

expect(uft.flatten(
tree,
function (node) { return node.items; },
function (node, parentNode, nodeId, parentId) {
return {
name: node.name,
id: nodeId,
pid: parentId
};
},
function () { return cnt++; }
)).to.eql(list);
expect(
uft.flatten(
tree,
function (node) { return node.items; },
function (node, parentNode, nodeId, parentId) {
return {
name: node.name,
id: nodeId,
pid: parentId
};
},
function () { return cnt++; }
)
).to.eql(list);
});

@@ -219,8 +228,59 @@

expect(uft.flatten(
tree,
function (node) { return node.items; },
function (node) { return {name: node.name}; }
)).to.eql(list);
expect(
uft.flatten(
tree,
function (node) { return node.items; },
function (node) { return {name: node.name}; }
)
).to.eql(list);
});
//test case taken from here - http://stackoverflow.com/q/23919887/4134913
it('should convert tree to list of "routes"', function () {
var tree = {
"f": {
"t": "100",
"f": {
"i": ['150'],
"b": ['300'],
"f": {
"k": 100
}
},
"l": ['255']
},
"c": {
"s": {
"t": ["100"]
},
"t": "100"
}
};
var list = ["ft", "ffi", "ffb", "fffk", "fl", "cst", "ct"];
function walk(tree) {
return map(tree, function (v, k) {
return {
name: k,
items: (typeof v !== 'object' || v instanceof Array) ? [] : walk(v)
};
});
}
function getChildNodes(node) {
return (node.items || []).map(function (item) {
return {
name: node.name + item.name,
items: item.items
};
});
}
expect(
uft.flatten(walk(tree), getChildNodes)
.filter(function (node) { return node.items.length === 0; })
.map(function (node) { return node.name; })
).to.eql(list);
});
});

@@ -264,8 +324,10 @@

expect(uft.unflatten(
list,
function (node, parentNode) { return node.pid === parentNode.id; },
function (node, parentNode) { parentNode.items.push(node); },
function (node) { return {id: node.id, items: []}; }
)).to.eql(tree);
expect(
uft.unflatten(
list,
function (node, parentNode) { return node.pid === parentNode.id; },
function (node, parentNode) { parentNode.items.push(node); },
function (node) { return {id: node.id, items: []}; }
)
).to.eql(tree);
});

@@ -294,8 +356,10 @@

expect(uft.unflatten(
list,
function (node, parentNode) { return node.pid === parentNode.id; },
function (node, parentNode) { parentNode.addChild(node); },
function (node) { return new Node(node.name); }
)).to.eql(tree);
expect(
uft.unflatten(
list,
function (node, parentNode) { return node.pid === parentNode.id; },
function (node, parentNode) { parentNode.addChild(node); },
function (node) { return new Node(node.name); }
)
).to.eql(tree);
});

@@ -331,9 +395,38 @@

expect(uft.unflatten(
list,
function (node, parentNode) { return node.pid === parentNode.id; },
function (node, parentNode) { parentNode.items.push(node); },
function (node) { return {id: node.id, items: []}; }
)).to.eql(tree);
expect(
uft.unflatten(
list,
function (node, parentNode) { return node.pid === parentNode.id; },
function (node, parentNode) { parentNode.items.push(node); },
function (node) { return {id: node.id, items: []}; }
)
).to.eql(tree);
});
});
it('should convert list to tree without converting nodes', function () {
var tree = [
{id: 3, pid: null, items: [
{id: 8, pid: 3, items: [
{id: 2, pid: 8, items: [
{id: 1, pid: 2, items: []}
]}
]}
]}
];
var list = [
{id: 8, pid: 3, items: []},
{id: 3, pid: null, items: []},
{id: 2, pid: 8, items: []},
{id: 1, pid: 2, items: []}
];
expect(
uft.unflatten(
list,
function (node, parentNode) { return node.pid === parentNode.id; },
function (node, parentNode) { parentNode.items.push(node); }
)
).to.eql(tree);
});
});

@@ -16,24 +16,14 @@ (function(root) {

var iterate = generateId === undefined
? function (flatten, nodes, parentNode) {
forEach(nodes, function (node) {
flatten.push(convertNode(node, parentNode));
var iterate = function (flatten, nodes, parentNode, parentNodeId) {
forEach(nodes, function (node) {
var nodeId = generateId === undefined ? undefined : generateId(node);
iterate(flatten, getChildNodes(node), node);
});
flatten.push(convertNode(node, parentNode, nodeId, parentNodeId));
return flatten;
}
: function (flatten, nodes, parentNode, parentNodeId) {
forEach(nodes, function (node) {
var nodeId = generateId(node);
iterate(flatten, getChildNodes(node), node, nodeId);
});
flatten.push(convertNode(node, parentNode, nodeId, parentNodeId));
return flatten;
};
iterate(flatten, getChildNodes(node), node, nodeId);
});
return flatten;
};
return iterate([], tree);

@@ -40,0 +30,0 @@ }

@@ -1,1 +0,1 @@

!function(n){"use strict";function u(n){return n}function t(n,t,r,i){r=void 0===r?u:r;var e=void 0===i?function(n,u,i){return o(u,function(u){n.push(r(u,i)),e(n,t(u),u)}),n}:function(n,u,f,d){return o(u,function(u){var o=i(u);n.push(r(u,f,o,d)),e(n,t(u),u,o)}),n};return e([],n)}function r(n,u,t,r){return void 0===r?i(n,function(r,o){var i=f(n,function(n){return u(o,n)});void 0===i?r.push(o):t(o,i)},[]):(n=e(n,function(n){return{"in":n,out:r(n)}}),i(n,function(r,o){var i=f(n,function(n){return u(o["in"],n["in"])});void 0===i?r.push(o.out):t(o.out,f(n,function(n){return n["in"]===i["in"]}).out)},[]))}var o=n._&&n._.forEach||require("lodash.foreach"),i=n._&&n._.transform||require("lodash.transform"),e=n._&&n._.map||require("lodash.map"),f=n._&&n._.find||require("lodash.find"),d={flatten:t,unflatten:r};"undefined"!=typeof module&&"undefined"!=typeof module.exports?module.exports=d:n.uft=d}(this);
!function(n){"use strict";function u(n){return n}function r(n,r,t,i){t=void 0===t?u:t;var e=function(n,u,f,d){return o(u,function(u){var o=void 0===i?void 0:i(u);n.push(t(u,f,o,d)),e(n,r(u),u,o)}),n};return e([],n)}function t(n,u,r,t){return void 0===t?i(n,function(t,o){var i=f(n,function(n){return u(o,n)});void 0===i?t.push(o):r(o,i)},[]):(n=e(n,function(n){return{"in":n,out:t(n)}}),i(n,function(t,o){var i=f(n,function(n){return u(o["in"],n["in"])});void 0===i?t.push(o.out):r(o.out,f(n,function(n){return n["in"]===i["in"]}).out)},[]))}var o=n._&&n._.forEach||require("lodash.foreach"),i=n._&&n._.transform||require("lodash.transform"),e=n._&&n._.map||require("lodash.map"),f=n._&&n._.find||require("lodash.find"),d={flatten:r,unflatten:t};"undefined"!=typeof module&&"undefined"!=typeof module.exports?module.exports=d:n.uft=d}(this);