Skip to content

Commit

Permalink
issue-#57, #87, #90 - Updated 'takeWhile' implementation to allow rec…
Browse files Browse the repository at this point in the history
…eiving iterables, and removed 'nullish' checks in 'instanceOf' method.
  • Loading branch information
elycruz committed Feb 16, 2024
1 parent 7f0cf26 commit 8f890aa
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 24 deletions.
2 changes: 1 addition & 1 deletion packages/fjl/src/function/toFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ export const
* function (function that returns given value untouched) else
* returns given value. (useful in functional composition).
*/
toFunction = <T>(x?: T): Nary => (isFunction(x) ?
toFunction = <T>(x?: T): Nary => (x && isFunction(x) ?
x : () => x
) as Nary;
13 changes: 8 additions & 5 deletions packages/fjl/src/list/take.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import {Slice} from "../types";
import {instanceOfSome} from "../object";

/**
* Returns the first `n` items from an iterable (e.g., string, array, generator, etc.).
* **Note:** string type is returned for strings, array type otherwise.
*/
export const take = <T>(n: number, xs: Iterable<T>): Iterable<T> => {
if (typeof xs === 'string' || Array.isArray(xs))
return xs.slice(0, n);
export const take = <T=any>(n: number, xs: Slice<T> | Iterable<T>): typeof xs | T[] => {
if (instanceOfSome(xs, String, Array))
return (xs as (string | T[])).slice(0, n);

const out = [] as T[];
for (const x of xs) {
if (n-- === 0) break;
out.push(x);
out.push(x as T);
}
return out;
},
Expand All @@ -18,4 +21,4 @@ export const take = <T>(n: number, xs: Iterable<T>): Iterable<T> => {
* Curried version of `take`.
*/
$take = <T>(n: number) =>
(xs: Iterable<T> | Generator<T>): Iterable<T> => take(n, xs);
(xs: Slice<T> | Iterable<T>): typeof xs | T[] => take(n, xs);
20 changes: 12 additions & 8 deletions packages/fjl/src/list/takeWhile.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import {Slice, TernaryPred} from "../types";
import {TernaryPred} from "../types";
import {instanceOf} from "../platform";

export const

/**
* Returns a slice containing elements that match predicate upto non-matching element.
* Returns items upto point where predicate doesn't hold.
* For string type returns a string.
*/
takeWhile = <T>(pred: TernaryPred, xs: Slice<T>): typeof xs => {
const limit = xs.length;
takeWhile = <T>(pred: TernaryPred, xs: Iterable<T>): typeof xs | T[] => {
let i = 0;
for (; i < limit; i += 1) {
if (!pred(xs[i], i, xs)) break;
const out = [];
for (const x of xs) {
if (!pred(x, i, xs)) break;
out.push(x);
i += 1;
}
return xs.slice(0, i);
return instanceOf(xs, String) ? out.join('') as typeof xs : out;
},

$takeWhile = <T>(pred: TernaryPred) =>
(xs: Slice<T>): typeof xs =>
(xs: Iterable<T>): typeof xs | T[] =>
takeWhile(pred, xs);
15 changes: 13 additions & 2 deletions packages/fjl/src/object/is.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

import {typeOf} from './typeOf';
import {instanceOf, keys} from '../platform/object';
import {$instanceOf, instanceOf, keys} from '../platform/object';
import {Constructable, TypeConstructor, TypeRef} from "../types";

export * from './isset';
Expand All @@ -12,7 +12,13 @@ export * from './isset';
const _primitive_constructors = Object.freeze([
String, Number, BigInt, Boolean, Symbol
]) as readonly Constructable[],
_classPrefixRegex = /^\s{0,3}class\s{1,3}/
_classPrefixRegex = /^\s{0,3}class\s{1,3}/,

/**
* Generator function constructor.
* @ref https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/GeneratorFunction
*/
GeneratorFunction = () => function* () {}.constructor
;

export const
Expand All @@ -32,6 +38,11 @@ export const
*/
isFunction = (x: any): boolean => instanceOf(x, Function),

/**
* Returns bool indicating whether a value is a generator function or not.
*/
isGeneratorFunction = (x: any) => instanceOf(x, GeneratorFunction as unknown as TypeConstructor),

/**
* Special case of `instanceOf` where the method checks if given value contains a constructor equal
* to one of the passed in ones, or if the value is an instance of one of the passed in ones.
Expand Down
2 changes: 1 addition & 1 deletion packages/fjl/src/object/isset.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export const

/**
* @deprecated Use `notNullish` instead - better for readability.
* @deprecated Use `notNullish`/`isNullish` instead - better for readability.
*/
isset = (x: any): boolean => x !== null && x !== undefined

Expand Down
9 changes: 4 additions & 5 deletions packages/fjl/src/platform/object/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import {flip, flip3, flip4, flip5} from "../../function/flip";
import {Constructable, ObjectStatics} from "../../types";
import {notNullish} from "../../object/is";

export const

Expand Down Expand Up @@ -68,15 +67,15 @@ export const
return agg;
}, {}) as ObjectStatics,

instanceOf = (x, X: Constructable): boolean =>
notNullish(x) && x.constructor === X || x instanceof X,
instanceOf = (x: any, X: Constructable): boolean =>
x.constructor === X || x instanceof X,

$instanceOf = (x) => (X: Constructable): boolean => instanceOf(x, X),
$instanceOf = (x: any) => (X: Constructable): boolean => instanceOf(x, X),

hasOwnProperty = Object.hasOwn ?? (<T extends object>(x: T, key: string | PropertyKey): boolean =>
// @note `Object.hasOwn` cannot be used here until it is more broadly
// adopted (until node v24+ release etc.).
notNullish(x) && Object.prototype.hasOwnProperty.call(x, key))
Object.prototype.hasOwnProperty.call(x, key))
,

$hasOwnProperty = <T extends object>(x: T) =>
Expand Down
2 changes: 0 additions & 2 deletions packages/fjl/tests/object/index_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,6 @@ describe('#object', function () {
[1, false],
[true, false],
[false, false],
[undefined, false],
[null, false],
[[], false],
[{}, false],
])
Expand Down

0 comments on commit 8f890aa

Please sign in to comment.