-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…llow any iterables to be passed in. Additionally updated some docs.
- Loading branch information
Showing
3 changed files
with
32 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,34 @@ | ||
import {findIndexWhere} from "./utils"; | ||
import {TernaryPred} from "../types"; | ||
import {instanceOf} from "../_platform"; | ||
|
||
export const | ||
|
||
/** | ||
* Returns a list without elements that match predicate. | ||
* Drops elements, from start of iterable, fulfilling predicate, and returns | ||
* a list (string if iterable is a string) of the remaining items. | ||
*/ | ||
dropWhile = <T>(p: TernaryPred, xs: string | T[]): typeof xs => { | ||
const limit = xs.length, | ||
splitPoint: number = findIndexWhere( | ||
(x, i, xs) => !p(x, i, xs), | ||
xs | ||
); | ||
return splitPoint === -1 ? xs.slice(limit) : | ||
xs.slice(splitPoint, limit); | ||
dropWhile = <T>(p: TernaryPred, xs: Iterable<T>): typeof xs | T[] => { | ||
let index = -1, | ||
thresholdReached = false; | ||
|
||
const out = []; | ||
|
||
for (const x of xs) { | ||
index++; | ||
const dropCurrent = p(x, index, xs); | ||
if (!thresholdReached && dropCurrent) continue; | ||
else if (!thresholdReached && !dropCurrent) thresholdReached = true; | ||
out.push(x); | ||
} | ||
|
||
return instanceOf(xs, String) ? (out.join("") as Iterable<T>) : out; | ||
}, | ||
|
||
/** | ||
* Curried version of `dropWhile`. | ||
*/ | ||
$dropWhile = <T>(p: TernaryPred) => | ||
(xs: string | T[]): typeof xs => | ||
(xs: Iterable<T>): typeof xs | T[] => | ||
dropWhile(p, xs) | ||
|
||
; |