From 23edd671370a7476a4a3315f969fe5fda2e11355 Mon Sep 17 00:00:00 2001 From: tsuyoshiwada Date: Sat, 24 Jun 2017 00:31:30 +0900 Subject: [PATCH 1/2] Fix bug a elements are not deleted at index 0 --- src/immutable-delete.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/immutable-delete.js b/src/immutable-delete.js index cb50a5e..0b362d9 100644 --- a/src/immutable-delete.js +++ b/src/immutable-delete.js @@ -12,7 +12,7 @@ * // -> resultArray ['a', 'b', 'd', 'e'] */ export default function immutableDelete(array, index) { - return index > 0 + return index >= 0 ? array.slice(0, index).concat(array.slice(index + 1)) : array.slice(0); } From 863c1e50c33c5c415c3817113db43d4925ec26eb Mon Sep 17 00:00:00 2001 From: tsuyoshiwada Date: Sat, 24 Jun 2017 00:32:18 +0900 Subject: [PATCH 2/2] Add immutableDelete tests for index 0 --- test/immutable-delete.spec.js | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/test/immutable-delete.spec.js b/test/immutable-delete.spec.js index 8d605e1..7745ad9 100644 --- a/test/immutable-delete.spec.js +++ b/test/immutable-delete.spec.js @@ -3,25 +3,30 @@ import chai from 'chai'; const expect = chai.expect; -const originalArray = ['a', 'b', 'c', 'd', 'e']; -const resultArray = del(originalArray, 2); +const arr = ['a', 'b', 'c', 'd', 'e']; +const res1 = del(arr, 2); +const res2 = del(arr, -2); +const res3 = del(arr, 0); describe('immutable-delete', () => { it('original array should stay untouched', () => { - expect(originalArray).to.not.eql(resultArray); + expect(arr).to.not.eql(res1); }); it('should have length 4', () => { - expect(resultArray).with.length(4); + expect(res1).with.length(4); }); it('element at index 2 should now be "d"', () => { - expect(resultArray[2]).to.equal('d'); + expect(res1[2]).to.equal('d'); }); it('result array should be equal to original if index is not positive integer', () => { - const res = del(originalArray, -2); - expect(res).with.length(originalArray.length); - expect(res).to.eql(['a', 'b', 'c', 'd', 'e']); + expect(res2).with.length(arr.length); + expect(res2).to.eql(['a', 'b', 'c', 'd', 'e']); + }); + + it('should delete "a" for index of 0', () => { + expect(res3).to.eql(['b', 'c', 'd', 'e']); }); });