Skip to content

Commit

Permalink
Merge pull request #117 from functional-jslib/dev
Browse files Browse the repository at this point in the history
Remove the use `unknown` as generic defaults and build improvements.
  • Loading branch information
elycruz authored Jun 1, 2024
2 parents 94eea55 + 42467e2 commit 472067e
Show file tree
Hide file tree
Showing 25 changed files with 153 additions and 106 deletions.
3 changes: 3 additions & 0 deletions packages/fjl-filter/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"extends": "../../tsconfig.json",
"files": [
"./src/index.ts"
],
"exclude": [
"dist/",
"node_modules/",
Expand Down
4 changes: 2 additions & 2 deletions packages/fjl-inputfilter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"name": "fjl-inputfilter",
"version": "1.3.0",
"description": "A collection of methods and classes for performing input filtering on incoming inputs.",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.mjs",
"typings": "dist/esm/index.d.ts",
"peerDependencies": {
"fjl": "^2.0.0-alpha",
Expand Down
2 changes: 1 addition & 1 deletion packages/fjl-inputfilter/src/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const
!input.required && (
!isset(value) || (
(isString(value) || isArray(value)) &&
!(value as unknown as Slice).length
!(value as Slice).length
)
),

Expand Down
3 changes: 3 additions & 0 deletions packages/fjl-inputfilter/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"compilerOptions": {
"baseUrl": "."
},
"files": [
"./src/index.ts"
],
"exclude": [
"dist/",
"node_modules/",
Expand Down
4 changes: 2 additions & 2 deletions packages/fjl-validator-recaptcha/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"name": "fjl-validator-recaptcha",
"version": "1.3.0",
"description": "A recaptcha validator.",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.mjs",
"typings": "dist/esm/index.d.ts",
"peerDependencies": {
"fjl": "^2.0.0-alhpa",
Expand Down
4 changes: 2 additions & 2 deletions packages/fjl-validator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"name": "fjl-validator",
"version": "0.8.0",
"description": "A collection of validators for validating user supplied inputs.",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.mjs",
"typings": "dist/esm/index.d.ts",
"peerDependencies": {
"fjl": "^2.0.0-alpha"
Expand Down
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
3 changes: 2 additions & 1 deletion packages/fjl-validator/src/stringLengthValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import {getErrorMsgByKey, toValidationResult, ValidatorResult} from './ValidationUtils';
import {assignDeep, curry, curry2, isString, typeOf} from 'fjl';
import {LenValidatorOptions, toLengthOptions} from "./lengthValidator";
import type {NumberIndexable} from "fjl";

export const

Expand Down Expand Up @@ -32,7 +33,7 @@ export const
$stringLengthValidatorNoNormalize = <T>(options: LenValidatorOptions<T>, value: T): ValidatorResult => {
const messages = [],
isOfType = isString(value),
valLength = isOfType ? (value as unknown as string).length : 0,
valLength = isOfType ? (value as NumberIndexable).length : 0,
isWithinRange = valLength >= options.min && valLength <= options.max;
if (!isOfType) {
messages.push(getErrorMsgByKey(options, 'NOT_OF_TYPE', value));
Expand Down
3 changes: 3 additions & 0 deletions packages/fjl-validator/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"compilerOptions": {
"baseUrl": "."
},
"files": [
"./src/index.ts"
],
"exclude": [
"dist/",
"node_modules/",
Expand Down
14 changes: 2 additions & 12 deletions packages/fjl/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"name": "fjl",
"version": "2.0.0-alpha.5",
"description": "Functional Javascript Library.",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
"main": "./dist/cjs/index.cjs",
"module": "./dist/esm/index.mjs",
"typings": "./dist/esm/index.d.ts",
"files": [
"./dist",
Expand All @@ -23,16 +23,6 @@
"prelude",
"combinators"
],
"exports": {
".": {
"require": "./dist/cjs/index.js",
"module": "./dist/esm/index.js"
},
"./*": {
"require": "./dist/cjs/*.js",
"module": "./dist/esm/*.js"
}
},
"author": "Ely De La Cruz <[email protected]>",
"license": "BSD-3-Clause",
"bugs": {
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)

;
14 changes: 8 additions & 6 deletions packages/fjl/src/list/dropWhileEnd.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import {findIndexWhereRight} from "./utils";
import {TernaryPred} from "../types";
import {Slice, TernaryPred} from "../types";
import {negateF3} from "../function";

export const

/**
* Drops items off end of list while predicate holds.
*/
dropWhileEnd = <T>(
pred: TernaryPred,
xs: string | T[]
dropWhileEnd = <T, TS extends Slice<T>>(
pred: TernaryPred<T, number, TS>,
xs: TS
): typeof xs => {
const splitPoint: number =
findIndexWhereRight(negateF3(pred), xs);
Expand All @@ -22,8 +22,10 @@ export const
/**
* Curried version of `dropWhileEnd`.
*/
$dropWhileEnd = <T>(p: TernaryPred) =>
(xs: string | T[]): typeof xs =>
$dropWhileEnd = <T, TS extends Slice<T>>(
p: TernaryPred<T, number, TS>
) =>
(xs: TS): typeof xs =>
dropWhileEnd(p, xs)

;
8 changes: 4 additions & 4 deletions packages/fjl/src/list/forEach.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import {ForEachOp} from "../types";
import {Ternary} from "../types";

export const

/**
* "For each" function - same as `[].forEach` except for iterables (strings, `Map`s, ...) in general.
*/
forEach = <T>(fn: ForEachOp, iter: Iterable<T>): void => {
forEach = <T, TS extends Iterable<T>>(fn: Ternary<T, number, TS>, iter: TS): void => {
if (!iter) return;
let ind = 0;
for (const x of iter) {
fn(x, ind++, iter);
}
},

$forEach = <T>(fn: ForEachOp) =>
(iter: Iterable<T>): void => forEach(fn, iter)
$forEach = <T, TS extends Iterable<T>>(fn: Ternary<T, number, TS>) =>
(iter: TS): void => forEach(fn, iter)

;
5 changes: 3 additions & 2 deletions packages/fjl/src/list/insertBy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import {splitAt} from "./splitAt";
import {OrderingFunc} from "./utils";
import {append} from "./append";
import {of} from "../object";
import {Slice} from "../types";

/**
* A version of `insert` that allows you to specify the ordering of the inserted
* item; Before/at, or after
*/
export const insertBy = <T extends string | any, TS extends string | T[]>(
export const insertBy = <T, TS extends Slice<T>>(
orderingFn: OrderingFunc<T>, x: T, xs: TS
): TS => {
const limit = xs.length;
Expand All @@ -23,7 +24,7 @@ export const insertBy = <T extends string | any, TS extends string | T[]>(
return append(xs, of(xs, x));
},

$insertBy = <T extends string | any, TS extends string | T[]>(
$insertBy = <T, TS extends Slice<T>>(
orderingFn: OrderingFunc<T>
) =>
(x: T) =>
Expand Down
23 changes: 13 additions & 10 deletions packages/fjl/src/list/map.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
import {Constructable, MapOp} from "../types";
import {ArrayTypeConstructor, Ternary} from "../types";

export const

/**
* Maps a function onto a ListLike (string or array) or a functor (value containing a map method).
* Maps a function over an "array type" container of values.
*/
map = <T = any>(
fn: MapOp,
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 Constructable)(limit);
out = new (xs.constructor as ArrayTypeConstructor)(limit);
for (let i = 0; i < limit; i += 1) {
out[i] = fn(xs[i], i, xs);
}
return out;
return out as ReturnType<typeof fn>[];
},

$map = <T = any>
(fn: MapOp) =>
(xs: T[]): ReturnType<typeof fn>[] =>
/**
* Curried version of `map` method.
*/
$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);
Loading

0 comments on commit 472067e

Please sign in to comment.