Skip to content

Commit

Permalink
Merge pull request #473 from Nimelrian/master
Browse files Browse the repository at this point in the history
nb argument of splice now defaults to 0 if omitted
  • Loading branch information
Yomguithereal authored Feb 6, 2017
2 parents 3e6a08a + ee7dfec commit 60753b1
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 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
6 changes: 6 additions & 0 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,12 @@ export function solveUpdate(affectedPaths, comparedPaths) {
* @return {array} - The spliced array.
*/
export function splice(array, startIndex, nb, ...elements) {
if (nb === undefined)
nb = array.length - startIndex;
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
7 changes: 4 additions & 3 deletions src/type.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,12 @@ type.primitive = function(target) {
* @return {boolean}
*/
type.splicer = function(target) {
if (!type.array(target) || target.length < 2)
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']) &&
type.number(target[1]);
return anyOf(target[0], ['number', 'function', 'object']);
};

/**
Expand Down
24 changes: 24 additions & 0 deletions test/suites/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,30 @@ describe('Helpers', function() {
[{name: 'John'}, {name: 'Paul'}]
);
});

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 or passing null', function () {
const array = [0, 1, 2, 3, 4];

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 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.throws(function () {
splice(array, 2, "a");
}, Error);

assert.throws(function () {
splice(array, 2, {});
}, Error);
});
});
});

/**
Expand Down

0 comments on commit 60753b1

Please sign in to comment.