MCPcopy Create free account
hub / github.com/denoland/std / encodeNumber

Function encodeNumber

cbor/_common_encode.ts:126–153  ·  view source on GitHub ↗
(
  input: number,
  output: Uint8Array,
  offset: number,
)

Source from the content-addressed store, hash-verified

124}
125
126function encodeNumber(
127 input: number,
128 output: Uint8Array,
129 offset: number,
130): number {
131 if (input % 1 === 0) {
132 const isNegative = input < 0;
133 if (isNegative && input <= -(2 ** 64)) {
134 throw new RangeError(
135 `Cannot encode number: It (${input}) exceeds -(2 ** 64) - 1`,
136 );
137 } else if (input >= 2 ** 64) {
138 throw new RangeError(
139 `Cannot encode number: It (${input}) exceeds 2 ** 64 - 1`,
140 );
141 }
142 return encodeHeader(
143 isNegative ? 0b001_00000 : 0b000_00000,
144 isNegative ? -input - 1 : input,
145 output,
146 offset,
147 );
148 }
149 const view = new DataView(output.buffer);
150 output[offset++] = 0b111_11011;
151 view.setFloat64(offset, input);
152 return offset + 8;
153}
154
155function encodeBigInt(
156 input: bigint,

Callers 2

encodeFunction · 0.70
encodeDateFunction · 0.70

Calls 1

encodeHeaderFunction · 0.85

Tested by

no test coverage detected