-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
43 lines (41 loc) · 1.24 KB
/
index.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
41
42
43
function getProxyMethods(options = {}) {
if (options.enableSnakeTeleport) {
return {
get(array, index) {
const parsedIndex = Number(index);
if (!isNaN(parsedIndex) && parsedIndex % 1 === 0) {
if (parsedIndex >= 0) {
return array[parsedIndex % array.length];
} else {
// TODO: Benchmark modulo approach against the while loop
return array[
(array.length - (Math.abs(parsedIndex) % array.length)) %
array.length
];
// while (parsedIndex < 0) parsedIndex += array.length;
// return array[parsedIndex];
}
} else {
// index is not an integer, godspeed
return array[index];
}
},
};
} else {
return {
get(array, index) {
const parsedIndex = Number(index);
if (isNaN(parsedIndex) || parsedIndex % 1 !== 0) {
return array[index];
} else {
return array.at(parsedIndex);
}
},
};
}
}
function getArrayWithNegativeIndexes(array, options) {
const arrayWithProxy = new Proxy(array, getProxyMethods(options));
return arrayWithProxy;
}
exports.getArrayWithNegativeIndexes = getArrayWithNegativeIndexes;