Skip to content

Commit 24652f1

Browse files
author
Philipp Alferov
committed
Delegate some tasks to dependencies
1 parent e4b01b2 commit 24652f1

File tree

3 files changed

+20
-27
lines changed

3 files changed

+20
-27
lines changed

index.js

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,23 @@
11
'use strict';
2-
3-
var exists = function(obj, key) {
4-
return obj != null && Object.hasOwnProperty.call(obj, key);
5-
};
6-
7-
var extend = function(destination, source) {
8-
for (var property in source) {
9-
destination[property] = source[property];
10-
}
11-
return destination;
12-
}
13-
14-
var isArray = Array.isArray || function(obj) {
15-
return toString.call(obj) === '[object Array]';
16-
};
2+
var isArray = require('isarray');
3+
var extend = require('shallow-object-extend');
4+
var exists = require('property-exists');
175

186
var createTree = function(array, rootNodes, customID) {
197
var tree = [];
208

21-
for (var key in rootNodes) {
22-
if (!exists(rootNodes, key)) {
9+
for (var rootNode in rootNodes) {
10+
if (!exists(rootNodes, rootNode)) {
2311
continue ;
2412
}
25-
var parentNode = rootNodes[key];
26-
var childNode = array[parentNode[customID]];
13+
var node = rootNodes[rootNode];
14+
var childNode = array[node[customID]];
2715

2816
if (childNode) {
29-
parentNode.children = createTree(array, childNode, customID);
17+
node.children = createTree(array, childNode, customID);
3018
}
3119

32-
tree.push(parentNode);
20+
tree.push(node);
3321
}
3422

3523
return tree;
@@ -53,7 +41,7 @@ var groupByParents = function(array, options) {
5341

5442
/**
5543
* arrayToTree
56-
* Convert a plain array of nodes (with pointers to parent nodes) to a tree
44+
* Convert a plain array of nodes (with pointers to parent nodes) to a nested data structure
5745
*
5846
* @name arrayToTree
5947
* @function

package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"test": "mocha --reporter nyan",
88
"tdd": "npm test -- --watch"
99
},
10-
"repository": "alferov/array-to-tree",
10+
"repository": "alferov/property-exists",
1111
"keywords": [
1212
"array",
1313
"list",
@@ -29,5 +29,10 @@
2929
"engines": {
3030
"node": ">=0.10.0"
3131
},
32-
"license": "MIT"
32+
"license": "MIT",
33+
"dependencies": {
34+
"isarray": "0.0.1",
35+
"property-exists": "^1.0.1",
36+
"shallow-object-extend": "0.0.1"
37+
}
3338
}

readme.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# array-to-tree [![Build Status](https://travis-ci.org/alferov/array-to-tree.svg?branch=master)](https://travis-ci.org/alferov/array-to-tree)
22

3-
> Convert a plain array of nodes (with pointers to parent nodes) to a tree.
3+
> Convert a plain array of nodes (with pointers to parent nodes) to a nested data structure.
44
5-
Solves a problem with conversion of retrieved from a database sets of data to a nested structure (i.e. navigation tree).
5+
Solves a problem with conversion of retrieved from a database sets of data to a nested data structure (i.e. navigation tree).
66

77
## Install
88

@@ -88,7 +88,7 @@ var tree = arrayToTree({
8888
## API
8989

9090
### `arrayToTree(options)`
91-
Convert a plain array of nodes (with pointers to parent nodes) to a tree.
91+
Convert a plain array of nodes (with pointers to parent nodes) to a a nested data structure.
9292

9393
#### Params
9494
**Object** `options`: An object containing the following fields:

0 commit comments

Comments
 (0)