Skip to content

Commit cb6520c

Browse files
feat: add array move util fn
1 parent 801538d commit cb6520c

File tree

8 files changed

+82
-1
lines changed

8 files changed

+82
-1
lines changed

.changeset/honest-pugs-hope.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@opentf/utils": minor
3+
---
4+
5+
Added array move utility fn.

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ lib
2626

2727
.parcel-cache
2828

29-
play.js
29+
play.js
30+
.turbo

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ await sleep(1000); // It suspends the exection for 1 second.
5252
- [asyncFilter](https://js-utils.pages.dev/Array/asyncFilter)
5353
- [groupBy](https://js-utils.pages.dev/Array/groupBy)
5454
- [range](https://js-utils.pages.dev/Array/range)
55+
- [move](https://js-utils.pages.dev/Array/move)
5556

5657
### Maths
5758

apps/website/docs/Array/move.mdx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
> It moves an array element from one index position to another.
2+
3+
## Syntax
4+
5+
```ts
6+
move(arr: T[], from: number, to: number): T[];
7+
```
8+
## Usage
9+
10+
```ts
11+
import { move } from '@opentf/utils';
12+
13+
move([], 0, 1);
14+
```
15+
16+
## Examples
17+
18+
```ts
19+
move([1, 2, 3], 0, 2) //=> [2, 3, 1]
20+
21+
move([1, 2, 3], 0, 5) //=> [2, 3, 1]
22+
23+
move([1, 2, 3], 5, 0) //=> [1, 2, 3]
24+
```

packages/utils/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ await sleep(1000); // It suspends the exection for 1 second.
5252
- [asyncFilter](https://js-utils.pages.dev/Array/asyncFilter)
5353
- [groupBy](https://js-utils.pages.dev/Array/groupBy)
5454
- [range](https://js-utils.pages.dev/Array/range)
55+
- [move](https://js-utils.pages.dev/Array/move)
5556

5657
### Maths
5758

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { move } from '../../src';
2+
3+
describe('Array > move', () => {
4+
test('empty array', () => {
5+
expect(move([], 0, 1)).toEqual([]);
6+
});
7+
8+
it('returns same ref', () => {
9+
const arr = [1, 5, 8];
10+
expect(move(arr, 0, 0)).toBe(arr);
11+
});
12+
13+
it('moves within range', () => {
14+
expect(move([1, 2, 3], 0, 2)).toEqual([2, 3, 1]);
15+
});
16+
17+
it('moves the element to the last index if to > length', () => {
18+
expect(move([1, 2, 3], 0, 5)).toEqual([2, 3, 1]);
19+
});
20+
21+
it('does not move the element', () => {
22+
expect(move([1, 2, 3], 5, 0)).toEqual([1, 2, 3]);
23+
});
24+
25+
test('negative index', () => {
26+
expect(move([1, 2, 3, 4, 5], 0, -1)).toEqual([2, 3, 4, 1, 5]);
27+
expect(move([1, 2, 3, 4, 5], -1, 2)).toEqual([1, 2, 5, 3, 4]);
28+
});
29+
});

packages/utils/src/array/move.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* It moves an array element from one index position to another.
3+
*
4+
* Note: It mutates the array.
5+
*
6+
* @example
7+
*
8+
* move([1, 2, 3], 0, 2) //=> [2, 3, 1]
9+
*/
10+
11+
export default function move<T>(arr: T[], from: number, to: number): T[] {
12+
if (arr.length === 0 || from === to || from >= arr.length) {
13+
return arr;
14+
}
15+
16+
arr.splice(to, 0, arr.splice(from, 1)[0]);
17+
18+
return arr;
19+
}

packages/utils/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export { default as arrayDiff } from './array/arrayDiff';
1111
export { default as range } from './array/range';
1212
export { default as asyncFilter } from './array/asyncFilter';
1313
export { default as groupBy } from './array/groupBy';
14+
export { default as move } from './array/move';
1415

1516
// Maths
1617
export { default as percentage } from './maths/percentage';

0 commit comments

Comments
 (0)