runcheck 0.2.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,4 +1,164 @@
1
1
  # Runcheck
2
2
 
3
- Runtime type checks for js/typescript with autofix support.
3
+ A lib for js/typescript runtime type checks with autofix support. Runcheck has the goal of being very lightweight. Because of that, it has only around [1.7kb Gzipped](https://bundlephobia.com/package/runcheck), has no dependencies and is tree-shakeable!
4
4
 
5
+ Obs: Runcheck is in Beta and it's api can still change
6
+
7
+ # Installation
8
+
9
+ ```bash
10
+ pnpm add runcheck
11
+ ```
12
+
13
+ # Basic types:
14
+
15
+ | runcheck type | ts type equivalent |
16
+ | ------------------------------- | ------------------------------------------------- |
17
+ | `rc_string` | `string` |
18
+ | `rc_number` | `number` |
19
+ | `rc_boolean` | `boolean` |
20
+ | `rc_any` | `any` |
21
+ | `rc_null` | `null` |
22
+ | `rc_undefined` | `undefined` |
23
+ | `rc_date` | `Date` |
24
+ | `rc_intanceof(instance: T)` | Classes typecheck in general |
25
+ | `rc_literals(...literals: T[])` | Type literal in general like `hello`, `true`, `1` |
26
+ | `rc_union(...types: T[])` | Union types in general like `string \| 1` |
27
+ | `rc_array<T>(type: T)` | `T[]` |
28
+ | `rc_tuple<T>(...types: T[])` | `[T, T]` |
29
+
30
+ # Object types:
31
+
32
+ ## `rc_object`
33
+
34
+ ```ts
35
+ const shape = rc_object({
36
+ name: rc_string,
37
+ age: rc_number,
38
+ isCool: rc_boolean,
39
+ })
40
+ ```
41
+
42
+ The `rc_object` will allow extra properties but, any extra propertie will be striped in parsing. To allow extra in parsing properties, use `rc_extends_obj`.
43
+
44
+ ## `rc_strict_obj`
45
+
46
+ The same as `rc_object` but, any extra propertie will be throw an error in parsing.
47
+
48
+ ## `rc_obj_intersection`
49
+
50
+ Allow to merge two `rc_object` types. Example:
51
+
52
+ ```ts
53
+ const shape = rc_obj_intersection(
54
+ rc_object({
55
+ name: rc_string,
56
+ age: rc_number,
57
+ isCool: rc_boolean,
58
+ }),
59
+ rc_object({
60
+ address: rc_string,
61
+ phone: rc_string,
62
+ }),
63
+ )
64
+ ```
65
+
66
+ # Parsing
67
+
68
+ ```ts
69
+ import { rc_parse } from 'runcheck'
70
+
71
+ const input = JSON.parse(jsonInput)
72
+
73
+ const parseResult = rc_parse(input, rc_array(rc_string))
74
+
75
+ if (parseResult.error) {
76
+ throw new Error(parseResult.errors.join('\n'))
77
+ // Errors are a array of strings
78
+ }
79
+
80
+ const result = parseResult.data
81
+ // Do something with result
82
+ ```
83
+
84
+ You can also use `rc_parser` to create a reusable parser.
85
+
86
+ ```ts
87
+ import { rc_parser } from 'runcheck'
88
+
89
+ const parser = rc_parser(rc_array(rc_string))
90
+
91
+ const parseResult = parser(jsonInput)
92
+ const parseResult2 = parser(jsonInput2)
93
+ ```
94
+
95
+ # Type assertion
96
+
97
+ Use `rc_is_valid` and `rc_validator` to do a simple type assertion.
98
+
99
+ ```ts
100
+ import { rc_is_valid } from 'runcheck'
101
+
102
+ const input = JSON.parse(jsonInput)
103
+
104
+ if (rc_is_valid(input, rc_array(rc_string))) {
105
+ // input will be inferred by ts as `string[]`
106
+ }
107
+ ```
108
+
109
+ # Autofixing and fallback values in parsing
110
+
111
+ Values can be autofixed and fallback values can be provided for parsing. The checks will pass but the result will return warnings messages.
112
+
113
+ ```ts
114
+ type SuccessResult = {
115
+ error: false
116
+ data: T
117
+ warnings: string[] | false
118
+ }
119
+ ```
120
+
121
+ ## Fallback
122
+
123
+ Use the method `rc_[type].withFallback(fallback)` to provide a fallback value if the input is not valid.
124
+
125
+ ```ts
126
+ const input = 'hello'
127
+
128
+ const result = rc_parse(input, rc_string.withFallback('world'))
129
+ ```
130
+
131
+ ## AutoFix
132
+
133
+ You can also use `rc_[type].autoFix()` to automatically fix the input if it is not valid.
134
+
135
+ ```ts
136
+ const input = 1
137
+
138
+ const result = rc_parse(
139
+ input,
140
+ rc_string.autoFix((input) => input.toString()),
141
+ )
142
+ ```
143
+
144
+ There are also some predefined autofixed types that you can import:
145
+
146
+ ```ts
147
+ import { rc_string_autofix, rc_boolean_autofix } from 'runcheck/autofixable'
148
+
149
+ // use like any other type
150
+ ```
151
+
152
+ # Performing custom checks
153
+
154
+ You can also use `rc_[type].where(customCheckFunction)` to perform custom checks.
155
+
156
+ ```ts
157
+ const input = 1
158
+
159
+ const positiveNumberType = rc_number.where((input) => input > 0)
160
+ ```
161
+
162
+ # Optional and nullable types
163
+
164
+ Use `rc_[type].optional()` to make a type optional, and `rc_[type].orNullish()` to make a type nullable.
@@ -0,0 +1 @@
1
+ "use strict";var c=Object.defineProperty;var l=Object.getOwnPropertyDescriptor;var T=Object.getOwnPropertyNames;var d=Object.prototype.hasOwnProperty;var p=(e,r)=>{for(var s in r)c(e,s,{get:r[s],enumerable:!0})},R=(e,r,s,i)=>{if(r&&typeof r=="object"||typeof r=="function")for(let n of T(r))!d.call(e,n)&&n!==s&&c(e,n,{get:()=>r[n],enumerable:!(i=l(r,n))||i.enumerable});return e};var _=e=>R(c({},"__esModule",{value:!0}),e);var k={};p(k,{rc_boolean_autofix:()=>O,rc_number_autofix:()=>$,rc_string_autofix:()=>A});module.exports=_(k);function x(e){return{...this,s:e}}function o(e,r,s,i){if(e.i&&r===void 0)return[!0,r];if(e.c&&r==null)return[!0,r];let n=i();if(n&&(n===!0||"data"in n)){let t=n===!0?r:n.data;return e.t&&!e.t(t)?[!1,[e.n(t)]]:[!0,t]}if(e.s!==void 0)return s.warnings.push(`Fallback used, ${e.n(r)}`),[!0,e.s];if(e.u&&e.o){let t=e.o(r);if(t)return e.t&&!e.t(t.fixed)?[!1,[e.n(t.fixed)]]:(s.warnings.push(`Autofixed from, ${e.n(r)}`),[!0,t.fixed])}return[!1,n&&"errors"in n?n.errors:[e.n(r)]]}function b(e){return{...this,u:!0,o:e}}function h(e){return{...this,t:e,e:`${this.e}_with_predicate`}}function g(){return{...this,i:!0}}function j(e){return`Type '${m(e)}' is not assignable to '${this.e}'`}function w(){return{...this,c:!0,e:`${this.e}_or_nullish`}}var a={withFallback:x,where:h,optional:g,n:j,nullable:w,withAutofix:b},S={...a,r(e,r){return o(this,e,r,()=>e===void 0)},e:"undefined"},P={...a,r(e,r){return o(this,e,r,()=>e===null)},e:"null"},I={...a,r(e,r){return o(this,e,r,()=>!0)},e:"any"},u={...a,r(e,r){return o(this,e,r,()=>typeof e=="boolean")},e:"boolean"},y={...a,r(e,r){return o(this,e,r,()=>typeof e=="string")},e:"string"},f={...a,r(e,r){return o(this,e,r,()=>typeof e=="number"&&!Number.isNaN(e))},e:"number"},v={...a,r(e,r){return o(this,e,r,()=>typeof e=="object"&&e instanceof Date&&!Number.isNaN(e.getTime()))},e:"date"};function m(e){if(typeof e=="object"){if(Array.isArray(e))return"array";if(!e)return"null"}return typeof e}var O=u.withAutofix(e=>e===0||e===1?{fixed:!!e}:e==="true"||e==="false"?{fixed:e==="true"}:!1),A=y.withAutofix(e=>typeof e=="number"&&!Number.isNaN(e)?{fixed:e.toString()}:!1),$=f.withAutofix(e=>{if(typeof e=="string"){let r=Number(e);if(!Number.isNaN(r))return{fixed:r}}return!1});0&&(module.exports={rc_boolean_autofix,rc_number_autofix,rc_string_autofix});
@@ -0,0 +1,7 @@
1
+ import { RcType } from './runcheck.js';
2
+
3
+ declare const rc_boolean_autofix: RcType<boolean>;
4
+ declare const rc_string_autofix: RcType<string>;
5
+ declare const rc_number_autofix: RcType<number>;
6
+
7
+ export { rc_boolean_autofix, rc_number_autofix, rc_string_autofix };
@@ -0,0 +1 @@
1
+ import{d as t,e as f,f as o}from"./chunk-TZCAUEQX.js";var s=t.withAutofix(r=>r===0||r===1?{fixed:!!r}:r==="true"||r==="false"?{fixed:r==="true"}:!1),u=f.withAutofix(r=>typeof r=="number"&&!Number.isNaN(r)?{fixed:r.toString()}:!1),x=o.withAutofix(r=>{if(typeof r=="string"){let e=Number(r);if(!Number.isNaN(e))return{fixed:e}}return!1});export{s as rc_boolean_autofix,x as rc_number_autofix,u as rc_string_autofix};
@@ -0,0 +1 @@
1
+ function x(e){return{...this,s:e}}function a(e,r,n,s){if(e.i&&r===void 0)return[!0,r];if(e.c&&r==null)return[!0,r];let t=s();if(t&&(t===!0||"data"in t)){let o=t===!0?r:t.data;return e.t&&!e.t(o)?[!1,[e.n(o)]]:[!0,o]}if(e.s!==void 0)return n.warnings.push(`Fallback used, ${e.n(r)}`),[!0,e.s];if(e.u&&e.o){let o=e.o(r);if(o)return e.t&&!e.t(o.fixed)?[!1,[e.n(o.fixed)]]:(n.warnings.push(`Autofixed from, ${e.n(r)}`),[!0,o.fixed])}return[!1,t&&"errors"in t?t.errors:[e.n(r)]]}function g(e){return{...this,u:!0,o:e}}function j(e){return{...this,t:e,e:`${this.e}_with_predicate`}}function w(){return{...this,i:!0}}function O(e){return`Type '${p(e)}' is not assignable to '${this.e}'`}function m(){return{...this,c:!0,e:`${this.e}_or_nullish`}}var i={withFallback:x,where:j,optional:w,n:O,nullable:m,withAutofix:g},P={...i,r(e,r){return a(this,e,r,()=>e===void 0)},e:"undefined"},S={...i,r(e,r){return a(this,e,r,()=>e===null)},e:"null"},I={...i,r(e,r){return a(this,e,r,()=>!0)},e:"any"},N={...i,r(e,r){return a(this,e,r,()=>typeof e=="boolean")},e:"boolean"},v={...i,r(e,r){return a(this,e,r,()=>typeof e=="string")},e:"string"},E={...i,r(e,r){return a(this,e,r,()=>typeof e=="number"&&!Number.isNaN(e))},e:"number"},K={...i,r(e,r){return a(this,e,r,()=>typeof e=="object"&&e instanceof Date&&!Number.isNaN(e.getTime()))},e:"date"};function F(e){return{...i,r(r,n){return a(this,r,n,()=>r instanceof e)},e:`instanceof_${e.name?`_${e.name}`:""}`}}function C(...e){if(e.length===0)throw new Error("rc_literal requires at least one literal");return{...i,r(r,n){return a(this,r,n,()=>{for(let s of e)if(r===s)return!0;return!1})},e:e.length==1?`${p(e[0])}_literal`:"literals"}}function z(...e){if(e.length===0)throw new Error("Unions should have at least one type");return{...i,r(r,n){return a(this,r,n,()=>{for(let s of e)if(s.r(r,n)[0])return!0;return!1})},e:e.map(r=>r.e).join(" | ")}}function T(e,r){if(e.startsWith("$[")||e.startsWith("$.")){let[n="",s]=e.split(": "),t=n.slice(1);return`$${r}${t}: ${s}`}return`$${r}: ${e}`}function l(e){return{...i,a:e,e:"object",r(r,n){return a(this,r,n,()=>{if(!A(r))return!1;let s=new Set(Object.keys(r)),t={},o=[];for(let[c,u]of Object.entries(e)){let y=c,f=r[c];s.delete(c);let[R,_]=u.r(f,n);if(R)t[y]=f;else{let b=_;for(let h of b)o.push(T(h,`.${c}`))}}if(this.e==="strict_obj"&&s.size>0)for(let c of s)o.push(`Key '${c}' is not defined in the object shape`);return o.length>0?{errors:o}:this.e.startsWith("extends_object")?{data:r}:{data:t}})}}}function B(e){return{...l(e),e:"extends_object"}}function M(e){return{...l(e),e:"strict_obj"}}function U(e,r){return l({...e.a,...r.a})}function d(e,r,n){let s=-1;for(let t of e){s++;let o=Array.isArray(r)?r[s]:r,[c,u]=o.r(t,n);if(!c)return{errors:u.map(y=>T(y,`[${s}]`))}}return!0}function V(e){return{...i,e:`${e.e}[]`,r(r,n){return a(this,r,n,()=>Array.isArray(r)?r.length===0?!0:d.call(this,r,e,n):!1)}}}function W(e){return{...i,e:`[${e.map(r=>r.e).join(", ")}]`,r(r,n){return a(this,r,n,()=>!Array.isArray(r)||r.length!==e.length?!1:d.call(this,r,e,n))}}}function $(e,r){let n={warnings:[]},[s,t]=r.r(e,n);return s?{error:!1,data:t,warnings:n.warnings.length>0?n.warnings:!1}:{error:!0,errors:t}}function D(e){return r=>$(r,e)}function k(e,r){let n={warnings:[]};return!!r.r(e,n)[0]}function q(e){return r=>k(r,e)}function p(e){if(typeof e=="object"){if(Array.isArray(e))return"array";if(!e)return"null"}return typeof e}function A(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)}export{P as a,S as b,I as c,N as d,v as e,E as f,K as g,F as h,C as i,z as j,l as k,B as l,M as m,U as n,V as o,W as p,$ as q,D as r,k as s,q as t};
package/dist/runcheck.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";var f=Object.defineProperty;var w=Object.getOwnPropertyDescriptor;var O=Object.getOwnPropertyNames;var m=Object.prototype.hasOwnProperty;var k=(e,r)=>{for(var n in r)f(e,n,{get:r[n],enumerable:!0})},$=(e,r,n,s)=>{if(r&&typeof r=="object"||typeof r=="function")for(let t of O(r))!m.call(e,t)&&t!==n&&f(e,t,{get:()=>r[t],enumerable:!(s=w(r,t))||s.enumerable});return e};var A=e=>$(f({},"__esModule",{value:!0}),e);var Z={};k(Z,{rc_any:()=>M,rc_array:()=>J,rc_boolean:()=>C,rc_date:()=>B,rc_extends_obj:()=>q,rc_instanceof:()=>U,rc_is_valid:()=>h,rc_literals:()=>V,rc_null:()=>K,rc_number:()=>z,rc_obj_intersection:()=>H,rc_object:()=>u,rc_parse:()=>R,rc_parser:()=>Q,rc_strict_obj:()=>G,rc_string:()=>W,rc_tuple:()=>L,rc_undefined:()=>F,rc_union:()=>D,rc_validator:()=>X});module.exports=A(Z);function N(e){return{...this,o:e}}function a(e,r,n,s){if(e.i&&r===void 0)return[!0,r];if(e.c&&r==null)return[!0,r];let t=s();if(t&&(t===!0||"data"in t)){let o=t===!0?r:t.data;return e.t&&!e.t(o)?[!1,[e.s(o)]]:[!0,o]}if(e.o!==void 0)return n.warnings.push(`Fallback used, ${e.s(r)}`),[!0,e.o];if(e.u&&e.n){let o=e.n(r);if(o)return e.t&&!e.t(o.fixed)?[!1,[e.s(o.fixed)]]:[!0,o.fixed]}return[!1,t&&"errors"in t?t.errors:[e.s(r)]]}function S(e){if(!this.n&&!e)throw new Error("This type don't have a default autofix and no custom one was provided");return{...this,u:!0,n:e||this.n}}function P(e){return{...this,t:e,e:`${this.e}_with_predicate`}}function v(){return{...this,i:!0}}function I(e){return`Type '${b(e)}' is not assignable to '${this.e}'`}function E(){return{...this,c:!0,e:`${this.e}_or_nullish`}}var i={withFallback:N,where:P,optional:v,s:I,orNullish:E,withAutofix:S},F={...i,r(e,r){return a(this,e,r,()=>e===void 0)},e:"undefined"},K={...i,r(e,r){return a(this,e,r,()=>e===null)},e:"null"},M={...i,r(e,r){return a(this,e,r,()=>!0)},e:"any"},C={...i,r(e,r){return a(this,e,r,()=>typeof e=="boolean")},e:"boolean",n(e){return e===0||e===1?{fixed:!!e}:e==="true"||e==="false"?{fixed:e==="true"}:!1}},W={...i,r(e,r){return a(this,e,r,()=>typeof e=="string")},e:"string",n(e){return typeof e=="number"&&!Number.isNaN(e)?{fixed:e.toString()}:!1}},z={...i,r(e,r){return a(this,e,r,()=>typeof e=="number"&&!Number.isNaN(e))},e:"number",n(e){if(typeof e=="string"){let r=Number(e);if(!Number.isNaN(r))return{fixed:r}}return!1}},B={...i,r(e,r){return a(this,e,r,()=>typeof e=="object"&&e instanceof Date&&!Number.isNaN(e.getTime()))},e:"date"};function U(e){return{...i,r(r,n){return a(this,r,n,()=>r instanceof e)},e:`instanceof_${e.name?`_${e.name}`:""}`}}function V(...e){if(e.length===0)throw new Error("rc_literal requires at least one literal");return{...i,r(r,n){return a(this,r,n,()=>{for(let s of e)if(r===s)return!0;return!1})},e:e.length==1?`${b(e[0])}_literal`:"literals"}}function D(...e){if(e.length===0)throw new Error("Unions should have at least one type");return{...i,r(r,n){return a(this,r,n,()=>{for(let s of e)if(s.r(r,n)[0])return!0;return!1})},e:e.map(r=>r.e).join(" | ")}}function d(e,r){if(e.startsWith("$[")||e.startsWith("$.")){let[n="",s]=e.split(": "),t=n.slice(1);return`$${r}${t}: ${s}`}return`$${r}: ${e}`}function u(e){return{...i,a:e,e:"object",r(r,n){return a(this,r,n,()=>{if(!Y(r))return!1;let s=new Set(Object.keys(r)),t={},o=[];for(let[c,y]of Object.entries(e)){let l=c,T=r[c];s.delete(c);let[_,x]=y.r(T,n);if(_)t[l]=T;else{let g=x;for(let j of g)o.push(d(j,`.${c}`))}}if(this.e==="strict_obj"&&s.size>0)for(let c of s)o.push(`Key '${c}' is not defined in the object shape`);return o.length>0?{errors:o}:this.e.startsWith("extends_object")?{data:r}:{data:t}})}}}function q(e){return{...u(e),e:"extends_object"}}function G(e){return{...u(e),e:"strict_obj"}}function H(e,r){return u({...e.a,...r.a})}function p(e,r,n){let s=-1;for(let t of e){s++;let o=Array.isArray(r)?r[s]:r,[c,y]=o.r(t,n);if(!c)return{errors:y.map(l=>d(l,`[${s}]`))}}return!0}function J(e){return{...i,e:`${e.e}[]`,r(r,n){return a(this,r,n,()=>Array.isArray(r)?r.length===0?!0:p.call(this,r,e,n):!1)}}}function L(e){return{...i,e:`[${e.map(r=>r.e).join(", ")}]`,r(r,n){return a(this,r,n,()=>!Array.isArray(r)||r.length!==e.length?!1:p.call(this,r,e,n))}}}function R(e,r){let n={warnings:[]},[s,t]=r.r(e,n);return s?{error:!1,data:t,warningMsgs:n.warnings.length>0?n.warnings:!1}:{error:!0,errors:t}}function Q(e){return r=>R(r,e)}function h(e,r){let n={warnings:[]};return!!r.r(e,n)[0]}function X(e){return r=>h(r,e)}function b(e){if(typeof e=="object"){if(Array.isArray(e))return"array";if(!e)return"null"}return typeof e}function Y(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)}0&&(module.exports={rc_any,rc_array,rc_boolean,rc_date,rc_extends_obj,rc_instanceof,rc_is_valid,rc_literals,rc_null,rc_number,rc_obj_intersection,rc_object,rc_parse,rc_parser,rc_strict_obj,rc_string,rc_tuple,rc_undefined,rc_union,rc_validator});
1
+ "use strict";var f=Object.defineProperty;var w=Object.getOwnPropertyDescriptor;var O=Object.getOwnPropertyNames;var m=Object.prototype.hasOwnProperty;var $=(e,r)=>{for(var n in r)f(e,n,{get:r[n],enumerable:!0})},k=(e,r,n,s)=>{if(r&&typeof r=="object"||typeof r=="function")for(let t of O(r))!m.call(e,t)&&t!==n&&f(e,t,{get:()=>r[t],enumerable:!(s=w(r,t))||s.enumerable});return e};var A=e=>k(f({},"__esModule",{value:!0}),e);var Z={};$(Z,{rc_any:()=>C,rc_array:()=>J,rc_boolean:()=>z,rc_date:()=>U,rc_extends_obj:()=>q,rc_instanceof:()=>V,rc_is_valid:()=>_,rc_literals:()=>W,rc_null:()=>F,rc_number:()=>M,rc_obj_intersection:()=>H,rc_object:()=>u,rc_parse:()=>R,rc_parser:()=>Q,rc_strict_obj:()=>G,rc_string:()=>B,rc_tuple:()=>L,rc_undefined:()=>K,rc_union:()=>D,rc_validator:()=>X});module.exports=A(Z);function P(e){return{...this,s:e}}function a(e,r,n,s){if(e.i&&r===void 0)return[!0,r];if(e.c&&r==null)return[!0,r];let t=s();if(t&&(t===!0||"data"in t)){let o=t===!0?r:t.data;return e.t&&!e.t(o)?[!1,[e.n(o)]]:[!0,o]}if(e.s!==void 0)return n.warnings.push(`Fallback used, ${e.n(r)}`),[!0,e.s];if(e.u&&e.o){let o=e.o(r);if(o)return e.t&&!e.t(o.fixed)?[!1,[e.n(o.fixed)]]:(n.warnings.push(`Autofixed from, ${e.n(r)}`),[!0,o.fixed])}return[!1,t&&"errors"in t?t.errors:[e.n(r)]]}function S(e){return{...this,u:!0,o:e}}function I(e){return{...this,t:e,e:`${this.e}_with_predicate`}}function N(){return{...this,i:!0}}function v(e){return`Type '${b(e)}' is not assignable to '${this.e}'`}function E(){return{...this,c:!0,e:`${this.e}_or_nullish`}}var i={withFallback:P,where:I,optional:N,n:v,nullable:E,withAutofix:S},K={...i,r(e,r){return a(this,e,r,()=>e===void 0)},e:"undefined"},F={...i,r(e,r){return a(this,e,r,()=>e===null)},e:"null"},C={...i,r(e,r){return a(this,e,r,()=>!0)},e:"any"},z={...i,r(e,r){return a(this,e,r,()=>typeof e=="boolean")},e:"boolean"},B={...i,r(e,r){return a(this,e,r,()=>typeof e=="string")},e:"string"},M={...i,r(e,r){return a(this,e,r,()=>typeof e=="number"&&!Number.isNaN(e))},e:"number"},U={...i,r(e,r){return a(this,e,r,()=>typeof e=="object"&&e instanceof Date&&!Number.isNaN(e.getTime()))},e:"date"};function V(e){return{...i,r(r,n){return a(this,r,n,()=>r instanceof e)},e:`instanceof_${e.name?`_${e.name}`:""}`}}function W(...e){if(e.length===0)throw new Error("rc_literal requires at least one literal");return{...i,r(r,n){return a(this,r,n,()=>{for(let s of e)if(r===s)return!0;return!1})},e:e.length==1?`${b(e[0])}_literal`:"literals"}}function D(...e){if(e.length===0)throw new Error("Unions should have at least one type");return{...i,r(r,n){return a(this,r,n,()=>{for(let s of e)if(s.r(r,n)[0])return!0;return!1})},e:e.map(r=>r.e).join(" | ")}}function d(e,r){if(e.startsWith("$[")||e.startsWith("$.")){let[n="",s]=e.split(": "),t=n.slice(1);return`$${r}${t}: ${s}`}return`$${r}: ${e}`}function u(e){return{...i,a:e,e:"object",r(r,n){return a(this,r,n,()=>{if(!Y(r))return!1;let s=new Set(Object.keys(r)),t={},o=[];for(let[c,y]of Object.entries(e)){let l=c,T=r[c];s.delete(c);let[h,x]=y.r(T,n);if(h)t[l]=T;else{let g=x;for(let j of g)o.push(d(j,`.${c}`))}}if(this.e==="strict_obj"&&s.size>0)for(let c of s)o.push(`Key '${c}' is not defined in the object shape`);return o.length>0?{errors:o}:this.e.startsWith("extends_object")?{data:r}:{data:t}})}}}function q(e){return{...u(e),e:"extends_object"}}function G(e){return{...u(e),e:"strict_obj"}}function H(e,r){return u({...e.a,...r.a})}function p(e,r,n){let s=-1;for(let t of e){s++;let o=Array.isArray(r)?r[s]:r,[c,y]=o.r(t,n);if(!c)return{errors:y.map(l=>d(l,`[${s}]`))}}return!0}function J(e){return{...i,e:`${e.e}[]`,r(r,n){return a(this,r,n,()=>Array.isArray(r)?r.length===0?!0:p.call(this,r,e,n):!1)}}}function L(e){return{...i,e:`[${e.map(r=>r.e).join(", ")}]`,r(r,n){return a(this,r,n,()=>!Array.isArray(r)||r.length!==e.length?!1:p.call(this,r,e,n))}}}function R(e,r){let n={warnings:[]},[s,t]=r.r(e,n);return s?{error:!1,data:t,warnings:n.warnings.length>0?n.warnings:!1}:{error:!0,errors:t}}function Q(e){return r=>R(r,e)}function _(e,r){let n={warnings:[]};return!!r.r(e,n)[0]}function X(e){return r=>_(r,e)}function b(e){if(typeof e=="object"){if(Array.isArray(e))return"array";if(!e)return"null"}return typeof e}function Y(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)}0&&(module.exports={rc_any,rc_array,rc_boolean,rc_date,rc_extends_obj,rc_instanceof,rc_is_valid,rc_literals,rc_null,rc_number,rc_obj_intersection,rc_object,rc_parse,rc_parser,rc_strict_obj,rc_string,rc_tuple,rc_undefined,rc_union,rc_validator});
@@ -1,7 +1,7 @@
1
1
  declare type RcParseResult<T> = {
2
2
  error: false;
3
3
  data: T;
4
- warningMsgs: string[] | false;
4
+ warnings: string[] | false;
5
5
  } | {
6
6
  error: true;
7
7
  errors: string[];
@@ -16,8 +16,8 @@ declare type RcType<T> = {
16
16
  readonly withFallback: (fallback: T) => RcType<T>;
17
17
  readonly where: (predicate: (input: T) => boolean) => RcType<T>;
18
18
  readonly optional: () => RcOptional<T>;
19
- readonly orNullish: () => RcType<T | null | undefined>;
20
- readonly withAutofix: (customAutofix?: (input: unknown) => false | {
19
+ readonly nullable: () => RcType<T | null | undefined>;
20
+ readonly withAutofix: (customAutofix: (input: unknown) => false | {
21
21
  fixed: T;
22
22
  }) => RcType<T>;
23
23
  };
package/dist/runcheck.js CHANGED
@@ -1 +1 @@
1
- function x(e){return{...this,o:e}}function a(e,r,n,s){if(e.i&&r===void 0)return[!0,r];if(e.c&&r==null)return[!0,r];let t=s();if(t&&(t===!0||"data"in t)){let o=t===!0?r:t.data;return e.t&&!e.t(o)?[!1,[e.s(o)]]:[!0,o]}if(e.o!==void 0)return n.warnings.push(`Fallback used, ${e.s(r)}`),[!0,e.o];if(e.u&&e.n){let o=e.n(r);if(o)return e.t&&!e.t(o.fixed)?[!1,[e.s(o.fixed)]]:[!0,o.fixed]}return[!1,t&&"errors"in t?t.errors:[e.s(r)]]}function g(e){if(!this.n&&!e)throw new Error("This type don't have a default autofix and no custom one was provided");return{...this,u:!0,n:e||this.n}}function j(e){return{...this,t:e,e:`${this.e}_with_predicate`}}function w(){return{...this,i:!0}}function O(e){return`Type '${p(e)}' is not assignable to '${this.e}'`}function m(){return{...this,c:!0,e:`${this.e}_or_nullish`}}var i={withFallback:x,where:j,optional:w,s:O,orNullish:m,withAutofix:g},N={...i,r(e,r){return a(this,e,r,()=>e===void 0)},e:"undefined"},S={...i,r(e,r){return a(this,e,r,()=>e===null)},e:"null"},P={...i,r(e,r){return a(this,e,r,()=>!0)},e:"any"},v={...i,r(e,r){return a(this,e,r,()=>typeof e=="boolean")},e:"boolean",n(e){return e===0||e===1?{fixed:!!e}:e==="true"||e==="false"?{fixed:e==="true"}:!1}},I={...i,r(e,r){return a(this,e,r,()=>typeof e=="string")},e:"string",n(e){return typeof e=="number"&&!Number.isNaN(e)?{fixed:e.toString()}:!1}},E={...i,r(e,r){return a(this,e,r,()=>typeof e=="number"&&!Number.isNaN(e))},e:"number",n(e){if(typeof e=="string"){let r=Number(e);if(!Number.isNaN(r))return{fixed:r}}return!1}},F={...i,r(e,r){return a(this,e,r,()=>typeof e=="object"&&e instanceof Date&&!Number.isNaN(e.getTime()))},e:"date"};function K(e){return{...i,r(r,n){return a(this,r,n,()=>r instanceof e)},e:`instanceof_${e.name?`_${e.name}`:""}`}}function M(...e){if(e.length===0)throw new Error("rc_literal requires at least one literal");return{...i,r(r,n){return a(this,r,n,()=>{for(let s of e)if(r===s)return!0;return!1})},e:e.length==1?`${p(e[0])}_literal`:"literals"}}function C(...e){if(e.length===0)throw new Error("Unions should have at least one type");return{...i,r(r,n){return a(this,r,n,()=>{for(let s of e)if(s.r(r,n)[0])return!0;return!1})},e:e.map(r=>r.e).join(" | ")}}function T(e,r){if(e.startsWith("$[")||e.startsWith("$.")){let[n="",s]=e.split(": "),t=n.slice(1);return`$${r}${t}: ${s}`}return`$${r}: ${e}`}function l(e){return{...i,a:e,e:"object",r(r,n){return a(this,r,n,()=>{if(!A(r))return!1;let s=new Set(Object.keys(r)),t={},o=[];for(let[c,u]of Object.entries(e)){let y=c,f=r[c];s.delete(c);let[R,h]=u.r(f,n);if(R)t[y]=f;else{let b=h;for(let _ of b)o.push(T(_,`.${c}`))}}if(this.e==="strict_obj"&&s.size>0)for(let c of s)o.push(`Key '${c}' is not defined in the object shape`);return o.length>0?{errors:o}:this.e.startsWith("extends_object")?{data:r}:{data:t}})}}}function W(e){return{...l(e),e:"extends_object"}}function z(e){return{...l(e),e:"strict_obj"}}function B(e,r){return l({...e.a,...r.a})}function d(e,r,n){let s=-1;for(let t of e){s++;let o=Array.isArray(r)?r[s]:r,[c,u]=o.r(t,n);if(!c)return{errors:u.map(y=>T(y,`[${s}]`))}}return!0}function U(e){return{...i,e:`${e.e}[]`,r(r,n){return a(this,r,n,()=>Array.isArray(r)?r.length===0?!0:d.call(this,r,e,n):!1)}}}function V(e){return{...i,e:`[${e.map(r=>r.e).join(", ")}]`,r(r,n){return a(this,r,n,()=>!Array.isArray(r)||r.length!==e.length?!1:d.call(this,r,e,n))}}}function k(e,r){let n={warnings:[]},[s,t]=r.r(e,n);return s?{error:!1,data:t,warningMsgs:n.warnings.length>0?n.warnings:!1}:{error:!0,errors:t}}function D(e){return r=>k(r,e)}function $(e,r){let n={warnings:[]};return!!r.r(e,n)[0]}function q(e){return r=>$(r,e)}function p(e){if(typeof e=="object"){if(Array.isArray(e))return"array";if(!e)return"null"}return typeof e}function A(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)}export{P as rc_any,U as rc_array,v as rc_boolean,F as rc_date,W as rc_extends_obj,K as rc_instanceof,$ as rc_is_valid,M as rc_literals,S as rc_null,E as rc_number,B as rc_obj_intersection,l as rc_object,k as rc_parse,D as rc_parser,z as rc_strict_obj,I as rc_string,V as rc_tuple,N as rc_undefined,C as rc_union,q as rc_validator};
1
+ import{a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t}from"./chunk-TZCAUEQX.js";export{c as rc_any,o as rc_array,d as rc_boolean,g as rc_date,l as rc_extends_obj,h as rc_instanceof,s as rc_is_valid,i as rc_literals,b as rc_null,f as rc_number,n as rc_obj_intersection,k as rc_object,q as rc_parse,r as rc_parser,m as rc_strict_obj,e as rc_string,p as rc_tuple,a as rc_undefined,j as rc_union,t as rc_validator};
package/package.json CHANGED
@@ -1,12 +1,15 @@
1
1
  {
2
2
  "name": "runcheck",
3
- "version": "0.2.0",
3
+ "description": "A tiny (less than 2 KiB Gzipped) and treeshakable! lib for typescript runtime type checks with autofix support",
4
+ "version": "0.5.0",
4
5
  "license": "MIT",
5
6
  "scripts": {
6
7
  "test": "vitest --ui",
7
8
  "test:run": "vitest run",
9
+ "benchmark": "tsm benchmarks/benchmark.ts",
10
+ "benchmark:generate-profile": "tsm benchmarks/generateProfiles.ts",
8
11
  "build": "pnpm test:run && pnpm build:no-test",
9
- "build:no-test": "tsup src/runcheck.ts --minify --dts && tsm scripts/removeInternalDeclarations.ts",
12
+ "build:no-test": "tsup --minify --dts && tsm scripts/removeInternalDeclarations.ts",
10
13
  "npm-publish": "./scripts/check-if-is-sync.sh && pnpm build && npm publish"
11
14
  },
12
15
  "files": [
@@ -22,7 +25,13 @@
22
25
  "exports": {
23
26
  ".": {
24
27
  "import": "./dist/runcheck.js",
28
+ "types": "./dist/runcheck.d.ts",
25
29
  "require": "./dist/runcheck.cjs"
30
+ },
31
+ "./autofixable": {
32
+ "import": "./dist/autofixable.js",
33
+ "types": "./dist/autofixable.d.ts",
34
+ "require": "./dist/autofixable.cjs"
26
35
  }
27
36
  },
28
37
  "sideEffects": false,
@@ -35,12 +44,15 @@
35
44
  "@typescript-eslint/parser": "^5.33.0",
36
45
  "@vitest/ui": "^0.21.1",
37
46
  "eslint": "^8.22.0",
47
+ "eslint-plugin-jest": "^26.8.2",
48
+ "mitata": "^0.1.6",
49
+ "myzod": "^1.8.8",
50
+ "tsm": "^2.2.2",
51
+ "tsup": "^6.2.2",
38
52
  "typescript": "^4.8.1-rc",
53
+ "v8-profiler-next": "^1.9.0",
39
54
  "vite": "^3.0.7",
40
55
  "vitest": "^0.21.1",
41
- "zod": "^3.18.0",
42
- "eslint-plugin-jest": "^26.8.2",
43
- "tsm": "^2.2.2",
44
- "tsup": "^6.2.2"
56
+ "zod": "^3.18.0"
45
57
  }
46
58
  }