enum-class
JavaScript enum type.
Last updated 5 years ago by bluejeansandrain .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install enum-class 
SYNC missed versions from official npm registry.

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.

Current Tags

  • 1.0.1                                ...           latest (5 years ago)

2 Versions

  • 1.0.1                                ...           5 years ago
  • 1.0.0                                ...           5 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (0)
None
Dependents (1)

Copyright 2014 - 2016 © taobao.org |