UNPKG

960 BJavaScriptView Raw
1var _curry3 =
2/*#__PURE__*/
3require("./internal/_curry3");
4/**
5 * Inserts the supplied element into the list, at the specified `index`. _Note that
6
7 * this is not destructive_: it returns a copy of the list with the changes.
8 * <small>No lists have been harmed in the application of this function.</small>
9 *
10 * @func
11 * @memberOf R
12 * @since v0.2.2
13 * @category List
14 * @sig Number -> a -> [a] -> [a]
15 * @param {Number} index The position to insert the element
16 * @param {*} elt The element to insert into the Array
17 * @param {Array} list The list to insert into
18 * @return {Array} A new Array with `elt` inserted at `index`.
19 * @example
20 *
21 * R.insert(2, 'x', [1,2,3,4]); //=> [1,2,'x',3,4]
22 */
23
24
25var insert =
26/*#__PURE__*/
27_curry3(function insert(idx, elt, list) {
28 idx = idx < list.length && idx >= 0 ? idx : list.length;
29 var result = Array.prototype.slice.call(list, 0);
30 result.splice(idx, 0, elt);
31 return result;
32});
33
34module.exports = insert;
\No newline at end of file