key-reflection

1.0.2 • Public • Published

key-reflection

npm version Build Status

Create an enum object from an array of keys with optional prefixes and suffixes. IE9+ Support. No dependencies.

Pass key-reflection an array of strings with an optional prefix/suffix and it will return an enum that you can use as a look up. Great for state stores like Redux.

Installation

npm install key-reflection --save

Usage

The first option is the array you wish to reflect. The second is an options object to set any prefixes or suffixes if desired.

const key_reflection = require('key-reflection');

const enums = key_reflection(["A", "B"]);
// {A: "A", B: "B"}

const prefixedEnums = key_reflection(["A", "B"], { prefix: 'PREFIX_' });
// {PREFIX_A: "PREFIX_A", PREFIX_B: "PREFIX_B"}

const suffixedEnums = key_reflection(["A", "B"], { suffix: '_SUFFIX' });
// {A_SUFFIX: "A_SUFFIX", B_SUFFIX: "B_SUFFIX"}

You can also target prefixes and suffixes at just the key or value depending on your needs as seen in the options below.

Options

prefix: Prefixes both the keys and values in the resulting array.

prefixKey: Prefixes the keys in the resulting array.

prefixValue: Prefixes the values in the resulting array.

suffix: Suffixes both the keys and values in the resulting array.

suffixKey: Suffixes the keys in the resulting array.

suffixValue: Suffixes the values in the resulting array.

Package Sidebar

Install

npm i key-reflection

Weekly Downloads

10

Version

1.0.2

License

MIT

Unpacked Size

6.34 kB

Total Files

6

Last publish

Collaborators

  • soluml