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

Function encode

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

Source from the content-addressed store, hash-verified

61}
62
63export function encode(
64 input: CborType,
65 output: Uint8Array,
66 offset: number,
67): number {
68 switch (typeof input) {
69 case "undefined":
70 output[offset++] = 0b111_10111;
71 break;
72 case "boolean":
73 output[offset++] = input ? 0b111_10101 : 0b111_10100;
74 break;
75 case "number":
76 return encodeNumber(input, output, offset);
77 case "bigint":
78 return encodeBigInt(input, output, offset);
79 case "string":
80 return encodeString(input, output, offset);
81 default:
82 if (input === null) output[offset++] = 0b111_10110;
83 else if (input instanceof Uint8Array) {
84 return encodeUint8Array(input, output, offset);
85 } else if (input instanceof Date) {
86 return encodeDate(input, output, offset);
87 } else if (input instanceof CborTag) {
88 return encodeTag(input, output, offset);
89 } else if (input instanceof Map) return encodeMap(input, output, offset);
90 else if (input instanceof Array) {
91 return encodeArray(input, output, offset);
92 } else return encodeObject(input, output, offset);
93 }
94 return offset;
95}
96
97function encodeHeader(
98 majorType: number,

Callers 6

encodeCborFunction · 0.90
encodeCborSequenceFunction · 0.90
encodeArrayFunction · 0.70
encodeObjectFunction · 0.70
encodeTagFunction · 0.70
encodeMapFunction · 0.70

Calls 9

encodeBigIntFunction · 0.85
encodeStringFunction · 0.85
encodeUint8ArrayFunction · 0.85
encodeDateFunction · 0.85
encodeTagFunction · 0.85
encodeMapFunction · 0.85
encodeArrayFunction · 0.85
encodeObjectFunction · 0.85
encodeNumberFunction · 0.70

Tested by

no test coverage detected