insert.js 943 B

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