Skip to content

Commit

Permalink
feat: issue-#57 - type fix
Browse files Browse the repository at this point in the history
- Cleaned up some comments.
- Cleaned up 'randStr', and 'randStrIter',
  method impls.
- Removed un-required bool check in
  'filter', and 'findWhere', methods.
- Fixed var name typo in 'types/' module.
- Replaced '@todo', in READMEs, to ensure
  Users with that name, do not get
  referenced in the parsed README, on
  github.
- Updated 'drop', and 'take' methods,
  to parse the incoming number to
  an 'absolute' number (ensures only
  positive numbers are used in
  iteration check).
- Removed 'not found' anchor link
  in 'fjl-validators' package README.
  • Loading branch information
elycruz committed Dec 27, 2024
1 parent 2dcc022 commit 6c08276
Show file tree
Hide file tree
Showing 19 changed files with 47 additions and 37 deletions.
7 changes: 3 additions & 4 deletions packages/fjl-validator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ Functional validator(s) implementation (inspired by Zend/Validator validators).
- [Requirements](#requirements)
- [Getting Started](#getting-started)
- [Docs](#docs)
- [Motivation](#motivations)
- [Development](#development)
- [Supported Platforms](#supported-platforms)
- [License](#license)
Expand Down Expand Up @@ -182,7 +181,7 @@ Returns valid validation options objects that can be used as validator options;
- `valueObscured {Boolean}`
- `valueObscurer {Function.<String>}` - Obscurer function; E.g. `x => "..."`
- `messageTemplates {Object.<String, (MessageTemplateCallback|Function|String)>}` - Key value pairs of error messages or error message callbacks (
See virtual type `MessageTemplateCallback` @todo here).
See virtual type `MessageTemplateCallback` TODO here).

##### Returns
`{ValidationOptions}` - A strictly typed options object; Merges passed in options onto strictly typed version which
Expand Down Expand Up @@ -347,8 +346,8 @@ import {digitValidator} from 'fjl-validator';
##### Parameters
- `value {*}`
- `options {ValidationOptions}`
@todo `MessageTemplateCallback`'s signature should be `f(options, value)` not other way around (`f(value, options)`).
@todo Should be changed in later version of lib.
TODO `MessageTemplateCallback`'s signature should be `f(options, value)` not other way around (`f(value, options)`).
TODO Should be changed in later version of lib.

#### `ValidationResult {Object}`
##### Properties
Expand Down
2 changes: 1 addition & 1 deletion packages/fjl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Reference 'dist/index.*.min.js', and/or 'dist/(esm/cjs)/index*' directly (or use

## Docs

@todo
TODO

### About Currying:

Expand Down
6 changes: 1 addition & 5 deletions packages/fjl/src/function/curry.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
/**
* @deprecated Use idiomatic currying, when currying is required, instead - `curry*` functions defined here are actually
* doing something that is not really currying - forcing currying on argument tuple sets (`(a, b, c)`) - true currying applies
* each argument to a new function (`(a) => (b) => c`, etc.). Hence why this module is deprecated, and idiomatic curry
* each argument to a new function (`(a) => (b) => c`, etc.). Hence, why this module is deprecated, and idiomatic curry
* is favored instead.
*
* @author edlc
*
* @memberOf function
*
* @description Curry and CurryN functions.
*/

Expand Down
3 changes: 2 additions & 1 deletion packages/fjl/src/list/drop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ export const
*/
drop = <T = any>(n: number, xs: Iterable<T>): T[] => {
const out = [] as T[];
let count = Math.abs(n);
for (const x of xs) {
if (n-- > 0) continue;
if (count-- > 0) continue;
out.push(x as T);
}
return out;
Expand Down
2 changes: 0 additions & 2 deletions packages/fjl/src/list/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ export const
const limit = xs.length,
out = xs.slice(0, 0);

if (!limit) return out;

// Filter items against predicate
for (let i = 0; i < limit; i += 1) {
const x = xs[i];
Expand Down
3 changes: 2 additions & 1 deletion packages/fjl/src/list/take.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
*/
export const take = <T>(n: number, xs: Iterable<any>): T[] => {
const out = [] as T[];
let count = Math.abs(n);
for (const x of xs) {
if (n-- === 0) break;
if (count-- === 0) break;
out.push(x);
}
return out;
Expand Down
1 change: 0 additions & 1 deletion packages/fjl/src/list/utils/findWhere.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export const
xs: NumberIndexable
): undefined | any => {
const limit = xs.length;
if (!limit) return;
for (let ind = 0; ind < limit; ind++) {
const elm = xs[ind];
if (pred(elm, ind, xs)) return elm;
Expand Down
2 changes: 1 addition & 1 deletion packages/fjl/src/list/utils/lastIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {NumberIndexable} from "../../types";
export const

/**
* Gets last index of a list/list-like (Array|String|Function etc.).
* Gets last index of a list/list-like (Array|String|Function etc.), or `-1`
*/
lastIndex = (xs: NumberIndexable): number => (xs?.length ?? 0) - 1
;
4 changes: 2 additions & 2 deletions packages/fjl/src/list/utils/reduce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import {NumberIndexable, ReduceOp} from "../../types";
export const

/**
* Reduces an iterable by given reduction function - same as [].reduce but also for strings/objects with defined iterators, etc.).
* **Note:** If iterable is falsy, aggregator gets returned.
* Reduces a number indexable structure by given reduction function - same as [].reduce but also for strings,
* and user-land number-indexable types.
*/
reduce = (op: ReduceOp, agg: any, xs: NumberIndexable): ReturnType<typeof op> => {
const limit = xs.length;
Expand Down
3 changes: 2 additions & 1 deletion packages/fjl/src/list/utils/reduceRight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {NumberIndexable, ReduceOp} from "../../types";
export const

/**
* Reduces a "number indexable", from right-to-left, by given reduction function (same as [].reduceRight but also for strings and arbitrary "number indexable" objects/etc.).
* Reduces a "number indexable", from right-to-left, by given reduction function (same as [].reduceRight but also for
* strings and arbitrary "number indexable" objects/etc.).
*/
reduceRight = (op: ReduceOp, agg: any, xs: NumberIndexable): ReturnType<typeof op> => {
const limit = xs.length;
Expand Down
2 changes: 1 addition & 1 deletion packages/fjl/src/number/randomNatNumber.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {randomNumber} from "./randomNumber";

/**
* Returns a random natural number, between given `min` and `max` values (inclusive).
* Returns a random natural number between given min and max values (inclusive).
*/
export const randomNatNumber = (min = 0, max = 1) =>
Math.round(randomNumber(min, max)),
Expand Down
2 changes: 1 addition & 1 deletion packages/fjl/src/number/randomNumber.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Returns a random number, between given `min` and `max` values (inclusive).
* Returns a random number between given min and max values (inclusive).
*/
export const randomNumber = (min = 0, max = 1) =>
Math.random() * (max - min) + min,
Expand Down
3 changes: 2 additions & 1 deletion packages/fjl/src/object/is.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ export const
isNullish(x) ? false : instanceOfSome(x, ..._primitive_constructors),

/**
* Safe (doesn't error out on nullish value) check for enumerable properties on given value.
* Safe (doesn't error out on nullish value (via safe version of `Object.keys`) check for enumerable
* properties on given value.
*
* @todo write tests.
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/fjl/src/object/jsonClone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export const

/**
* Clones passed in value using `JSON.parse(JSON.stringify(...))` pattern.
* @todo Should [tentatively] be using `structuredClone` here (in a future (far-off release)).
* @todo Should [tentatively] be using `structuredClone` here (in a future release).
*/
jsonClone = (x: any): any => JSON.parse(JSON.stringify(x))

Expand Down
1 change: 1 addition & 0 deletions packages/fjl/src/object/lookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const
* Method is null safe (will not throw on `null` or `undefined`).
*
* @todo Should take `obj` as first argument.
* @todo Should be strictly typed.
*/
lookup = (key, obj): any => obj[key],

Expand Down
24 changes: 18 additions & 6 deletions packages/fjl/src/string/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* @description Contains functions for strings.
*/
import {$split, split} from '../_platform/string';
import {range} from "../list";
import {randNatNum} from "../number";
import {id} from "../function/id";

Expand Down Expand Up @@ -85,14 +84,27 @@ export const
/**
* Generates a random character.
*/
randChar = (min = 0, max = 0x10FFFF): string =>
String.fromCharCode(randNatNum(min, max)),
randChar = (minCharCode = 0, maxCharCode = 0x10FFFF): string =>
String.fromCharCode(randNatNum(minCharCode, maxCharCode)),

/**
* Generates a random string.
*/
randStr = (min = 0, max = 100): string =>
range(min, max) // @todo should use a generator here.
.reduce(str => str + randChar(min, max), '')
randStr = (strMinLen = 0, strMaxLen = 100, minCharCode = 0, maxCharCode = 100): string => {
let out = '';
do {
out += randChar(minCharCode, maxCharCode)
} while (out.length < strMinLen);
return out.slice(0, strMaxLen);
},

/**
* Generator that generates random strings using given min, and max, lengths.
* Note: Should be used with 'for ... of' loop, or similar.
*/
randStrIter = function *(strMinLen = 0, strMaxLen = 100, minCharCode = 0, maxCharCode = 100) {
while (true) {
yield randStr(strMinLen, strMaxLen, minCharCode, maxCharCode);
}
}
;
2 changes: 1 addition & 1 deletion packages/fjl/src/types/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
## 'types/'

@todo
TODO
7 changes: 4 additions & 3 deletions packages/fjl/src/types/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export type NumberIndexable<T=any> = ({
/**
* 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.
* *Note: Typed array types are not compatible with this type, as they don't have definitions for most of
* the slice methods required by this type.
*/
export interface Slice<T = any> extends Iterable<T> {
readonly length: number;
Expand All @@ -36,9 +37,9 @@ export interface Slice<T = any> extends Iterable<T> {
}

/**
* @deprecated Use your own type and/or existing native/otherwise types.
* @deprecated Use your own type and/or existing native, or other, types.
*
* Any type that contains a "readonly" `name` (functions, et al.) property.
* Any type that contains a "readonly" `name` (e.g, functions, et al.) property.
*/
export interface Nameable {
readonly name: string;
Expand Down
8 changes: 4 additions & 4 deletions packages/fjl/src/types/native.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export type DefinePropertyFunc<T> = (pd: PropertyDescriptor, propName: string, applicand: T) => T;
export type DefinePropertyFunc<T> = (pd: PropertyDescriptor, propName: string, applicant: T) => T;

export type DefinePropertiesFunc<T> = (pdm: PropertyDescriptorMap, applicand: T) => T;
export type DefinePropertiesFunc<T> = (pdm: PropertyDescriptorMap, applicant: T) => T;

export type GetOwnPropertyDescriptorFunc = (propName: string, applicand: any) => PropertyDescriptor;
export type GetOwnPropertyDescriptorFunc = (propName: string, applicant: any) => PropertyDescriptor;

export type CreateFunc = (pdm: PropertyDescriptorMap, applicand: any) => any;
export type CreateFunc = (pdm: PropertyDescriptorMap, applicant: any) => any;

export type IsFunc = (Type: any, value: any) => boolean;

Expand Down

0 comments on commit 6c08276

Please sign in to comment.