Skip to content

Commit

Permalink
Supplying nonparseable nb argument now throws.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nimelrian committed Feb 6, 2017
1 parent d28587d commit ee7dfec
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,11 +302,16 @@ var newList = cursor.select('one').shift('two');
Splices the selected list. This will of course fail if the selected node is not a list.

The `splice` specifications works the same as for [`Array.prototype.splice`](https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Array/splice).
There is one exception though: Per specification, splice deletes no values if the `deleteCount` argument is not parseable as a number.
The `splice` implementation of Baobab instead throws an error, if the given `deleteCount` argument could not be parsed.

```js
// Splicing the list
var newList = cursor.splice([1, 1]);

// Omitting the deleteCount argument makes splice delete no elements.
var newList = cursor.splice([1]);

// Inserting an item etc.
var newList = cursor.splice([1, 0, 'newItem']);
var newList = cursor.splice([1, 0, 'newItem1', 'newItem2']);
Expand Down
8 changes: 4 additions & 4 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -570,12 +570,12 @@ export function solveUpdate(affectedPaths, comparedPaths) {
* @return {array} - The spliced array.
*/
export function splice(array, startIndex, nb, ...elements) {
if (nb === undefined) {
if (nb === undefined)
nb = array.length - startIndex;
}
else if (nb === null || Number.isNaN(Number.parseInt(nb))) {
else if (nb === null)
nb = 0;
}
else if (Number.isNaN(Number.parseInt(nb)))
throw new Error(`argument nb ${nb} can not be parsed into a number!`);
nb = Math.max(0, nb);

// Solving startIndex
Expand Down
2 changes: 2 additions & 0 deletions src/type.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ type.primitive = function(target) {
type.splicer = function(target) {
if (!type.array(target) || target.length < 1)
return false;
if(target.length > 1 && Number.isNaN(Number.parseInt(target[1])))
return false;

return anyOf(target[0], ['number', 'function', 'object']);
};
Expand Down
12 changes: 8 additions & 4 deletions test/suites/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ describe('Helpers', function() {
});

describe('Issue #472 - tree/cursor.splice does not conform with the specification as of ES6 (ECMAScript 2015)', function () {
it('should be possible to splice an array when omitting the nb (deleteCount) argument', function () {
it('should be possible to splice an array when omitting the nb (deleteCount) argument or passing null', function () {
const array = [0, 1, 2, 3, 4];

assert.deepEqual(splice(array, 2), [0, 1]);
Expand All @@ -235,12 +235,16 @@ describe('Helpers', function() {
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 () {
it('should throw an error when supplying an argument for nb (deleteCount) 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.throws(function () {
splice(array, 2, "a");
}, Error);

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

0 comments on commit ee7dfec

Please sign in to comment.