UNPKG

@taquito/michelson-encoder

Version:

converts michelson data and types into convenient JS/TS objects

95 lines (94 loc) 2.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ListToken = exports.ListValidationError = void 0; const token_1 = require("./token"); /** * @category Error * @description Error that indicates a failure happening when parsing encoding/executing a List */ class ListValidationError extends token_1.TokenValidationError { constructor(value, token, message) { super(value, token, message); this.value = value; this.token = token; this.name = 'ListValidationError'; } } exports.ListValidationError = ListValidationError; class ListToken extends token_1.Token { constructor(val, idx, fac) { super(val, idx, fac); this.val = val; this.idx = idx; this.fac = fac; } get valueSchema() { return this.createToken(this.val.args[0], this.idx); } /** * @throws {@link ListValidationError} */ validate(value) { if (!Array.isArray(value)) { throw new ListValidationError(value, this, `Value ${JSON.stringify(value)} is not a valid array`); } } /** * @throws {@link ListValidationError} */ Encode(args) { const val = args.pop(); this.validate(val); const schema = this.createToken(this.val.args[0], 0); return val.reduce((prev, current) => { return [...prev, schema.EncodeObject(current)]; }, []); } /** * @throws {@link ListValidationError} */ Execute(val, semantics) { const schema = this.createToken(this.val.args[0], 0); this.validate(val); return val.reduce((prev, current) => { return [...prev, schema.Execute(current, semantics)]; }, []); } /** * @throws {@link ListValidationError} */ EncodeObject(args, semantic) { const schema = this.createToken(this.val.args[0], 0); this.validate(args); if (semantic && semantic[ListToken.prim]) { return semantic[ListToken.prim](args); } return args.reduce((prev, current) => { return [...prev, schema.EncodeObject(current)]; }, []); } /** * @deprecated ExtractSchema has been deprecated in favor of generateSchema * */ ExtractSchema() { return { [ListToken.prim]: this.valueSchema.ExtractSchema(), }; } generateSchema() { return { __michelsonType: ListToken.prim, schema: this.valueSchema.generateSchema(), }; } findAndReturnTokens(tokenToFind, tokens) { if (ListToken.prim === tokenToFind) { tokens.push(this); } this.createToken(this.val.args[0], this.idx).findAndReturnTokens(tokenToFind, tokens); return tokens; } } exports.ListToken = ListToken; ListToken.prim = 'list';