Skip to content

Commit

Permalink
Add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
jalal246 committed Mar 2, 2020
1 parent ad6e873 commit 5d9a9f7
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
74 changes: 74 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# move-position

> move element in given array form position A to position B :scissors:
```bash
npm install move-position
```

## move

```js
/**
* Move element form/to
*
* @param {Array} [arr=[]]
* @param {number} from
* @param {number} to
* @param {boolean} [isMutate=true]
* @returns {Array}
*/
const modifiedArr = move(arr, from, to, isMutate);
```

### Example(1)

```js
const input = ["a1", "b1", "c1"];

// move element form index:0 to index:2
const result = move(input, 0, 2);

// ["b1", "c1", "a1"];
```

## moveMultiple

```js
const modifiedArr = moveMultiple([arr1, arr2], from, to, isMutate);
```

### Example(2)

```js
const input1 = ["a1", "b1", "c1"];
const input2 = ["a2", "b2", "c2"];

const total = [input1, input2];

const result = moveMultiple(total, 2, 0);

// result[0] > ["c1", "a1", "b1"];
// result[1] > ["c2", "a2", "b2"];
```

### Related projects

- [packageSorter](https://github.com/jalal246/packageSorter) - Sorting packages
for monorepos production.

- [builderz](https://github.com/jalal246/builderz) - Building your project with zero config.

- [corename](https://github.com/jalal246/corename) - Extracts package name.

- [get-info](https://github.com/jalal246/get-info) - Utility functions for projects production.

## Tests

```sh
npm test
```

## License

This project is licensed under the [GPL-3.0 License](https://github.com/jalal246/move-position/blob/master/LICENSE)
26 changes: 26 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/**
*
*
* @param {*} arr
* @param {*} from
* @param {*} to
* @returns
*/
function isNotInRange(arr, from, to) {
const { length } = arr;

Expand All @@ -11,6 +19,15 @@ function isNotInRange(arr, from, to) {
);
}

/**
* Move element form/to
*
* @param {Array} [arr=[]]
* @param {number} from
* @param {number} to
* @param {boolean} [isMutate=true]
* @returns {Array}
*/
function move(arr = [], from, to, isMutate = true) {
if (isNotInRange(arr, from, to)) return arr;

Expand All @@ -21,6 +38,15 @@ function move(arr = [], from, to, isMutate = true) {
return modified;
}

/**
* Move multiple arrays element from the same index.
*
* @param {Array} [arr=[]] array contain arrays to be changed
* @param {number} from
* @param {number} to
* @param {boolean} [isMutate=true]
* @returns {Array}
*/
function moveMultiple(multiArr, from, to, isMutate) {
return multiArr.map(arr => move(arr, from, to, isMutate));
}
Expand Down

0 comments on commit 5d9a9f7

Please sign in to comment.