source: node_modules/ramda/es/takeWhile.js@ d24f17c

main
Last change on this file since d24f17c was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 1.4 KB
Line 
1import _curry2 from "./internal/_curry2.js";
2import _dispatchable from "./internal/_dispatchable.js";
3import _xtakeWhile from "./internal/_xtakeWhile.js";
4import slice from "./slice.js";
5/**
6 * Returns a new list containing the first `n` elements of a given list,
7 * passing each value to the supplied predicate function, and terminating when
8 * the predicate function returns `false`. Excludes the element that caused the
9 * predicate function to fail. The predicate function is passed one argument:
10 * *(value)*.
11 *
12 * Dispatches to the `takeWhile` method of the second argument, if present.
13 *
14 * Acts as a transducer if a transformer is given in list position.
15 *
16 * @func
17 * @memberOf R
18 * @since v0.1.0
19 * @category List
20 * @sig (a -> Boolean) -> [a] -> [a]
21 * @sig (a -> Boolean) -> String -> String
22 * @param {Function} fn The function called per iteration.
23 * @param {Array} xs The collection to iterate over.
24 * @return {Array} A new array.
25 * @see R.dropWhile, R.transduce, R.addIndex
26 * @example
27 *
28 * const isNotFour = x => x !== 4;
29 *
30 * R.takeWhile(isNotFour, [1, 2, 3, 4, 3, 2, 1]); //=> [1, 2, 3]
31 *
32 * R.takeWhile(x => x !== 'd' , 'Ramda'); //=> 'Ram'
33 */
34
35var takeWhile =
36/*#__PURE__*/
37_curry2(
38/*#__PURE__*/
39_dispatchable(['takeWhile'], _xtakeWhile, function takeWhile(fn, xs) {
40 var idx = 0;
41 var len = xs.length;
42
43 while (idx < len && fn(xs[idx])) {
44 idx += 1;
45 }
46
47 return slice(0, idx, xs);
48}));
49
50export default takeWhile;
Note: See TracBrowser for help on using the repository browser.