array-to-tree
Advanced tools
Comparing version
27
index.js
'use strict'; | ||
var isArray = require('isarray'); | ||
var extend = require('extend'); | ||
var exists = require('property-exists'); | ||
var isArray = require('lodash.isarray'); | ||
var assign = require('lodash.assign'); | ||
@@ -13,3 +12,3 @@ var createTree = function(array, rootNodes, customID) { | ||
if (!node && !exists(rootNodes, rootNode)) { | ||
if (!node && !rootNodes.hasOwnProperty(rootNode)) { | ||
continue ; | ||
@@ -29,16 +28,13 @@ } | ||
var groupByParents = function(array, options) { | ||
var parents = {}; | ||
array.forEach(function(item) { | ||
return array.reduce(function(prev, item) { | ||
var parentID = item[options.parentProperty] || options.rootID; | ||
if (parentID && exists(parents, parentID)) { | ||
parents[parentID].push(item); | ||
return ; | ||
if (parentID && prev.hasOwnProperty(parentID)) { | ||
prev[parentID].push(item); | ||
return prev; | ||
} | ||
parents[parentID] = [item]; | ||
}); | ||
return parents; | ||
prev[parentID] = [item]; | ||
return prev; | ||
}, {}); | ||
}; | ||
@@ -65,4 +61,3 @@ | ||
module.exports = function arrayToTree(data, options) { | ||
data = data || []; | ||
options = extend({ | ||
options = assign({ | ||
parentProperty: 'parent_id', | ||
@@ -69,0 +64,0 @@ customID: 'id', |
{ | ||
"name": "array-to-tree", | ||
"version": "2.1.11", | ||
"version": "2.1.12", | ||
"description": "Convert a plain array of nodes (with pointers to parent nodes) to a tree", | ||
@@ -9,3 +9,3 @@ "main": "index.js", | ||
"tdd": "npm test -- --watch", | ||
"release": "npm version ${BUMP:-\"patch\"} --no-git-tag-version && git add package.json && git commit -m \":octocat: Bump to $(cat package.json | jq -r .version)\" && git tag $(cat package.json | jq -r .version) && git push && git push --tags && npm publish" | ||
"release": "npm version ${BUMP:-\"patch\"} --no-git-tag-version && git add package.json && git commit -m \":octocat: Bump to $(cat package.json | json version)\" && git tag $(cat package.json | json version) && git push && git push --tags && npm publish" | ||
}, | ||
@@ -23,2 +23,3 @@ "repository": "alferov/array-to-tree", | ||
"devDependencies": { | ||
"json": "^9.0.3", | ||
"chai": "^3.2.0", | ||
@@ -37,6 +38,5 @@ "eslint": "^1.4.1", | ||
"dependencies": { | ||
"extend": "^3.0.0", | ||
"isarray": "0.0.1", | ||
"property-exists": "^1.0.1" | ||
"lodash.assign": "^4.0.6", | ||
"lodash.isarray": "^4.0.0" | ||
} | ||
} |
@@ -59,3 +59,3 @@ 'use strict'; | ||
it('should return the same array if there is no pointer to parent', function() { | ||
it('returns the same array if there is no pointer to parent', function() { | ||
@@ -70,3 +70,3 @@ var modified = initial.map(function(item) { | ||
}); | ||
}) | ||
}); | ||
@@ -73,0 +73,0 @@ describe('with different options', function() { |
2
-33.33%81978
-0.06%4
33.33%179
-1.65%+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
- Removed
- Removed
- Removed