Skip to content

Commit

Permalink
feat: issue-#57,#113,#114,#115,#116 - Type fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
elycruz committed Jun 1, 2024
1 parent abab433 commit 42467e2
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 49 deletions.
4 changes: 2 additions & 2 deletions packages/fjl-validator/src/ValidationUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export interface ValidatorResult<T = any> {
value?: T;
}

export type Validator<T = unknown> = Unary<T, ValidatorResult<T>>;
export type Validator<T=any> = Unary<T, ValidatorResult<T>>;

export const

Expand Down Expand Up @@ -72,7 +72,7 @@ export const
* @param options {...Object}
* @returns {Object}
*/
toValidationOptions = <T = unknown>(...options: ValidatorOptions<T>[]): ValidatorOptions<T> =>
toValidationOptions = <T>(...options: ValidatorOptions<T>[]): ValidatorOptions<T> =>
assignDeep(defineEnumProps([
[Object, 'messageTemplates', {}],
[Boolean, 'valueObscured', false],
Expand Down
16 changes: 12 additions & 4 deletions packages/fjl/src/list/concatMap.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
import {concat} from "../_platform/slice";
import {map} from "./map";
import {MapOp, Slice} from "../types";
import type {Ternary} from "../types";

export const

/**
* Map a function over all the elements of a container of containers and concatenate the results.
*/
concatMap = <T = any>(fn: MapOp, arr: Slice<T>[]): any[] =>
concatMap = <T=any, TS extends any[]=any[]>(
fn: Ternary<T, number, TS[]>,
arr: TS[]
): ReturnType<typeof fn>[] =>
concat(map(fn, arr)),

$concatMap = <T = any>(fn: MapOp) =>
(arr: T[][]): any[] =>
/**
* Curried version of `concatMap`.
*/
$concatMap = <T=any, TS extends any[]=any[]>(
fn: Ternary<T, number, TS[]>
) =>
(arr: TS[]): ReturnType<typeof fn>[] =>
concatMap(fn, arr)

;
12 changes: 6 additions & 6 deletions packages/fjl/src/list/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ export const
/**
* Maps a function over an "array type" container of values.
*/
map = <T = any>(
fn: Ternary<T, number, T[]>,
xs: T[]
map = <T=any, TS extends any[]=any[]>(
fn: Ternary<T, number, TS>,
xs: TS
): ReturnType<typeof fn>[] => {
const limit = xs.length,
out = new (xs.constructor as ArrayTypeConstructor)(limit);
Expand All @@ -20,8 +20,8 @@ export const
/**
* Curried version of `map` method.
*/
$map = <T = any>
(fn: Ternary<T, number, T[]>) =>
(xs: T[]): ReturnType<typeof fn>[] =>
$map = <T=any, TS extends any[]=any[]>
(fn: Ternary<T, number, TS>) =>
(xs: TS): ReturnType<typeof fn>[] =>
map(fn, xs)
;
26 changes: 13 additions & 13 deletions packages/fjl/src/list/mapAccumL.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Quaternary} from "../types";
import type {Quaternary} from "../types";

/**
* Performs a `reduce` and a `map` all in one (from left-to-right);
Expand All @@ -7,16 +7,16 @@ import {Quaternary} from "../types";
* returns a tuple containing the aggregated value,
* and the collected mapped values.
*/
export const mapAccumL = <Agg, X>(
op: (agg?: Agg, x?: X, i?: number, xs?: X[]) => [Agg, X],
zero: Agg,
xs: X[]
): [Agg, typeof xs] => {
export const mapAccumL = <AccumVal, B, MapOfB, Bs extends B[], MapOfBs extends MapOfB[]>(
op: Quaternary<AccumVal, B, number, Bs, [AccumVal, MapOfB]>,
zero: AccumVal,
xs: Bs
): [AccumVal, MapOfBs] => {
const limit = xs.length;

if (!limit) return [zero, xs.slice(0)];
if (!limit) return [zero, xs.slice(0) as unknown as MapOfBs];

const mapped = xs.slice(0, 0);
const mapped = [];
let agg = zero;

for (let i = 0; i < limit; i++) {
Expand All @@ -26,12 +26,12 @@ export const mapAccumL = <Agg, X>(
mapped.push(tuple[1]);
}

return [agg, mapped];
return [agg, mapped as unknown as MapOfBs];
},

$mapAccumL = <Agg, X>(
op: Quaternary<Agg, X, number, X[], [Agg, X]>
$mapAccumL = <AccumVal, B, MapOfB, Bs extends B[], MapOfBs extends MapOfB[]>(
op: Quaternary<AccumVal, B, number, Bs, [AccumVal, MapOfB]>,
) =>
(zero: Agg) =>
(xs: X[]): [Agg, typeof xs] =>
(zero: AccumVal) =>
(xs: Bs): [AccumVal, MapOfBs] =>
mapAccumL(op, zero, xs);
34 changes: 18 additions & 16 deletions packages/fjl/src/list/mapAccumR.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,39 @@
import {MapAccumOp, Slice} from "../types";
import {Quaternary, Slice} from "../types";

export const

/**
* Performs a map and a reduce, all in one (from right-to-left), on each item, and returns the result of the reduce,
* and the map, as a tuple.
* Performs a map and a reduce, all in one (from right-to-left), on each item in the given array, and
* returns the result of the reduce, and the map, as a tuple, respectively.
*/
mapAccumR = <A, B, C>(
op: MapAccumOp<A, B, C>,
zero: A,
xs: Slice<B>
): [A, Slice<C>] => {
const limit = xs.length;
mapAccumR = <AccumVal, B, MapOfB, Bs extends Slice<B>, MapOfBs extends Slice<MapOfB>>(
op: Quaternary<AccumVal, B, number, Bs, [AccumVal, MapOfB]>,
zero: AccumVal,
bs: Bs
): [AccumVal, MapOfBs] => {
const limit = bs.length;

if (!limit) return [zero, xs.slice(0) as unknown as Slice<C>];
if (!limit) return [zero, bs.slice(0) as unknown as MapOfBs];

const mapped = [];

let agg = zero;

for (let i = limit - 1; i >= 0; i--) {
const tuple = op(agg, xs[i] as B, i);
const tuple = op(agg, bs[i] as B, i, bs);
agg = tuple[0];
mapped.push(tuple[1]);
}

return [agg, mapped];
return [agg, mapped as unknown as MapOfBs];
},

/**
* Curried version of `mapAccumR`.
*/
$mapAccumR = <A, B, C>(op: MapAccumOp<A, B, C>) =>
(zero: A) =>
(xs: Slice<B>): [A, Slice] =>
mapAccumR(op, zero, xs);
$mapAccumR = <AccumVal, B, MapOfB, Bs extends Slice<B>, MapOfBs extends Slice<MapOfB>>(
op: Quaternary<AccumVal, B, number, Bs, [AccumVal, MapOfB]>,
) =>
(zero: AccumVal) =>
(bs: Bs): [AccumVal, MapOfBs] =>
mapAccumR(op, zero, bs);
4 changes: 2 additions & 2 deletions packages/fjl/src/list/zipWith3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ export const
/**
* Zips 3 lists using tupling function.
*/
zipWith3 = <T = unknown>(op: ZipWith3Op<T>, xs1: T[], xs2: T[], xs3: T[]): T[][] => zipWithN(op, xs1, xs2, xs3),
zipWith3 = <T=any>(op: ZipWith3Op<T>, xs1: T[], xs2: T[], xs3: T[]): T[][] => zipWithN(op, xs1, xs2, xs3),

$zipWith3 = <T = unknown>(op: ZipWith3Op<T>) =>
$zipWith3 = <T=any>(op: ZipWith3Op<T>) =>
(xs1: T[]) =>
(xs2: T[]) =>
(xs3: T[]): T[][] => zipWith3(op, xs1, xs2, xs3);
2 changes: 1 addition & 1 deletion packages/fjl/src/types/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* Represents types that have a `length` property, and that are "number" indexable.
*/
export type NumberIndexable<T = unknown> = ({
export type NumberIndexable<T=any> = ({
length: number;
[index: number]: T;
} |
Expand Down
9 changes: 4 additions & 5 deletions packages/fjl/src/types/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@ export type MapOp<T = any, FtrT = any, FtrT2 extends FtrT = any> = (x: T, i?: nu
*
* Reduce operation function type.
*/
export type ReduceOp<T = any, FtrT = any, ZeroT = any> = (agg: ZeroT, x?: T, i?: number | keyof FtrT, xs?: FtrT) => ZeroT;
export type ReduceOp<T=any, FtrT=any, ZeroT=any> =
(agg: ZeroT, x?: T, i?: number | keyof FtrT, xs?: FtrT) => ZeroT;

/**
* @deprecated Use `Quinary` instead.
*
* "Map + Accumulate", A.K.A. Map-Reduce, function type.
*/
export type MapAccumOp<A = any, B = any, C = any, Ind = number | string, Functor = Slice<B>> =
(agg?: A, b?: B, i?: Ind, bs?: Functor) => [A, C];
export type MapAccumOp<AccumVal=any, B=any, MapOfB=any, Index=number|string, SliceOfBs extends Slice<B>=Slice<B>> =
(agg?: AccumVal, b?: B, i?: Index, bs?: SliceOfBs) => [AccumVal, MapOfB];

/**
* @deprecated Use `TernaryPred` instead.
Expand Down

0 comments on commit 42467e2

Please sign in to comment.