npm

Need private packages and team management tools?Check out npm Orgs. »

crenum

1.0.0 • Public • Published

Crenum

A tiny utility for creating Enums in JavaScript. Works in any ES5+ compliant environment.

  • AMD build is not provided yet.
  • ESM export is not provided yet.

Installation

$ npm install crenum
# or 
$ yarn add crenum

Usage example

var Enum = require('crenum');
var Color = new Enum(['red', 'blue', 'green']);
/*
{
  RED: 'RED',
  BLUE: 'BLUE',
  GREEN: 'GREEN'
}
*/
var myColor = Color.RED; // 'RED'

API

new Enum(items, options?);

items - Array of at least 2 unique non-empty string constants. Required.

options - Options object.

  • options.strict (boolean) - Apply type and integrity checks to the first argument, may throw TypeError. Default true. Use false when you are sure about your input and need to create many objects on the fly.
  • options.transform ((value: string) => string) - Transform string representations of enum elements. Default value => UPPER_SNAKE_CASE_VALUE. Use it to transform your enum values to different cases. Make sure that the values after transforming stay compliant with options.strict.
  • options.numericValues (boolean) - Use integer indexes (starting from 1) as enum element values instead of strings. Default false.

Returned object is frozen (cannot be modified) and prototype-less (no builtin properties).

License

MIT

Keywords

install

npm i crenum

Downloadsweekly downloads

2

version

1.0.0

license

MIT

homepage

gitlab.com

repository

Gitgitlab

last publish

collaborators

  • avatar
Report a vulnerability