Debian Codegraph @

node/@bufbuild/protobuf/dist/commonjs/codegenv2/enum.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.enumDesc = enumDesc;
17 exports.tsEnum = tsEnum;
18 exports.objEnum = objEnum;
19 /**
20 * Hydrate an enum descriptor.
21 *
22 * @private
23 */
24 function enumDesc(file, path, ...paths) {
25 if (paths.length == 0) {
26 return file.enums[path];
27 }
28 const e = paths.pop(); // we checked length above
29 return paths.reduce((acc, cur) => acc.nestedMessages[cur], file.messages[path]).nestedEnums[e];
30 }
31 /**
32 * Construct a TypeScript enum object at runtime from a descriptor.
33 *
34 * The returned object is identical to a transpiled TS enum and includes the
35 * reverse mapping, see https://www.typescriptlang.org/docs/handbook/enums.html#reverse-mappings
36 */
37 function tsEnum(desc) {
38 const enumObject = {};
39 for (const value of desc.values) {
40 enumObject[value.localName] = value.number;
41 enumObject[value.number] = value.localName;
42 }
43 return enumObject;
44 }
45 /**
46 * Construct an object enum at runtime from a descriptor.
47 *
48 * The returned object is a record of enum value name to integer value. It's
49 * a subset of transpiled TS enums - it does not include the reverse mapping,
50 * and only supports lookup by value name.
51 */
52 function objEnum(desc) {
53 const enumObject = {};
54 for (const value of desc.values) {
55 enumObject[value.localName] = value.number;
56 }
57 return enumObject;
58 }