Skip to content

Commit

Permalink
issue-#57, #87 - Updated 'drop' types, and dropWhile declaration to a…
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
elycruz committed Feb 26, 2024
1 parent 9576400 commit 951ec02
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 19 deletions.
6 changes: 5 additions & 1 deletion packages/fjl/src/_platform/object/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ export const
}, {}) as ObjectStatics,

/**
* Functional version of `instanceof` operator.
* Functional `instanceof` combinator which first checks
* if passed in value's constructor is equal to the given one, and if result is `false`
* checks whether value is an instance of the given constructor.
*/
instanceOf = (x: any, X: Constructable): boolean =>
x.constructor === X || x instanceof X,
Expand All @@ -79,6 +81,8 @@ export const

/**
* Functional version of `Object.prototype.hasOwnProperty`.
* Note: Is defined as `Object.hasOwn` if available, otherwise uses
* `Object.prototype.hasOwnProperty` in declaration.
*/
hasOwnProperty = Object.hasOwn ?? (<T extends object>(x: T, key: string | PropertyKey): boolean =>
// @note `Object.hasOwn` cannot be used here until it is more broadly
Expand Down
12 changes: 5 additions & 7 deletions packages/fjl/src/list/drop.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import {Slice} from "../types";
import {instanceOfSome} from "../object";

export const

/**
* Drops `n` items from start of list.
*
* **Note:** Returns `string` type for strings, and `Array` type otherwise.
* Drops `n` items from start of iterable and returns a corresponding list (
* string (if iterable is a string), or array).
*/
drop = <T = any>(n: number, xs: Slice<T> | Iterable<T>): typeof xs | T[] => {
drop = <T = any>(n: number, xs: Iterable<T>): typeof xs | T[] => {
if (instanceOfSome(xs, String, Array))
return (xs as (string | T[])).slice(n);
return (xs as (string | T[])).slice(n) as typeof xs;

const out = [] as T[];
for (const x of xs) {
Expand All @@ -26,6 +24,6 @@ export const
* @curried
*/
$drop = <T = any>(n: number) =>
(xs: Slice<T> | Iterable<T>): typeof xs | T[] => drop(n, xs)
(xs: Iterable<T>): typeof xs | T[] => drop(n, xs)

;
33 changes: 22 additions & 11 deletions packages/fjl/src/list/dropWhile.ts
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)

;

0 comments on commit 951ec02

Please sign in to comment.