Skip to content

Commit

Permalink
Added more checks and tests for splice helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Nimelrian committed Feb 3, 2017
1 parent be507a6 commit d28587d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,9 @@ export function splice(array, startIndex, nb, ...elements) {
if (nb === undefined) {
nb = array.length - startIndex;
}
else if (nb === null || Number.isNaN(Number.parseInt(nb))) {
nb = 0;
}
nb = Math.max(0, nb);

// Solving startIndex
Expand Down
10 changes: 10 additions & 0 deletions test/suites/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,16 @@ describe('Helpers', function() {
assert.deepEqual(splice(array, 2), [0, 1]);

assert.deepEqual(splice(array, -2), [0, 1, 2]);

assert.deepEqual(splice(array, 2, null), [0, 1, 2, 3, 4]);
});

it('should delete no values when supplying an argument which is not parseable as number', function () {
const array = [0, 1, 2, 3, 4];

assert.deepEqual(splice(array, 2, "a"), [0, 1, 2, 3, 4]);

assert.deepEqual(splice(array, 2, {}), [0, 1, 2, 3, 4]);
});
});
});
Expand Down

0 comments on commit d28587d

Please sign in to comment.