(ctx: EncodeContext, layout: Layout, value: unknown, w: Writer)
| 4119 | } |
| 4120 | |
| 4121 | function encodeValue(ctx: EncodeContext, layout: Layout, value: unknown, w: Writer): void { |
| 4122 | switch (layout._) { |
| 4123 | case "literal": |
| 4124 | if (!hasLiteral(layout, value)) encodeFail(literalExpected(layout), value, ctx.options) |
| 4125 | encodeValue(ctx, layout.leaf, value, w) |
| 4126 | return |
| 4127 | case "bool": |
| 4128 | if (typeof value !== "boolean") encodeFail("a boolean", value, ctx.options) |
| 4129 | w.byte(value === true ? 1 : 0) |
| 4130 | return |
| 4131 | case "null": |
| 4132 | if (value !== null) encodeFail("null", value, ctx.options) |
| 4133 | return |
| 4134 | case "undefined": |
| 4135 | if (value !== undefined) encodeFail("undefined", value, ctx.options) |
| 4136 | return |
| 4137 | case "number": { |
| 4138 | if (typeof value !== "number") encodeFail("a number", value, ctx.options) |
| 4139 | if (isVarintNumber(value)) { |
| 4140 | w.numberVarint(value) |
| 4141 | return |
| 4142 | } |
| 4143 | const scale = decimalScale(value) |
| 4144 | if (scale > 0) { |
| 4145 | w.numberVarint(Math.round(value * POW10[scale])) |
| 4146 | w.byte(scale) |
| 4147 | } else { |
| 4148 | w.f64(value) |
| 4149 | } |
| 4150 | return |
| 4151 | } |
| 4152 | case "int": { |
| 4153 | // `disableChecks` can bypass the integer check used to select this layout. |
| 4154 | if (!Number.isSafeInteger(value)) encodeFail("an integer", value, ctx.options) |
| 4155 | w.numberVarint(value as number) |
| 4156 | return |
| 4157 | } |
| 4158 | case "string": { |
| 4159 | if (typeof value !== "string") encodeFail("a string", value, ctx.options) |
| 4160 | const dict = ctx.dict |
| 4161 | if (dict === undefined) w.string(value) |
| 4162 | else encodeString(dict, layout, value, w) |
| 4163 | return |
| 4164 | } |
| 4165 | case "symbol": |
| 4166 | if (typeof value !== "symbol") encodeFail("a symbol", value, ctx.options) |
| 4167 | encodeSymbol(ctx, value, w) |
| 4168 | return |
| 4169 | case "bytes": |
| 4170 | if (!(value instanceof Uint8Array)) encodeFail("a Uint8Array", value, ctx.options) |
| 4171 | w.bytes(value) |
| 4172 | return |
| 4173 | case "bigint": |
| 4174 | if (typeof value !== "bigint") encodeFail("a bigint", value, ctx.options) |
| 4175 | w.zigzag(value) |
| 4176 | return |
| 4177 | // `disableChecks` can bypass the range check used to select these layouts. |
| 4178 | case "i32": |
no test coverage detected