1.0.0 • Published 7 years ago

crenum v1.0.0

Weekly downloads
-
License
MIT
Repository
gitlab
Last release
7 years ago

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

1.0.0

7 years ago