Skip to content

Commit 35e4f7b

Browse files
committed
Merge remote-tracking branch 'gh/master'
2 parents eb81a5e + f247472 commit 35e4f7b

18 files changed

+1867
-1385
lines changed

.eslintrc

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,14 @@
1-
root: true
2-
3-
env:
4-
node: true
5-
mocha: true
6-
7-
extends:
8-
"eslint:recommended"
9-
10-
rules:
11-
array-bracket-spacing: [2, "never"]
12-
block-scoped-var: 2
13-
brace-style: [2, "1tbs"]
14-
camelcase: 1
15-
curly: 2
16-
eol-last: 2
17-
eqeqeq: [2, "smart"]
18-
indent: [2, 2, { "SwitchCase": 1 }]
19-
max-depth: [1, 3]
20-
max-len: [1, 80]
21-
max-statements: [1, 15]
22-
new-cap: 1
23-
no-extend-native: 2
24-
no-mixed-spaces-and-tabs: 2
25-
no-trailing-spaces: 2
26-
no-unused-vars: 1
27-
no-use-before-define: [2, "nofunc"]
28-
object-curly-spacing: [2, "never"]
29-
quotes: [2, "single", "avoid-escape"]
30-
semi: [2, "always"]
31-
space-after-keywords: [2, "always"]
32-
space-unary-ops: 2
1+
{
2+
"extends": "eslint:recommended",
3+
"rules": {
4+
"comma-dangle": "off"
5+
},
6+
"env": {
7+
"browser": true,
8+
"node": true,
9+
"mocha": true
10+
},
11+
"parserOptions": {
12+
"ecmaVersion": 6
13+
}
14+
}

.prettierrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"singleQuote": true,
3+
"trailingComma": "none"
4+
}

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ notifications:
77
email: false
88
node_js:
99
- 'stable'
10+
- '6'
1011
branches:
1112
except:
1213
- "/^v\\d+\\.\\d+\\.\\d+$/"

index.js

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
'use strict';
2-
var property = require('nested-property');
3-
var keyBy = require('lodash.keyby');
2+
const property = require('nested-property');
3+
const keyBy = require('lodash.keyby');
44

5-
var isArray = Array.isArray || function (arg) {
6-
return Object.prototype.toString.call(arg) === '[object Array]';
7-
};
8-
9-
var createTree = function (array, rootNodes, customID, childrenProperty) {
10-
var tree = [];
5+
const createTree = (array, rootNodes, customID, childrenProperty) => {
6+
const tree = [];
117

12-
for (var rootNode in rootNodes) {
13-
var node = rootNodes[rootNode];
14-
var childNode = array[node[customID]];
8+
for (const rootNode in rootNodes) {
9+
const node = rootNodes[rootNode];
10+
const childNode = array[node[customID]];
1511

1612
if (!node && !rootNodes.hasOwnProperty(rootNode)) {
1713
continue;
1814
}
1915

2016
if (childNode) {
21-
node[childrenProperty] = createTree(array, childNode, customID, childrenProperty);
17+
node[childrenProperty] = createTree(
18+
array,
19+
childNode,
20+
customID,
21+
childrenProperty
22+
);
2223
}
2324

2425
tree.push(node);
@@ -27,11 +28,11 @@ var createTree = function (array, rootNodes, customID, childrenProperty) {
2728
return tree;
2829
};
2930

30-
var groupByParents = function (array, options) {
31-
var arrayByID = keyBy(array, options.customID);
31+
const groupByParents = (array, options) => {
32+
const arrayByID = keyBy(array, options.customID);
3233

33-
return array.reduce(function (prev, item) {
34-
var parentID = property.get(item, options.parentProperty);
34+
return array.reduce((prev, item) => {
35+
let parentID = property.get(item, options.parentProperty);
3536
if (!parentID || !arrayByID.hasOwnProperty(parentID)) {
3637
parentID = options.rootID;
3738
}
@@ -58,24 +59,34 @@ var groupByParents = function (array, options) {
5859
* @param {Object} options An object containing the following fields:
5960
*
6061
* - `parentProperty` (String): A name of a property where a link to
61-
* a parent node could be found. Default: 'parent_id'
62+
* a parent node could be found. Default: 'parent_id'
6263
* - `customID` (String): An unique node identifier. Default: 'id'
64+
* - `childrenProperty` (String): A name of a property where children nodes
65+
* are going to be stored. Default: 'children'.
6366
*
6467
* @return {Array} Result of transformation
6568
*/
6669

6770
module.exports = function arrayToTree(data, options) {
68-
options = Object.assign({
69-
childrenProperty: 'children',
70-
parentProperty: 'parent_id',
71-
customID: 'id',
72-
rootID: '0'
73-
}, options);
71+
options = Object.assign(
72+
{
73+
parentProperty: 'parent_id',
74+
childrenProperty: 'children',
75+
customID: 'id',
76+
rootID: '0'
77+
},
78+
options
79+
);
7480

75-
if (!isArray(data)) {
81+
if (!Array.isArray(data)) {
7682
throw new TypeError('Expected an object but got an invalid argument');
7783
}
7884

79-
var grouped = JSON.parse(JSON.stringify(groupByParents(data, options))); // clone
80-
return createTree(grouped, grouped[options.rootID], options.customID, options.childrenProperty);
85+
const grouped = groupByParents(data.slice(), options);
86+
return createTree(
87+
grouped,
88+
grouped[options.rootID],
89+
options.customID,
90+
options.childrenProperty
91+
);
8192
};

0 commit comments

Comments
 (0)