UNPKG

1.53 kBJavaScriptView Raw
1var _curry2 =
2/*#__PURE__*/
3require("./internal/_curry2");
4
5var _dispatchable =
6/*#__PURE__*/
7require("./internal/_dispatchable");
8
9var _xtakeWhile =
10/*#__PURE__*/
11require("./internal/_xtakeWhile");
12
13var slice =
14/*#__PURE__*/
15require("./slice");
16/**
17 * Returns a new list containing the first `n` elements of a given list,
18 * passing each value to the supplied predicate function, and terminating when
19 * the predicate function returns `false`. Excludes the element that caused the
20 * predicate function to fail. The predicate function is passed one argument:
21 * *(value)*.
22 *
23 * Dispatches to the `takeWhile` method of the second argument, if present.
24 *
25 * Acts as a transducer if a transformer is given in list position.
26 *
27 * @func
28 * @memberOf R
29 * @since v0.1.0
30 * @category List
31 * @sig (a -> Boolean) -> [a] -> [a]
32 * @sig (a -> Boolean) -> String -> String
33 * @param {Function} fn The function called per iteration.
34 * @param {Array} xs The collection to iterate over.
35 * @return {Array} A new array.
36 * @see R.dropWhile, R.transduce, R.addIndex
37 * @example
38 *
39 * const isNotFour = x => x !== 4;
40 *
41 * R.takeWhile(isNotFour, [1, 2, 3, 4, 3, 2, 1]); //=> [1, 2, 3]
42 *
43 * R.takeWhile(x => x !== 'd' , 'Ramda'); //=> 'Ram'
44 */
45
46
47var takeWhile =
48/*#__PURE__*/
49_curry2(
50/*#__PURE__*/
51_dispatchable(['takeWhile'], _xtakeWhile, function takeWhile(fn, xs) {
52 var idx = 0;
53 var len = xs.length;
54
55 while (idx < len && fn(xs[idx])) {
56 idx += 1;
57 }
58
59 return slice(0, idx, xs);
60}));
61
62module.exports = takeWhile;
\No newline at end of file