Skip to content

Commit 691b6e4

Browse files
committed
add array helper functions
1 parent c0afacb commit 691b6e4

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

rollup.config.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,24 @@ import replace from '@rollup/plugin-replace';
1515
import { terser } from 'rollup-plugin-terser';
1616
import typescript from 'rollup-plugin-typescript2';
1717

18+
/**
19+
* Helper functions to compensate for the fact that JS can't handle negative array indices very well
20+
*
21+
* TODO `insertAt` is only exported so the integrations config can inject the `commonjs` plugin, for localforage (used
22+
* in the offline plugin). Once that's fixed to no longer be necessary, this can stop being exported.
23+
*/
24+
function getLastElement(array) {
25+
return array[array.length - 1];
26+
}
27+
export function insertAt(arr, index, insertee) {
28+
const newArr = [...arr];
29+
// Add 1 to the array length so that the inserted element ends up in the right spot with respect to the length of the
30+
// new array (which will be one element longer), rather than that of the current array
31+
const destinationIndex = index >= 0 ? index : arr.length + 1 + index;
32+
newArr.splice(destinationIndex, 0, insertee);
33+
return newArr;
34+
}
35+
1836
/**
1937
* Create a plugin to add an identification banner to the top of stand-alone bundles.
2038
*

0 commit comments

Comments
 (0)