Skip to content

Commit a7ced49

Browse files
authored
Merge pull request jsmapr1#4 from jmarking/master
Fix erroneous behavior if removable does not exist in the items array.
2 parents aa2e517 + e8d543f commit a7ced49

File tree

4 files changed

+38
-6
lines changed

4 files changed

+38
-6
lines changed

arrays/spread/slice.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
// # START:removeItemSlice
44
function removeItem(items, removable) {
5-
const index = items.indexOf(removable);
6-
return items.slice(0, index).concat(items.slice(index + 1));
5+
if (items.includes(removable)) {
6+
const index = items.indexOf(removable);
7+
return items.slice(0, index).concat(items.slice(index + 1));
8+
}
9+
return items;
710
}
811
// # END:removeItemSlice
912

arrays/spread/splice.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
/* eslint-disable no-unused-vars */
22
// # START:removeItemSplice
33
function removeItem(items, removable) {
4-
const index = items.indexOf(removable);
5-
items.splice(index, 1);
4+
if (items.includes(removable)) {
5+
const index = items.indexOf(removable);
6+
items.splice(index, 1);
7+
}
68
return items;
79
}
810
// # END:removeItemSplice

arrays/spread/spread.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
// # START:removeItemSpread
44
function removeItem(items, removable) {
5-
const index = items.indexOf(removable);
6-
return [...items.slice(0, index), ...items.slice(index + 1)];
5+
if (items.includes(removable)) {
6+
const index = items.indexOf(removable);
7+
return [...items.slice(0, index), ...items.slice(index + 1)];
8+
}
9+
return items;
710
}
811
// # END:removeItemSpread
912

arrays/spread/spread.spec.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,28 @@ describe('spread operator', () => {
5353
expect(formatBook(...values)).toEqual('Reasons and Persons by Derek Parfit $19.99');
5454
expect(formatBook(...Object.values(book))).toEqual('Reasons and Persons by Derek Parfit $19.99');
5555
});
56+
57+
it('should not modify with a loop', () => {
58+
const before = ['apple', 'banana', 'orange'];
59+
const after = ['apple', 'banana', 'orange'];
60+
expect(removeItemProblem(before, 'peach')).toEqual(after);
61+
});
62+
63+
it('should not modify with a slice', () => {
64+
const before = ['apple', 'banana', 'orange'];
65+
const after = ['apple', 'banana', 'orange'];
66+
expect(removeItemSlice(before, 'peach')).toEqual(after);
67+
});
68+
69+
it('should not modify with a splice', () => {
70+
const before = ['apple', 'banana', 'orange'];
71+
const after = ['apple', 'banana', 'orange'];
72+
expect(removeItemSplice(before, 'peach')).toEqual(after);
73+
});
74+
75+
it('should not modify with a spread', () => {
76+
const before = ['apple', 'banana', 'orange'];
77+
const after = ['apple', 'banana', 'orange'];
78+
expect(removeItem(before, 'peach')).toEqual(after);
79+
});
5680
});

0 commit comments

Comments
 (0)