runcheck 0.1.0 → 0.2.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,3 +1,4 @@
1
1
  # Runcheck
2
2
 
3
- Runtime type checks for typescript
3
+ Runtime type checks for js/typescript with autofix support.
4
+
@@ -0,0 +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});
@@ -0,0 +1,62 @@
1
+ declare type RcParseResult<T> = {
2
+ error: false;
3
+ data: T;
4
+ warningMsgs: string[] | false;
5
+ } | {
6
+ error: true;
7
+ errors: string[];
8
+ };
9
+ declare type RcInferType<T extends RcType<any>> = T extends RcType<infer U> ? U : never;
10
+ declare type RcOptional<T> = RcType<T | undefined>;
11
+ declare type ParseResultCtx = {
12
+ warnings: string[];
13
+ };
14
+ declare type InternalParseResult<T> = [success: true, data: T] | [success: false, errors: string[]];
15
+ declare type RcType<T> = {
16
+ readonly withFallback: (fallback: T) => RcType<T>;
17
+ readonly where: (predicate: (input: T) => boolean) => RcType<T>;
18
+ readonly optional: () => RcOptional<T>;
19
+ readonly orNullish: () => RcType<T | null | undefined>;
20
+ readonly withAutofix: (customAutofix?: (input: unknown) => false | {
21
+ fixed: T;
22
+ }) => RcType<T>;
23
+ };
24
+ declare const rc_undefined: RcType<undefined>;
25
+ declare const rc_null: RcType<null>;
26
+ declare const rc_any: RcType<any>;
27
+ declare const rc_boolean: RcType<boolean>;
28
+ declare const rc_string: RcType<string>;
29
+ declare const rc_number: RcType<number>;
30
+ declare const rc_date: RcType<Date>;
31
+ declare function rc_instanceof<T extends Function>(classToCheck: T): RcType<T>;
32
+ declare function rc_literals<T extends (string | number | boolean)[]>(...literals: T): RcType<T[number]>;
33
+ declare function rc_union<T extends RcType<any>[]>(...types: T): RcType<RcInferType<T[number]>>;
34
+ declare type RcObject = Record<string, RcType<any>>;
35
+ declare type TypeOfObjectType<T extends RcObject> = {
36
+ [K in keyof T]: RcInferType<T[K]>;
37
+ };
38
+ declare type RcObjType<T extends RcObject> = RcType<TypeOfObjectType<T>>;
39
+ declare function rc_object<T extends RcObject>(shape: T): RcObjType<T>;
40
+ declare function rc_extends_obj<T extends RcObject>(shape: T): RcObjType<T>;
41
+ declare function rc_strict_obj<T extends RcObject>(shape: T): RcObjType<T>;
42
+ declare function rc_obj_intersection<A extends RcObject, B extends RcObject>(a: RcObjType<A>, b: RcObjType<B>): RcObjType<A & B>;
43
+ declare function rc_array<T extends RcType<any>>(type: T): RcType<RcInferType<T>[]>;
44
+ declare type MapTupleToTypes<T extends readonly [...any[]]> = {
45
+ -readonly [K in keyof T]: RcInferType<T[K]>;
46
+ };
47
+ /**
48
+ * Check for a tuple of types
49
+ *
50
+ * TS equivalent example: [string, number, boolean]
51
+ */
52
+ declare function rc_tuple<T extends readonly RcType<any>[]>(types: T): RcType<MapTupleToTypes<T>>;
53
+ /**
54
+ * Parse a runcheck type. If valid return the valid input, with warning for autofix
55
+ * and fallback, or the errors if invalid
56
+ */
57
+ declare function rc_parse<S>(input: any, type: RcType<S>): RcParseResult<S>;
58
+ declare function rc_parser<S>(type: RcType<S>): (input: any) => RcParseResult<S>;
59
+ declare function rc_is_valid<S>(input: any, type: RcType<S>): input is S;
60
+ declare function rc_validator<S>(type: RcType<S>): (input: any) => input is S;
61
+
62
+ export { RcInferType, RcParseResult, RcType, 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 };
package/dist/runcheck.js CHANGED
@@ -1,7 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.main = void 0;
4
- function main() {
5
- console.log('Work in progress...');
6
- }
7
- exports.main = main;
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};
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "runcheck",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "license": "MIT",
5
5
  "scripts": {
6
- "test": "vitest run",
7
- "test:ui": "vitest --ui",
8
- "build": "vitest run && tsc -p tsconfig.prod.json",
9
- "npm-publish": "pnpm run build && npm publish"
6
+ "test": "vitest --ui",
7
+ "test:run": "vitest run",
8
+ "build": "pnpm test:run && pnpm build:no-test",
9
+ "build:no-test": "tsup src/runcheck.ts --minify --dts && tsm scripts/removeInternalDeclarations.ts",
10
+ "npm-publish": "./scripts/check-if-is-sync.sh && pnpm build && npm publish"
10
11
  },
11
12
  "files": [
12
13
  "dist"
@@ -14,16 +15,32 @@
14
15
  "packageManager": "^pnpm@7.6.0",
15
16
  "repository": "github:lucasols/runcheck",
16
17
  "author": "Lucas Santos",
17
- "main": "./dist/runcheck.js",
18
+ "type": "module",
19
+ "main": "./dist/runcheck.cjs",
20
+ "module": "./dist/runcheck.js",
18
21
  "types": "./dist/runcheck.d.ts",
22
+ "exports": {
23
+ ".": {
24
+ "import": "./dist/runcheck.js",
25
+ "require": "./dist/runcheck.cjs"
26
+ }
27
+ },
28
+ "sideEffects": false,
19
29
  "engines": {
20
30
  "node": ">=14.6.0"
21
31
  },
22
32
  "devDependencies": {
23
33
  "@types/node": "^18.7.2",
34
+ "@typescript-eslint/eslint-plugin": "^5.33.0",
35
+ "@typescript-eslint/parser": "^5.33.0",
24
36
  "@vitest/ui": "^0.21.1",
25
- "typescript": "^4.7.4",
37
+ "eslint": "^8.22.0",
38
+ "typescript": "^4.8.1-rc",
26
39
  "vite": "^3.0.7",
27
- "vitest": "^0.21.1"
40
+ "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"
28
45
  }
29
46
  }