key-book
v1.1.2
Published
create frozen string reference look ups from a string array
Downloads
7
Readme
key-book 🔑
Create string reference look ups from a string array in JavaScript ❄️
Pass key-book
an array of strings with an optional prefix/suffix and it will return a frozen object that you can use as a look up.
Mitigate the risk of string typos in your applications where errors aren't likely to be thrown.
Usage
const mirror = require('key-book');
const enums = mirror(["A", "B", "C"]);
enums; // {A: "A", B: "B", C: "C"}
enums.A = "B";
enums; // {A: "A", B: "B", C: "C"}
const prefixedEnums = mirror(["A", "B", "C"], "PREFIX_");
prefixedEnums; // {PREFIX_A: "PREFIX_A", PREFIX_B: "PREFIX_B", PREFIX_C: "PREFIX_C"}
const suffixedEnums = mirror(["A", "B", "C"], undefined, "_SUFFIX");
suffixedEnums; // {A_SUFFIX: "A_SUFFIX", B_SUFFIX: "B_SUFFIX", C_SUFFIX: "C_SUFFIX"}
Background
I wrote about the rationale for using a package like key-book
in this article.
@jh3y 2019 MIT