Comparing version 0.2.0-alpha.4 to 0.2.0
@@ -17,8 +17,6 @@ export { bool } from "./bool"; | ||
export { Enum } from "./Enum"; | ||
export { MapCodec } from "./Map"; | ||
export { Option } from "./Option"; | ||
export { Result } from "./Result"; | ||
export { SetCodec } from "./Set"; | ||
export { Struct } from "./Struct"; | ||
export { Tuple } from "./Tuple"; | ||
export { Vector } from "./Vector"; |
import { Decoder, Encoder, Codec } from "../types"; | ||
export declare const Option: { | ||
<T>(inner: Codec<T>): Codec<T | undefined>; | ||
enc: <T_1>(inner: Encoder<T_1>) => Encoder<T_1 | undefined>; | ||
dec: <T_2>(inner: Decoder<T_2>) => Decoder<T_2 | undefined>; | ||
<T>(inner: Codec<T>): Codec<void | T | undefined>; | ||
enc: <T_1>(inner: Encoder<T_1>) => Encoder<void | T_1 | undefined>; | ||
dec: <T_2>(inner: Decoder<T_2>) => Decoder<void | T_2 | undefined>; | ||
}; |
@@ -42,6 +42,4 @@ var __defProp = Object.defineProperty; | ||
Enum: () => Enum, | ||
MapCodec: () => MapCodec, | ||
Option: () => Option, | ||
Result: () => Result, | ||
SetCodec: () => SetCodec, | ||
Struct: () => Struct, | ||
@@ -378,35 +376,2 @@ Tuple: () => Tuple, | ||
// src/codecs/Tuple.ts | ||
var TupleDec = (...decoders) => toInternalBytes((bytes) => decoders.map((decoder) => decoder(bytes))); | ||
var TupleEnc = (...encoders) => (values) => mergeUint8(...values.map((value, idx) => encoders[idx](value))); | ||
var Tuple = (...codecs) => createCodec(TupleEnc(...codecs.map(([encoder]) => encoder)), TupleDec(...codecs.map(([, decoder]) => decoder))); | ||
Tuple.enc = TupleEnc; | ||
Tuple.dec = TupleDec; | ||
// src/codecs/Vector.ts | ||
var VectorEnc = (inner, size) => size > 0 ? (value) => mergeUint8(...value.map(inner)) : (value) => mergeUint8(compact.enc(value.length), ...value.map(inner)); | ||
function VectorDec(getter, size) { | ||
return toInternalBytes((bytes) => { | ||
const nElements = size > 0 ? size : compact.dec(bytes); | ||
const result = new Array(nElements); | ||
for (let i = 0; i < nElements; i++) { | ||
const current = getter(bytes); | ||
result[i] = current; | ||
} | ||
return result; | ||
}); | ||
} | ||
function Vector(inner, size) { | ||
return createCodec(VectorEnc(inner[0], size), VectorDec(inner[1], size)); | ||
} | ||
Vector.enc = VectorEnc; | ||
Vector.dec = VectorDec; | ||
// src/codecs/Map.ts | ||
var MapCodecEnc = (key, value) => enhanceEncoder(Vector.enc(Tuple.enc(key, value)), (input) => Array.from(input.entries())); | ||
var MapCodecDec = (key, value) => enhanceDecoder(Vector.dec(Tuple.dec(key, value)), (entries) => new Map(entries)); | ||
var MapCodec = (key, value) => createCodec(MapCodecEnc(key[0], value[0]), MapCodecDec(key[1], value[1])); | ||
MapCodec.enc = MapCodecEnc; | ||
MapCodec.dec = MapCodecDec; | ||
// src/codecs/Option.ts | ||
@@ -449,8 +414,8 @@ var OptionDec = (inner) => toInternalBytes((bytes) => { | ||
// src/codecs/Set.ts | ||
var SetEnc = (value) => enhanceEncoder(Vector.enc(value), (input) => Array.from(input)); | ||
var SetDec = (value) => enhanceDecoder(Vector.dec(value), (input) => new Set(input)); | ||
var SetCodec = (value) => createCodec(SetEnc(value[0]), SetDec(value[1])); | ||
SetCodec.enc = SetEnc; | ||
SetCodec.dec = SetDec; | ||
// src/codecs/Tuple.ts | ||
var TupleDec = (...decoders) => toInternalBytes((bytes) => decoders.map((decoder) => decoder(bytes))); | ||
var TupleEnc = (...encoders) => (values) => mergeUint8(...values.map((value, idx) => encoders[idx](value))); | ||
var Tuple = (...codecs) => createCodec(TupleEnc(...codecs.map(([encoder]) => encoder)), TupleDec(...codecs.map(([, decoder]) => decoder))); | ||
Tuple.enc = TupleEnc; | ||
Tuple.dec = TupleDec; | ||
@@ -469,2 +434,21 @@ // src/codecs/Struct.ts | ||
Struct.dec = StructDec; | ||
// src/codecs/Vector.ts | ||
var VectorEnc = (inner, size) => size > 0 ? (value) => mergeUint8(...value.map(inner)) : (value) => mergeUint8(compact.enc(value.length), ...value.map(inner)); | ||
function VectorDec(getter, size) { | ||
return toInternalBytes((bytes) => { | ||
const nElements = size > 0 ? size : compact.dec(bytes); | ||
const result = new Array(nElements); | ||
for (let i = 0; i < nElements; i++) { | ||
const current = getter(bytes); | ||
result[i] = current; | ||
} | ||
return result; | ||
}); | ||
} | ||
function Vector(inner, size) { | ||
return createCodec(VectorEnc(inner[0], size), VectorDec(inner[1], size)); | ||
} | ||
Vector.enc = VectorEnc; | ||
Vector.dec = VectorDec; | ||
//# sourceMappingURL=scale-ts.cjs.development.js.map |
@@ -1,2 +0,2 @@ | ||
var k=Object.defineProperty;var xe=Object.getOwnPropertyDescriptor;var De=Object.getOwnPropertyNames;var Oe=Object.prototype.hasOwnProperty;var le=(e,t)=>{for(var o in t)k(e,o,{get:t[o],enumerable:!0})},Ce=(e,t,o,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let c of De(t))!Oe.call(e,c)&&c!==o&&k(e,c,{get:()=>t[c],enumerable:!(n=xe(t,c))||n.enumerable});return e};var Ie=e=>Ce(k({},"__esModule",{value:!0}),e);var S=(e,t,o)=>{if(!t.has(e))throw TypeError("Cannot "+o)};var v=(e,t,o)=>(S(e,t,"read from private field"),o?o.call(e):t.get(e)),j=(e,t,o)=>{if(t.has(e))throw TypeError("Cannot add the same private member more than once");t instanceof WeakSet?t.add(e):t.set(e,o)},M=(e,t,o,n)=>(S(e,t,"write to private field"),n?n.call(e,o):t.set(e,o),o);var Xe={};le(Xe,{Bytes:()=>b,Enum:()=>K,MapCodec:()=>A,Option:()=>B,Result:()=>w,SetCodec:()=>U,Struct:()=>V,Tuple:()=>y,Vector:()=>x,_void:()=>J,bool:()=>T,compact:()=>l,createCodec:()=>r,enhanceCodec:()=>h,enhanceDecoder:()=>O,enhanceEncoder:()=>D,i128:()=>G,i16:()=>X,i32:()=>$,i64:()=>q,i8:()=>W,str:()=>N,u128:()=>H,u16:()=>L,u32:()=>_,u64:()=>F,u8:()=>f});module.exports=Ie(Xe);var r=(e,t)=>{let o=[e,t];return o.enc=e,o.dec=t,o},D=(e,t)=>o=>e(t(o)),O=(e,t)=>o=>t(e(o)),h=([e,t],o,n)=>r(D(e,o),O(t,n));var R={0:0,1:1,2:2,3:3,4:4,5:5,6:6,7:7,8:8,9:9,a:10,b:11,c:12,d:13,e:14,f:15,A:10,B:11,C:12,D:13,E:14,F:15};function ge(e){let t=e.length%2,o=(e[1]==="x"?2:0)+t,n=(e.length-o)/2+t,c=new Uint8Array(n);t&&(c[0]=0|R[e[2]]);for(let i=0;i<n;){let p=o+i*2,C=R[e[p]],ye=R[e[p+1]];c[t+i++]=C<<4|ye}return c}var g,E=class extends Uint8Array{constructor(){super(...arguments);j(this,g,0)}useBytes(o){M(this,g,v(this,g)+o)}get usedBytes(){return v(this,g)}};g=new WeakMap;var s=e=>t=>e(t instanceof E?t:new E(typeof t=="string"?ge(t).buffer:t instanceof Uint8Array?t.buffer:t));var d={u8:"u8",u16:"u16",u32:"u32",u64:"u64",i8:"i8",i16:"i16",i32:"i32",i64:"i64"},P={[d.u8]:[1,"getUint8","setUint8"],[d.u16]:[2,"getUint16","setUint16"],[d.u32]:[4,"getUint32","setUint32"],[d.u64]:[8,"getBigUint64","setBigUint64"],[d.i8]:[1,"getInt8","setInt8"],[d.i16]:[2,"getInt16","setInt16"],[d.i32]:[4,"getInt32","setInt32"],[d.i64]:[8,"getBigInt64","setBigInt64"]};function m(e){let[t,o]=P[e];return s(n=>{let c=new DataView(n.buffer)[o](n.usedBytes,!0);return n.useBytes(t),c})}function u(e){let[t,,o]=P[e];return n=>{let c=new Uint8Array(t);return new DataView(c.buffer)[o](0,n,!0),c}}var a=(...e)=>{let t=e.reduce((n,c)=>n+c.byteLength,0),o=new Uint8Array(t);for(let n=0,c=0;n<e.length;n++){let i=e[n];o.set(i,c),c+=i.byteLength}return o};function I(e,t){return Object.fromEntries(Object.entries(e).map(([o,n])=>[o,t(n,o)]))}var Te=u(d.u8),Ee=m(d.u8),f=r(Te,Ee);var be=e=>e?1:0,T=h(f,be,Boolean);var Ke={[1]:d.u8,[2]:d.u16,[4]:d.u32},Ae=s(e=>{let t=e.usedBytes,o=e[t],n=o&3;if(n!==3){let C=n===2?4:n+1;return m(Ke[C])(e)>>>2}let c=(o>>>2)+4,i=t+1;e.useBytes(c+1);let p=0n;for(let C=c-1;C>=0;C--)p=p<<8n|BigInt(e[i+C]);return p}),Be=e=>{if(e<0)throw new Error(`Wrong Compat input (${e})`);if(e<1<<6){let n=new Uint8Array(1);return n[0]=Number(e)<<2,n}if(e<1<<14){let n=new Uint8Array(2),c=(Number(e)<<2)+1;return new DataView(n.buffer).setUint16(0,c,!0),n}if(e<1<<30){let n=new Uint8Array(4),c=(Number(e)<<2)+2;return new DataView(n.buffer).setUint32(0,c,!0),n}let t=[],o=BigInt(e);for(;o>0;)t.push(Number(o)),o>>=8n;return t.unshift((t.length-4<<2)+3),new Uint8Array(t)},l=r(Be,Ae);var we=new TextEncoder,Ue=e=>{let t=we.encode(e);return a(l.enc(t.length),t)},Ve=new TextDecoder,ke=s(e=>{let t=l.dec(e),o=new Uint8Array(e.buffer,e.usedBytes,t);return e.useBytes(t),Ve.decode(o)}),N=r(Ue,ke);var ve=u(d.u16),he=m(d.u16),L=r(ve,he);var Re=u(d.u32),Se=m(d.u32),_=r(Re,Se);var je=u(d.u64),Me=m(d.u64),F=r(je,Me);var Pe=e=>{let t=new Uint8Array(16),o=new DataView(t.buffer);return o.setBigUint64(0,e,!0),o.setBigUint64(8,e>>64n,!0),t},Ne=s(e=>{let t=new DataView(e.buffer),o=t.getBigUint64(e.usedBytes,!0),n=t.getBigUint64(e.usedBytes+8,!0);return e.useBytes(16),(n<<64n)+o}),H=r(Pe,Ne);var Le=u(d.i8),_e=m(d.i8),W=r(Le,_e);var X=r(u(d.i16),m(d.i16));var $=r(u(d.i32),m(d.i32));var q=r(u(d.i64),m(d.i64));var Fe=e=>{let t=new Uint8Array(16),o=new DataView(t.buffer);return o.setBigUint64(0,e,!0),o.setBigInt64(8,e>>64n,!0),t},He=s(e=>{let t=new DataView(e.buffer),o=t.getBigUint64(e.usedBytes,!0),n=t.getBigInt64(e.usedBytes+8,!0);return e.useBytes(16),(n<<64n)+o}),G=r(Fe,He);var We=new Uint8Array(0),J=r(()=>We,Function.prototype);var Q=e=>t=>t.length===e?t:t.slice(0,e),Y=e=>s(t=>{let o=t.usedBytes,n=e!==1/0?e:t.byteLength-o,c=new Uint8Array(t.buffer,o,n);return t.useBytes(n),c}),b=e=>r(Q(e),Y(e));b.enc=Q;b.dec=Y;var Z=e=>{let t=Object.keys(e);return({tag:o,value:n})=>{let c=t.indexOf(o);return a(f.enc(c),e[o](n))}},z=e=>{let t=Object.entries(e);return s(o=>{let n=f.dec(o),[c,i]=t[n],p=i(o);return{tag:c,value:p}})},K=e=>r(Z(I(e,([t])=>t)),z(I(e,([,t])=>t)));K.enc=Z;K.dec=z;var ee=(...e)=>s(t=>e.map(o=>o(t))),te=(...e)=>t=>a(...t.map((o,n)=>e[n](o))),y=(...e)=>r(te(...e.map(([t])=>t)),ee(...e.map(([,t])=>t)));y.enc=te;y.dec=ee;var oe=(e,t)=>t>0?o=>a(...o.map(e)):o=>a(l.enc(o.length),...o.map(e));function ne(e,t){return s(o=>{let n=t>0?t:l.dec(o),c=new Array(n);for(let i=0;i<n;i++){let p=e(o);c[i]=p}return c})}function x(e,t){return r(oe(e[0],t),ne(e[1],t))}x.enc=oe;x.dec=ne;var re=(e,t)=>D(x.enc(y.enc(e,t)),o=>Array.from(o.entries())),ce=(e,t)=>O(x.dec(y.dec(e,t)),o=>new Map(o)),A=(e,t)=>r(re(e[0],t[0]),ce(e[1],t[1]));A.enc=re;A.dec=ce;var de=e=>s(t=>{let o=f.dec(t);if(o!==0)return e===T[1]?o===1:e(t)}),se=e=>t=>{let o=new Uint8Array(1);return t===void 0?(o[0]=0,o):(o[0]=1,e===T[0]?(o[0]=t?1:2,o):a(o,e(t)))},B=e=>r(se(e[0]),de(e[1]));B.enc=se;B.dec=de;var ie=(e,t)=>s(o=>{let c=f[1](o)===0,p=(c?e:t)(o);return{success:c,value:p}}),me=(e,t)=>({success:o,value:n})=>a(f[0](o?0:1),(o?e:t)(n)),w=(e,t)=>r(me(e[0],t[0]),ie(e[1],t[1]));w.dec=ie;w.enc=me;var ue=e=>D(x.enc(e),t=>Array.from(t)),ae=e=>O(x.dec(e),t=>new Set(t)),U=e=>r(ue(e[0]),ae(e[1]));U.enc=ue;U.dec=ae;var pe=e=>{let t=Object.keys(e);return D(y.enc(...Object.values(e)),o=>t.map(n=>o[n]))},fe=e=>{let t=Object.keys(e);return O(y.dec(...Object.values(e)),o=>Object.fromEntries(o.map((n,c)=>[t[c],n])))},V=e=>r(pe(I(e,t=>t[0])),fe(I(e,t=>t[1])));V.enc=pe;V.dec=fe; | ||
var w=Object.defineProperty;var me=Object.getOwnPropertyDescriptor;var ue=Object.getOwnPropertyNames;var ae=Object.prototype.hasOwnProperty;var pe=(e,t)=>{for(var n in t)w(e,n,{get:t[n],enumerable:!0})},fe=(e,t,n,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let c of ue(t))!ae.call(e,c)&&c!==n&&w(e,c,{get:()=>t[c],enumerable:!(o=me(t,c))||o.enumerable});return e};var ye=e=>fe(w({},"__esModule",{value:!0}),e);var h=(e,t,n)=>{if(!t.has(e))throw TypeError("Cannot "+n)};var k=(e,t,n)=>(h(e,t,"read from private field"),n?n.call(e):t.get(e)),j=(e,t,n)=>{if(t.has(e))throw TypeError("Cannot add the same private member more than once");t instanceof WeakSet?t.add(e):t.set(e,n)},V=(e,t,n,o)=>(h(e,t,"write to private field"),o?o.call(e,n):t.set(e,n),n);var Le={};pe(Le,{Bytes:()=>E,Enum:()=>b,Option:()=>A,Result:()=>K,Struct:()=>B,Tuple:()=>O,Vector:()=>U,_void:()=>q,bool:()=>I,compact:()=>y,createCodec:()=>r,enhanceCodec:()=>v,enhanceDecoder:()=>T,enhanceEncoder:()=>g,i128:()=>$,i16:()=>M,i32:()=>W,i64:()=>X,i8:()=>H,str:()=>P,u128:()=>F,u16:()=>N,u32:()=>L,u64:()=>_,u8:()=>f});module.exports=ye(Le);var r=(e,t)=>{let n=[e,t];return n.enc=e,n.dec=t,n},g=(e,t)=>n=>e(t(n)),T=(e,t)=>n=>t(e(n)),v=([e,t],n,o)=>r(g(e,n),T(t,o));var R={0:0,1:1,2:2,3:3,4:4,5:5,6:6,7:7,8:8,9:9,a:10,b:11,c:12,d:13,e:14,f:15,A:10,B:11,C:12,D:13,E:14,F:15};function xe(e){let t=e.length%2,n=(e[1]==="x"?2:0)+t,o=(e.length-n)/2+t,c=new Uint8Array(o);t&&(c[0]=0|R[e[2]]);for(let i=0;i<o;){let p=n+i*2,x=R[e[p]],ie=R[e[p+1]];c[t+i++]=x<<4|ie}return c}var D,C=class extends Uint8Array{constructor(){super(...arguments);j(this,D,0)}useBytes(n){V(this,D,k(this,D)+n)}get usedBytes(){return k(this,D)}};D=new WeakMap;var s=e=>t=>e(t instanceof C?t:new C(typeof t=="string"?xe(t).buffer:t instanceof Uint8Array?t.buffer:t));var d={u8:"u8",u16:"u16",u32:"u32",u64:"u64",i8:"i8",i16:"i16",i32:"i32",i64:"i64"},S={[d.u8]:[1,"getUint8","setUint8"],[d.u16]:[2,"getUint16","setUint16"],[d.u32]:[4,"getUint32","setUint32"],[d.u64]:[8,"getBigUint64","setBigUint64"],[d.i8]:[1,"getInt8","setInt8"],[d.i16]:[2,"getInt16","setInt16"],[d.i32]:[4,"getInt32","setInt32"],[d.i64]:[8,"getBigInt64","setBigInt64"]};function m(e){let[t,n]=S[e];return s(o=>{let c=new DataView(o.buffer)[n](o.usedBytes,!0);return o.useBytes(t),c})}function u(e){let[t,,n]=S[e];return o=>{let c=new Uint8Array(t);return new DataView(c.buffer)[n](0,o,!0),c}}var a=(...e)=>{let t=e.reduce((o,c)=>o+c.byteLength,0),n=new Uint8Array(t);for(let o=0,c=0;o<e.length;o++){let i=e[o];n.set(i,c),c+=i.byteLength}return n};function l(e,t){return Object.fromEntries(Object.entries(e).map(([n,o])=>[n,t(o,n)]))}var Oe=u(d.u8),le=m(d.u8),f=r(Oe,le);var De=e=>e?1:0,I=v(f,De,Boolean);var Ie={[1]:d.u8,[2]:d.u16,[4]:d.u32},ge=s(e=>{let t=e.usedBytes,n=e[t],o=n&3;if(o!==3){let x=o===2?4:o+1;return m(Ie[x])(e)>>>2}let c=(n>>>2)+4,i=t+1;e.useBytes(c+1);let p=0n;for(let x=c-1;x>=0;x--)p=p<<8n|BigInt(e[i+x]);return p}),Te=e=>{if(e<0)throw new Error(`Wrong Compat input (${e})`);if(e<1<<6){let o=new Uint8Array(1);return o[0]=Number(e)<<2,o}if(e<1<<14){let o=new Uint8Array(2),c=(Number(e)<<2)+1;return new DataView(o.buffer).setUint16(0,c,!0),o}if(e<1<<30){let o=new Uint8Array(4),c=(Number(e)<<2)+2;return new DataView(o.buffer).setUint32(0,c,!0),o}let t=[],n=BigInt(e);for(;n>0;)t.push(Number(n)),n>>=8n;return t.unshift((t.length-4<<2)+3),new Uint8Array(t)},y=r(Te,ge);var Ce=new TextEncoder,Ee=e=>{let t=Ce.encode(e);return a(y.enc(t.length),t)},be=new TextDecoder,Ae=s(e=>{let t=y.dec(e),n=new Uint8Array(e.buffer,e.usedBytes,t);return e.useBytes(t),be.decode(n)}),P=r(Ee,Ae);var Ke=u(d.u16),Be=m(d.u16),N=r(Ke,Be);var Ue=u(d.u32),we=m(d.u32),L=r(Ue,we);var ke=u(d.u64),ve=m(d.u64),_=r(ke,ve);var Re=e=>{let t=new Uint8Array(16),n=new DataView(t.buffer);return n.setBigUint64(0,e,!0),n.setBigUint64(8,e>>64n,!0),t},he=s(e=>{let t=new DataView(e.buffer),n=t.getBigUint64(e.usedBytes,!0),o=t.getBigUint64(e.usedBytes+8,!0);return e.useBytes(16),(o<<64n)+n}),F=r(Re,he);var je=u(d.i8),Ve=m(d.i8),H=r(je,Ve);var M=r(u(d.i16),m(d.i16));var W=r(u(d.i32),m(d.i32));var X=r(u(d.i64),m(d.i64));var Se=e=>{let t=new Uint8Array(16),n=new DataView(t.buffer);return n.setBigUint64(0,e,!0),n.setBigInt64(8,e>>64n,!0),t},Pe=s(e=>{let t=new DataView(e.buffer),n=t.getBigUint64(e.usedBytes,!0),o=t.getBigInt64(e.usedBytes+8,!0);return e.useBytes(16),(o<<64n)+n}),$=r(Se,Pe);var Ne=new Uint8Array(0),q=r(()=>Ne,Function.prototype);var G=e=>t=>t.length===e?t:t.slice(0,e),J=e=>s(t=>{let n=t.usedBytes,o=e!==1/0?e:t.byteLength-n,c=new Uint8Array(t.buffer,n,o);return t.useBytes(o),c}),E=e=>r(G(e),J(e));E.enc=G;E.dec=J;var Q=e=>{let t=Object.keys(e);return({tag:n,value:o})=>{let c=t.indexOf(n);return a(f.enc(c),e[n](o))}},Y=e=>{let t=Object.entries(e);return s(n=>{let o=f.dec(n),[c,i]=t[o],p=i(n);return{tag:c,value:p}})},b=e=>r(Q(l(e,([t])=>t)),Y(l(e,([,t])=>t)));b.enc=Q;b.dec=Y;var Z=e=>s(t=>{let n=f.dec(t);if(n!==0)return e===I[1]?n===1:e(t)}),z=e=>t=>{let n=new Uint8Array(1);return t===void 0?(n[0]=0,n):(n[0]=1,e===I[0]?(n[0]=t?1:2,n):a(n,e(t)))},A=e=>r(z(e[0]),Z(e[1]));A.enc=z;A.dec=Z;var ee=(e,t)=>s(n=>{let c=f[1](n)===0,p=(c?e:t)(n);return{success:c,value:p}}),te=(e,t)=>({success:n,value:o})=>a(f[0](n?0:1),(n?e:t)(o)),K=(e,t)=>r(te(e[0],t[0]),ee(e[1],t[1]));K.dec=ee;K.enc=te;var ne=(...e)=>s(t=>e.map(n=>n(t))),oe=(...e)=>t=>a(...t.map((n,o)=>e[o](n))),O=(...e)=>r(oe(...e.map(([t])=>t)),ne(...e.map(([,t])=>t)));O.enc=oe;O.dec=ne;var re=e=>{let t=Object.keys(e);return g(O.enc(...Object.values(e)),n=>t.map(o=>n[o]))},ce=e=>{let t=Object.keys(e);return T(O.dec(...Object.values(e)),n=>Object.fromEntries(n.map((o,c)=>[t[c],o])))},B=e=>r(re(l(e,t=>t[0])),ce(l(e,t=>t[1])));B.enc=re;B.dec=ce;var de=(e,t)=>t>0?n=>a(...n.map(e)):n=>a(y.enc(n.length),...n.map(e));function se(e,t){return s(n=>{let o=t>0?t:y.dec(n),c=new Array(o);for(let i=0;i<o;i++){let p=e(n);c[i]=p}return c})}function U(e,t){return r(de(e[0],t),se(e[1],t))}U.enc=de;U.dec=se; | ||
//# sourceMappingURL=scale-ts.cjs.production.min.js.map |
@@ -327,35 +327,2 @@ var __accessCheck = (obj, member, msg) => { | ||
// src/codecs/Tuple.ts | ||
var TupleDec = (...decoders) => toInternalBytes((bytes) => decoders.map((decoder) => decoder(bytes))); | ||
var TupleEnc = (...encoders) => (values) => mergeUint8(...values.map((value, idx) => encoders[idx](value))); | ||
var Tuple = (...codecs) => createCodec(TupleEnc(...codecs.map(([encoder]) => encoder)), TupleDec(...codecs.map(([, decoder]) => decoder))); | ||
Tuple.enc = TupleEnc; | ||
Tuple.dec = TupleDec; | ||
// src/codecs/Vector.ts | ||
var VectorEnc = (inner, size) => size > 0 ? (value) => mergeUint8(...value.map(inner)) : (value) => mergeUint8(compact.enc(value.length), ...value.map(inner)); | ||
function VectorDec(getter, size) { | ||
return toInternalBytes((bytes) => { | ||
const nElements = size > 0 ? size : compact.dec(bytes); | ||
const result = new Array(nElements); | ||
for (let i = 0; i < nElements; i++) { | ||
const current = getter(bytes); | ||
result[i] = current; | ||
} | ||
return result; | ||
}); | ||
} | ||
function Vector(inner, size) { | ||
return createCodec(VectorEnc(inner[0], size), VectorDec(inner[1], size)); | ||
} | ||
Vector.enc = VectorEnc; | ||
Vector.dec = VectorDec; | ||
// src/codecs/Map.ts | ||
var MapCodecEnc = (key, value) => enhanceEncoder(Vector.enc(Tuple.enc(key, value)), (input) => Array.from(input.entries())); | ||
var MapCodecDec = (key, value) => enhanceDecoder(Vector.dec(Tuple.dec(key, value)), (entries) => new Map(entries)); | ||
var MapCodec = (key, value) => createCodec(MapCodecEnc(key[0], value[0]), MapCodecDec(key[1], value[1])); | ||
MapCodec.enc = MapCodecEnc; | ||
MapCodec.dec = MapCodecDec; | ||
// src/codecs/Option.ts | ||
@@ -398,8 +365,8 @@ var OptionDec = (inner) => toInternalBytes((bytes) => { | ||
// src/codecs/Set.ts | ||
var SetEnc = (value) => enhanceEncoder(Vector.enc(value), (input) => Array.from(input)); | ||
var SetDec = (value) => enhanceDecoder(Vector.dec(value), (input) => new Set(input)); | ||
var SetCodec = (value) => createCodec(SetEnc(value[0]), SetDec(value[1])); | ||
SetCodec.enc = SetEnc; | ||
SetCodec.dec = SetDec; | ||
// src/codecs/Tuple.ts | ||
var TupleDec = (...decoders) => toInternalBytes((bytes) => decoders.map((decoder) => decoder(bytes))); | ||
var TupleEnc = (...encoders) => (values) => mergeUint8(...values.map((value, idx) => encoders[idx](value))); | ||
var Tuple = (...codecs) => createCodec(TupleEnc(...codecs.map(([encoder]) => encoder)), TupleDec(...codecs.map(([, decoder]) => decoder))); | ||
Tuple.enc = TupleEnc; | ||
Tuple.dec = TupleDec; | ||
@@ -418,9 +385,26 @@ // src/codecs/Struct.ts | ||
Struct.dec = StructDec; | ||
// src/codecs/Vector.ts | ||
var VectorEnc = (inner, size) => size > 0 ? (value) => mergeUint8(...value.map(inner)) : (value) => mergeUint8(compact.enc(value.length), ...value.map(inner)); | ||
function VectorDec(getter, size) { | ||
return toInternalBytes((bytes) => { | ||
const nElements = size > 0 ? size : compact.dec(bytes); | ||
const result = new Array(nElements); | ||
for (let i = 0; i < nElements; i++) { | ||
const current = getter(bytes); | ||
result[i] = current; | ||
} | ||
return result; | ||
}); | ||
} | ||
function Vector(inner, size) { | ||
return createCodec(VectorEnc(inner[0], size), VectorDec(inner[1], size)); | ||
} | ||
Vector.enc = VectorEnc; | ||
Vector.dec = VectorDec; | ||
export { | ||
Bytes, | ||
Enum, | ||
MapCodec, | ||
Option, | ||
Result, | ||
SetCodec, | ||
Struct, | ||
@@ -427,0 +411,0 @@ Tuple, |
{ | ||
"version": "0.2.0-alpha.4", | ||
"version": "0.2.0", | ||
"repository": { | ||
@@ -4,0 +4,0 @@ "type": "git", |
208
README.md
@@ -28,5 +28,5 @@ # scale-ts | ||
Something really cool about this library is that by having composable codecs | ||
which have very good typings, then the inferred types of the resulting codecs | ||
also have really good typings. For instance, the inferred types of codec | ||
defined above are: | ||
and higher order codecs with really good typings, then the inferred types of | ||
the custom codecs are also really good. For instance, the inferred types of | ||
the `myCodec` defined above are: | ||
*/ | ||
@@ -44,9 +44,11 @@ | ||
}> | ||
/* | ||
That's very useful, because on the one hand we will get a TS error if we try | ||
to pass an invalid input to the encoder, for instance TS will complain that | ||
the `value` of the property `event` is not valid, in the following example: | ||
*/ | ||
Therefore, we won't get typing errors as long as we pass a valide interface | ||
to the encoder: | ||
const encodedData: ArrayBuffer = myCodec.enc({ | ||
event: { tag: 'allOrNothing', value: true }, | ||
myCodec.enc({ | ||
event: { tag: 'one', value: 5 }, | ||
name: "Some name", | ||
@@ -57,6 +59,18 @@ id: 100, | ||
console.log(bufferToHex(encodedData)) | ||
// => 0x6400000024536f6d65206e616d650c0100000002000000030000000201 | ||
/* | ||
On the other hand, the result of the decoded value, also has that same interface, | ||
which is extremely useful. | ||
const decodedData = myCodec.dec(encodedData) | ||
An example on how to encoded/decode a valid value: | ||
*/ | ||
myCodec.enc({ | ||
id: 100, | ||
name: "Some name", | ||
friendIds: [1, 2, 3], | ||
event: { tag: "allOrNothing" as const, value: true }, | ||
}) | ||
// => 0x6400000024536f6d65206e616d650c0100000002000000030000000301 | ||
const decodedData = myCodec.dec(0x6400000024536f6d65206e616d650c0100000002000000030000000301) | ||
// also possible: | ||
@@ -85,3 +99,3 @@ // const decodedData = myCodec.dec("0x6400000024536f6d65206e616d650c0100000002000000030000000201") | ||
In this library you won't find common definitions like `AccountId`. However, | ||
since the definitions of this library are enhanceable and composable, it's | ||
since the definitions of this library are enhanceable and composable, it is | ||
very easy to create new custom definitions. For instance, the implementation of | ||
@@ -109,7 +123,30 @@ the `bool` Codec looks like this: | ||
## API | ||
## API - Codecs | ||
### bool | ||
### [Fixed-width integers](https://docs.substrate.io/v3/advanced/scale-codec/#fixed-width-integers) | ||
Supported codecs are: `u8`, `u16`, `u32`, `u64`, `u128`, | ||
`i8`, `i16`, `i32`, `i64`, `i128` | ||
```ts | ||
i128.enc(-18676936063680574795862633153229949450n) | ||
// => 0xf6f5f4f3f2f1f0f9f8f7f6f5f4f3f2f1 | ||
i128.dec("0xf6f5f4f3f2f1f0f9f8f7f6f5f4f3f2f1") | ||
// => -18676936063680574795862633153229949450n | ||
``` | ||
### [Compact/general integers](https://docs.substrate.io/v3/advanced/scale-codec/#compactgeneral-integers) | ||
```ts | ||
compact.enc(65535) | ||
// => 0xfeff0300 | ||
compact.dec("0xfeff0300") | ||
// => 65535 | ||
``` | ||
### [bool](https://docs.substrate.io/v3/advanced/scale-codec/#boolean) | ||
```ts | ||
bool.enc(false) | ||
@@ -122,55 +159,150 @@ // => 0x00 | ||
### Bytes | ||
### [Option](https://docs.substrate.io/v3/advanced/scale-codec/#options) | ||
Sometimes, mainly when creating your custom codecs, it's usefull to have a | ||
codec that simply reads/writes a certain amount of bytes. For example, see the | ||
example above for creating a custom `AccountId` codec. | ||
```ts | ||
cosnt optionalCompact = Option(compact) | ||
optionalCompact.enc() | ||
// => 0x00 | ||
optionalCompact.enc(undefined) | ||
// => 0x00 | ||
optionalCompact.enc(1) | ||
// => 0x0104 | ||
cosnt optionalBool = Option(bool) | ||
optionalBool.enc() | ||
// => 0x00 | ||
optionalBool.enc(true) | ||
// => 0x01 | ||
optionalBool.enc(false) | ||
// => 0x02 | ||
``` | ||
### [Result](https://docs.substrate.io/v3/advanced/scale-codec/#results) | ||
```ts | ||
const threeBytes = Bytes(3) | ||
const resultCodec = Result(u8, bool) | ||
threeBytes.enc(new Uint8Array([0, 15, 255])) | ||
// => 0x000fff | ||
resultCodec.enc({ success: true, value: 42 }) | ||
// => 0x002a | ||
bool.dec("0x000fff00") | ||
// => 0x000fff | ||
resultCodec.enc({ success: false, value: false }) | ||
// => 0x0100 | ||
``` | ||
### [Compact/general integers](https://docs.substrate.io/v3/advanced/scale-codec/#compactgeneral-integers) | ||
### [Vector](https://docs.substrate.io/v3/advanced/scale-codec/#vectors-lists-series-sets) | ||
Supported codecs are: `compact`, `u8`, `u16`, `u32`, `u64`, `u128`, `i8`, `i16`, | ||
`i32`, `i64`, `i128` | ||
Dynamic, for when the size is known at run time: | ||
```ts | ||
compact.enc(65535) | ||
// => 0xfeff0300 | ||
const numbers = Vector(u16) | ||
compact.dec("0xfeff0300") | ||
// => 65535 | ||
numbers.enc([4, 8, 15, 16, 23, 42]) | ||
// => 0x18040008000f00100017002a00 | ||
``` | ||
i128.enc(-18676936063680574795862633153229949450n) | ||
// => 0xf6f5f4f3f2f1f0f9f8f7f6f5f4f3f2f1 | ||
Fixed, for when the size is known at compile time: | ||
i128.dec("0xf6f5f4f3f2f1f0f9f8f7f6f5f4f3f2f1") | ||
// => -18676936063680574795862633153229949450n | ||
```ts | ||
const fiveNumbers = Vector(u16, 5) | ||
numbers.enc([4, 8, 15, 16, 23]) | ||
// => 0x040008000f0010001700 | ||
``` | ||
### date32/date64 | ||
### [str](https://docs.substrate.io/v3/advanced/scale-codec/#strings) | ||
```ts | ||
str.enc("a$¢ह€한𐍈😃") | ||
// => 0x546124c2a2e0a4b9e282aced959cf0908d88f09f9883 | ||
``` | ||
### [Tuple](https://docs.substrate.io/v3/advanced/scale-codec/#tuples) | ||
```ts | ||
const compactAndBool = Tuple(compact, bool) | ||
compactAndBool.enc([3, false]) | ||
// => 0x0c00 | ||
``` | ||
### Enum | ||
### [Struct](https://docs.substrate.io/v3/advanced/scale-codec/#data-structures) | ||
```ts | ||
const myCodec = { | ||
id: u32, | ||
name: str, | ||
friendIds: Vector(u32), | ||
event: Enum({ | ||
_void, | ||
one: str, | ||
many: Vector(str), | ||
allOrNothing: bool, | ||
}), | ||
} | ||
myCodec.enc({ | ||
id: 100, | ||
name: "Some name", | ||
friendIds: [1, 2, 3], | ||
event: { tag: "allOrNothing" as const, value: true }, | ||
}) | ||
// => 0x6400000024536f6d65206e616d650c0100000002000000030000000301 | ||
``` | ||
### hex | ||
### [Enum](https://docs.substrate.io/v3/advanced/scale-codec/#enumerations-tagged-unions) | ||
```ts | ||
const myCodec = Enum({ | ||
nothingHere: _void, | ||
someNumber: u8, | ||
trueOrFalse: bool, | ||
optionalBool: Option(bool), | ||
optVoid: Option(_void), | ||
}) | ||
myCodec.enc({ tag: "nothingHere" }) | ||
// => 0x00 | ||
myCodec.dec("0x012a") | ||
// => { tag: "someNumber", value: 42 } | ||
``` | ||
### | ||
### Bytes | ||
Sometimes, mainly when creating your custom codecs, it's usefull to have a | ||
codec that simply reads/writes a certain amount of bytes. For example, see the | ||
example above for creating a custom `AccountId` codec. | ||
```ts | ||
const threeBytes = Bytes(3) | ||
threeBytes.enc(new Uint8Array([0, 15, 255])) | ||
// => 0x000fff | ||
bool.dec("0x000fff00") | ||
// => 0x000fff | ||
``` | ||
### `_void` | ||
This is a special codec that it's mostly useful in combination with `Enum`, | ||
its type is `Codec<void>`, and as you can imagine calling `_void.enc()` produces | ||
an empty `Uint8Array` and calling `_void.dec()` returns `undefined`. | ||
## API - Utils | ||
TODO: document them | ||
### createCodec | ||
### enhanceEncoder | ||
### enhanceDecoder | ||
### enhanceCodec |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
303
180423
43
1438