Skip to content

Commit

Permalink
Merge pull request #1 from jalal246/dev
Browse files Browse the repository at this point in the history
Add new feat (moveMultiIndex)
  • Loading branch information
jalal246 committed Mar 27, 2020
2 parents 94e1ffc + c5259f5 commit c00b15b
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 26 deletions.
56 changes: 48 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -10,7 +10,7 @@ npm install move-position

```js
/**
* Move element form/to
* Moves element form/to index.
*
* @param {Array} [arr=[]]
* @param {number} from
Expand All @@ -26,16 +26,25 @@ 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"];
```

## 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)
Expand All @@ -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 inputs = [input1, input2];

const result = moveMultiple(total, 2, 0);
const result = moveMultiArr(inputs, 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
Expand All @@ -61,7 +98,10 @@ const result = moveMultiple(total, 2, 0);

- [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

Expand Down
40 changes: 29 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -20,7 +20,7 @@ function isNotInRange(arr, from, to) {
}

/**
* Move element form/to
* Moves element form/to index.
*
* @param {Array} [arr=[]]
* @param {number} from
Expand All @@ -39,19 +39,37 @@ function move(arr = [], from, to, isMutate = true) {
}

/**
* Move 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}
*/
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
};
41 changes: 34 additions & 7 deletions test.js
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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"];
Expand All @@ -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);
});
});

0 comments on commit c00b15b

Please sign in to comment.