• Stars
    star
    662
  • Rank 67,524 (Top 2 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 8 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

More Repositories

1

webpack-es6-demo

JavaScript
213
star
2

proto-js

Proto.js – prototypes as classes
JavaScript
173
star
3

re-template-tag

JavaScript
151
star
4

tree-shaking-demo

JavaScript
147
star
5

react-starter-project

Minimal starter project for React: webpack + hot module reloading + ES6 and JSX via Babel
JavaScript
130
star
6

type-right

JavaScript
119
star
7

webpack-babel-demo

JavaScript
102
star
8

strint

String-encoded integers in JavaScript
JavaScript
95
star
9

stringio

Convert strings to Node.js streams and vice versa
TypeScript
94
star
10

generator-examples

Example code from the blog post “ES6 generators in depth”
JavaScript
74
star
11

enums

A simple enum implementation for JavaScript
JavaScript
73
star
12

openurl

Node.js module for opening a URL via the operating system
JavaScript
71
star
13

class-js

Class.js – a simple JavaScript inheritance API
JavaScript
58
star
14

op_overload

Fake operator overloading used by a type Point
JavaScript
47
star
15

node-es6-demo

JavaScript
46
star
16

minimal-react

JavaScript
45
star
17

demo_promise

JavaScript
37
star
18

iterable

HTML
30
star
19

async-iter-demo

JavaScript
27
star
20

exploring-reasonml

26
star
21

async-off

TypeScript
20
star
22

exploring-js

20
star
23

babel-on-node

Demo project demonstrating how to use the ES6 transpiler Babel on Node.js
JavaScript
19
star
24

esnext-demo

Enable pkg.esnext for *untranspiled* source code
JavaScript
19
star
25

ts-demo-webpack

JavaScript
18
star
26

markcheck

Markcheck tests Markdown code blocks – to prevent errors in documentation (readmes, blogs, books, etc.)
TypeScript
18
star
27

toy-promise

JavaScript
17
star
28

node-babel-dynamic-demo

JavaScript
17
star
29

reasonml-demo-modules

OCaml
17
star
30

scripts-help

JavaScript
16
star
31

shared-array-buffer-demo

JavaScript
15
star
32

html_demos

Demos of HTML document features
JavaScript
14
star
33

promise-examples

JavaScript
13
star
34

js-complaints

Common JavaScript complaints
13
star
35

set-methods-polyfill

A polyfill for the ECMAScript proposal “Set Methods for JavaScript”
TypeScript
13
star
36

deep-js

13
star
37

node-babel-static-demo

JavaScript
12
star
38

lobrow

Load Node.js modules on web browsers
JavaScript
12
star
39

module-bindings-demo

JavaScript
11
star
40

es6-shim

A shim for useful code that will probably be in ECMAScript 6
JavaScript
10
star
41

reasonml-demo-functors

OCaml
9
star
42

babel-config-demo

JavaScript
9
star
43

tackling-ts

9
star
44

exploring-es2018-es2019

8
star
45

jsrepl

Prototype: combine code editing with a JavaScript command line
JavaScript
8
star
46

iterator-helpers-polyfill

TypeScript
8
star
47

lazylines

JavaScript
7
star
48

strmap

A simple map from strings to values in JavaScript
JavaScript
7
star
49

js-feature-matrix

JavaScript
7
star
50

imports-are-views-demo

JavaScript
7
star
51

ts-demo-npm-cjs

A minimal repository that demonstrates how to create CommonJS-based npm packages via TypeScript
TypeScript
7
star
52

env-var

JavaScript
6
star
53

r700_mac

Making the Logitech Presenter R700 work with Macs.
6
star
54

jsreload

Scripts that help with automatically reloading a tab in Safari whenever a file changes
JavaScript
6
star
55

reasonml-demo-iterators

OCaml
6
star
56

async-examples

HTML
6
star
57

universal_modules

A lightweight module system that works both in browsers and Node.js
JavaScript
6
star
58

jsdom_demo

This project shows you how to use jsdom and jQuery on Node.js to examine and transform HTML.
JavaScript
6
star
59

typed-array-demos

HTML
5
star
60

jasmine-intro

JavaScript
5
star
61

npm-bin-demo

JavaScript
4
star
62

inheritance-by-example

Source code for the blog post “JavaScript inheritance by example”
JavaScript
4
star
63

nodejs-tools

Helpers for Node.js that I’m using in my projects
TypeScript
4
star
64

helpers

Helpers for JavaScript and TypeScript that I’m using in my projects
TypeScript
4
star
65

nodejs-shell-scripting

3
star
66

incsync

Incremental backups for Unix (Mac OS X, Linux, ...) via rsync. Think: poor man’s Time Machine.
Shell
3
star
67

copy_url

A bookmarklet for copying URL and title of the current page (for Twitter etc.)
JavaScript
3
star
68

regexps

Several useful regular expressions, unit-tested
2
star
69

demo-shell-scripts

JavaScript
2
star
70

es6_wsh_code

Exercises for my ES6 workshop
JavaScript
2
star
71

snowpack-install-demo

JavaScript
2
star
72

wix-demo

JavaScript
2
star
73

mocha-test-repo

JavaScript
1
star
74

ootools

Library with helpers for JavaScript objects and inheritance
JavaScript
1
star
75

babel-decorator-error

JavaScript
1
star
76

babel-exponentation-bug

JavaScript
1
star
77

log-args

JavaScript
1
star
78

server-side-rendered-css

TypeScript
1
star