jasone 0.0.3 → 0.0.4
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/index.cjs +1 -1
- package/index.d.cts +3 -3
- package/index.d.ts +3 -3
- package/index.js +1 -1
- package/package.json +1 -1
package/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var c=Object.defineProperty;var g=Object.getOwnPropertyDescriptor;var I=Object.getOwnPropertyNames;var b=Object.prototype.hasOwnProperty;var w=(t,e)=>{for(var r in e)c(t,r,{get:e[r],enumerable:!0})},J=(t,e,r,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of I(e))!b.call(t,o)&&o!==r&&c(t,o,{get:()=>e[o],enumerable:!(n=g(e,o))||n.enumerable});return t};var x=t=>J(c({},"__esModule",{value:!0}),t);var E={};w(E,{Jasone:()=>
|
|
1
|
+
"use strict";var c=Object.defineProperty;var g=Object.getOwnPropertyDescriptor;var I=Object.getOwnPropertyNames;var b=Object.prototype.hasOwnProperty;var w=(t,e)=>{for(var r in e)c(t,r,{get:e[r],enumerable:!0})},J=(t,e,r,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of I(e))!b.call(t,o)&&o!==r&&c(t,o,{get:()=>e[o],enumerable:!(n=g(e,o))||n.enumerable});return t};var x=t=>J(c({},"__esModule",{value:!0}),t);var E={};w(E,{Jasone:()=>d,bigIntType:()=>a,createType:()=>s,dateType:()=>y,defaultTypes:()=>l,mapType:()=>T,regExpType:()=>f,setType:()=>m,undefinedType:()=>u,urlType:()=>h});module.exports=x(E);var s=t=>t;var a=s({matches:t=>typeof t=="bigint",typeId:2,encode:t=>({bigint:t.toString()}),decode:({bigint:t})=>BigInt(t)});var y=s({target:Date,typeId:1,encode:t=>({timestamp:t.getTime()}),decode:({timestamp:t})=>new Date(t)});var T=s({target:Map,typeId:5,encode:(t,e)=>({map:Array.from(t.entries().map(([r,n])=>[e(r),e(n)]))}),decode:({map:t},e)=>new Map(t.map(([r,n])=>{if(r===void 0||n===void 0)throw new Error("Illegal value received.",{cause:t});return[e(r),e(n)]}))});var f=s({target:RegExp,typeId:3,encode:t=>({source:t.source,flags:t.flags}),decode:({source:t,flags:e})=>new RegExp(t,e)});var m=s({target:Set,typeId:4,encode:(t,e)=>({set:Array.from(t.values().map(r=>e(r)))}),decode:({set:t},e)=>new Set(t.map(r=>e(r)))});var u=s({matches:t=>t===void 0,typeId:0,encode:()=>({}),decode:()=>{}});var h=s({target:URL,typeId:6,encode:t=>({url:t.toString()}),decode:({url:t})=>new URL(t)});var l=[u,y,a,f,m,T,h];var d=class t{#e;#r=new Map;#n=new Map;#o=[];constructor(e={}){this.#e=e.typeIdentifier??"$";for(let r of e.types??[])this.register(r)}register(e){this.#r.set(e.typeId,e.decode),"matches"in e?this.#o.push({matches:e.matches,typeId:e.typeId,encode:e.encode}):this.#n.set(e.target,{typeId:e.typeId,encode:e.encode})}encode(e){switch(typeof e){case"string":case"number":case"boolean":return e;case"object":if(e===null)return null;if(Array.isArray(e))return e.map(o=>this.encode(o));if(Object.getPrototypeOf(e)===Object.prototype){let o=Object.fromEntries(Object.entries(e).map(([i,p])=>[i,this.encode(p)]));return this.#e in o&&(o[this.#e]=[this.encode(o[this.#e])]),o}}let r;if(typeof e=="object"&&e!==null&&(r=this.#n.get(e.constructor)),r||(r=this.#o.find(({matches:o})=>o(e))),!r)throw new Error("No encoder found.",{cause:e});let n=r.encode(e,this.encode.bind(this));return{[this.#e]:r.typeId,...n}}decode(e){return this.#t(e)}#t(e,r=!1){switch(typeof e){case"string":case"number":case"boolean":return e;case"object":if(e===null)return null;if(Array.isArray(e))return e.map(n=>this.#t(n));if(this.#e in e&&!Array.isArray(e)&&!r){let n=e[this.#e];if(Array.isArray(n)){if(n.length!==1)throw new Error("Illegal value received. Escaped type identifiers must have exactly one element.",{cause:e});let[i]=n,p={...e};return p[this.#e]=i,this.#t(p,!0)}let o=this.#r.get(n);if(!o)throw new Error(`No decoder found for type id: ${n}`,{cause:e});return o(e,this.decode.bind(this))}if(Object.getPrototypeOf(e)===Object.prototype)return Object.fromEntries(Object.entries(e).map(([n,o])=>[n,this.#t(o)]));break}throw new Error("Illegal value received.",{cause:e})}serialize(e){return this.encode(e)}deserialize(e){return this.decode(e)}stringify(e){return JSON.stringify(this.encode(e))}parse(e){return this.decode(JSON.parse(e))}static default=new t({types:l});static register=t.default.register.bind(t.default);static encode=t.default.encode.bind(t.default);static serialize=t.default.serialize.bind(t.default);static decode=t.default.decode.bind(t.default);static deserialize=t.default.deserialize.bind(t.default);static stringify=t.default.stringify.bind(t.default);static parse=t.default.parse.bind(t.default)};0&&(module.exports={Jasone,bigIntType,createType,dateType,defaultTypes,mapType,regExpType,setType,undefinedType,urlType});
|
package/index.d.cts
CHANGED
|
@@ -29,7 +29,7 @@ type TypeEncoder<TType = unknown, TJson extends Record<string, JsonValue> = Reco
|
|
|
29
29
|
*/
|
|
30
30
|
type TypeDecoder<TType = unknown, TJson extends Record<string, JsonValue> = Record<string, JsonValue>> = (value: TJson, decode: <T = unknown>(value: JsonValue) => T) => TType;
|
|
31
31
|
type MatchesFn<TType = unknown> = (value: unknown) => value is TType;
|
|
32
|
-
type ClassLike<TInstance> = new (...args:
|
|
32
|
+
type ClassLike<TInstance> = new <T>(...args: any[]) => TInstance;
|
|
33
33
|
declare const typeOf: "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function";
|
|
34
34
|
type TypeOf = typeof typeOf;
|
|
35
35
|
|
|
@@ -88,7 +88,7 @@ declare const dateType: TypeTransformer<Date, {
|
|
|
88
88
|
timestamp: number;
|
|
89
89
|
}>;
|
|
90
90
|
|
|
91
|
-
declare const mapType: TypeTransformer<Map<
|
|
91
|
+
declare const mapType: TypeTransformer<Map<unknown, unknown>, {
|
|
92
92
|
map: JsonValue[][];
|
|
93
93
|
}>;
|
|
94
94
|
|
|
@@ -97,7 +97,7 @@ declare const regExpType: TypeTransformer<RegExp, {
|
|
|
97
97
|
flags: string;
|
|
98
98
|
}>;
|
|
99
99
|
|
|
100
|
-
declare const setType: TypeTransformer<Set<
|
|
100
|
+
declare const setType: TypeTransformer<Set<unknown>, {
|
|
101
101
|
set: JsonValue[];
|
|
102
102
|
}>;
|
|
103
103
|
|
package/index.d.ts
CHANGED
|
@@ -29,7 +29,7 @@ type TypeEncoder<TType = unknown, TJson extends Record<string, JsonValue> = Reco
|
|
|
29
29
|
*/
|
|
30
30
|
type TypeDecoder<TType = unknown, TJson extends Record<string, JsonValue> = Record<string, JsonValue>> = (value: TJson, decode: <T = unknown>(value: JsonValue) => T) => TType;
|
|
31
31
|
type MatchesFn<TType = unknown> = (value: unknown) => value is TType;
|
|
32
|
-
type ClassLike<TInstance> = new (...args:
|
|
32
|
+
type ClassLike<TInstance> = new <T>(...args: any[]) => TInstance;
|
|
33
33
|
declare const typeOf: "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function";
|
|
34
34
|
type TypeOf = typeof typeOf;
|
|
35
35
|
|
|
@@ -88,7 +88,7 @@ declare const dateType: TypeTransformer<Date, {
|
|
|
88
88
|
timestamp: number;
|
|
89
89
|
}>;
|
|
90
90
|
|
|
91
|
-
declare const mapType: TypeTransformer<Map<
|
|
91
|
+
declare const mapType: TypeTransformer<Map<unknown, unknown>, {
|
|
92
92
|
map: JsonValue[][];
|
|
93
93
|
}>;
|
|
94
94
|
|
|
@@ -97,7 +97,7 @@ declare const regExpType: TypeTransformer<RegExp, {
|
|
|
97
97
|
flags: string;
|
|
98
98
|
}>;
|
|
99
99
|
|
|
100
|
-
declare const setType: TypeTransformer<Set<
|
|
100
|
+
declare const setType: TypeTransformer<Set<unknown>, {
|
|
101
101
|
set: JsonValue[];
|
|
102
102
|
}>;
|
|
103
103
|
|
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var o=t=>t;var c=o({matches:t=>typeof t=="bigint",typeId:2,encode:t=>({bigint:t.toString()}),decode:({bigint:t})=>BigInt(t)});var a=o({target:Date,typeId:1,encode:t=>({timestamp:t.getTime()}),decode:({timestamp:t})=>new Date(t)});var y=o({
|
|
1
|
+
var o=t=>t;var c=o({matches:t=>typeof t=="bigint",typeId:2,encode:t=>({bigint:t.toString()}),decode:({bigint:t})=>BigInt(t)});var a=o({target:Date,typeId:1,encode:t=>({timestamp:t.getTime()}),decode:({timestamp:t})=>new Date(t)});var y=o({target:Map,typeId:5,encode:(t,e)=>({map:Array.from(t.entries().map(([r,n])=>[e(r),e(n)]))}),decode:({map:t},e)=>new Map(t.map(([r,n])=>{if(r===void 0||n===void 0)throw new Error("Illegal value received.",{cause:t});return[e(r),e(n)]}))});var T=o({target:RegExp,typeId:3,encode:t=>({source:t.source,flags:t.flags}),decode:({source:t,flags:e})=>new RegExp(t,e)});var f=o({target:Set,typeId:4,encode:(t,e)=>({set:Array.from(t.values().map(r=>e(r)))}),decode:({set:t},e)=>new Set(t.map(r=>e(r)))});var m=o({matches:t=>t===void 0,typeId:0,encode:()=>({}),decode:()=>{}});var u=o({target:URL,typeId:6,encode:t=>({url:t.toString()}),decode:({url:t})=>new URL(t)});var h=[m,a,c,T,f,y,u];var i=class t{#e;#r=new Map;#n=new Map;#o=[];constructor(e={}){this.#e=e.typeIdentifier??"$";for(let r of e.types??[])this.register(r)}register(e){this.#r.set(e.typeId,e.decode),"matches"in e?this.#o.push({matches:e.matches,typeId:e.typeId,encode:e.encode}):this.#n.set(e.target,{typeId:e.typeId,encode:e.encode})}encode(e){switch(typeof e){case"string":case"number":case"boolean":return e;case"object":if(e===null)return null;if(Array.isArray(e))return e.map(s=>this.encode(s));if(Object.getPrototypeOf(e)===Object.prototype){let s=Object.fromEntries(Object.entries(e).map(([d,p])=>[d,this.encode(p)]));return this.#e in s&&(s[this.#e]=[this.encode(s[this.#e])]),s}}let r;if(typeof e=="object"&&e!==null&&(r=this.#n.get(e.constructor)),r||(r=this.#o.find(({matches:s})=>s(e))),!r)throw new Error("No encoder found.",{cause:e});let n=r.encode(e,this.encode.bind(this));return{[this.#e]:r.typeId,...n}}decode(e){return this.#t(e)}#t(e,r=!1){switch(typeof e){case"string":case"number":case"boolean":return e;case"object":if(e===null)return null;if(Array.isArray(e))return e.map(n=>this.#t(n));if(this.#e in e&&!Array.isArray(e)&&!r){let n=e[this.#e];if(Array.isArray(n)){if(n.length!==1)throw new Error("Illegal value received. Escaped type identifiers must have exactly one element.",{cause:e});let[d]=n,p={...e};return p[this.#e]=d,this.#t(p,!0)}let s=this.#r.get(n);if(!s)throw new Error(`No decoder found for type id: ${n}`,{cause:e});return s(e,this.decode.bind(this))}if(Object.getPrototypeOf(e)===Object.prototype)return Object.fromEntries(Object.entries(e).map(([n,s])=>[n,this.#t(s)]));break}throw new Error("Illegal value received.",{cause:e})}serialize(e){return this.encode(e)}deserialize(e){return this.decode(e)}stringify(e){return JSON.stringify(this.encode(e))}parse(e){return this.decode(JSON.parse(e))}static default=new t({types:h});static register=t.default.register.bind(t.default);static encode=t.default.encode.bind(t.default);static serialize=t.default.serialize.bind(t.default);static decode=t.default.decode.bind(t.default);static deserialize=t.default.deserialize.bind(t.default);static stringify=t.default.stringify.bind(t.default);static parse=t.default.parse.bind(t.default)};export{i as Jasone,c as bigIntType,o as createType,a as dateType,h as defaultTypes,y as mapType,T as regExpType,f as setType,m as undefinedType,u as urlType};
|