3.0.0 • Published 2 years ago

enum-class v3.0.0

Weekly downloads
3
License
ISC
Repository
github
Last release
2 years ago

enum-class

JavaScript enumerable type.

Features

  • Enumerable constants can be checked for type safety using the instanceof operator. * MyEnum.contant instanceof MyEnum
  • Enumerable constants can have explicitly defined values. * new Enum({ foo: "value" })
  • Enumerable constants with the same name and value, in two different types, are not strictly equal. * Enum1.foo !== Enum2.foo
  • Enumerable types are frozen.

Example

const Enum = require("enum-class");

Without Values

var MyEnum = Enum.create("foo", "bar");

MyEnum.foo.valueOf(); // 0
MyEnum.bar.valueOf(); // 1

MyEnum.foo.toString(); // "foo"
MyEnum.bar.toString(); // "bar"

MyEnum.foo === 0; // false
MyEnum.foo === "foo"; // false
MyEnum.get("foo"); // MyEnum.foo

{ [foo.foo]: true }; // { "foo": true }

MyEnum.names; // [ "foo", "bar" ]
MyEnum.values; // [ 0, 1 ]
MyEnum.constants; // [ MyEnum.foo, MyEnum.bar ]

MyEnum instanceof Enum; // true
MyEnum.foo instanceof Enum; // true
MyEnum.foo instanceof MyEnum; // true

With Values

var MyEnum = Enum.create({ foo: 2, bar: 4 });

MyEnum.foo.valueOf(); // 2
MyEnum.foo.valueOf(); // 4

MyEnum.foo.toString(); // "foo"
MyEnum.bar.toString(); // "bar"

API

new Enum("foo", "bar", ...);

new Enum("foo", "bar", ...);

new Enum({ foo: 0, bar: 1, ... });

Enum.create("foo", "bar", ...);

Enum.create("foo", "bar", ...);

Enum.create({ foo: 0, bar: 1, ... });

Return a new Enum instance with the defined constants.

enum.names

Return a list of the enum names.

enum.values

Return a list of the enum values.

enum.constants

Return a list of the enum constants.

enum.get(name)

Return the enum constant that matches name.

enum.constant.toString()

Return the constant name string.

enum.constant.valueOf()

Return the contant value.

3.0.0

2 years ago

2.0.0

2 years ago

1.0.1

9 years ago

1.0.0

9 years ago