Skip to content

Commit

Permalink
Merge pull request #2 from jalal246/dev
Browse files Browse the repository at this point in the history
Update docs
  • Loading branch information
jalal246 committed May 6, 2020
2 parents 99bddbb + d61c676 commit 437d6da
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 55 deletions.
104 changes: 52 additions & 52 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"];
Expand All @@ -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<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 <FromToObj>)
```

### 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
Expand All @@ -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.
8 changes: 5 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// eslint-disable-next-line import/no-extraneous-dependencies
const { expect } = require("chai");

const { move, moveMultiArr, moveMultiIndex } = require("./index");
Expand All @@ -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);
});
Expand Down Expand Up @@ -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);
Expand All @@ -84,7 +86,7 @@ describe("move-position", () => {
"folo-utils",
"folo-withcontext",
"folo-layout",
"folo-forms"
"folo-forms",
];

expect(result).to.have.deep.members(expected);
Expand Down

0 comments on commit 437d6da

Please sign in to comment.