(ctx: EncodeContext, arr: ReadonlyArray<unknown>, count: number, w: Writer)
| 4003 | const numberRunScales: Array<number> = [] |
| 4004 | |
| 4005 | function encodeNumberRun(ctx: EncodeContext, arr: ReadonlyArray<unknown>, count: number, w: Writer) { |
| 4006 | let varint = true |
| 4007 | let decimal = true |
| 4008 | for (let i = 0; i < count; i++) { |
| 4009 | const value = arr[i] |
| 4010 | if (typeof value !== "number") { |
| 4011 | issuePath[issuePathLen++] = i |
| 4012 | encodeFail("a number", value, ctx.options) |
| 4013 | } |
| 4014 | // Once both narrower modes are ruled out, only the type check remains. |
| 4015 | if (!varint && !decimal) continue |
| 4016 | if (isVarintNumber(value)) { |
| 4017 | const magnitude = value < 0 ? -value : value |
| 4018 | if (magnitude > DECIMAL_MANTISSA_MAX) decimal = false |
| 4019 | } else { |
| 4020 | varint = false |
| 4021 | const scale = decimalScale(value) |
| 4022 | if (scale === 0) decimal = false |
| 4023 | else numberRunScales[i] = scale |
| 4024 | } |
| 4025 | } |
| 4026 | if (varint) { |
| 4027 | w.byte(NUMBER_RUN_VARINT) |
| 4028 | for (let i = 0; i < count; i++) w.numberVarint(arr[i] as number) |
| 4029 | } else if (decimal) { |
| 4030 | w.byte(NUMBER_RUN_DECIMAL) |
| 4031 | for (let i = 0; i < count; i++) { |
| 4032 | const value = arr[i] as number |
| 4033 | // Integers take scale 0 even when a scale would fit, matching the |
| 4034 | // classification pass writing scales only for non-integers. |
| 4035 | const scale = Number.isInteger(value) ? 0 : numberRunScales[i] |
| 4036 | const mantissa = scale === 0 ? value : Math.round(value * POW10[scale]) |
| 4037 | const negative = mantissa < 0 || (mantissa === 0 && 1 / mantissa < 0) |
| 4038 | const magnitude = negative ? -mantissa : mantissa |
| 4039 | w.uvarint((magnitude * 2 + (negative ? 1 : 0)) * 16 + scale) |
| 4040 | } |
| 4041 | } else { |
| 4042 | w.byte(NUMBER_RUN_F64) |
| 4043 | for (let i = 0; i < count; i++) w.f64(arr[i] as number) |
| 4044 | } |
| 4045 | } |
| 4046 | |
| 4047 | function findDiscriminator(variants: ReadonlyArray<VariantRow>): Discriminator | undefined { |
| 4048 | if (variants.length < 2 || variants.some((variant) => variant.tuple)) return undefined |
no test coverage detected