array-move
Advanced tools
Comparing version 3.0.1 to 4.0.0
@@ -1,41 +0,49 @@ | ||
declare const arrayMove: { | ||
/** | ||
Clones the given `array`, moves the item to a new position in the new array, and then returns the new array. The given `array` is not mutated. | ||
/** | ||
Clones the given `array`, moves the item to a new position in the new array, and then returns the new array. The given `array` is not mutated. | ||
@param array - The array with the item to move. | ||
@param from - Index of item to move. If negative, it will begin that many elements from the end. | ||
@param to - Index of where to move the item. If negative, it will begin that many elements from the end. | ||
@returns A new array with the item moved to the new position. | ||
@param array - The array with the item to move. | ||
@param fromIndex - The index of item to move. If negative, it will begin that many elements from the end. | ||
@param toIndex - The index of where to move the item. If negative, it will begin that many elements from the end. | ||
@returns A new array with the item moved to the new position. | ||
@example | ||
``` | ||
import arrayMove = require('array-move'); | ||
@example | ||
``` | ||
import {arrayMoveImmutable} from 'array-move'; | ||
const input = ['a', 'b', 'c']; | ||
const input = ['a', 'b', 'c']; | ||
const array1 = arrayMove(input, 1, 2); | ||
console.log(array1); | ||
//=> ['a', 'c', 'b'] | ||
const array1 = arrayMoveImmutable(input, 1, 2); | ||
console.log(array1); | ||
//=> ['a', 'c', 'b'] | ||
const array2 = arrayMove(input, -1, 0); | ||
console.log(array2); | ||
//=> ['c', 'a', 'b'] | ||
const array2 = arrayMoveImmutable(input, -1, 0); | ||
console.log(array2); | ||
//=> ['c', 'a', 'b'] | ||
const array3 = arrayMove(input, -2, -3); | ||
console.log(array3); | ||
//=> ['b', 'a', 'c'] | ||
``` | ||
*/ | ||
<ValueType>(array: ReadonlyArray<ValueType>, from: number, to: number): ValueType[]; | ||
const array3 = arrayMoveImmutable(input, -2, -3); | ||
console.log(array3); | ||
//=> ['b', 'a', 'c'] | ||
``` | ||
*/ | ||
export function arrayMoveImmutable<ValueType>(array: readonly ValueType[], fromIndex: number, toIndex: number): ValueType[]; | ||
/** | ||
Moves the item to the new position in the input array. Useful for huge arrays where absolute performance is needed. | ||
/** | ||
Moves the item to the new position in the input array. Useful for huge arrays where absolute performance is needed. | ||
@param array - The array to modify. | ||
@param from - Index of item to move. If negative, it will begin that many elements from the end. | ||
@param to - Index of where to move the item. If negative, it will begin that many elements from the end. | ||
*/ | ||
mutate(array: unknown[], from: number, to: number): void; | ||
}; | ||
@param array - The array to modify. | ||
@param fromIndex - The index of item to move. If negative, it will begin that many elements from the end. | ||
@param toIndex - The index of where to move the item. If negative, it will begin that many elements from the end. | ||
export = arrayMove; | ||
@example | ||
``` | ||
import {arrayMoveMutable} from 'array-move'; | ||
const input = ['a', 'b', 'c']; | ||
arrayMoveMutable(input, 1, 2); | ||
console.log(input); | ||
//=> ['a', 'c', 'b'] | ||
``` | ||
*/ | ||
export function arrayMoveMutable(array: unknown[], fromIndex: number, toIndex: number): void; |
21
index.js
@@ -1,21 +0,16 @@ | ||
'use strict'; | ||
export function arrayMoveMutable(array, fromIndex, toIndex) { | ||
const startIndex = fromIndex < 0 ? array.length + fromIndex : fromIndex; | ||
const arrayMoveMutate = (array, from, to) => { | ||
const startIndex = from < 0 ? array.length + from : from; | ||
if (startIndex >= 0 && startIndex < array.length) { | ||
const endIndex = to < 0 ? array.length + to : to; | ||
const endIndex = toIndex < 0 ? array.length + toIndex : toIndex; | ||
const [item] = array.splice(from, 1); | ||
const [item] = array.splice(fromIndex, 1); | ||
array.splice(endIndex, 0, item); | ||
} | ||
}; | ||
} | ||
const arrayMove = (array, from, to) => { | ||
export function arrayMoveImmutable(array, fromIndex, toIndex) { | ||
array = [...array]; | ||
arrayMoveMutate(array, from, to); | ||
arrayMoveMutable(array, fromIndex, toIndex); | ||
return array; | ||
}; | ||
module.exports = arrayMove; | ||
module.exports.mutate = arrayMoveMutate; | ||
} |
{ | ||
"name": "array-move", | ||
"version": "3.0.1", | ||
"version": "4.0.0", | ||
"description": "Move an array item to a different position", | ||
@@ -13,4 +13,6 @@ "license": "MIT", | ||
}, | ||
"type": "module", | ||
"exports": "./index.js", | ||
"engines": { | ||
"node": ">=10" | ||
"node": "^12.20.0 || ^14.13.1 || >=16.0.0" | ||
}, | ||
@@ -32,6 +34,6 @@ "scripts": { | ||
"devDependencies": { | ||
"ava": "^2.4.0", | ||
"tsd": "^0.9.0", | ||
"xo": "^0.25.3" | ||
"ava": "^3.15.0", | ||
"tsd": "^0.17.0", | ||
"xo": "^0.44.0" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
# array-move [![Build Status](https://travis-ci.com/sindresorhus/array-move.svg?branch=master)](https://travis-ci.com/github/sindresorhus/array-move) | ||
# array-move | ||
@@ -14,15 +14,15 @@ > Move an array item to a different position | ||
```js | ||
const arrayMove = require('array-move'); | ||
import {arrayMoveImmutable} from 'array-move'; | ||
const input = ['a', 'b', 'c']; | ||
const array1 = arrayMove(input, 1, 2); | ||
const array1 = arrayMoveImmutable(input, 1, 2); | ||
console.log(array1); | ||
//=> ['a', 'c', 'b'] | ||
const array2 = arrayMove(input, -1, 0); | ||
const array2 = arrayMoveImmutable(input, -1, 0); | ||
console.log(array2); | ||
//=> ['c', 'a', 'b'] | ||
const array3 = arrayMove(input, -2, -3); | ||
const array3 = arrayMoveImmutable(input, -2, -3); | ||
console.log(array3); | ||
@@ -34,7 +34,7 @@ //=> ['b', 'a', 'c'] | ||
### arrayMove(array, from, to) | ||
### arrayMoveImmutable(array, fromIndex, toIndex) | ||
Clones the given `array`, moves the item to a new position in the new array, and then returns the new array. The given `array` is not mutated. | ||
### arrayMove.mutate(array, from, to) | ||
### arrayMoveMutable(array, fromIndex, toIndex) | ||
@@ -47,12 +47,16 @@ Moves the item to the new position in the `array` array. Useful for huge arrays where absolute performance is needed. | ||
#### from | ||
#### fromIndex | ||
Type: `number` | ||
Index of item to move. If negative, it will begin that many elements from the end. | ||
The index of item to move. | ||
#### to | ||
If negative, it will begin that many elements from the end. | ||
#### toIndex | ||
Type: `number` | ||
Index of where to move the item. If negative, it will begin that many elements from the end. | ||
The index of where to move the item. | ||
If negative, it will begin that many elements from the end. |
5064
50
60
Yes