@onflow/decode
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -1,2 +0,2 @@ | ||
var e=function(e,r,n){try{return Number(e)}catch(e){throw new Error("Decode Number Error : "+n.join("."))}},r=function(e){return e},n=function(){return null},t=function(e,r,n){return e?a(e,r,n):null},o=function(e){return{address:e.address,type:e.type}},c=function(e,r,n){return e.map(function(e){return a(e,r,n.concat([e.type]))})},d=function(e,r,n){return e.reduce(function(e,t){e[a(t.key,r,n.concat([t.key]))]=a(t.value,r,n.concat([t.key]))},{})},u=function(e,r,n){return e.fields.reduce(function(e,t){return e[t.name]=a(t.value,r,n.concat([t.name])),e},{})},i={"[U]Int":e,Int:e,"[U]Int8":e,Int8:e,"[U]Int16":e,Int16:e,"[U]Int32":e,Int32:e,"[U]Int64":e,Int64:e,"[U]Int128":e,Int128:e,"[U]Int256":e,Int256:e,Word8:e,Word16:e,Word32:e,Word64:e,"[U]Fix64":e,Fix64:e,String:r,Character:r,Bool:r,Address:r,Void:n,Optional:t,Reference:o,Array:c,Dictionary:d,Event:u,Resource:u,Struct:u},a=function(e,r,n){var t=r[e.type];if(!t)throw new Error("Undefined Decoder Error: "+e.type+"@"+n.join("."));return t(e.value,r,n)},s=function(e,r,n){void 0===r&&(r={}),void 0===n&&(n=[]);var t=Object.assign({},i,r);return a(e,t,n)};exports.decodeNumber=e,exports.decodeImplicit=r,exports.decodeVoid=n,exports.decodeOptional=t,exports.decodeReference=o,exports.decodeArray=c,exports.decodeDictionary=d,exports.decodeComposite=u,exports.defaultDecoders=i,exports.recurseDecode=a,exports.decode=s,exports.decodeResponse=function(e,r){void 0===r&&(r={});var n=Object.assign({},i,r);return s(e.payload,n)}; | ||
var e=require("@onflow/bytes");function r(){return(r=Object.assign||function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var o in t)Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o])}return e}).apply(this,arguments)}var t=function(e,r,t){try{try{return Promise.resolve(Number(e))}catch(e){throw new Error("Decode Number Error : "+t.join("."))}}catch(e){return Promise.reject(e)}},o=function(e){return Promise.resolve(e)},n=function(){return Promise.resolve(null)},c=function(e,r,t){return Promise.resolve(e?m(e,r,t):null)},s=function(e){try{return Promise.resolve({address:e.address,type:e.type})}catch(e){return Promise.reject(e)}},i=function(e,r,t){try{return Promise.resolve(Promise.all(e.map(function(e){return new Promise(function(o){try{return Promise.resolve(m(e,r,[].concat(t,[e.type]))).then(o)}catch(e){return Promise.reject(e)}})})))}catch(e){return Promise.reject(e)}},u=function(e,r,t){try{return Promise.resolve(e.reduce(function(e,o){try{return Promise.resolve(e).then(function(n){return e=n,Promise.resolve(m(o.key,r,[].concat(t,[o.key]))).then(function(n){return Promise.resolve(m(o.value,r,[].concat(t,[o.key]))).then(function(r){return e[n]=r,e})})})}catch(e){return Promise.reject(e)}},Promise.resolve({})))}catch(e){return Promise.reject(e)}},d=function(e,r,t){try{return Promise.resolve(e.fields.reduce(function(e,o){try{return Promise.resolve(e).then(function(n){return e=n,Promise.resolve(m(o.value,r,[].concat(t,[o.name]))).then(function(r){return e[o.name]=r,e})})}catch(e){return Promise.reject(e)}},Promise.resolve({}))).then(function(t){var o=e.id&&l(r,e.id);return o?Promise.resolve(o(t)):t})}catch(e){return Promise.reject(e)}},a={UInt:t,Int:t,UInt8:t,Int8:t,UInt16:t,Int16:t,UInt32:t,Int32:t,UInt64:t,Int64:t,UInt128:t,Int128:t,UInt256:t,Int256:t,Word8:t,Word16:t,Word32:t,Word64:t,UFix64:t,Fix64:t,String:o,Character:o,Bool:o,Address:o,Void:n,Optional:c,Reference:s,Array:i,Dictionary:u,Event:d,Resource:d,Struct:d},l=function(e,r){var t=Object.keys(e).find(function(e){return/^\/.*\/$/.test(e)?new RegExp(e.substring(1,e.length-1)).test(r):e===r});return r&&t&&e[t]},m=function(e,r,t){try{var o=l(r,e.type);if(!o)throw new Error("Undefined Decoder Error: "+e.type+"@"+t.join("."));return Promise.resolve(o(e.value,r,t))}catch(e){return Promise.reject(e)}},v=function(e,t,o){void 0===t&&(t={}),void 0===o&&(o=[]);var n=r({},a,{},t);return Promise.resolve(m(e,n,o))};exports.decode=v,exports.decodeArray=i,exports.decodeComposite=d,exports.decodeDictionary=u,exports.decodeImplicit=o,exports.decodeNumber=t,exports.decodeOptional=c,exports.decodeReference=s,exports.decodeResponse=function(t,o){void 0===o&&(o={});try{var n=r({},a,{},o),c=e.bytesToString(e.bytes(t.encodedData)),s=JSON.parse(c);return Promise.resolve(v(s,n))}catch(e){return Promise.reject(e)}},exports.decodeVoid=n,exports.decoderLookup=l,exports.defaultDecoders=a,exports.recurseDecode=m; | ||
//# sourceMappingURL=decode.js.map |
@@ -1,2 +0,2 @@ | ||
var n=function(n,r,t){try{return Number(n)}catch(n){throw new Error("Decode Number Error : "+t.join("."))}},r=function(n){return n},t=function(){return null},e=function(n,r,t){return n?d(n,r,t):null},o=function(n){return{address:n.address,type:n.type}},u=function(n,r,t){return n.map(function(n){return d(n,r,t.concat([n.type]))})},c=function(n,r,t){return n.reduce(function(n,e){n[d(e.key,r,t.concat([e.key]))]=d(e.value,r,t.concat([e.key]))},{})},i=function(n,r,t){return n.fields.reduce(function(n,e){return n[e.name]=d(e.value,r,t.concat([e.name])),n},{})},a={"[U]Int":n,Int:n,"[U]Int8":n,Int8:n,"[U]Int16":n,Int16:n,"[U]Int32":n,Int32:n,"[U]Int64":n,Int64:n,"[U]Int128":n,Int128:n,"[U]Int256":n,Int256:n,Word8:n,Word16:n,Word32:n,Word64:n,"[U]Fix64":n,Fix64:n,String:r,Character:r,Bool:r,Address:r,Void:t,Optional:e,Reference:o,Array:u,Dictionary:c,Event:i,Resource:i,Struct:i},d=function(n,r,t){var e=r[n.type];if(!e)throw new Error("Undefined Decoder Error: "+n.type+"@"+t.join("."));return e(n.value,r,t)},f=function(n,r,t){void 0===r&&(r={}),void 0===t&&(t=[]);var e=Object.assign({},a,r);return d(n,e,t)},I=function(n,r){void 0===r&&(r={});var t=Object.assign({},a,r);return f(n.payload,t)};export{n as decodeNumber,r as decodeImplicit,t as decodeVoid,e as decodeOptional,o as decodeReference,u as decodeArray,c as decodeDictionary,i as decodeComposite,a as defaultDecoders,d as recurseDecode,f as decode,I as decodeResponse}; | ||
import{bytesToString as r,bytes as e}from"@onflow/bytes";function t(){return(t=Object.assign||function(r){for(var e=1;e<arguments.length;e++){var t=arguments[e];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(r[n]=t[n])}return r}).apply(this,arguments)}var n=function(r,e,t){try{try{return Promise.resolve(Number(r))}catch(r){throw new Error("Decode Number Error : "+t.join("."))}}catch(r){return Promise.reject(r)}},o=function(r){return Promise.resolve(r)},i=function(){return Promise.resolve(null)},c=function(r,e,t){return Promise.resolve(r?f(r,e,t):null)},u=function(r){try{return Promise.resolve({address:r.address,type:r.type})}catch(r){return Promise.reject(r)}},s=function(r,e,t){try{return Promise.resolve(Promise.all(r.map(function(r){return new Promise(function(n){try{return Promise.resolve(f(r,e,[].concat(t,[r.type]))).then(n)}catch(r){return Promise.reject(r)}})})))}catch(r){return Promise.reject(r)}},a=function(r,e,t){try{return Promise.resolve(r.reduce(function(r,n){try{return Promise.resolve(r).then(function(o){return r=o,Promise.resolve(f(n.key,e,[].concat(t,[n.key]))).then(function(o){return Promise.resolve(f(n.value,e,[].concat(t,[n.key]))).then(function(e){return r[o]=e,r})})})}catch(r){return Promise.reject(r)}},Promise.resolve({})))}catch(r){return Promise.reject(r)}},m=function(r,e,t){try{return Promise.resolve(r.fields.reduce(function(r,n){try{return Promise.resolve(r).then(function(o){return r=o,Promise.resolve(f(n.value,e,[].concat(t,[n.name]))).then(function(e){return r[n.name]=e,r})})}catch(r){return Promise.reject(r)}},Promise.resolve({}))).then(function(t){var n=r.id&&v(e,r.id);return n?Promise.resolve(n(t)):t})}catch(r){return Promise.reject(r)}},l={UInt:n,Int:n,UInt8:n,Int8:n,UInt16:n,Int16:n,UInt32:n,Int32:n,UInt64:n,Int64:n,UInt128:n,Int128:n,UInt256:n,Int256:n,Word8:n,Word16:n,Word32:n,Word64:n,UFix64:n,Fix64:n,String:o,Character:o,Bool:o,Address:o,Void:i,Optional:c,Reference:u,Array:s,Dictionary:a,Event:m,Resource:m,Struct:m},v=function(r,e){var t=Object.keys(r).find(function(r){return/^\/.*\/$/.test(r)?new RegExp(r.substring(1,r.length-1)).test(e):r===e});return e&&t&&r[t]},f=function(r,e,t){try{var n=v(e,r.type);if(!n)throw new Error("Undefined Decoder Error: "+r.type+"@"+t.join("."));return Promise.resolve(n(r.value,e,t))}catch(r){return Promise.reject(r)}},P=function(r,e,n){void 0===e&&(e={}),void 0===n&&(n=[]);var o=t({},l,{},e);return Promise.resolve(f(r,o,n))},d=function(n,o){void 0===o&&(o={});try{var i=t({},l,{},o),c=r(e(n.encodedData)),u=JSON.parse(c);return Promise.resolve(P(u,i))}catch(r){return Promise.reject(r)}};export{P as decode,s as decodeArray,m as decodeComposite,a as decodeDictionary,o as decodeImplicit,n as decodeNumber,c as decodeOptional,u as decodeReference,d as decodeResponse,i as decodeVoid,v as decoderLookup,l as defaultDecoders,f as recurseDecode}; | ||
//# sourceMappingURL=decode.module.js.map |
@@ -1,2 +0,2 @@ | ||
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n(e.decode={})}(this,function(e){var n=function(e,n,t){try{return Number(e)}catch(e){throw new Error("Decode Number Error : "+t.join("."))}},t=function(e){return e},r=function(){return null},o=function(e,n,t){return e?f(e,n,t):null},c=function(e){return{address:e.address,type:e.type}},d=function(e,n,t){return e.map(function(e){return f(e,n,t.concat([e.type]))})},i=function(e,n,t){return e.reduce(function(e,r){e[f(r.key,n,t.concat([r.key]))]=f(r.value,n,t.concat([r.key]))},{})},u=function(e,n,t){return e.fields.reduce(function(e,r){return e[r.name]=f(r.value,n,t.concat([r.name])),e},{})},a={"[U]Int":n,Int:n,"[U]Int8":n,Int8:n,"[U]Int16":n,Int16:n,"[U]Int32":n,Int32:n,"[U]Int64":n,Int64:n,"[U]Int128":n,Int128:n,"[U]Int256":n,Int256:n,Word8:n,Word16:n,Word32:n,Word64:n,"[U]Fix64":n,Fix64:n,String:t,Character:t,Bool:t,Address:t,Void:r,Optional:o,Reference:c,Array:d,Dictionary:i,Event:u,Resource:u,Struct:u},f=function(e,n,t){var r=n[e.type];if(!r)throw new Error("Undefined Decoder Error: "+e.type+"@"+t.join("."));return r(e.value,n,t)},s=function(e,n,t){void 0===n&&(n={}),void 0===t&&(t=[]);var r=Object.assign({},a,n);return f(e,r,t)};e.decodeNumber=n,e.decodeImplicit=t,e.decodeVoid=r,e.decodeOptional=o,e.decodeReference=c,e.decodeArray=d,e.decodeDictionary=i,e.decodeComposite=u,e.defaultDecoders=a,e.recurseDecode=f,e.decode=s,e.decodeResponse=function(e,n){void 0===n&&(n={});var t=Object.assign({},a,n);return s(e.payload,t)}}); | ||
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("@onflow/bytes")):"function"==typeof define&&define.amd?define(["exports","@onflow/bytes"],r):r((e=e||self).decode={},e.bytes)}(this,function(e,r){function t(){return(t=Object.assign||function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])}return e}).apply(this,arguments)}var n=function(e,r,t){try{try{return Promise.resolve(Number(e))}catch(e){throw new Error("Decode Number Error : "+t.join("."))}}catch(e){return Promise.reject(e)}},o=function(e){return Promise.resolve(e)},c=function(){return Promise.resolve(null)},i=function(e,r,t){return Promise.resolve(e?m(e,r,t):null)},s=function(e){try{return Promise.resolve({address:e.address,type:e.type})}catch(e){return Promise.reject(e)}},u=function(e,r,t){try{return Promise.resolve(Promise.all(e.map(function(e){return new Promise(function(n){try{return Promise.resolve(m(e,r,[].concat(t,[e.type]))).then(n)}catch(e){return Promise.reject(e)}})})))}catch(e){return Promise.reject(e)}},d=function(e,r,t){try{return Promise.resolve(e.reduce(function(e,n){try{return Promise.resolve(e).then(function(o){return e=o,Promise.resolve(m(n.key,r,[].concat(t,[n.key]))).then(function(o){return Promise.resolve(m(n.value,r,[].concat(t,[n.key]))).then(function(r){return e[o]=r,e})})})}catch(e){return Promise.reject(e)}},Promise.resolve({})))}catch(e){return Promise.reject(e)}},a=function(e,r,t){try{return Promise.resolve(e.fields.reduce(function(e,n){try{return Promise.resolve(e).then(function(o){return e=o,Promise.resolve(m(n.value,r,[].concat(t,[n.name]))).then(function(r){return e[n.name]=r,e})})}catch(e){return Promise.reject(e)}},Promise.resolve({}))).then(function(t){var n=e.id&&l(r,e.id);return n?Promise.resolve(n(t)):t})}catch(e){return Promise.reject(e)}},f={UInt:n,Int:n,UInt8:n,Int8:n,UInt16:n,Int16:n,UInt32:n,Int32:n,UInt64:n,Int64:n,UInt128:n,Int128:n,UInt256:n,Int256:n,Word8:n,Word16:n,Word32:n,Word64:n,UFix64:n,Fix64:n,String:o,Character:o,Bool:o,Address:o,Void:c,Optional:i,Reference:s,Array:u,Dictionary:d,Event:a,Resource:a,Struct:a},l=function(e,r){var t=Object.keys(e).find(function(e){return/^\/.*\/$/.test(e)?new RegExp(e.substring(1,e.length-1)).test(r):e===r});return r&&t&&e[t]},m=function(e,r,t){try{var n=l(r,e.type);if(!n)throw new Error("Undefined Decoder Error: "+e.type+"@"+t.join("."));return Promise.resolve(n(e.value,r,t))}catch(e){return Promise.reject(e)}},v=function(e,r,n){void 0===r&&(r={}),void 0===n&&(n=[]);var o=t({},f,{},r);return Promise.resolve(m(e,o,n))};e.decode=v,e.decodeArray=u,e.decodeComposite=a,e.decodeDictionary=d,e.decodeImplicit=o,e.decodeNumber=n,e.decodeOptional=i,e.decodeReference=s,e.decodeResponse=function(e,n){void 0===n&&(n={});try{var o=t({},f,{},n),c=r.bytesToString(r.bytes(e.encodedData)),i=JSON.parse(c);return Promise.resolve(v(i,o))}catch(e){return Promise.reject(e)}},e.decodeVoid=c,e.decoderLookup=l,e.defaultDecoders=f,e.recurseDecode=m}); | ||
//# sourceMappingURL=decode.umd.js.map |
{ | ||
"name": "@onflow/decode", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Flow SDK Decoder", | ||
@@ -21,5 +21,5 @@ "license": "Apache-2.0", | ||
"devDependencies": { | ||
"jest": "25.1.0", | ||
"jest": "25.3.0", | ||
"jest-esm-transformer": "1.0.0", | ||
"microbundle": "0.11.0" | ||
"microbundle": "0.12.0-next.8" | ||
}, | ||
@@ -32,3 +32,3 @@ "source": "src/decode.js", | ||
"prepublishOnly": "npm test && npm run build", | ||
"test": "jest", | ||
"test": "jest --verbose", | ||
"build": "microbundle", | ||
@@ -38,3 +38,5 @@ "test:watch": "jest --watch", | ||
}, | ||
"dependencies": {} | ||
"dependencies": { | ||
"@onflow/bytes": "0.0.1" | ||
} | ||
} |
@@ -1,2 +0,4 @@ | ||
export const decodeNumber = (num, _, stack) => { | ||
import {bytes, bytesToString} from "@onflow/bytes" | ||
export const decodeNumber = async (num, _, stack) => { | ||
try { | ||
@@ -9,41 +11,54 @@ return Number(num) | ||
export const decodeImplicit = i => i | ||
export const decodeImplicit = async (i) => i | ||
export const decodeVoid = () => null | ||
export const decodeVoid = async () => null | ||
export const decodeOptional = (optional, decoders, stack) => | ||
optional ? recurseDecode(optional, decoders, stack) : null | ||
export const decodeOptional = async (optional, decoders, stack) => | ||
optional ? await recurseDecode(optional, decoders, stack) : null | ||
export const decodeReference = v => ({address: v.address, type: v.type}) | ||
export const decodeReference = async (v) => ({address: v.address, type: v.type}) | ||
export const decodeArray = (array, decoders, stack) => | ||
array.map(v => recurseDecode(v, decoders, [...stack, v.type])) | ||
export const decodeArray = async (array, decoders, stack) => | ||
await Promise.all( | ||
array.map( | ||
(v) => | ||
new Promise(async (res) => | ||
res(await recurseDecode(v, decoders, [...stack, v.type])) | ||
) | ||
) | ||
) | ||
export const decodeDictionary = (dictionary, decoders, stack) => | ||
dictionary.reduce((acc, v) => { | ||
export const decodeDictionary = async (dictionary, decoders, stack) => | ||
await dictionary.reduce(async (acc, v) => { | ||
acc = await acc | ||
acc[ | ||
recurseDecode(v.key, decoders, [...stack, v.key]) | ||
] = recurseDecode(v.value, decoders, [...stack, v.key]) | ||
}, {}) | ||
await recurseDecode(v.key, decoders, [...stack, v.key]) | ||
] = await recurseDecode(v.value, decoders, [...stack, v.key]) | ||
return acc | ||
}, Promise.resolve({})) | ||
export const decodeComposite = (composite, decoders, stack) => | ||
composite.fields.reduce((acc, v) => { | ||
acc[v.name] = recurseDecode(v.value, decoders, [...stack, v.name]) | ||
export const decodeComposite = async (composite, decoders, stack) => { | ||
const decoded = await composite.fields.reduce(async (acc, v) => { | ||
acc = await acc | ||
acc[v.name] = await recurseDecode(v.value, decoders, [...stack, v.name]) | ||
return acc | ||
}, {}) | ||
}, Promise.resolve({})) | ||
const decoder = composite.id && decoderLookup(decoders, composite.id) | ||
return decoder ? await decoder(decoded) : decoded | ||
} | ||
export const defaultDecoders = { | ||
"[U]Int": decodeNumber, | ||
UInt: decodeNumber, | ||
Int: decodeNumber, | ||
"[U]Int8": decodeNumber, | ||
UInt8: decodeNumber, | ||
Int8: decodeNumber, | ||
"[U]Int16": decodeNumber, | ||
UInt16: decodeNumber, | ||
Int16: decodeNumber, | ||
"[U]Int32": decodeNumber, | ||
UInt32: decodeNumber, | ||
Int32: decodeNumber, | ||
"[U]Int64": decodeNumber, | ||
UInt64: decodeNumber, | ||
Int64: decodeNumber, | ||
"[U]Int128": decodeNumber, | ||
UInt128: decodeNumber, | ||
Int128: decodeNumber, | ||
"[U]Int256": decodeNumber, | ||
UInt256: decodeNumber, | ||
Int256: decodeNumber, | ||
@@ -54,3 +69,3 @@ Word8: decodeNumber, | ||
Word64: decodeNumber, | ||
"[U]Fix64": decodeNumber, | ||
UFix64: decodeNumber, | ||
Fix64: decodeNumber, | ||
@@ -71,4 +86,15 @@ String: decodeImplicit, | ||
export const recurseDecode = (decodeInstructions, decoders, stack) => { | ||
let decoder = decoders[decodeInstructions.type] | ||
export const decoderLookup = (decoders, lookup) => { | ||
const found = Object.keys(decoders).find(decoder => { | ||
if (/^\/.*\/$/.test(decoder)) { | ||
const reg = new RegExp(decoder.substring(1, decoder.length - 1)) | ||
return reg.test(lookup) | ||
} | ||
return decoder === lookup | ||
}) | ||
return lookup && found && decoders[found] | ||
} | ||
export const recurseDecode = async (decodeInstructions, decoders, stack) => { | ||
let decoder = decoderLookup(decoders, decodeInstructions.type) | ||
if (!decoder) | ||
@@ -78,15 +104,22 @@ throw new Error( | ||
) | ||
return decoder(decodeInstructions.value, decoders, stack) | ||
return await decoder(decodeInstructions.value, decoders, stack) | ||
} | ||
// TODO: Implement correctly once the JSON encoding is returned from the access API. | ||
export const decode = (decodeInstructions, customDecoders = {}, stack = []) => { | ||
export const decode = async ( | ||
decodeInstructions, | ||
customDecoders = {}, | ||
stack = [] | ||
) => { | ||
let decoders = {...defaultDecoders, ...customDecoders} | ||
return recurseDecode(decodeInstructions, decoders, stack) | ||
return await recurseDecode(decodeInstructions, decoders, stack) | ||
} | ||
// TODO: Implement correctly once the JSON encoding is returned form the access API. | ||
export const decodeResponse = (response, customDecoders = {}) => { | ||
export const decodeResponse = async (response, customDecoders = {}) => { | ||
let decoders = {...defaultDecoders, ...customDecoders} | ||
return decode(response.payload, decoders) | ||
const encoded = response.encodedData | ||
const decodeInstructions = bytesToString(bytes(encoded)) | ||
const decodeInstructionsJson = JSON.parse(decodeInstructions) | ||
return await decode(decodeInstructionsJson, decoders) | ||
} |
@@ -1,10 +0,472 @@ | ||
import assert from "assert" | ||
import {decode} from "./decode" | ||
describe("decode", () => { | ||
it("returns the correct reponse given a json-cdc payload", () => { | ||
describe("unit tests to cover all types", () => { | ||
it("returns the correct response given a json-cdc payload 2 OPTIONAL", async () => { | ||
const payload = {type: "Optional", value: null} | ||
const decoded = await decode(payload) | ||
expect(decoded).toStrictEqual(null) | ||
}) | ||
it("returns the correct response given a json-cdc payload 3 OPTIONAL", async () => { | ||
const payload = {type: "Optional", value: {type: "Int", value: "42"}} | ||
const decoded = await decode(payload) | ||
expect(decoded).toStrictEqual(42) | ||
}) | ||
it("returns the correct response given a json-cdc payload 4 VOID", async () => { | ||
const payload = {type: "Void"} | ||
const decoded = await decode(payload) | ||
expect(decoded).toStrictEqual(null) | ||
}) | ||
it("returns the correct response given a json-cdc payload 5 BOOLEAN", async () => { | ||
const payload = {type: "Bool", value: true} | ||
const decoded = await decode(payload) | ||
expect(decoded).toStrictEqual(true) | ||
}) | ||
it("returns the correct response given a json-cdc payload 6 BOOLEAN", async () => { | ||
const payload = {type: "Bool", value: false} | ||
const decoded = await decode(payload) | ||
expect(decoded).toStrictEqual(false) | ||
}) | ||
it("returns the correct response given a json-cdc payload 7 STRING", async () => { | ||
const payload = {type: "String", value: ""} | ||
const decoded = await decode(payload) | ||
expect(decoded).toStrictEqual("") | ||
}) | ||
it("returns the correct response given a json-cdc payload 8 STRING", async () => { | ||
const payload = {type: "String", value: "foo"} | ||
const decoded = await decode(payload) | ||
expect(decoded).toStrictEqual("foo") | ||
}) | ||
it("returns the correct response given a json-cdc payload 9 ADDRESS", async () => { | ||
const payload = { | ||
type: "Address", | ||
value: "0x0102030405000000000000000000000000000000", | ||
} | ||
const decoded = await decode(payload) | ||
expect(decoded).toStrictEqual("0x0102030405000000000000000000000000000000") | ||
}) | ||
it("returns the correct response given a json-cdc payload 10 ADDRESS", async () => { | ||
const payload = { | ||
type: "Address", | ||
value: "0x0102030405000000000000000000000000000000", | ||
} | ||
const decoded = await decode(payload) | ||
expect(decoded).toStrictEqual("0x0102030405000000000000000000000000000000") | ||
}) | ||
it("returns the correct response given a json-cdc payload 10 INT", async () => { | ||
const payload = {type: "Int", value: "-42"} | ||
const decoded = await decode(payload) | ||
expect(decoded).toStrictEqual(-42) | ||
}) | ||
it("returns the correct response given a json-cdc payload 11 INT", async () => { | ||
const payload = { | ||
type: "Int", | ||
value: | ||
"115792089237316195423570985008687907853269984665640564039457584007913129639945", | ||
} | ||
const decoded = await decode(payload) | ||
expect(decoded).toStrictEqual(115792089237316195423570985008687907853269984665640564039457584007913129639945) | ||
}) | ||
it("returns the correct response given a json-cdc payload 12 INT", async () => { | ||
const payload = { | ||
type: "Int", | ||
value: | ||
"-57896044618658097711785492504343953926634992332820282019728792003956564819978", | ||
} | ||
const decoded = await decode(payload) | ||
expect(decoded).toStrictEqual(-57896044618658097711785492504343953926634992332820282019728792003956564819978) | ||
}) | ||
it("returns the correct response given a json-cdc payload 13 INT", async () => { | ||
const payload = {type: "Int", value: "0"} | ||
const decoded = await decode(payload) | ||
expect(decoded).toStrictEqual(0) | ||
}) | ||
it("returns the correct response given a json-cdc payload 14 INT8", async () => { | ||
const payload = {type: "Int8", value: "-128"} | ||
const decoded = await decode(payload) | ||
expect(decoded).toStrictEqual(-128) | ||
}) | ||
it("returns the correct response given a json-cdc payload 15 INT16", async () => { | ||
const payload = {type: "Int16", value: "32767"} | ||
const decoded = await decode(payload) | ||
expect(decoded).toStrictEqual(32767) | ||
}) | ||
it("returns the correct response given a json-cdc payload 16 INT32", async () => { | ||
const payload = {type: "Int32", value: "2147483647"} | ||
const decoded = await decode(payload) | ||
expect(decoded).toStrictEqual(2147483647) | ||
}) | ||
it("returns the correct response given a json-cdc payload 17 INT64", async () => { | ||
const payload = {type: "Int64", value: "-9223372036854775808"} | ||
const decoded = await decode(payload) | ||
expect(decoded).toStrictEqual(-9223372036854775808) | ||
}) | ||
it("returns the correct response given a json-cdc payload 18 INT128", async () => { | ||
const payload = { | ||
type: "Int128", | ||
value: "-170141183460469231731687303715884105727", | ||
} | ||
const decoded = await decode(payload) | ||
expect(decoded).toStrictEqual(-170141183460469231731687303715884105727) | ||
}) | ||
it("returns the correct response given a json-cdc payload 19 INT256", async () => { | ||
const payload = { | ||
type: "Int256", | ||
value: | ||
"57896044618658097711785492504343953926634992332820282019728792003956564819967", | ||
} | ||
const decoded = await decode(payload) | ||
expect(decoded).toStrictEqual(57896044618658097711785492504343953926634992332820282019728792003956564819967) | ||
}) | ||
it("returns the correct response given a json-cdc payload 20 UINT", async () => { | ||
const payload = { | ||
type: "UInt", | ||
value: | ||
"115792089237316195423570985008687907853269984665640564039457584007913129639945", | ||
} | ||
const decoded = await decode(payload) | ||
expect(decoded).toStrictEqual(115792089237316195423570985008687907853269984665640564039457584007913129639945) | ||
}) | ||
it("returns the correct response given a json-cdc payload 21 UINT8", async () => { | ||
const payload = {type: "UInt8", value: "255"} | ||
const decoded = await decode(payload) | ||
expect(decoded).toStrictEqual(255) | ||
}) | ||
it("returns the correct response given a json-cdc payload 22 UINT16", async () => { | ||
const payload = {type: "UInt16", value: "65535"} | ||
const decoded = await decode(payload) | ||
expect(decoded).toStrictEqual(65535) | ||
}) | ||
it("returns the correct response given a json-cdc payload 23 UINT32", async () => { | ||
const payload = {type: "UInt32", value: "4294967295"} | ||
const decoded = await decode(payload) | ||
expect(decoded).toStrictEqual(4294967295) | ||
}) | ||
it("returns the correct response given a json-cdc payload 24 UINT64", async () => { | ||
const payload = {type: "UInt64", value: "18446744073709551615"} | ||
const decoded = await decode(payload) | ||
expect(decoded).toStrictEqual(18446744073709551615) | ||
}) | ||
it("returns the correct response given a json-cdc payload 25 UINT128", async () => { | ||
const payload = { | ||
type: "UInt128", | ||
value: "340282366920938463463374607431768211455", | ||
} | ||
const decoded = await decode(payload) | ||
expect(decoded).toStrictEqual(340282366920938463463374607431768211455) | ||
}) | ||
it("returns the correct response given a json-cdc payload 26 UINT256", async () => { | ||
const payload = { | ||
type: "UInt256", | ||
value: | ||
"115792089237316195423570985008687907853269984665640564039457584007913129639935", | ||
} | ||
const decoded = await decode(payload) | ||
expect(decoded).toStrictEqual(115792089237316195423570985008687907853269984665640564039457584007913129639935) | ||
}) | ||
it("returns the correct response given a json-cdc payload 27 WORD8", async () => { | ||
const payload = {type: "Word8", value: "255"} | ||
const decoded = await decode(payload) | ||
expect(decoded).toStrictEqual(255) | ||
}) | ||
it("returns the correct response given a json-cdc payload 28 WORD16", async () => { | ||
const payload = {type: "Word16", value: "65535"} | ||
const decoded = await decode(payload) | ||
expect(decoded).toStrictEqual(65535) | ||
}) | ||
it("returns the correct response given a json-cdc payload 29 WORD32", async () => { | ||
const payload = {type: "Word32", value: "4294967295"} | ||
const decoded = await decode(payload) | ||
expect(decoded).toStrictEqual(4294967295) | ||
}) | ||
it("returns the correct response given a json-cdc payload 30 WORD64", async () => { | ||
const payload = {type: "Word64", value: "18446744073709551615"} | ||
const decoded = await decode(payload) | ||
expect(decoded).toStrictEqual(18446744073709551615) | ||
}) | ||
it("returns the correct response given a json-cdc payload 31 FIX64", async () => { | ||
const payload = {type: "Fix64", value: "789.00123010"} | ||
const decoded = await decode(payload) | ||
expect(decoded).toStrictEqual(789.0012301) | ||
}) | ||
it("returns the correct response given a json-cdc payload 32 FIX64", async () => { | ||
const payload = {type: "Fix64", value: "-12345.00678900"} | ||
const decoded = await decode(payload) | ||
expect(decoded).toStrictEqual(-12345.006789) | ||
}) | ||
it("returns the correct response given a json-cdc payload 33 UFIX64", async () => { | ||
const payload = {type: "UFix64", value: "789.00123010"} | ||
const decoded = await decode(payload) | ||
expect(decoded).toStrictEqual(789.0012301) | ||
}) | ||
it("returns the correct response given a json-cdc payload 34 ARRAY", async () => { | ||
const payload = { | ||
type: "Array", | ||
value: [ | ||
{type: "Int", value: "1"}, | ||
{type: "Int", value: "2"}, | ||
{type: "Int", value: "3"}, | ||
], | ||
} | ||
const decoded = await decode(payload) | ||
expect(decoded).toStrictEqual([1,2,3]) | ||
}) | ||
it("returns the correct response given a json-cdc payload 35 ARRAY", async () => { | ||
const payload = { | ||
type: "Array", | ||
value: [ | ||
{ | ||
type: "Resource", | ||
value: { | ||
id: "test.Foo", | ||
fields: [{name: "bar", value: {type: "Int", value: "1"}}], | ||
}, | ||
}, | ||
{ | ||
type: "Resource", | ||
value: { | ||
id: "test.Foo", | ||
fields: [{name: "bar", value: {type: "Int", value: "2"}}], | ||
}, | ||
}, | ||
{ | ||
type: "Resource", | ||
value: { | ||
id: "test.Foo", | ||
fields: [{name: "bar", value: {type: "Int", value: "3"}}], | ||
}, | ||
}, | ||
], | ||
} | ||
const decoded = await decode(payload) | ||
expect(decoded).toStrictEqual([{bar: 1}, {bar: 2}, {bar: 3}]) | ||
}) | ||
it("returns the correct response given a json-cdc payload 36 DICTIONARY", async () => { | ||
const payload = { | ||
type: "Dictionary", | ||
value: [ | ||
{key: {type: "String", value: "a"}, value: {type: "Int", value: "1"}}, | ||
{key: {type: "String", value: "b"}, value: {type: "Int", value: "2"}}, | ||
{key: {type: "String", value: "c"}, value: {type: "Int", value: "3"}}, | ||
], | ||
} | ||
const decoded = await decode(payload) | ||
expect(decoded).toStrictEqual({a: 1, b: 2, c: 3}) | ||
}) | ||
it("returns the correct response given a json-cdc payload 37 DICTIONARY", async () => { | ||
const payload = { | ||
type: "Dictionary", | ||
value: [ | ||
{ | ||
key: {type: "String", value: "a"}, | ||
value: { | ||
type: "Dictionary", | ||
value: [ | ||
{ | ||
key: {type: "String", value: "1"}, | ||
value: {type: "Int", value: "1"}, | ||
}, | ||
], | ||
}, | ||
}, | ||
{ | ||
key: {type: "String", value: "b"}, | ||
value: { | ||
type: "Dictionary", | ||
value: [ | ||
{ | ||
key: {type: "String", value: "2"}, | ||
value: {type: "Int", value: "2"}, | ||
}, | ||
], | ||
}, | ||
}, | ||
{ | ||
key: {type: "String", value: "c"}, | ||
value: { | ||
type: "Dictionary", | ||
value: [ | ||
{ | ||
key: {type: "String", value: "3"}, | ||
value: {type: "Int", value: "3"}, | ||
}, | ||
], | ||
}, | ||
}, | ||
], | ||
} | ||
const decoded = await decode(payload) | ||
expect(decoded).toStrictEqual({a: {1: 1}, b: {2: 2}, c: {3: 3}}) | ||
}) | ||
it("returns the correct response given a json-cdc payload 38 DICTIONARY", async () => { | ||
const payload = { | ||
type: "Dictionary", | ||
value: [ | ||
{ | ||
key: {type: "String", value: "a"}, | ||
value: { | ||
type: "Resource", | ||
value: { | ||
id: "test.Foo", | ||
fields: [{name: "bar", value: {type: "Int", value: "1"}}], | ||
}, | ||
}, | ||
}, | ||
{ | ||
key: {type: "String", value: "b"}, | ||
value: { | ||
type: "Resource", | ||
value: { | ||
id: "test.Foo", | ||
fields: [{name: "bar", value: {type: "Int", value: "2"}}], | ||
}, | ||
}, | ||
}, | ||
{ | ||
key: {type: "String", value: "c"}, | ||
value: { | ||
type: "Resource", | ||
value: { | ||
id: "test.Foo", | ||
fields: [{name: "bar", value: {type: "Int", value: "3"}}], | ||
}, | ||
}, | ||
}, | ||
], | ||
} | ||
const decoded = await decode(payload) | ||
expect(decoded).toStrictEqual({a: {bar: 1}, b: {bar: 2}, c: {bar: 3}}) | ||
}) | ||
it("returns the correct response given a json-cdc payload 39 RESOURCE", async () => { | ||
const payload = { | ||
type: "Resource", | ||
value: { | ||
id: "test.Foo", | ||
fields: [{name: "bar", value: {type: "Int", value: "42"}}], | ||
}, | ||
} | ||
const decoded = await decode(payload) | ||
expect(decoded).toStrictEqual({bar: 42}) | ||
}) | ||
it("returns the correct response given a json-cdc payload 40 RESOURCE", async () => { | ||
const payload = { | ||
type: "Resource", | ||
value: { | ||
id: "test.Foo", | ||
fields: [ | ||
@@ -17,11 +479,3 @@ { | ||
id: "test.Bar", | ||
fields: [ | ||
{ | ||
name: "x", | ||
value: { | ||
type: "Int", | ||
value: "42", | ||
}, | ||
}, | ||
], | ||
fields: [{name: "x", value: {type: "Int", value: "42"}}], | ||
}, | ||
@@ -34,10 +488,608 @@ }, | ||
const decoded = decode(payload) | ||
const decoded = await decode(payload) | ||
assert.deepEqual(decoded, { | ||
bar: { | ||
x: 42, | ||
expect(decoded).toStrictEqual({bar: {x: 42}}) | ||
}) | ||
it("returns the correct response given a json-cdc payload 41 STRUCT", async () => { | ||
const payload = { | ||
type: "Struct", | ||
value: { | ||
id: "test.FooStruct", | ||
fields: [ | ||
{name: "a", value: {type: "Int", value: "1"}}, | ||
{name: "b", value: {type: "String", value: "foo"}}, | ||
], | ||
}, | ||
} | ||
const decoded = await decode(payload) | ||
expect(decoded).toStrictEqual({a: 1, b: "foo"}) | ||
}) | ||
it("returns the correct response given a json-cdc payload 42 STRUCT", async () => { | ||
const payload = { | ||
type: "Struct", | ||
value: { | ||
id: "test.FooStruct", | ||
fields: [ | ||
{name: "a", value: {type: "String", value: "foo"}}, | ||
{ | ||
name: "b", | ||
value: { | ||
type: "Resource", | ||
value: { | ||
id: "test.Foo", | ||
fields: [{name: "bar", value: {type: "Int", value: "42"}}], | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
} | ||
const decoded = await decode(payload) | ||
expect(decoded).toStrictEqual({a: "foo", b: {bar: 42}}) | ||
}) | ||
it("returns the correct response given a json-cdc payload 43 STRUCT", async () => { | ||
const payload = { | ||
type: "Struct", | ||
value: { | ||
id: "test.FooStruct", | ||
fields: [ | ||
{name: "a", value: {type: "String", value: "foo"}}, | ||
{ | ||
name: "b", | ||
value: { | ||
type: "Resource", | ||
value: { | ||
id: "test.Foo", | ||
fields: [{name: "bar", value: {type: "Int", value: "42"}}], | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
} | ||
const decoded = await decode(payload) | ||
expect(decoded).toStrictEqual({a: "foo", b: {bar: 42}}) | ||
}) | ||
it("returns the correct response given a json-cdc payload 44 EVENT", async () => { | ||
const payload = { | ||
type: "Event", | ||
value: { | ||
id: "test.FooEvent", | ||
fields: [ | ||
{name: "a", value: {type: "Int", value: "1"}}, | ||
{name: "b", value: {type: "String", value: "foo"}}, | ||
], | ||
}, | ||
} | ||
const decoded = await decode(payload) | ||
expect(decoded).toStrictEqual({a: 1, b: "foo"}) | ||
}) | ||
it("returns the correct response given a json-cdc payload 44 EVENT", async () => { | ||
const payload = { | ||
type: "Event", | ||
value: { | ||
id: "test.FooEvent", | ||
fields: [ | ||
{name: "a", value: {type: "String", value: "foo"}}, | ||
{ | ||
name: "b", | ||
value: { | ||
type: "Resource", | ||
value: { | ||
id: "test.Foo", | ||
fields: [{name: "bar", value: {type: "Int", value: "42"}}], | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
} | ||
const decoded = await decode(payload) | ||
expect(decoded).toStrictEqual({a: "foo", b: {bar: 42}}) | ||
}) | ||
}) | ||
// Boolean | ||
const genBool = () => { | ||
const OPTIONS = [true, false] | ||
const value = OPTIONS[~~(Math.random() * OPTIONS.length)] | ||
return {payload: {type: "Bool", value}, decoded: value} | ||
} | ||
const genBoolSpec = () => { | ||
const {payload, decoded} = genBool() | ||
return { | ||
label: `Boolean -- ${payload.value}`, | ||
payload, | ||
decoded, | ||
} | ||
} | ||
// Void | ||
const genVoid = () => { | ||
return { | ||
payload: {type: "Void"}, | ||
decoded: null, | ||
} | ||
} | ||
const genVoidSpec = () => { | ||
const {payload, decoded} = genVoid() | ||
return { | ||
label: `Void`, | ||
payload, | ||
decoded, | ||
} | ||
} | ||
// Int | ||
const genInt = () => { | ||
const minInt256 = -57896044618658097711785492504343953926634992332820282019728792003956564819978 | ||
const maxUInt256 = 115792089237316195423570985008687907853269984665640564039457584007913129639945 | ||
const ranInt = ~~(Math.random() * (maxUInt256 - minInt256) + minInt256) | ||
return { | ||
payload: {type: "Int", value: ranInt.toString()}, | ||
decoded: ranInt, | ||
} | ||
} | ||
const genIntSpec = () => { | ||
const {payload, decoded} = genInt() | ||
return { | ||
label: `Int -- ${decoded}`, | ||
payload, | ||
decoded, | ||
} | ||
} | ||
// String | ||
const genString = () => { | ||
const stringLen = ~~(100 * Math.random()) | ||
const ranString = Math.random() | ||
.toString(36) | ||
.replace(/[^a-z]+/g, "") | ||
.substr(0, stringLen) | ||
return { | ||
payload: {type: "String", value: ranString}, | ||
decoded: ranString, | ||
} | ||
} | ||
const genStringSpec = () => { | ||
const {payload, decoded} = genString() | ||
return { | ||
label: `String -- ${decoded}`, | ||
payload, | ||
decoded, | ||
} | ||
} | ||
// Dictionary | ||
const genDictionary = (depth = 0) => { | ||
const MAXDEPTH = 5 | ||
if (depth >= MAXDEPTH) { | ||
return { | ||
payload: {type: "Dictionary", value: []}, | ||
decoded: {}, | ||
} | ||
} | ||
const OPTIONS = [ | ||
genString, | ||
genInt, | ||
genBool, | ||
genVoid, | ||
() => genDictionary(depth++), | ||
() => genResource(depth++), | ||
() => genStruct(depth++), | ||
() => genEvent(depth++), | ||
() => genArray(depth++), | ||
] | ||
const dictionaryLength = ~~(Math.random() * 10) | ||
const arr = Array.from({length: dictionaryLength}).reduce( | ||
(acc) => { | ||
const {payload: valPayload, decoded: val} = OPTIONS[ | ||
~~(Math.random() * OPTIONS.length) | ||
]() | ||
const {payload: keyPayload, decoded: ranStringKey} = genString() | ||
acc.dict.push({ | ||
key: keyPayload, | ||
value: valPayload, | ||
}) | ||
acc.decoded = { | ||
...acc.decoded, | ||
[ranStringKey]: val, | ||
} | ||
return acc | ||
}, | ||
{dict: [], decoded: {}} | ||
) | ||
return { | ||
payload: {type: "Dictionary", value: arr.dict}, | ||
decoded: arr.decoded, | ||
} | ||
} | ||
const genDictionarySpec = () => { | ||
const {payload, decoded} = genDictionary() | ||
return { | ||
label: `Dictionary`, | ||
payload, | ||
decoded, | ||
} | ||
} | ||
const genResource = (depth = 0) => { | ||
const MAXDEPTH = 5 | ||
if (depth >= MAXDEPTH) { | ||
return { | ||
payload: {type: "Resource", value: {fields: []}}, | ||
decoded: {}, | ||
} | ||
} | ||
const OPTIONS = [ | ||
genString, | ||
genInt, | ||
genBool, | ||
genVoid, | ||
() => genDictionary(depth++), | ||
() => genResource(depth++), | ||
() => genStruct(depth++), | ||
() => genEvent(depth++), | ||
() => genArray(depth++), | ||
] | ||
const fieldsLength = ~~(Math.random() * 10) | ||
const res = Array.from({length: fieldsLength}).reduce( | ||
(acc) => { | ||
const {payload: valPayload, decoded: val} = OPTIONS[ | ||
~~(Math.random() * OPTIONS.length) | ||
]() | ||
const {decoded: ranStringName} = genString() | ||
acc.fields.push({ | ||
name: ranStringName, | ||
value: valPayload, | ||
}) | ||
acc.decoded = { | ||
...acc.decoded, | ||
[ranStringName]: val, | ||
} | ||
return acc | ||
}, | ||
{fields: [], decoded: {}} | ||
) | ||
return { | ||
payload: {type: "Resource", value: {fields: res.fields}}, | ||
decoded: res.decoded, | ||
} | ||
} | ||
const genResourceSpec = () => { | ||
const {payload, decoded} = genResource() | ||
return { | ||
label: `Resource`, | ||
payload, | ||
decoded, | ||
} | ||
} | ||
const genStruct = (depth = 0) => { | ||
const MAXDEPTH = 5 | ||
if (depth >= MAXDEPTH) { | ||
return { | ||
payload: {type: "Struct", value: {fields: []}}, | ||
decoded: {}, | ||
} | ||
} | ||
const OPTIONS = [ | ||
genString, | ||
genInt, | ||
genBool, | ||
genVoid, | ||
() => genDictionary(depth++), | ||
() => genResource(depth++), | ||
() => genStruct(depth++), | ||
() => genEvent(depth++), | ||
() => genArray(depth++), | ||
] | ||
const fieldsLength = ~~(Math.random() * 10) | ||
const res = Array.from({length: fieldsLength}).reduce( | ||
(acc) => { | ||
const {payload: valPayload, decoded: val} = OPTIONS[ | ||
~~(Math.random() * OPTIONS.length) | ||
]() | ||
const {decoded: ranStringName} = genString() | ||
acc.fields.push({ | ||
name: ranStringName, | ||
value: valPayload, | ||
}) | ||
acc.decoded = { | ||
...acc.decoded, | ||
[ranStringName]: val, | ||
} | ||
return acc | ||
}, | ||
{fields: [], decoded: {}} | ||
) | ||
return { | ||
payload: {type: "Struct", value: {fields: res.fields}}, | ||
decoded: res.decoded, | ||
} | ||
} | ||
const genStructSpec = () => { | ||
const {payload, decoded} = genStruct() | ||
return { | ||
label: `Struct`, | ||
payload, | ||
decoded, | ||
} | ||
} | ||
const genEvent = (depth = 0) => { | ||
const MAXDEPTH = 5 | ||
if (depth >= MAXDEPTH) { | ||
return { | ||
payload: {type: "Event", value: {fields: []}}, | ||
decoded: {}, | ||
} | ||
} | ||
const OPTIONS = [ | ||
genString, | ||
genInt, | ||
genBool, | ||
genVoid, | ||
() => genDictionary(depth++), | ||
() => genResource(depth++), | ||
() => genStruct(depth++), | ||
() => genEvent(depth++), | ||
() => genArray(depth++), | ||
] | ||
const fieldsLength = ~~(Math.random() * 10) | ||
const res = Array.from({length: fieldsLength}).reduce( | ||
(acc) => { | ||
const {payload: valPayload, decoded: val} = OPTIONS[ | ||
~~(Math.random() * OPTIONS.length) | ||
]() | ||
const {decoded: ranStringName} = genString() | ||
acc.fields.push({ | ||
name: ranStringName, | ||
value: valPayload, | ||
}) | ||
acc.decoded = { | ||
...acc.decoded, | ||
[ranStringName]: val, | ||
} | ||
return acc | ||
}, | ||
{fields: [], decoded: {}} | ||
) | ||
return { | ||
payload: {type: "Event", value: {fields: res.fields}}, | ||
decoded: res.decoded, | ||
} | ||
} | ||
const genEventSpec = () => { | ||
const {payload, decoded} = genEvent() | ||
return { | ||
label: `Event`, | ||
payload, | ||
decoded, | ||
} | ||
} | ||
const genArray = (depth = 0) => { | ||
const MAXDEPTH = 5 | ||
if (depth >= MAXDEPTH) { | ||
return { | ||
payload: {type: "Array", value: []}, | ||
decoded: [], | ||
} | ||
} | ||
const OPTIONS = [ | ||
genString, | ||
genInt, | ||
genBool, | ||
genVoid, | ||
() => genDictionary(depth++), | ||
() => genResource(depth++), | ||
() => genStruct(depth++), | ||
() => genEvent(depth++), | ||
() => genArray(depth++), | ||
] | ||
const fieldsLength = ~~(Math.random() * 10) | ||
const arr = Array.from({length: fieldsLength}).reduce( | ||
(acc) => { | ||
const {payload, decoded} = OPTIONS[~~(Math.random() * OPTIONS.length)]() | ||
acc.values.push(payload) | ||
acc.decoded.push(decoded) | ||
return acc | ||
}, | ||
{values: [], decoded: []} | ||
) | ||
return { | ||
payload: {type: "Array", value: arr.values}, | ||
decoded: arr.decoded, | ||
} | ||
} | ||
const genArraySpec = () => { | ||
const {payload, decoded} = genArray() | ||
return { | ||
label: `Array`, | ||
payload, | ||
decoded, | ||
} | ||
} | ||
const times = (fn) => { | ||
const OPTS = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | ||
return Array.from({length: OPTS[~~(Math.random() * OPTS.length)]}, () => fn) | ||
} | ||
describe("generative tests", () => { | ||
;[ | ||
{ | ||
label: "optional", | ||
payload: {type: "Optional", value: null}, | ||
decoded: null, | ||
}, | ||
...times(genBoolSpec), | ||
...times(genVoidSpec), | ||
...times(genIntSpec), | ||
...times(genStringSpec), | ||
...times(genDictionarySpec), | ||
...times(genResourceSpec), | ||
...times(genStructSpec), | ||
...times(genEventSpec), | ||
...times(genArraySpec), | ||
] | ||
.filter((d) => d != null) | ||
.map((d) => { | ||
return typeof d === "function" ? d() : d | ||
}) | ||
.forEach(({label, payload, decoded, custom}) => { | ||
it(label, async () => | ||
expect(await decode(payload, custom || undefined)).toStrictEqual(decoded) | ||
) | ||
}) | ||
}) | ||
describe("custom decoder tests", () => { | ||
it("decodes using a custom decoder correctly", async () => { | ||
const resource = { | ||
type: "Resource", | ||
value: { | ||
id: "test.Foo", | ||
fields: [{name: "bar", value: {type: "Int", value: "1"}}], | ||
}, | ||
} | ||
const fooDecoder = async (resource) => ({ | ||
hello: "world", | ||
}) | ||
const decoded = await decode(resource, {"test.Foo": fooDecoder}) | ||
expect(decoded).toStrictEqual({ | ||
hello: "world", | ||
}) | ||
}) | ||
it("decodes using a custom nested decoder correctly", async () => { | ||
const resource = { | ||
type: "Resource", | ||
value: { | ||
id: "test.Jeff", | ||
fields: [ | ||
{name: "firstName", value: {type: "String", value: "Jeff"}}, | ||
{name: "lastName", value: {type: "String", value: "Doyle"}} | ||
] | ||
} | ||
} | ||
const Jeff = function (resource) { | ||
if (!(this instanceof Jeff)) return new Jeff(resource) | ||
this.firstName = resource.firstName | ||
this.lastName = resource.lastName | ||
this.printName = () => `${this.firstName} ${this.lastName}` | ||
} | ||
const jeffDecoder = async (resource) => { | ||
return Jeff(resource) | ||
} | ||
const decoded = await decode(resource, { "test.Jeff": jeffDecoder }, []) | ||
expect(decoded.printName()).toStrictEqual("Jeff Doyle") | ||
}) | ||
it("decodes using a cusotm nested decoder correctly", async () => { | ||
const resource = { | ||
type: "Resource", | ||
value: { | ||
id: "test.CryptoKitty", | ||
fields: [ | ||
{name: "kittyName", value: {type: "String", value: "Sir Meowsers"}}, | ||
{name: "kittyHat", value: { | ||
type: "Resource", | ||
value: { | ||
id: "test.CryptoKittyHat", | ||
fields: [ | ||
{name: "kittyHatName", value: {type: "String", value: "Yankee With No Brim"}} | ||
] | ||
} | ||
}} | ||
] | ||
} | ||
} | ||
const kittyHatDecoder = async (kittyHat) => ({ | ||
name: kittyHat.kittyHatName | ||
}) | ||
const kittyDecoder = async (kitty) => ({ | ||
name: kitty.kittyName, | ||
hat: kitty.kittyHat | ||
}) | ||
const decoded = await decode(resource, { | ||
"/test.CryptoKitty$/": kittyDecoder, | ||
"/test.CryptoKittyHat$/": kittyHatDecoder, | ||
}) | ||
expect(decoded).toStrictEqual({ | ||
name: "Sir Meowsers", | ||
hat: { | ||
name: "Yankee With No Brim" | ||
} | ||
}) | ||
}) | ||
it("decodes using a custom decoder with regex lookup", async () => { | ||
const resource = { | ||
type: "Resource", | ||
value: { | ||
id: "test.CryptoKitty", | ||
fields: [ | ||
{name: "kittyName", value: {type: "String", value: "Sir Meowsers"}}, | ||
{name: "kittyHat", value: { | ||
type: "Resource", | ||
value: { | ||
id: "test.CryptoKittyHat", | ||
fields: [ | ||
{name: "kittyHatName", value: {type: "String", value: "Yankee With No Brim"}} | ||
] | ||
} | ||
}} | ||
] | ||
} | ||
} | ||
const kittyHatDecoder = async (kittyHat) => ({ | ||
name: kittyHat.kittyHatName | ||
}) | ||
const kittyDecoder = async (kitty) => ({ | ||
name: kitty.kittyName, | ||
hat: kitty.kittyHat | ||
}) | ||
const decoded = await decode(resource, { | ||
"/.CryptoKittyHat$/": kittyHatDecoder, | ||
"/.CryptoKitty$/": kittyDecoder, | ||
}) | ||
expect(decoded).toStrictEqual({ | ||
name: "Sir Meowsers", | ||
hat: { | ||
name: "Yankee With No Brim" | ||
} | ||
}) | ||
}) | ||
}) |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
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
71554
13
1084
1
3
1
+ Added@onflow/bytes@0.0.1
+ Added@onflow/bytes@0.0.1(transitive)