diff --git a/README.md b/README.md index 716486a..8f302aa 100644 --- a/README.md +++ b/README.md @@ -6,22 +6,17 @@ npm install move-position ``` -## move +## API + +### move + +Moves element form one index to another ```js -/** - * Moves element form/to index. - * - * @param {Array} [arr=[]] - * @param {number} from - * @param {number} to - * @param {boolean} [isMutate=true] - * @returns {Array} - */ -const modifiedArr = move(arr, from, to, isMutate); +move(targetedArr: Array, from: number, to: number, isMutate?: boolean) ``` -### Example(1) +#### Example - Mutate true ```js const input = ["a", "b", "c"]; @@ -32,63 +27,73 @@ const result = move(input, 0, 2); // ["b", "c", "a"]; ``` -## moveMultiArr +Since `isMutate` is `true` by default: ```js -/** - * 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); +input === result; // true ``` -### Example(2) +#### Example - Mutate false ```js -const input1 = ["a1", "b1", "c1"]; -const input2 = ["a2", "b2", "c2"]; +const result = move(input, 0, 2, false); -const inputs = [input1, input2]; +input === result; // false +``` -const result = moveMultiArr(inputs, 2, 0); +### moveMultiArr -// result[0] > ["c1", "a1", "b1"]; -// result[1] > ["c2", "a2", "b2"]; +Moves the same index in multiple arrays + +```js +moveMultiArr(targetedArr: Array, from: number, to: number, isMutate?: boolean) ``` -## moveMultiIndex +#### Example - Move Multiple Arrays ```js -/** - * Moves multiple indexes in the same array - * - * @param {Array} [arr=[]] - * @param {Object[]} movingMap - * @returns {Array} new Array with index changes - */ -const modifiedArr = moveMultiIndex(arr, [{from, to}, ...]); +const input1 = ["a1", "b1", "c1"]; +const input2 = ["a2", "b2", "c2"]; + +const result = moveMultiArr([input1, input2], 2, 0); + +// result = [ +// ["c1", "a1", "b1"], +// ["c2", "a2", "b2"], +// ]; +``` + +### moveMultiIndex + +```ts +moveMultiArr(targetedArr: Array, movingMap: Array ) ``` -### Example(3) +#### Example - Move Multiple Index ```js const input = ["a", "b", "c"]; const movingMap = [ { from: 0, to: 2 }, - { from: 2, to: 1 } + { from: 2, to: 1 }, ]; const result = moveMultiIndex(input, movingMap); -// result > [ 'a', 'c', 'a' ] +// result = ["a", "c", "a"]; ``` +## Tests + +```sh +npm test +``` + +## License + +This project is licensed under the [GPL-3.0 License](https://github.com/jalal246/move-position/blob/master/LICENSE) + ### Related projects - [packageSorter](https://github.com/jalal246/packageSorter) - Sorting packages @@ -101,14 +106,9 @@ const result = moveMultiIndex(input, movingMap); - [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 - -```sh -npm test -``` - -## License +- [textics](https://github.com/jalal246/textics) & + [textics-stream](https://github.com/jalal246/textics-stream) - Counts lines, + words, chars and spaces for a given string. -This project is licensed under the [GPL-3.0 License](https://github.com/jalal246/move-position/blob/master/LICENSE) +- [folo](https://github.com/jalal246/folo) - Form & Layout Components Built with + React. diff --git a/test.js b/test.js index d74f4d2..e08033a 100644 --- a/test.js +++ b/test.js @@ -1,3 +1,4 @@ +// eslint-disable-next-line import/no-extraneous-dependencies const { expect } = require("chai"); const { move, moveMultiArr, moveMultiIndex } = require("./index"); @@ -19,6 +20,7 @@ describe("move-position", () => { const result = move(input, 0, 2); const expected = ["b1", "c1", "a1"]; + expect(result).to.have.ordered.members(expected); expect(input).to.have.ordered.members(expected); }); @@ -68,14 +70,14 @@ describe("move-position", () => { "folo-forms", "folo-layout", "folo-utils", - "folo-withcontext" + "folo-withcontext", ]; const movingMap = [ { from: 2, to: 0 }, { from: 3, to: 1 }, { from: 1, to: 2 }, - { from: 0, to: 3 } + { from: 0, to: 3 }, ]; const result = moveMultiIndex(input, movingMap); @@ -84,7 +86,7 @@ describe("move-position", () => { "folo-utils", "folo-withcontext", "folo-layout", - "folo-forms" + "folo-forms", ]; expect(result).to.have.deep.members(expected);