Skip to content

Commit

Permalink
Add new function
Browse files Browse the repository at this point in the history
  • Loading branch information
jalal246 committed Mar 27, 2020
1 parent b6112b6 commit 2aa5481
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 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 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) {
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
};

0 comments on commit 2aa5481

Please sign in to comment.