From 5aef8c964b29e6824f859006c2349b6514e7a017 Mon Sep 17 00:00:00 2001 From: Junior Date: Mon, 1 Feb 2021 21:15:36 +0000 Subject: [PATCH 1/8] reformat badge listing and title --- README.md | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 33f0052..cb09e55 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,18 @@ -# [@tinkerstorm/validate](https://npmjs.org/package/@tinkerstorm/validate) +# @tinkerstorm/validate -![CI status](https://github.com/TinkerStorm/validate/workflows/CI/badge.svg) -![NPM build](https://badgen.net/npm/v/@tinkerstorm/validate) -![NPM types](https://badgen.net/npm/types/@tinkerstorm/validate) -![GitHub license](https://badgen.net/github/license/TinkerStorm/validate) -![GitHub stars](https://badgen.net/github/stars/TinkerStorm/validate) +[![npm (version)](https://badgen.net/npm/v/@tinkerstorm/validate)](https://npm.im/@tinkerstorm/validate) +[![npm (types)](https://badgen.net/npm/types/@tinkerstorm/validate)](https://npm.im/@tinkerstorm/validate) +[![npm (downloads)](https://badgen.net/npm/dw/@tinkerstorm/validate)](https://npm.im/@tinkerstorm/validate) +[![npm (dependants)](https://badgen.net/npm/dependents/@tinkerstorm/validate)](https://npm.im/@tinkerstorm/validate) -## Open Issues +[![github (workflow)](https://github.com/TinkerStorm/validate/workflows/Build/badge.svg)](https://github.com/TinkerStorm/validate/actions) +[![github (stars)](https://badgen.net/github/stars/TinkerStorm/validate)](https://github.com/TinkerStorm/validate/stargazers) +[![github (open-issues)](https://badgen.net/github/open-issues/TinkerStorm/validate)](https://github.com/TinkerStorm/validate/issues) +[![github (open-prs)](https://badgen.net/github/open-prs/TinkerStorm/validate)](https://github.com/TinkerStorm/validate/pulls) -- [#4 - Thrown error scope](https://github.com/TinkerStorm/validate/issues/4) +[![linter (xo)](https://badgen.net/badge/🎨%20linter/xo/690DCA)](https://github.com/xojs/xo) +[![test env (ava)](https://badgen.net/badge/🧰%20toolbox/ava/F84824)](https://github.com/avajs/ava) +[![discord (chat)](https://badgen.net/badge/discord/chat/7289DA?icon=discord)](https://discord.gg/Bb3JQQG) ## API From c3f0af67c7efffebd19c25d28f691109325e2327 Mon Sep 17 00:00:00 2001 From: Junior Date: Mon, 1 Feb 2021 21:16:17 +0000 Subject: [PATCH 2/8] provide cohesive argument description + reformat table --- README.md | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index cb09e55..0b1451f 100644 --- a/README.md +++ b/README.md @@ -25,20 +25,21 @@ Where the function is called from (human-readable string). #### args A list of arguments that define how the function accepts parameters. +> The argument is to be passed as an array, which is then deconstructed into the positional descriptors below. --- -### ValidateOptions - -| Argument | Type | Description | -| ------------------ | ------------------------ | ---------------------------------------------------------------------------- | -| `name` | string | Name of the argument as a string literal. | -| `value` | any | The value of the argument as anything that the `types` and `options` accept. | -| `types` | string, object, function | An array of type resolvers. | -| `options` | object? | Argument options, can be omitted. | -| `options.inverse` | boolean? | Flip the condition state. | -| `options.rest` | boolean? | Process the argument as an array. | -| `options.optional` | boolean? | Make the argument not throw if it's null / undefined. Short hand check. | +### ValidateOptions as [name, value, types, options?] + +| Argument | Type | Description | +| ------------------ | -------------------------- | ---------------------------------------------------------------------------- | +| `name` | `string` | Name of the argument as a string literal. | +| `value` | `any` | The value of the argument as anything that the `types` and `options` accept. | +| `types` | `string, object, function` | An array of type resolvers. | +| `options` | `object?` | Argument options, can be omitted. | +| `options.inverse` | `boolean?` | Flip the condition state. | +| `options.rest` | `boolean?` | Process the argument as an array. | +| `options.optional` | `boolean?` | Make the argument not throw if it's null / undefined. Short hand check. | ## API (Future) From 724d56cf73145365dd2390031cdc8fbc95bc5d3f Mon Sep 17 00:00:00 2001 From: Junior Date: Mon, 1 Feb 2021 21:16:50 +0000 Subject: [PATCH 3/8] replace code block with core explaination --- README.md | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/README.md b/README.md index 0b1451f..6c4db89 100644 --- a/README.md +++ b/README.md @@ -52,18 +52,7 @@ A list of arguments that define how the function accepts parameters. Why not? Some code is designed to look like it shouldn't exist, but does anyway and works just as well. It was originally designed to be a simple 'private' method that would sit in a class structure or module without the need to use it elsewhere. -```js -const typeofAny = (target, ...types) => types.some(type => typeof target === type); - -// first attempt did not include the options parameter -function validate(origin, args) { - for (let [varName, value, anyof] of args) { - if (!typeofAny(value, ...anyof)) { - throw new TypeError(`${varName} (${value}) from ${origin} is not of any type ${anyof}`); - } - } -} -``` +At the base level of functionality, it verifies the type declarations, then object derivatives / class structures, and finally functions. While it may not seem like much, the overarching functions with the array, inverse and optional support provide some functionality I had never intended to begin with. At the end of it all, this first working version probably took around 3 or 4 hours (including testing and minor patches). From 60e35aa830e9488d87db69086334600c00e4e66d Mon Sep 17 00:00:00 2001 From: Junior Date: Mon, 1 Feb 2021 21:17:27 +0000 Subject: [PATCH 4/8] add additional reference to toolset from sindresorhus --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6c4db89..d8a1ba5 100644 --- a/README.md +++ b/README.md @@ -61,4 +61,5 @@ At the end of it all, this first working version probably took around 3 or 4 hou While I do like what I've accomplished here, it is still a joke in many ways and there are people I'd like to thank for supporting what I do or those who I look up to as inspiration. - [@Koltonix](https://github.com/Koltonix) and [@Mistiare](https://github.com/Mistiare) for their guidance and support. -- [@sindresorhus](https://github.com/sindresorhus) for continuing to provide the basis on how I, personally, should build my modules and projects (or at least a target to aim for). \ No newline at end of file +- [@sindresorhus](https://github.com/sindresorhus) for continuing to provide the basis on how I, personally, should build my modules and projects (or at least a target to aim for). + > Also for the amazing toolset of [xo](https://github.com/xojs/xo) and [ava](https://avajs.dev). \ No newline at end of file From 3e22e0a0bb7fe956965df394ba29b92acb11edc9 Mon Sep 17 00:00:00 2001 From: Junior Date: Mon, 1 Feb 2021 21:19:24 +0000 Subject: [PATCH 5/8] add possibility for providing preconstructed condition predicates * it may end up as part of the default export or as file seperate from the main export (though, bundling it in with the default is probably easier for the type declarations) --- README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/README.md b/README.md index d8a1ba5..085b47b 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,23 @@ A list of arguments that define how the function accepts parameters. - `validate.one(origin: string, name: string, value: any, types: (string | object | () => boolean)[], options?: ValidateOptions)` - `validate.arg(types: (string | object | () => boolean)[], options?: ValidateOptions)` +--- + +- `validate.checks[key: string](value?: any) => boolean` +- `validate.checks.exists(value?: any) => boolean` - Possiblity to move `options.optional` to this predicate... +- `validate.checks.truthy(value?: any) => boolean` +- `validate.checks.int(value: int?) => boolean` + +### Type bound + +*This would make use of predicate construction by passing the context of the previous scope through to the child.* + +- `validate.checks.min(n: T) => (value: T?) => boolean` +- `validate.checks.max(n: T) => (value: T?) => boolean` +- `validate.checks.between(low: T, high: T) => (value: T?) => boolean` + > The generic of `T` would infer that the accepted type has already been checked or is yet to be checked, but will default to that of primative `number`. + > - Could also be applied to `string` or `Array` when making use of the `.length` property. Either would work. + ## Why? Why not? Some code is designed to look like it shouldn't exist, but does anyway and works just as well. It was originally designed to be a simple 'private' method that would sit in a class structure or module without the need to use it elsewhere. From 06cb3450458ce307fadebf739f1c479b076919a0 Mon Sep 17 00:00:00 2001 From: Junior Date: Mon, 1 Feb 2021 21:28:34 +0000 Subject: [PATCH 6/8] add future optional check predicate --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 085b47b..3cca787 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,8 @@ A list of arguments that define how the function accepts parameters. --- - `validate.checks[key: string](value?: any) => boolean` -- `validate.checks.exists(value?: any) => boolean` - Possiblity to move `options.optional` to this predicate... +- `validate.checks.exists(value?: any) => boolean` +- `validate.checks.optional(value?: any) => boolean` - Possiblity to move `options.optional` to this predicate... - `validate.checks.truthy(value?: any) => boolean` - `validate.checks.int(value: int?) => boolean` From 156d7e78d210d2619a6fead9c36a9437f1b40d9f Mon Sep 17 00:00:00 2001 From: Junior Date: Tue, 2 Feb 2021 20:03:03 +0000 Subject: [PATCH 7/8] add internal type definitions, unsure if they'll ever be used. --- index.d.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/index.d.ts b/index.d.ts index f7af72a..850e2d3 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,5 +1,13 @@ // Internal +/* +declare function error(origin: string, arg: string, value: Value, types: any[]): void +declare function signal(value: Value, type: any): boolean +declare function multi(values: Values, types: any[]): boolean +declare function check(value: Value, types: any[], options?: ValidateOptions): boolean +declare function condition(value: Value, result: boolean, options?: ValidateOptions): boolean +*/ + // Exported interface ValidateOptions { From 560d106f5359b9f0b72bb1b248f4a2e14108505a Mon Sep 17 00:00:00 2001 From: Junior Date: Tue, 2 Feb 2021 20:12:11 +0000 Subject: [PATCH 8/8] clarify the use of .valueOf() over .length --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3cca787..94166e3 100644 --- a/README.md +++ b/README.md @@ -64,7 +64,7 @@ A list of arguments that define how the function accepts parameters. - `validate.checks.max(n: T) => (value: T?) => boolean` - `validate.checks.between(low: T, high: T) => (value: T?) => boolean` > The generic of `T` would infer that the accepted type has already been checked or is yet to be checked, but will default to that of primative `number`. - > - Could also be applied to `string` or `Array` when making use of the `.length` property. Either would work. + > - Could also be applied to `string` or `Array` when making use of the `.length` property or `.valueOf()` method. Either would work. ## Why?