From b6112b651e055d3c339b44009ac1298c401004d9 Mon Sep 17 00:00:00 2001 From: jalal246 Date: Fri, 27 Mar 2020 17:45:49 +0200 Subject: [PATCH 1/5] Add new test case that requires new changes --- test.js | 41 ++++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/test.js b/test.js index 5319f86..d74f4d2 100644 --- a/test.js +++ b/test.js @@ -1,6 +1,6 @@ const { expect } = require("chai"); -const { move, moveMultiple } = require("./index"); +const { move, moveMultiArr, moveMultiIndex } = require("./index"); describe("move-position", () => { it("returns the same input when params is invalid", () => { @@ -13,7 +13,7 @@ describe("move-position", () => { expect(result2).to.have.ordered.members(input); }); - it("move with mutation (default)", () => { + it("moves with mutation (default)", () => { const input = ["a1", "b1", "c1"]; const result = move(input, 0, 2); @@ -23,7 +23,7 @@ describe("move-position", () => { expect(input).to.have.ordered.members(expected); }); - it("move without mutation", () => { + it("moves without mutation", () => { const input = ["a1", "b1", "c1"]; const result = move(input, 0, 2, false); @@ -33,13 +33,13 @@ describe("move-position", () => { expect(input).to.have.ordered.members(input); }); - it("move multiple arrays with mutation (default)", () => { + it("moves multiple arrays with mutation (default)", () => { const input1 = ["a1", "b1", "c1"]; const input2 = ["a2", "b2", "c2"]; const total = [input1, input2]; - const result = moveMultiple(total, 2, 0); + const result = moveMultiArr(total, 2, 0); const expectedInput1 = ["c1", "a1", "b1"]; const expectedInput2 = ["c2", "a2", "b2"]; @@ -49,17 +49,44 @@ describe("move-position", () => { expect(input1).to.have.ordered.members(result[0]); }); - it("move multiple arrays without mutation", () => { + it("moves multiple arrays without mutation", () => { const input1 = ["a1", "b1", "c1"]; const input2 = ["a2", "b2", "c2"]; const total = [input1, input2]; - const result = moveMultiple(total, 2, 0, false); + const result = moveMultiArr(total, 2, 0, false); const expectedInput1 = ["c1", "a1", "b1"]; expect(result[0]).to.have.deep.members(expectedInput1); expect(input1).to.not.have.ordered.members(result[0]); }); + + it("tests with movingMap: Apply multi changes to same array", () => { + const input = [ + "folo-forms", + "folo-layout", + "folo-utils", + "folo-withcontext" + ]; + + const movingMap = [ + { from: 2, to: 0 }, + { from: 3, to: 1 }, + { from: 1, to: 2 }, + { from: 0, to: 3 } + ]; + + const result = moveMultiIndex(input, movingMap); + + const expected = [ + "folo-utils", + "folo-withcontext", + "folo-layout", + "folo-forms" + ]; + + expect(result).to.have.deep.members(expected); + }); }); From 2aa548101d08505ea34ec5d526de1e1f2028d832 Mon Sep 17 00:00:00 2001 From: jalal246 Date: Fri, 27 Mar 2020 17:50:50 +0200 Subject: [PATCH 2/5] Add new function --- index.js | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 67c2acd..eab7c5b 100644 --- a/index.js +++ b/index.js @@ -1,9 +1,9 @@ /** + * validator * - * - * @param {*} arr - * @param {*} from - * @param {*} to + * @param {Array} arr - array to validate + * @param {number} from - targeted index + * @param {number} to - targeted index * @returns */ function isNotInRange(arr, from, to) { @@ -20,7 +20,7 @@ function isNotInRange(arr, from, to) { } /** - * Move element form/to + * Moves element form/to index. * * @param {Array} [arr=[]] * @param {number} from @@ -39,7 +39,7 @@ function move(arr = [], from, to, isMutate = true) { } /** - * Move multiple arrays element from the same index. + * Moves multiple arrays element from the same index. * * @param {Array} [arr=[]] array contain arrays to be changed * @param {number} from @@ -47,11 +47,29 @@ function move(arr = [], from, to, isMutate = true) { * @param {boolean} [isMutate=true] * @returns {Array} */ -function moveMultiple(multiArr, from, to, isMutate) { +function moveMultiArr(multiArr, from, to, isMutate) { return multiArr.map(arr => move(arr, from, to, isMutate)); } +/** + * Moves multiple indexes in the same array. + * + * @param {Array} [arr=[]] + * @param {{ from, to }[]} movingMap + * @returns {Array} new Array with index changes + */ +function moveMultiIndex(arr = [], movingMap) { + const modified = arr.slice(); + + movingMap.forEach(({ from, to }) => { + modified[to] = arr[from]; + }); + + return modified; +} + module.exports = { move, - moveMultiple + moveMultiArr, + moveMultiIndex }; From fe0e5bd2eb0eea349e838513a562cacceffad30c Mon Sep 17 00:00:00 2001 From: jalal246 Date: Fri, 27 Mar 2020 18:26:42 +0200 Subject: [PATCH 3/5] Update desc with examples --- README.md | 47 ++++++++++++++++++++++++++++++++++++++++++----- index.js | 8 ++++---- 2 files changed, 46 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 833f744..682a59e 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ npm install move-position ```js /** - * Move element form/to + * Moves element form/to index. * * @param {Array} [arr=[]] * @param {number} from @@ -32,10 +32,19 @@ const result = move(input, 0, 2); // ["b", "c", "a"]; ``` -## moveMultiple +## moveMultiArr ```js -const modifiedArr = moveMultiple([arr1, arr2, ...], from, to, isMutate); +/** + * Moves the same index in multiple arrays + * + * @param {Array} [arr=[]] Array contain arrays to be changed + * @param {number} from - targeted index + * @param {number} to - targeted index + * @param {boolean} [isMutate=true] + * @returns {Array} + */ +const modifiedArr = moveMultiArr([arr1, arr2, ...], from, to, isMutate); ``` ### Example(2) @@ -44,14 +53,42 @@ const modifiedArr = moveMultiple([arr1, arr2, ...], from, to, isMutate); const input1 = ["a1", "b1", "c1"]; const input2 = ["a2", "b2", "c2"]; -const total = [input1, input2]; +const allInputs = [input1, input2]; -const result = moveMultiple(total, 2, 0); +const result = moveMultiArr(allInputs, 2, 0); // result[0] > ["c1", "a1", "b1"]; // result[1] > ["c2", "a2", "b2"]; ``` +## moveMultiIndex + +```js +/** + * Moves multiple indexes in the same array. + * + * @param {Array} [arr=[]] + * @param {{ from, to }[]} movingMap + * @returns {Array} new Array with index changes + */ +const modifiedArr = moveMultiIndex(arr, [{from, to}, ...]); +``` + +### Example(3) + +```js +const input = ["a", "b", "c"]; + +const movingMap = [ + { from: 0, to: 2 }, + { from: 2, to: 1 } +]; + +const result = moveMultiIndex(input, movingMap); + +// result > [ 'a', 'c', 'a' ] +``` + ### Related projects - [packageSorter](https://github.com/jalal246/packageSorter) - Sorting packages diff --git a/index.js b/index.js index eab7c5b..c4c0e8c 100644 --- a/index.js +++ b/index.js @@ -39,11 +39,11 @@ function move(arr = [], from, to, isMutate = true) { } /** - * Moves multiple arrays element from the same index. + * Moves the same index in multiple arrays * - * @param {Array} [arr=[]] array contain arrays to be changed - * @param {number} from - * @param {number} to + * @param {Array} [arr=[]] Array contain arrays to be changed + * @param {number} from - targeted index + * @param {number} to - targeted index * @param {boolean} [isMutate=true] * @returns {Array} */ From adb6551fce2fdee3563607dc6b634f6146bf2dd9 Mon Sep 17 00:00:00 2001 From: jalal246 Date: Fri, 27 Mar 2020 18:31:42 +0200 Subject: [PATCH 4/5] update related projects --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 682a59e..381ad6f 100644 --- a/README.md +++ b/README.md @@ -98,7 +98,10 @@ const result = moveMultiIndex(input, movingMap); - [corename](https://github.com/jalal246/corename) - Extracts package name. -- [get-info](https://github.com/jalal246/get-info) - Utility functions for projects production. +- [get-info](https://github.com/jalal246/get-info) - Utility functions for + projects production. + +- [textics](https://github.com/jalal246/textics) & [textics-stream](https://github.com/jalal246/textics-stream) - Counts lines, words, chars and spaces for a given string. ## Tests From c5259f56a2a2e45211f4fd6a3c6909996f5ad0db Mon Sep 17 00:00:00 2001 From: jalal246 Date: Fri, 27 Mar 2020 18:34:59 +0200 Subject: [PATCH 5/5] typos --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 381ad6f..71f065e 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # move-position -> move element in given array form index-A to index-B :scissors: +> Move element in given array form index-A to index-B :scissors: ```bash npm install move-position @@ -26,7 +26,7 @@ const modifiedArr = move(arr, from, to, isMutate); ```js const input = ["a", "b", "c"]; -// move element form index:0 to index:2 +// move element form index=0, to index=2 const result = move(input, 0, 2); // ["b", "c", "a"]; @@ -53,9 +53,9 @@ const modifiedArr = moveMultiArr([arr1, arr2, ...], from, to, isMutate); const input1 = ["a1", "b1", "c1"]; const input2 = ["a2", "b2", "c2"]; -const allInputs = [input1, input2]; +const inputs = [input1, input2]; -const result = moveMultiArr(allInputs, 2, 0); +const result = moveMultiArr(inputs, 2, 0); // result[0] > ["c1", "a1", "b1"]; // result[1] > ["c2", "a2", "b2"]; @@ -65,7 +65,7 @@ const result = moveMultiArr(allInputs, 2, 0); ```js /** - * Moves multiple indexes in the same array. + * Moves multiple indexes in the same array * * @param {Array} [arr=[]] * @param {{ from, to }[]} movingMap