[d24f17c] | 1 | import _curry2 from "./internal/_curry2.js";
|
---|
| 2 | import _dispatchable from "./internal/_dispatchable.js";
|
---|
| 3 | import _xtakeWhile from "./internal/_xtakeWhile.js";
|
---|
| 4 | import 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 |
|
---|
| 35 | var 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 |
|
---|
| 50 | export default takeWhile; |
---|