UNPKG

1.02 kBJavaScriptView Raw
1var _checkForMethod =
2/*#__PURE__*/
3require("./internal/_checkForMethod");
4
5var _curry2 =
6/*#__PURE__*/
7require("./internal/_curry2");
8/**
9 * Creates a new list with the separator interposed between elements.
10 *
11 * Dispatches to the `intersperse` method of the second argument, if present.
12 *
13 * @func
14 * @memberOf R
15 * @since v0.14.0
16 * @category List
17 * @sig a -> [a] -> [a]
18 * @param {*} separator The element to add to the list.
19 * @param {Array} list The list to be interposed.
20 * @return {Array} The new list.
21 * @example
22 *
23 * R.intersperse('a', ['b', 'n', 'n', 's']); //=> ['b', 'a', 'n', 'a', 'n', 'a', 's']
24 */
25
26
27var intersperse =
28/*#__PURE__*/
29_curry2(
30/*#__PURE__*/
31_checkForMethod('intersperse', function intersperse(separator, list) {
32 var out = [];
33 var idx = 0;
34 var length = list.length;
35
36 while (idx < length) {
37 if (idx === length - 1) {
38 out.push(list[idx]);
39 } else {
40 out.push(list[idx], separator);
41 }
42
43 idx += 1;
44 }
45
46 return out;
47}));
48
49module.exports = intersperse;
\No newline at end of file