@taquito/michelson-encoder
Version:
converts michelson data and types into convenient JS/TS objects
90 lines (89 loc) • 2.65 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SetToken = exports.SetValidationError = void 0;
const token_1 = require("./token");
/**
* @category Error
* @description Error that indicates a failure happening when parsing encoding/executing a Set
*/
class SetValidationError extends token_1.TokenValidationError {
constructor(value, token, message) {
super(value, token, message);
this.value = value;
this.token = token;
this.name = 'SetValidationError';
}
}
exports.SetValidationError = SetValidationError;
class SetToken extends token_1.Token {
constructor(val, idx, fac) {
super(val, idx, fac);
this.val = val;
this.idx = idx;
this.fac = fac;
}
get KeySchema() {
return this.createToken(this.val.args[0], 0);
}
/**
* @throws {@link SetValidationError}
*/
validate(value) {
if (!Array.isArray(value)) {
throw new SetValidationError(value, this, `Value ${JSON.stringify(value)} is not an array`);
}
}
/**
* @throws {@link SetValidationError}
*/
Encode(args) {
const val = args.pop();
this.validate(val);
return val
.sort((a, b) => this.KeySchema.compare(a, b))
.reduce((prev, current) => {
return [...prev, this.KeySchema.EncodeObject(current)];
}, []);
}
Execute(val, semantics) {
return val.reduce((prev, current) => {
return [...prev, this.KeySchema.Execute(current, semantics)];
}, []);
}
/**
* @throws {@link SetValidationError}
*/
EncodeObject(args, semantic) {
this.validate(args);
if (semantic && semantic[SetToken.prim]) {
return semantic[SetToken.prim](args);
}
return args
.sort((a, b) => this.KeySchema.compare(a, b))
.reduce((prev, current) => {
return [...prev, this.KeySchema.EncodeObject(current)];
}, []);
}
/**
* @deprecated ExtractSchema has been deprecated in favor of generateSchema
*
*/
ExtractSchema() {
return SetToken.prim;
}
generateSchema() {
return {
__michelsonType: SetToken.prim,
schema: this.KeySchema.generateSchema(),
};
}
findAndReturnTokens(tokenToFind, tokens) {
if (SetToken.prim === tokenToFind) {
tokens.push(this);
}
this.KeySchema.findAndReturnTokens(tokenToFind, tokens);
return tokens;
}
}
exports.SetToken = SetToken;
SetToken.prim = 'set';