Debian Codegraph @

node/@bufbuild/protobuf/dist/commonjs/codegenv2/scalar.js

raw
1 "use strict";
2 // Copyright 2021-2026 Buf Technologies, Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 Object.defineProperty(exports, "__esModule", { value: true });
16 exports.scalarTypeScriptType = scalarTypeScriptType;
17 exports.scalarJsonType = scalarJsonType;
18 const descriptors_js_1 = require("../descriptors.js");
19 /**
20 * Return the TypeScript type (as a string) for the given scalar type.
21 */
22 function scalarTypeScriptType(scalar, longAsString) {
23 switch (scalar) {
24 case descriptors_js_1.ScalarType.STRING:
25 return "string";
26 case descriptors_js_1.ScalarType.BOOL:
27 return "boolean";
28 case descriptors_js_1.ScalarType.UINT64:
29 case descriptors_js_1.ScalarType.SFIXED64:
30 case descriptors_js_1.ScalarType.FIXED64:
31 case descriptors_js_1.ScalarType.SINT64:
32 case descriptors_js_1.ScalarType.INT64:
33 return longAsString ? "string" : "bigint";
34 case descriptors_js_1.ScalarType.BYTES:
35 return "Uint8Array";
36 default:
37 return "number";
38 }
39 }
40 /**
41 * Return the JSON type (as a string) for the given scalar type.
42 */
43 function scalarJsonType(scalar) {
44 switch (scalar) {
45 case descriptors_js_1.ScalarType.DOUBLE:
46 case descriptors_js_1.ScalarType.FLOAT:
47 return `number | "NaN" | "Infinity" | "-Infinity"`;
48 case descriptors_js_1.ScalarType.UINT64:
49 case descriptors_js_1.ScalarType.SFIXED64:
50 case descriptors_js_1.ScalarType.FIXED64:
51 case descriptors_js_1.ScalarType.SINT64:
52 case descriptors_js_1.ScalarType.INT64:
53 return "string";
54 case descriptors_js_1.ScalarType.INT32:
55 case descriptors_js_1.ScalarType.FIXED32:
56 case descriptors_js_1.ScalarType.UINT32:
57 case descriptors_js_1.ScalarType.SFIXED32:
58 case descriptors_js_1.ScalarType.SINT32:
59 return "number";
60 case descriptors_js_1.ScalarType.STRING:
61 return "string";
62 case descriptors_js_1.ScalarType.BOOL:
63 return "boolean";
64 case descriptors_js_1.ScalarType.BYTES:
65 return "string";
66 }
67 }