enumify

WebJar for enumify

License

License

MIT
GroupId

GroupId

org.webjars.npm
ArtifactId

ArtifactId

enumify
Last Version

Last Version

1.0.4
Release Date

Release Date

Type

Type

jar
Description

Description

enumify
WebJar for enumify
Project URL

Project URL

http://webjars.org
Source Code Management

Source Code Management

https://github.com/rauschma/enumify

Download enumify

How to add to project

<!-- https://jarcasting.com/artifacts/org.webjars.npm/enumify/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>enumify</artifactId>
    <version>1.0.4</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/enumify/
implementation 'org.webjars.npm:enumify:1.0.4'
// https://jarcasting.com/artifacts/org.webjars.npm/enumify/
implementation ("org.webjars.npm:enumify:1.0.4")
'org.webjars.npm:enumify:jar:1.0.4'
<dependency org="org.webjars.npm" name="enumify" rev="1.0.4">
  <artifact name="enumify" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='enumify', version='1.0.4')
)
libraryDependencies += "org.webjars.npm" % "enumify" % "1.0.4"
[org.webjars.npm/enumify "1.0.4"]

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

There are no modules declared in this project.

Enumify

A JavaScript library that helps with the enum pattern. Also supports TypeScript.

Installation:

npm install enumify

Basic usage

  class Color extends Enumify {
    static red = new Color();
    static orange = new Color();
    static yellow = new Color();
    static green = new Color();
    static blue = new Color();
    static purple = new Color();
    static _ = this.closeEnum(); // TypeScript: Color.closeEnum()
  }

  // Instance properties
  assert.equal(
    Color.red.enumKey, 'red');
  assert.equal(
    Color.red.enumOrdinal, 0);
  
  // Prototype methods
  assert.equal(
    'Color: ' + Color.red, // .toString()
    'Color: Color.red');
  
  // Static `.enumKeys` and static `.enumValues`
  assert.deepEqual(
    Color.enumKeys,
    ['red', 'orange', 'yellow', 'green', 'blue', 'purple']);
  assert.deepEqual(
    Color.enumValues,
    [ Color.red, Color.orange, Color.yellow,
      Color.green, Color.blue, Color.purple ]);

  // Static `.enumValueOf()`
  assert.equal(
    Color.enumValueOf('yellow'),
    Color.yellow);
  
  // Iterability
  const result = [];
  const iterated = [...Color];
  for (const c of Color) {
    result.push('Color: ' + c);
  }
  assert.deepEqual(
    iterated, [
      Color.red,
      Color.orange,
      Color.yellow,
      Color.green,
      Color.blue,
      Color.purple,
    ]);

More examples

See:

  • ts/test/index_test.ts
  • ts/test/state.ts

Run tests like this (after compiling TypeScript, e.g. via npm run build):

npm t dist/test/index_test.js

Support for public static fields

The enum pattern and Enumify are based on public static fields. Support for them currently looks as follows:

Further reading

Versions

Version
1.0.4