jsmodern
Version:
An extension to existing JavaScript, influenced by other great languages such as Rust, Dart, Java, Golang, etc.
24 lines • 741 B
JavaScript
export const insert = {
label: 'insert',
fn: function arrayInsert(index, element) {
const ctx = this;
const len = ctx.length;
if (null == index || 'number' !== typeof (index) || index < 0 || index > len) {
throw new TypeError(`Array index out of bound`);
}
if (!index) {
ctx.unshift(element);
}
else if (index === len) {
ctx.push(element);
}
else {
const startSlice = ctx.slice(0, index);
const endSlice = ctx.slice(index);
ctx.length = 0;
for (const n of startSlice.concat(element, endSlice))
ctx.push(n);
}
},
};
//# sourceMappingURL=insert.js.map