UNPKG

@anycable/protobuf-encoder

Version:

Protobuf encoder for AnyCable client

28 lines (25 loc) 675 B
// This is a wrapper around enum objects, generated by protobufjs library // Protobufjs compiles enums as a POJOs, but with a catch: their prototypes // hold inversed hash of id-value pairs. // // E.g. for enum in .proto file // enum Dummy { // foo = 0; // bar = 1; // } // will be generated an object{ foo: 0, bar: 1 }, // and its prototype will be { 0: foo, 1: bar } // // This class leverages that feature export class EnumWrapper { constructor(enumValues) { this.values = enumValues this.valuesById = Object.getPrototypeOf(enumValues) } getIdByValue(value) { return this.values[value] } getValueById(id) { return this.valuesById[id] } }