Fixed size codec should implement this function instead of unpack
.
Create a new codec which shares the same underlying molecule type but with different JavaScript type.
This is useful to create a different view wrapper.
import { mol } from "@ckb-cobuild/molecule";
const ByteOpt = mol.option("ByteOpt", mol.byte);
const BooleanOpt = ByteOpt.around({
safeParse: (input: boolean | null) => mol.parseSuccess(input),
willPack: (input: boolean | null) =>
input !== null ? (input ? 1 : 0) : null,
didUnpack: (value: number | null) => (value !== null ? value !== 0 : null),
});
Chain a parse
method before.
import { mol } from "@ckb-cobuild/molecule";
const ByteCoerce = mol.byte.beforeParse((input: any) => Number(input));
Chain a safeParse
method before.
import { mol } from "@ckb-cobuild/molecule";
const ByteCoerce = mol.byte.beforeSafeParse((input: any) => {
const result = parseInt(input);
if (!Number.isNaN(result)) {
return mol.parseSuccess(result);
}
return mol.parseError("Not a number");
});
Direct dependencies of this codec.
Pack value into a molecule buffer.
Pack the value and append the buffer to the provided writer.
Take the advantage of writer and append the buffer as blocks. The writer can concatenate all blocks into one buffer for the final result.
Parse a compatible input.
Parse a compatible input. Return a result instead of throwing.
Unpack the value from the molecule buffer.
Readonly
fixedReadonly
innerReadonly
nameName of this codec used to export .mol file.
Readonly
orderGenerated using TypeDoc
The fixed size codec does not require length header when used inside other structures.
See
Molecule Encoding Spec