forked from angus-c/just
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
26 lines (24 loc) · 771 Bytes
/
Copy pathindex.js
File metadata and controls
26 lines (24 loc) · 771 Bytes
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
module.exports = insert;
/*
insert([1, 2, 5, 6], ['a', 'c', 'e'], 2); // [1, 2, 'a', 'c', 'e', 5, 6]
insert([1, 2, 5, 6], 'a', 2); // [1, 2, 'a', 5, 6]
insert([1, 2, 5, 6], ['a', 'c', 'e'], 0); // ['a', 'c', 'e', 1, 2, 5, 6]
insert([1, 2, 5, 6], ['a', 'c', 'e']); // ['a', 'c', 'e', 1, 2, 5, 6]
*/
function insert(arr1, arr2, index) {
if (!Array.isArray(arr1)) {
throw new Error('expected an array for first argument');
}
if (arguments.length > 2 && typeof index != 'number') {
throw new Error('expected a number for third argument');
}
if (!Array.isArray(arr2)) {
arr2 = [arr2];
}
if (!index) {
return arr2.concat(arr1);
}
var front = arr1.slice(0, index);
var back = arr1.slice(index);
return front.concat(arr2, back);
}