diff --git a/snippet_data/snippets.json b/snippet_data/snippets.json index 7d55849e0..4a3cbaf9d 100644 --- a/snippet_data/snippets.json +++ b/snippet_data/snippets.json @@ -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", diff --git a/snippets/shank.md b/snippets/shank.md new file mode 100644 index 000000000..daa944d19 --- /dev/null +++ b/snippets/shank.md @@ -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'] +``` + diff --git a/tag_database b/tag_database index 55c9927df..0487802f4 100644 --- a/tag_database +++ b/tag_database @@ -248,6 +248,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 diff --git a/test/shank/shank.js b/test/shank/shank.js new file mode 100644 index 000000000..bf9076a48 --- /dev/null +++ b/test/shank/shank.js @@ -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; diff --git a/test/shank/shank.test.js b/test/shank/shank.test.js new file mode 100644 index 000000000..e14414748 --- /dev/null +++ b/test/shank/shank.test.js @@ -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']); +}); +