Skip to content

Commit be737ad

Browse files
author
Philipp Alferov
committed
Change API
1 parent cf5b9e8 commit be737ad

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

index.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,15 @@ function orderByParents(list) {
3333
return parents;
3434
}
3535

36-
module.exports = function(obj) {
37-
if (!util.isArray(obj)) {
36+
module.exports = function(options) {
37+
var config = util._extend({ parentProperty: 'parent_id', data: [] }, options);
38+
var data = config.data;
39+
40+
if (!util.isArray(data)) {
3841
throw new Error('Expected an object but got an invalid argument');
3942
}
40-
var cloned = obj.slice();
43+
44+
var cloned = data.slice();
4145
var ordered = orderByParents(cloned);
4246
return createTree(ordered, ordered[0]);
4347
};

test/test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe('parent pointer array to tree', function() {
1010
describe('expected behavior', function() {
1111

1212
before(function() {
13-
current = toTree(initial);
13+
current = toTree({ data: initial });
1414
});
1515

1616
it('should not modify passed object', function() {
@@ -44,12 +44,12 @@ describe('parent pointer array to tree', function() {
4444

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

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

5555
})

0 commit comments

Comments
 (0)