Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bafa834

Browse files
committed
feat: Provide takeShort() & takeWithFallback()
1 parent b5612cb commit bafa834

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

src/sequence.js

+24-2
Original file line numberDiff line numberDiff line change
@@ -1168,6 +1168,7 @@ const skipWhile = curry('skipWhile', (seq, pred) => {
11681168
* elements if the input sequence was shorter than `no` elements.
11691169
*
11701170
* @function
1171+
* @alias takeShort
11711172
* @param {Sequence} seq Any sequence for which iter() is defined
11721173
* @param {Number} no The number of elements to take
11731174
* @returns {Iterator} The first element for which pred returns false
@@ -1179,10 +1180,12 @@ const tryTake = curry('tryTake', (seq, no) => {
11791180
range0(no),
11801181
map(() => tryNext(seq, Nothing)),
11811182
takeUntilVal(Nothing),
1182-
list
1183+
list,
11831184
);
11841185
});
11851186

1187+
const takeShort = tryTake;
1188+
11861189
/**
11871190
* Version of tryTake that will throw IteratorEnded
11881191
* if the given iterable is too short.
@@ -1196,7 +1199,24 @@ const tryTake = curry('tryTake', (seq, no) => {
11961199
const take = curry('take', (seq, no) => pipe(
11971200
range0(no),
11981201
map(() => next(seq)),
1199-
list
1202+
list,
1203+
));
1204+
1205+
/**
1206+
* Yields an iterator of the first `no` elements in the given
1207+
* in the sequence. If the sequence is too short, the fallback
1208+
* parameter will be substituted.
1209+
*
1210+
* @function
1211+
* @param {Sequence} seq Any sequence for which iter() is defined
1212+
* @param {Number} no The number of elements to take
1213+
* @param {Any} fallback The value to supply if the input sequence is too short
1214+
* @returns {Array} The elements taken
1215+
*/
1216+
const takeWithFallback = curry('takeWithFallback', (seq, no, fallback) => pipe(
1217+
concat(range0(no)),
1218+
map(() => tryNext(seq, fallback)),
1219+
list,
12001220
));
12011221

12021222
/**
@@ -1676,7 +1696,9 @@ module.exports = {
16761696
skip,
16771697
skipWhile,
16781698
tryTake,
1699+
takeShort,
16791700
take,
1701+
takeWithFallback,
16801702
takeWhile,
16811703
takeUntilVal,
16821704
takeDef,

test/sequence.test.js

+15-6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const {
2222
trySecond, tryLast, seqEq, each, find, tryFind, contains, count, list,
2323
uniq, join, dict, obj, into, foldl, foldr, any, all, sum, product, map,
2424
filter, reject, reverse, enumerate, trySkip, skip, skipWhile, tryTake,
25+
takeShort, takeWithFallback,
2526
take, takeWhile, takeUntilVal, takeDef, flat, concat, prepend, append,
2627
mapSort, zipLeast, zip, zipLongest, zipLeast2, zip2, zipLongest2,
2728
slidingWindow, trySlidingWindow, lookahead, mod, union, union2,
@@ -309,18 +310,26 @@ it('skipWhile', () => {
309310
it('take/...', () => {
310311
ckEqSeq(tryTake(4)(range0(10)), [0, 1, 2, 3]);
311312
ckEqSeq(tryTake(4)(range0(2)), [0, 1]);
313+
ckEqSeq(tryTake(4)([]), []);
314+
ckEqSeq(takeWithFallback(4, 99)(range0(2)), [0, 1, 99, 99]);
315+
ckEqSeq(takeWithFallback(4, 99)([]), [99, 99, 99, 99]);
312316
ckEqSeq(take(2)(range0(2)), [0, 1]);
313317
ckThrows(IteratorEnded, () => take(4)(range0(2)));
314318

315-
const it = iter(range0(8));
319+
const it = iter(range0(12));
316320
ckEq(take(it, 0), []);
317-
ckEq(take(it, 3), [0,1,2]);
318-
ckEq(take(it, 2), [3,4]);
321+
ckEq(take(it, 3), [0, 1, 2]);
322+
ckEq(take(it, 2), [3, 4]);
319323
ckEq(tryTake(it, 0), []);
320-
ckEq(tryTake(it, 1), [5]);
321-
ckEq(tryTake(it, 2), [6,7]);
324+
ckEq(takeShort(it, 1), [5]);
325+
ckEq(tryTake(it, 2), [6, 7]);
326+
ckEq(takeWithFallback(it, 0, null), []);
327+
ckEq(takeWithFallback(it, 2, null), [8, 9]);
328+
ckEq(takeWithFallback(it, 4, null), [10, 11, null, null]);
329+
ckEq(takeWithFallback(it, 0, null), []);
322330
ckEq(take(it, 0), []);
323-
ckEq(tryTake(it, 0), []);
331+
ckEq(takeShort(it, 0), []);
332+
ckEq(takeWithFallback(it, 0, null), []);
324333
});
325334

326335
it('takeWhile()', () => {

0 commit comments

Comments
 (0)