@particle/make-enum

5.0.0 • Public • Published

@particle/make-enum

Turn an array of values into an object that will throw an error when getting a non existing property to avoid typos in enum values

Note: relies on Proxy which is a Javascript feature that cannot be polyfilled, so it's not safe to use in the browser.

Installation

npm install @particle/make-enum --save
const makeEnum = require('@particle/make-enum');

API

@particle/make-enum


module.exports(values) ⇒ Object

Turn an array of values into an object that will throw an error when getting a non existing property to avoid typos in enum values

Kind: Exported function
Returns: Object - The extended array object where enum values can be accessed as properties

Param Type Description
values Array The array of string values for the enum

Example

const states = makeEnum(['new', 'in_progress']);
// states.new === 'new'
// states.inprogress throws

const nestedEnum = makeEnum(['animals.cow', 'animals', 'animals.eagle']);
// To compare with the string, the enum must be basted to a string
// nestedEnum.animals.toString() === 'animals'
// `${nestedEnum.animals.cow}` === 'animals.cow'

NOTE: Unfortunately, docs have a nasty habit of falling out of date. When in doubt, check usage in tests

Readme

Keywords

none

Package Sidebar

Install

npm i @particle/make-enum

Weekly Downloads

283

Version

5.0.0

License

UNLICENSED

Unpacked Size

7.85 kB

Total Files

4

Last publish

Collaborators

  • jackson-particle
  • mike.grundy.npm
  • mohitparticle
  • eugene_particle
  • kmok23
  • karlag
  • cole-abbeduto-particle
  • pedrosordo
  • guohui
  • sbrust
  • particlebot
  • no1089
  • carlos.hdez
  • keeramis5
  • monkbroc
  • suda
  • mstan
  • sergeuz
  • debbie23
  • avtolstoy
  • mlauer
  • wraithan
  • azenk
  • bwalach
  • parteberseth
  • rickkas7
  • mrlambchop_particle
  • hugo.montero
  • mphsi
  • davidbbe