Skip to content

Commit ba8da3b

Browse files
committed
feat: add new move function
1 parent 5634e9f commit ba8da3b

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

packages/@ionic/utils-array/src/__tests__/index.ts

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { concurrentFilter, conform, filter, reduce, map } from '../';
1+
import { concurrentFilter, conform, filter, reduce, map, move } from '../';
22

33
describe('@ionic/utils-array', () => {
44

@@ -212,4 +212,53 @@ describe('@ionic/utils-array', () => {
212212

213213
});
214214

215+
describe('move', () => {
216+
217+
const array = ['a', 'b', 'c'];
218+
219+
it('should move first element to last element', () => {
220+
const result = move(array, 0, 2);
221+
expect(result).toEqual(['b', 'c', 'a']);
222+
});
223+
224+
it('should move last element to first element', () => {
225+
const result = move(array, 2, 0);
226+
expect(result).toEqual(['c', 'a', 'b']);
227+
});
228+
229+
it('should not move element with equal indexes', () => {
230+
const result = move(array, 1, 1);
231+
expect(result).toEqual(['a', 'b', 'c']);
232+
expect(result).not.toBe(array);
233+
});
234+
235+
describe('out of bounds', () => {
236+
it('should leave array unchanged for from index greater than array length', () => {
237+
const result = move(array, 5, 0);
238+
expect(result).toEqual(['a', 'b', 'c']);
239+
expect(result).not.toBe(array);
240+
});
241+
242+
it('should leave array unchanged for to index greater than array length', () => {
243+
const result = move(array, 0, 5);
244+
expect(result).toEqual(['a', 'b', 'c']);
245+
expect(result).not.toBe(array);
246+
});
247+
248+
it('should leave array unchanged for from index less than zero', () => {
249+
const result = move(array, -1, 0);
250+
expect(result).toEqual(['a', 'b', 'c']);
251+
expect(result).not.toBe(array);
252+
});
253+
254+
it('should leave array unchanged for to index less than zero', () => {
255+
const result = move(array, 0, -1);
256+
expect(result).toEqual(['a', 'b', 'c']);
257+
expect(result).not.toBe(array);
258+
});
259+
260+
});
261+
262+
});
263+
215264
});

packages/@ionic/utils-array/src/index.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,23 @@ export async function reduce<T, U>(array: T[] | readonly T[], callback: (accumul
7676

7777
return value;
7878
}
79+
80+
/**
81+
* Move an item in an array by index.
82+
*
83+
* This function will return a new array with the item in the `fromIndex`
84+
* position moved to the `toIndex` position.
85+
*/
86+
export function move<T>(array: readonly T[], fromIndex: number, toIndex: number): T[] {
87+
const element = array[fromIndex];
88+
const result = [...array];
89+
90+
if (fromIndex <= -1 || toIndex <= -1 || fromIndex >= array.length || toIndex >= array.length) {
91+
return result;
92+
}
93+
94+
result.splice(fromIndex, 1);
95+
result.splice(toIndex, 0, element);
96+
97+
return result;
98+
}

0 commit comments

Comments
 (0)