Skip to content

Commit cf5b9e8

Browse files
author
Philipp Alferov
committed
Prevent invalid arguments entry
1 parent ca2caf7 commit cf5b9e8

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
'use strict';
2+
var util = require('util');
23

34
function createTree(list, rootNodes) {
45
var tree = [];
@@ -33,6 +34,9 @@ function orderByParents(list) {
3334
}
3435

3536
module.exports = function(obj) {
37+
if (!util.isArray(obj)) {
38+
throw new Error('Expected an object but got an invalid argument');
39+
}
3640
var cloned = obj.slice();
3741
var ordered = orderByParents(cloned);
3842
return createTree(ordered, ordered[0]);

test/test.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ var current;
99
describe('parent pointer array to tree', function() {
1010
describe('expected behavior', function() {
1111

12-
beforeEach(function() {
12+
before(function() {
1313
current = toTree(initial);
1414
});
1515

@@ -40,5 +40,17 @@ describe('parent pointer array to tree', function() {
4040
expect(current).to.be.deep.equal(expected);
4141
});
4242

43+
});
44+
45+
describe('with incorrect arguments', function() {
46+
it('should return an empty array if the empty array passed', function() {
47+
expect(toTree([])).to.be.deep.equal([]);
48+
});
49+
50+
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/);
53+
});
54+
4355
})
4456
});

0 commit comments

Comments
 (0)