@liskhq/lisk-codec
Version:
Implementation of decoder and encoder using Lisk JSON schema according to the Lisk protocol
20 lines • 805 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.readString = exports.writeString = void 0;
const bytes_1 = require("./bytes");
const writeString = (value) => {
const normalized = value.normalize('NFC');
const stringBuffer = Buffer.from(normalized, 'utf8');
return (0, bytes_1.writeBytes)(stringBuffer);
};
exports.writeString = writeString;
const readString = (buffer, offset) => {
const [value, size] = (0, bytes_1.readBytes)(buffer, offset);
const decodedStr = new TextDecoder('utf8', { fatal: true }).decode(value);
if (decodedStr.normalize('NFC') !== decodedStr) {
throw new Error('UTF8 bytes include non-normalized bytes');
}
return [decodedStr, size];
};
exports.readString = readString;
//# sourceMappingURL=string.js.map