Skip to content

Commit

Permalink
feat: issue-#103 - fjl-validator cleanup
Browse files Browse the repository at this point in the history
- Cleaned types in:
  - 'dropWhileEnd' - Made method stricter, and
   more flexible (!?!?) at the same time.
  - 'forEach' - Made type clearer, and more
   flexible.
  - 'map' - Made type clearer.
  - 'tails', and 'subsequence' - Updated these to
   use 'Slice' type.
- Removed deprecation tag from:
  - 'ArrayTypeConstructor', and,
  - 'TypedArray'
- Cleaned up some doc blocks.
- Cleaned up some doc blocks.
- Deprecated 'types/data/list' type method types.
- Consolidated some code in rollup.config.
- Updated rollup.config to output cjs/mjs files
 with cjs/mjs extensions, respectively.
- Performed (small) type cleanups in
 fjl-inputFilter, and fjl-validator string
 length validator.
- package.json - Removed 'exports' field -
 Currently very difficult to support for our use
  case so the feature is tabled until we could
  solve the 'exporting type files for cjs, and
   mjs' issue.
  • Loading branch information
elycruz committed Jun 1, 2024
1 parent 8d67e63 commit 9d184f1
Show file tree
Hide file tree
Showing 20 changed files with 101 additions and 63 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
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
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
15 changes: 9 additions & 6 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,
fn: Ternary<T, number, T[]>,
xs: T[]
): 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>[];
},

/**
* Curried version of `map` method.
*/
$map = <T = any>
(fn: MapOp) =>
(fn: Ternary<T, number, T[]>) =>
(xs: T[]): ReturnType<typeof fn>[] =>
map(fn, xs)
;
3 changes: 2 additions & 1 deletion packages/fjl/src/list/subsequences.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {push} from "./push";
import {append} from "./append";
import {Slice} from "../types";

export const
/**
Expand All @@ -10,7 +11,7 @@ export const
* will generate 65536 sub-sequences! So caution should be taken to not
* use this with sequences above a certain length on certain platform (the browser thread in specific).
*/
subsequences = <TS extends string | any[]>(xs: TS): TS[] => {
subsequences = <TS extends Slice>(xs: TS): TS[] => {
const listLen = xs.length,
len = Math.pow(2, listLen),
appender = listLen && Array.isArray(xs) ? push : append,
Expand Down
4 changes: 3 additions & 1 deletion packages/fjl/src/list/tails.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {Slice} from "../types";

export const

/**
Expand All @@ -6,7 +8,7 @@ export const
* shallowEquals(tails('abc'), ['abc', 'bc', 'c',''])
* ```
*/
tails = (xs: string | any[]): any[] => {
tails = <TS extends Slice>(xs: TS): TS[] => {
const limit = xs.length;
if (!limit) {
return [xs.slice(0, 0)];
Expand Down
4 changes: 2 additions & 2 deletions packages/fjl/src/object/assignDeep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const
/**
* Merges all objects down into one (takes two or more args).
*/
assignDeep = (obj0, ...objs) => {
assignDeep = (obj0: any, ...objs: any[]): any => {
if (!obj0) return obj0;

return objs.reduce((topAgg, obj) => {
Expand Down Expand Up @@ -42,6 +42,6 @@ export const
/**
* Curried version of `assignDeep`.
*/
$assignDeep = (obj0) => (...objs) => assignDeep(obj0, objs)
$assignDeep = (obj0: any) => (...objs: any[]) => assignDeep(obj0, objs)

;
27 changes: 16 additions & 11 deletions packages/fjl/src/types/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
*/

/**
* Represents strings, arrays, and/or, any structures that contain a `length`, and number indices.
*
* @todo Consider using `Iterable<T>` type, instead of this type, where it makes sense.
* Represents types that have a `length` property, and that are "number" indexable.
*/
export type NumberIndexable<T = unknown> = ({
export type NumberIndexable<T = unknown> = ({
length: number;
[index: number]: T;
} |
Expand All @@ -20,14 +18,16 @@ export type NumberIndexable<T = unknown> = ({
});

/**
* The Slice type represents the intersection of string, array, and/or array-like, types.
* The Slice type represents the intersection of string, array, and/or (compatible*) array-like, types.
*
* *Typed array types are not compatible with this type, as they don't definitions for most of the slice methods.
*/
export interface Slice<T = any> extends Iterable<T> {
readonly length: number;

[index: number]: any;

at(i: number): any; // @note Method not supported by older versions of typescript versions.
at(i: number): any; // @note Method not supported by older versions of typescript.
concat(...items: (Slice<T> | ConcatArray<any>)[]): this;
includes(x: any): boolean;
indexOf(x: any): number;
Expand Down Expand Up @@ -57,7 +57,7 @@ export interface Lengthable {
}

/**
* @deprecated Use types directly.
* Represents "built-in" typed arrays.
*/
export type TypedArray = (
Int8Array |
Expand All @@ -75,14 +75,19 @@ export type TypedArray = (
};

/**
* @deprecated Use types directly.
* @deprecated Use constructor array types directly.
*
* Represents built-in Array types (Typed arrays, and `Array`).
*/
export type ArrayType<T> = Array<T> | TypedArray;
export type ArrayType<T> = {
constructor: ArrayTypeConstructor,
} & NumberIndexable<T>

/**
* @deprecated Use types directly.
* Represents built-in Array type constructors.
*/
export type ArrayTypeConstructor = ArrayConstructor |
export type ArrayTypeConstructor =
ArrayConstructor |
Int8ArrayConstructor |
Uint8ArrayConstructor |
Int16ArrayConstructor |
Expand Down
31 changes: 26 additions & 5 deletions packages/fjl/src/types/list.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,39 @@
import {NumberIndexable, Slice} from "./data";
import {TernaryPred} from "./arity";

// @todo Normalize these types - They should accept same type params instead of mixed length params.

export type ForEachOp<T = any, FtrT = any> = (x: T, i?: number | keyof FtrT, xs?: FtrT) => void | any;
/**
* @deprecated Use `Ternary` type instead.
*
* Foreach operation function type.
*/
export type ForEachOp<T, IterT extends Iterable<T>> = (x: T, i?: number | keyof IterT, xs?: IterT) => void | any;

export type MapOp<T = any, FtrT = any> = (x: T, i?: number | keyof FtrT, xs?: FtrT) => FtrT;
/**
* @deprecated Use `Ternary` instead.
*
* Map operation function type.
*/
export type MapOp<T = any, FtrT = any, FtrT2 extends FtrT = any> = (x: T, i?: number | keyof FtrT, xs?: FtrT) => FtrT2;

export type ReduceOp<T = any, FnctrT = any, ZeroT = any> = (agg: ZeroT, x?: T, i?: number | keyof FnctrT, xs?: FnctrT) => ZeroT;
/**
* @deprecated Use `Quaternary` instead.
*
* Reduce operation function type.
*/
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];

/**
* @deprecated Use `TernaryPred` instead.
*
* Predicate for Slice operation func type.
* @todo Should this be `PredForNumIndexable`, instead?
*/
export type PredForSlice<T = any, TS extends NumberIndexable<T> = NumberIndexable<T>> = TernaryPred<T, number, TS>;
3 changes: 3 additions & 0 deletions packages/fjl/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"compilerOptions": {
"declaration": false
},
"files": [
"./src/index.ts"
],
"exclude": [
"dist/",
"node_modules/",
Expand Down
Loading

0 comments on commit 9d184f1

Please sign in to comment.