UNPKG

@aeternity/aepp-calldata

Version:
50 lines (49 loc) 1.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _FateData = _interopRequireDefault(require("./FateData.cjs")); var _FateTypes = require("../FateTypes.cjs"); var _int2ByteArray = require("../utils/int2ByteArray.cjs"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } const toString = data => { if (data instanceof Uint8Array) { const decoder = new TextDecoder(); return decoder.decode(data); } return data.toString(); }; const toBytes = data => { if (typeof data === 'string') { const encoder = new TextEncoder(); return encoder.encode(data); } return data; }; const isUnicodeString = data => { const bytes = toBytes(toString(data)); return (0, _int2ByteArray.byteArray2Int)(data) === (0, _int2ByteArray.byteArray2Int)(bytes); }; class FateString extends _FateData.default { constructor(value) { super('string'); this._value = toBytes(value); } get type() { return (0, _FateTypes.FateTypeString)(); } toString() { return toString(this._value); } toBytes() { return this._value; } valueOf() { if (isUnicodeString(this._value)) { return this.toString(); } return this._value; } } var _default = exports.default = FateString;