Comparing version 0.2.6 to 0.2.7
@@ -187,3 +187,3 @@ var __defProp = Object.defineProperty; | ||
const kind = init & 3; | ||
if (kind !== 3) | ||
if (kind < 3) | ||
return decoders[kind](bytes) >>> 2; | ||
@@ -233,3 +233,3 @@ const nBytes = (init >>> 2) + 4; | ||
} | ||
while (bigValue >= MIN_U32) { | ||
if (bigValue >= MIN_U32) { | ||
buffers.push(u32[0](Number(bigValue & U32_MASK))); | ||
@@ -239,3 +239,3 @@ bigValue >>= 32n; | ||
let smValue = Number(bigValue); | ||
while (smValue >= MIN_U16) { | ||
if (smValue >= MIN_U16) { | ||
buffers.push(u16[0](smValue)); | ||
@@ -242,0 +242,0 @@ smValue >>= 16; |
@@ -1,2 +0,2 @@ | ||
var g=Object.defineProperty;var ce=Object.getOwnPropertyDescriptor;var de=Object.getOwnPropertyNames;var se=Object.prototype.hasOwnProperty;var ie=(e,n,t)=>n in e?g(e,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[n]=t;var ae=(e,n)=>{for(var t in n)g(e,t,{get:n[t],enumerable:!0})},ue=(e,n,t,o)=>{if(n&&typeof n=="object"||typeof n=="function")for(let r of de(n))!se.call(e,r)&&r!==t&&g(e,r,{get:()=>n[r],enumerable:!(o=ce(n,r))||o.enumerable});return e};var me=e=>ue(g({},"__esModule",{value:!0}),e);var V=(e,n,t)=>(ie(e,typeof n!="symbol"?n+"":n,t),t);var Me={};ae(Me,{Bytes:()=>I,Enum:()=>K,Option:()=>C,Result:()=>w,Struct:()=>U,Tuple:()=>p,Vector:()=>k,_void:()=>F,bool:()=>D,compact:()=>f,createCodec:()=>c,enhanceCodec:()=>h,enhanceDecoder:()=>T,enhanceEncoder:()=>E,i128:()=>Ee,i16:()=>le,i32:()=>De,i64:()=>xe,i8:()=>Oe,str:()=>Y,u128:()=>ge,u16:()=>O,u32:()=>l,u64:()=>A,u8:()=>a});module.exports=me(Me);var c=(e,n)=>{let t=[e,n];return t.enc=e,t.dec=n,t},E=(e,n)=>t=>e(n(t)),T=(e,n)=>t=>n(e(t)),h=([e,n],t,o)=>c(E(e,t),T(n,o));var _={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 fe(e){let n=e.length%2,t=(e[1]==="x"?2:0)+n,o=(e.length-t)/2+n,r=new Uint8Array(o);n&&(r[0]=0|_[e[2]]);for(let d=0;d<o;){let u=t+d*2,B=_[e[u]],v=_[e[u+1]];r[n+d++]=B<<4|v}return r}var b=class extends Uint8Array{constructor(t){super(t);V(this,"i",0);V(this,"v");this.v=new DataView(t)}},s=e=>n=>e(n instanceof b?n:new b(typeof n=="string"?fe(n).buffer:n instanceof Uint8Array?n.buffer:n));var i=(...e)=>{let n=e.reduce((o,r)=>o+r.byteLength,0),t=new Uint8Array(n);for(let o=0,r=0;o<e.length;o++){let d=e[o];t.set(d,r),r+=d.byteLength}return t};function y(e,n){return Object.fromEntries(Object.entries(e).map(([t,o])=>[t,n(o,t)]))}function pe(e,n){return s(t=>{let o=t.v[n](t.i,!0);return t.i+=e,o})}function ye(e,n){return t=>{let o=new Uint8Array(e);return new DataView(o.buffer)[n](0,t,!0),o}}function m(e,n,t){return c(ye(e,t),pe(e,n))}var a=m(1,"getUint8","setUint8"),O=m(2,"getUint16","setUint16"),l=m(4,"getUint32","setUint32"),A=m(8,"getBigUint64","setBigUint64"),Oe=m(1,"getInt8","setInt8"),le=m(2,"getInt16","setInt16"),De=m(4,"getInt32","setInt32"),xe=m(8,"getBigInt64","setBigInt64"),L=e=>{let n=new Uint8Array(16),t=new DataView(n.buffer);return t.setBigInt64(0,e,!0),t.setBigInt64(8,e>>64n,!0),n},N=e=>s(n=>{let{v:t,i:o}=n,r=t.getBigUint64(o,!0),d=t[e?"getBigInt64":"getBigUint64"](o+8,!0);return n.i+=16,d<<64n|r}),ge=c(L,N()),Ee=c(L,N(1));var Te=e=>e?1:0,D=h(a,Te,Boolean);var P=[a[1],O[1],l[1],A[1]],be=s(e=>{let n=e.i,t=e[n],o=t&3;if(o!==3)return P[o](e)>>>2;let r=(t>>>2)+4;e.i++;let d=r/8|0,u=r%8,B=u/4|0;u%=4;let v=[u%2,u/2|0,B,d],R=0n,j=0n,M=4n;for(let x=0;x<4;x++){M*=2n;let re=v[x];for(let S=0;S<re;S++)R=BigInt(P[x](e))<<j|R,j+=M}return R}),Ae=1n<<56n,Ie=1<<24,Ke=256,Ce=4294967295n,we=1<<6,Ue=1<<14,ke=1<<30,Be=e=>{if(e<0)throw new Error(`Wrong compact input (${e})`);let n=Number(e)<<2;if(e<we)return a[0](n);if(e<Ue)return O[0](n|1);if(e<ke)return l[0](n|2);let t=[new Uint8Array(1)],o=BigInt(e);for(;o>=Ae;)t.push(A[0](o)),o>>=64n;for(;o>=Ie;)t.push(l[0](Number(o&Ce))),o>>=32n;let r=Number(o);for(;r>=Ke;)t.push(O[0](r)),r>>=16;r&&t.push(a[0](r));let d=i(...t);return d[0]=d.length-5<<2|3,d},f=c(Be,be);var ve=new TextEncoder,Re=e=>{let n=ve.encode(e);return i(f.enc(n.length),n)},Ve=new TextDecoder,he=s(e=>{let n=f.dec(e),t=new DataView(e.buffer,e.i,n);return e.i+=n,Ve.decode(t)}),Y=c(Re,he);var _e=()=>{},je=new Uint8Array(0),F=c(()=>je,_e);var H=e=>n=>n.length===e?n:n.slice(0,e),W=e=>s(n=>{let t=n.i,o=e!==1/0?e:n.byteLength-t,r=new Uint8Array(n.buffer,t,o);return n.i+=o,r}),I=e=>c(H(e),W(e));I.enc=H;I.dec=W;var G=e=>{let n=Object.keys(e);return({tag:t,value:o})=>{let r=n.indexOf(t);return i(a.enc(r),e[t](o))}},X=e=>{let n=Object.entries(e);return s(t=>{let o=a.dec(t),[r,d]=n[o];return{tag:r,value:d(t)}})},K=e=>c(G(y(e,([n])=>n)),X(y(e,([,n])=>n)));K.enc=G;K.dec=X;var $=e=>s(n=>{let t=a.dec(n);if(t!==0)return e===D[1]?t===1:e(n)}),q=e=>n=>{let t=new Uint8Array(1);return n===void 0?(t[0]=0,t):(t[0]=1,e===D[0]?(t[0]=n?1:2,t):i(t,e(n)))},C=e=>c(q(e[0]),$(e[1]));C.enc=q;C.dec=$;var J=(e,n)=>s(t=>{let o=a[1](t)===0,d=(o?e:n)(t);return{success:o,value:d}}),Q=(e,n)=>({success:t,value:o})=>i(a[0](t?0:1),(t?e:n)(o)),w=(e,n)=>c(Q(e[0],n[0]),J(e[1],n[1]));w.dec=J;w.enc=Q;var Z=(...e)=>s(n=>e.map(t=>t(n))),z=(...e)=>n=>i(...n.map((t,o)=>e[o](t))),p=(...e)=>c(z(...e.map(([n])=>n)),Z(...e.map(([,n])=>n)));p.enc=z;p.dec=Z;var ee=e=>{let n=Object.keys(e);return E(p.enc(...Object.values(e)),t=>n.map(o=>t[o]))},ne=e=>{let n=Object.keys(e);return T(p.dec(...Object.values(e)),t=>Object.fromEntries(t.map((o,r)=>[n[r],o])))},U=e=>c(ee(y(e,n=>n[0])),ne(y(e,n=>n[1])));U.enc=ee;U.dec=ne;var te=(e,n)=>n>=0?t=>i(...t.map(e)):t=>i(f.enc(t.length),...t.map(e));function oe(e,n){return s(t=>{let o=n>=0?n:f.dec(t),r=new Array(o);for(let d=0;d<o;d++){let u=e(t);r[d]=u}return r})}function k(e,n){return c(te(e[0],n),oe(e[1],n))}k.enc=te;k.dec=oe; | ||
var g=Object.defineProperty;var ce=Object.getOwnPropertyDescriptor;var de=Object.getOwnPropertyNames;var se=Object.prototype.hasOwnProperty;var ie=(e,n,t)=>n in e?g(e,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[n]=t;var ae=(e,n)=>{for(var t in n)g(e,t,{get:n[t],enumerable:!0})},ue=(e,n,t,o)=>{if(n&&typeof n=="object"||typeof n=="function")for(let r of de(n))!se.call(e,r)&&r!==t&&g(e,r,{get:()=>n[r],enumerable:!(o=ce(n,r))||o.enumerable});return e};var me=e=>ue(g({},"__esModule",{value:!0}),e);var V=(e,n,t)=>(ie(e,typeof n!="symbol"?n+"":n,t),t);var Me={};ae(Me,{Bytes:()=>I,Enum:()=>K,Option:()=>C,Result:()=>w,Struct:()=>U,Tuple:()=>p,Vector:()=>k,_void:()=>F,bool:()=>l,compact:()=>f,createCodec:()=>c,enhanceCodec:()=>h,enhanceDecoder:()=>T,enhanceEncoder:()=>E,i128:()=>Ee,i16:()=>De,i32:()=>le,i64:()=>xe,i8:()=>Oe,str:()=>Y,u128:()=>ge,u16:()=>O,u32:()=>D,u64:()=>A,u8:()=>a});module.exports=me(Me);var c=(e,n)=>{let t=[e,n];return t.enc=e,t.dec=n,t},E=(e,n)=>t=>e(n(t)),T=(e,n)=>t=>n(e(t)),h=([e,n],t,o)=>c(E(e,t),T(n,o));var _={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 fe(e){let n=e.length%2,t=(e[1]==="x"?2:0)+n,o=(e.length-t)/2+n,r=new Uint8Array(o);n&&(r[0]=0|_[e[2]]);for(let d=0;d<o;){let u=t+d*2,B=_[e[u]],v=_[e[u+1]];r[n+d++]=B<<4|v}return r}var b=class extends Uint8Array{constructor(t){super(t);V(this,"i",0);V(this,"v");this.v=new DataView(t)}},s=e=>n=>e(n instanceof b?n:new b(typeof n=="string"?fe(n).buffer:n instanceof Uint8Array?n.buffer:n));var i=(...e)=>{let n=e.reduce((o,r)=>o+r.byteLength,0),t=new Uint8Array(n);for(let o=0,r=0;o<e.length;o++){let d=e[o];t.set(d,r),r+=d.byteLength}return t};function y(e,n){return Object.fromEntries(Object.entries(e).map(([t,o])=>[t,n(o,t)]))}function pe(e,n){return s(t=>{let o=t.v[n](t.i,!0);return t.i+=e,o})}function ye(e,n){return t=>{let o=new Uint8Array(e);return new DataView(o.buffer)[n](0,t,!0),o}}function m(e,n,t){return c(ye(e,t),pe(e,n))}var a=m(1,"getUint8","setUint8"),O=m(2,"getUint16","setUint16"),D=m(4,"getUint32","setUint32"),A=m(8,"getBigUint64","setBigUint64"),Oe=m(1,"getInt8","setInt8"),De=m(2,"getInt16","setInt16"),le=m(4,"getInt32","setInt32"),xe=m(8,"getBigInt64","setBigInt64"),L=e=>{let n=new Uint8Array(16),t=new DataView(n.buffer);return t.setBigInt64(0,e,!0),t.setBigInt64(8,e>>64n,!0),n},N=e=>s(n=>{let{v:t,i:o}=n,r=t.getBigUint64(o,!0),d=t[e?"getBigInt64":"getBigUint64"](o+8,!0);return n.i+=16,d<<64n|r}),ge=c(L,N()),Ee=c(L,N(1));var Te=e=>e?1:0,l=h(a,Te,Boolean);var P=[a[1],O[1],D[1],A[1]],be=s(e=>{let n=e.i,t=e[n],o=t&3;if(o<3)return P[o](e)>>>2;let r=(t>>>2)+4;e.i++;let d=r/8|0,u=r%8,B=u/4|0;u%=4;let v=[u%2,u/2|0,B,d],R=0n,j=0n,M=4n;for(let x=0;x<4;x++){M*=2n;let re=v[x];for(let S=0;S<re;S++)R=BigInt(P[x](e))<<j|R,j+=M}return R}),Ae=1n<<56n,Ie=1<<24,Ke=256,Ce=4294967295n,we=1<<6,Ue=1<<14,ke=1<<30,Be=e=>{if(e<0)throw new Error(`Wrong compact input (${e})`);let n=Number(e)<<2;if(e<we)return a[0](n);if(e<Ue)return O[0](n|1);if(e<ke)return D[0](n|2);let t=[new Uint8Array(1)],o=BigInt(e);for(;o>=Ae;)t.push(A[0](o)),o>>=64n;o>=Ie&&(t.push(D[0](Number(o&Ce))),o>>=32n);let r=Number(o);r>=Ke&&(t.push(O[0](r)),r>>=16),r&&t.push(a[0](r));let d=i(...t);return d[0]=d.length-5<<2|3,d},f=c(Be,be);var ve=new TextEncoder,Re=e=>{let n=ve.encode(e);return i(f.enc(n.length),n)},Ve=new TextDecoder,he=s(e=>{let n=f.dec(e),t=new DataView(e.buffer,e.i,n);return e.i+=n,Ve.decode(t)}),Y=c(Re,he);var _e=()=>{},je=new Uint8Array(0),F=c(()=>je,_e);var H=e=>n=>n.length===e?n:n.slice(0,e),W=e=>s(n=>{let t=n.i,o=e!==1/0?e:n.byteLength-t,r=new Uint8Array(n.buffer,t,o);return n.i+=o,r}),I=e=>c(H(e),W(e));I.enc=H;I.dec=W;var G=e=>{let n=Object.keys(e);return({tag:t,value:o})=>{let r=n.indexOf(t);return i(a.enc(r),e[t](o))}},X=e=>{let n=Object.entries(e);return s(t=>{let o=a.dec(t),[r,d]=n[o];return{tag:r,value:d(t)}})},K=e=>c(G(y(e,([n])=>n)),X(y(e,([,n])=>n)));K.enc=G;K.dec=X;var $=e=>s(n=>{let t=a.dec(n);if(t!==0)return e===l[1]?t===1:e(n)}),q=e=>n=>{let t=new Uint8Array(1);return n===void 0?(t[0]=0,t):(t[0]=1,e===l[0]?(t[0]=n?1:2,t):i(t,e(n)))},C=e=>c(q(e[0]),$(e[1]));C.enc=q;C.dec=$;var J=(e,n)=>s(t=>{let o=a[1](t)===0,d=(o?e:n)(t);return{success:o,value:d}}),Q=(e,n)=>({success:t,value:o})=>i(a[0](t?0:1),(t?e:n)(o)),w=(e,n)=>c(Q(e[0],n[0]),J(e[1],n[1]));w.dec=J;w.enc=Q;var Z=(...e)=>s(n=>e.map(t=>t(n))),z=(...e)=>n=>i(...n.map((t,o)=>e[o](t))),p=(...e)=>c(z(...e.map(([n])=>n)),Z(...e.map(([,n])=>n)));p.enc=z;p.dec=Z;var ee=e=>{let n=Object.keys(e);return E(p.enc(...Object.values(e)),t=>n.map(o=>t[o]))},ne=e=>{let n=Object.keys(e);return T(p.dec(...Object.values(e)),t=>Object.fromEntries(t.map((o,r)=>[n[r],o])))},U=e=>c(ee(y(e,n=>n[0])),ne(y(e,n=>n[1])));U.enc=ee;U.dec=ne;var te=(e,n)=>n>=0?t=>i(...t.map(e)):t=>i(f.enc(t.length),...t.map(e));function oe(e,n){return s(t=>{let o=n>=0?n:f.dec(t),r=new Array(o);for(let d=0;d<o;d++){let u=e(t);r[d]=u}return r})}function k(e,n){return c(te(e[0],n),oe(e[1],n))}k.enc=te;k.dec=oe; | ||
//# sourceMappingURL=scale-ts.cjs.production.min.js.map |
@@ -140,3 +140,3 @@ var __defProp = Object.defineProperty; | ||
const kind = init & 3; | ||
if (kind !== 3) | ||
if (kind < 3) | ||
return decoders[kind](bytes) >>> 2; | ||
@@ -186,3 +186,3 @@ const nBytes = (init >>> 2) + 4; | ||
} | ||
while (bigValue >= MIN_U32) { | ||
if (bigValue >= MIN_U32) { | ||
buffers.push(u32[0](Number(bigValue & U32_MASK))); | ||
@@ -192,3 +192,3 @@ bigValue >>= 32n; | ||
let smValue = Number(bigValue); | ||
while (smValue >= MIN_U16) { | ||
if (smValue >= MIN_U16) { | ||
buffers.push(u16[0](smValue)); | ||
@@ -195,0 +195,0 @@ smValue >>= 16; |
{ | ||
"version": "0.2.6", | ||
"version": "0.2.7", | ||
"repository": { | ||
@@ -4,0 +4,0 @@ "type": "git", |
@@ -105,4 +105,9 @@ # scale-ts | ||
const booleanToNumber = (value: boolean) => (value ? 1 : 0) | ||
const numberToBoolean = Boolean | ||
export const bool: Codec<boolean> = enhanceCodec(u8, booleanToNumber, Boolean) | ||
export const bool: Codec<boolean> = enhanceCodec( | ||
u8, | ||
booleanToNumber, | ||
numberToBoolean, | ||
) | ||
``` | ||
@@ -159,2 +164,3 @@ | ||
Normal cases: | ||
```ts | ||
@@ -174,2 +180,3 @@ cosnt optionalCompact = Option(compact) | ||
Exceptionally, if the input is `bool`, then it always returns one byte: | ||
```ts | ||
@@ -263,3 +270,3 @@ cosnt optionalBool = Option(bool) | ||
```ts | ||
const myCodec = Enum({ | ||
const { enc, dec } = Enum({ | ||
nothingHere: _void, | ||
@@ -272,6 +279,6 @@ someNumber: u8, | ||
myCodec.enc({ tag: "nothingHere" }) | ||
enc({ tag: "nothingHere" }) | ||
// => 0x00 | ||
myCodec.dec("0x012a") | ||
dec("0x012a") | ||
// => { tag: "someNumber", value: 42 } | ||
@@ -287,8 +294,8 @@ ``` | ||
```ts | ||
const threeBytes = Bytes(3) | ||
const [encode, decode] = Bytes(3) | ||
threeBytes.enc(new Uint8Array([0, 15, 255])) | ||
encode(new Uint8Array([0, 15, 255])) | ||
// => 0x000fff | ||
threeBytes.dec("0x000fff00") | ||
decode("0x000fff00") | ||
// => 0x000fff | ||
@@ -365,6 +372,3 @@ ``` | ||
): Codec<Map<K, V>> => | ||
createCodec( | ||
MapEncoder(key.enc, value.enc), | ||
MapDecoder(key.dec, value.dec), | ||
) | ||
createCodec(MapEncoder(key.enc, value.enc), MapDecoder(key.dec, value.dec)) | ||
@@ -371,0 +375,0 @@ MapCodec.enc = MapEncoder |
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
480
163353