shank() - array - duplicates Array.prototype.splice() except returns a new array instead of mutating in-place
Update shank.md Update shank.md Update shank.js Update shank.test.js Update tag_database remove testlog
This commit is contained in:
@ -5186,6 +5186,17 @@
|
||||
"hash": "a5a49641c0de1a0a80f192c5be195c448ab0fe0856bba4e1f246fe70af7b5431"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "shank",
|
||||
"type": "snippet",
|
||||
"attributes": {
|
||||
"fileName": "shank.md",
|
||||
"text": "Duplicates Array.prototype.splice, except it returns a new array rather than mutating the original array in place.",
|
||||
"tags": [
|
||||
"array"
|
||||
]
|
||||
},
|
||||
},
|
||||
{
|
||||
"id": "show",
|
||||
"type": "snippet",
|
||||
|
||||
23
snippets/shank.md
Normal file
23
snippets/shank.md
Normal file
@ -0,0 +1,23 @@
|
||||
### shank
|
||||
|
||||
Has the same functionality as [`Array.prototype.splice()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice), but returning a new array instead of mutating the original array.
|
||||
|
||||
Use `Array.slice()` and `Array.concat()` to get a new array with the new contents after removing existing elements and/or adding new elements.
|
||||
Omit the second argument, `index`, to start at `0`.
|
||||
Omit the third argument, `delCount`, to remove `0` elements.
|
||||
Omit the fourth argument, `elements`, in order to not add any new elements.
|
||||
|
||||
```js
|
||||
const shank = (arr, index = 0, delCount = 0, ...elements) =>
|
||||
arr.slice(0, index)
|
||||
.concat(elements)
|
||||
.concat(arr.slice(index + delCount));
|
||||
```
|
||||
|
||||
```js
|
||||
const names = ['alpha', 'bravo', 'charlie'];
|
||||
const namesAndDelta = shank(names, 1, 0, 'delta'); // [ 'alpha', 'delta', 'bravo', 'charlie' ]
|
||||
const namesNoBravo = shank(names, 1, 1); // [ 'alpha', 'charlie' ]
|
||||
console.log(names); // ['alpha', 'bravo', 'charlie']
|
||||
```
|
||||
|
||||
@ -247,6 +247,7 @@ sdbm:math,utility,intermediate
|
||||
serializeCookie:utility,string,intermediate
|
||||
setStyle:browser,beginner
|
||||
shallowClone:object,beginner
|
||||
shank:array,intermediate
|
||||
show:browser,css,beginner
|
||||
shuffle:array,random,intermediate
|
||||
similarity:array,math,beginner
|
||||
|
||||
5
test/shank/shank.js
Normal file
5
test/shank/shank.js
Normal file
@ -0,0 +1,5 @@
|
||||
const shank = (arr, index = 0, delCount = 0, ...elements) =>
|
||||
arr.slice(0, index)
|
||||
.concat(elements)
|
||||
.concat(arr.slice(index + delCount));
|
||||
module.exports = shank;
|
||||
22
test/shank/shank.test.js
Normal file
22
test/shank/shank.test.js
Normal file
@ -0,0 +1,22 @@
|
||||
const expect = require("expect");
|
||||
const shank = require("./shank.js");
|
||||
|
||||
test("shank is a Function", () => {
|
||||
expect(shank).toBeInstanceOf(Function);
|
||||
});
|
||||
|
||||
const names = ['alpha', 'bravo', 'charlie'];
|
||||
|
||||
test("Returns an array with the added elements.", () => {
|
||||
expect(shank(names, 1, 0, 'delta')).toEqual(['alpha', 'delta', 'bravo', 'charlie']);
|
||||
});
|
||||
|
||||
test("Returns an array with the removed elements.", () => {
|
||||
expect(shank(names, 1, 1)).toEqual(['alpha', 'charlie']);
|
||||
});
|
||||
|
||||
test("Does not mutate the original array", () => {
|
||||
shank(names, 1, 0, 'delta');
|
||||
expect(names).toEqual(['alpha', 'bravo', 'charlie']);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user