Skip to content

Commit 2f3921b

Browse files
author
Philipp Alferov
committed
Use data as the first argument
1 parent 723b24e commit 2f3921b

File tree

2 files changed

+13
-16
lines changed

2 files changed

+13
-16
lines changed

index.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,25 +45,24 @@ var groupByParents = function(array, options) {
4545
*
4646
* @name arrayToTree
4747
* @function
48+
*
49+
* @param {Array} data An array of data
4850
* @param {Object} options An object containing the following fields:
4951
*
5052
* - `parentProperty` (String): A name of a property where a link to
5153
* a parent node could be found. Default: 'parent_id'
52-
* - `data` (Array): An array of data
5354
* - `customID` (String): An unique node identifier. Default: 'id'
5455
*
5556
* @return {Array} Result of transformation
5657
*/
5758

58-
module.exports = function arrayToTree(options) {
59+
module.exports = function arrayToTree(data, options) {
60+
data = data || [];
5961
options = extend({
6062
parentProperty: 'parent_id',
61-
data: [],
6263
customID: 'id',
6364
rootID: '0'
64-
}, options);
65-
66-
var data = options.data;
65+
}, options || {});
6766

6867
if (!isArray(data)) {
6968
throw new Error('Expected an object but got an invalid argument');

test/test.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe('array-to-tree', function() {
1212
describe('with valid arguments', function() {
1313

1414
beforeEach(function() {
15-
current = toTree({ data: initial });
15+
current = toTree(initial);
1616
});
1717

1818
it('should not modify passed object', function() {
@@ -45,14 +45,14 @@ describe('array-to-tree', function() {
4545

4646
describe('with invalid arguments', function() {
4747
it('should return an empty array if the empty array passed', function() {
48-
expect(toTree({ data: [] })).to.be.deep.equal([]);
48+
expect(toTree([])).to.be.deep.equal([]);
4949
});
5050

5151
it('should throw an error if wrong arguments passed', function() {
52-
expect(toTree.bind(null, { data: 'string' }))
52+
expect(toTree.bind(null, 'string'))
5353
.to.throw(/invalid argument/);
5454

55-
expect(toTree.bind(null, { data: {} }))
55+
expect(toTree.bind(null, {}))
5656
.to.throw(/invalid argument/);
5757
});
5858

@@ -63,19 +63,17 @@ describe('array-to-tree', function() {
6363
return item;
6464
});
6565

66-
expect(toTree({ data: modified }))
66+
expect(toTree(modified))
6767
.to.be.deep.equal(modified);
6868
});
6969
})
7070

7171
describe('with different options', function() {
7272
it('should work with custom parents links', function() {
7373

74-
current = toTree({
75-
data: customInitial,
76-
parentProperty: 'parent',
77-
customID: '_id'
78-
});
74+
current = toTree(
75+
customInitial,
76+
{ parentProperty: 'parent', customID: '_id' });
7977

8078
expect(current)
8179
.to.be.deep.equal(customExpected);

0 commit comments

Comments
 (0)