UNPKG

diffusion

Version:

Diffusion JavaScript client

42 lines (41 loc) 954 B
"use strict"; /** * @module Util.Enumerize * * @brief Utility function for turning objects into Enum like objects */ Object.defineProperty(exports, "__esModule", { value: true }); exports.enumerize = void 0; /** * Add reverse lookup to an enum-like object in order to imitate TypeScript * style enums. Each value must be an object that defines a numeric `id` * property. * * **Example:** * ``` * const enumLike = { * FOO: { id: 0, msg: 'This is a Foo'}, * BAR: { id: 1, msg: 'This is a Bar'} * } * * enumerize(enumLike); * * // enumLike = { * // FOO: { id: 0, msg: 'This is a Foo'}, * // BAR: { id: 1, msg: 'This is a Bar'}, * // 0: 'FOO', * // 1: 'BAR' * // } * ``` * * @param obj the enum-like object */ function enumerize(obj) { Object.keys(obj).forEach(function (key) { var id = obj[key].id; if (id !== undefined) { obj[id] = key; } }); } exports.enumerize = enumerize;