-
Notifications
You must be signed in to change notification settings - Fork 354
/
array.js
40 lines (32 loc) · 997 Bytes
/
array.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { isArray } from '@ember/array';
import { assert } from '@ember/debug';
/**
Genericizes `objectAt` so it can be run against a normal array or an Ember array
@param {object|Array} arr
@param {number} index
@return {any}
*/
export function objectAt(arr, index) {
assert(
'arr must be an instance of a Javascript Array or implement `objectAt`',
isArray(arr) || typeof arr.objectAt === 'function'
);
if (typeof arr.objectAt === 'function') {
return arr.objectAt(index);
}
return arr[index];
}
export function splice(items, start, count, ...add) {
if (typeof items.replace === 'function' && typeof items.objectAt === 'function') {
return items.replace(start, count, add);
}
return items.splice(start, count, ...add);
}
/**
* Cycle shift an internal [start..end] to [start + 1...end, start].
*/
export function move(items, start, end) {
let sourceItem = objectAt(items, start);
splice(items, start, 1);
splice(items, end, 0, sourceItem);
}