├── .babelrc ├── .npmignore ├── .gitignore ├── circle.yml ├── lib └── enum.js ├── dist └── enum.js ├── test └── index.js ├── package.json ├── LICENSE └── README.md /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["es2015"] 3 | } -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | test/ 3 | *.log 4 | *.DS_Store 5 | lib/ -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | !.gitignore 2 | !.babelrc 3 | 4 | node_modules/ 5 | *.log 6 | *.DS_Store -------------------------------------------------------------------------------- /circle.yml: -------------------------------------------------------------------------------- 1 | machine: 2 | node: 3 | version: 4.0.0 4 | 5 | dependencies: 6 | override: 7 | - npm install -------------------------------------------------------------------------------- /lib/enum.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | function Enum () { 4 | if (!(this instanceof Enum)) return new Enum(...arguments) 5 | Array.from(arguments).forEach(arg => { 6 | this[arg] = Symbol(arg) 7 | }) 8 | } 9 | 10 | module.exports = Enum 11 | -------------------------------------------------------------------------------- /dist/enum.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | function Enum() { 4 | var _this = this; 5 | 6 | if (!(this instanceof Enum)) return new (Function.prototype.bind.apply(Enum, [null].concat(Array.prototype.slice.call(arguments))))(); 7 | Array.from(arguments).forEach(function (arg) { 8 | _this[arg] = Symbol(arg); 9 | }); 10 | } 11 | 12 | module.exports = Enum; -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Test env: node 4.0.0 3 | */ 4 | 'use strict' 5 | 6 | const test = require('tape') 7 | const Enum = require('../') 8 | const whatEver = require('../') 9 | 10 | const ORIENTATION = Enum('NORTH', 'SOUTH', 'WEST', 'EAST') 11 | const ORIEN = Enum('NORTH', 'SOUTH', 'WEST', 'EAST') 12 | 13 | test('Enum can import as other name', function(t) { 14 | t.deepEqual(Enum, whatEver) 15 | t.end() 16 | }) 17 | 18 | test('Enum case should be equal', function(t) { 19 | t.equal(ORIENTATION.NORTH, ORIENTATION.NORTH) 20 | t.end() 21 | }) 22 | 23 | test('Different enum case with same name should not be equal', function(t) { 24 | t.notEqual(ORIENTATION.NORTH, ORIEN.NORTH) 25 | t.end() 26 | }) 27 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "es6-enum", 3 | "version": "1.1.0", 4 | "description": "Enum type made by es6", 5 | "main": "dist/enum.js", 6 | "scripts": { 7 | "check": "standard ./lib/enum.js | snazzy", 8 | "test": "npm run build && npm run check && node test/index.js | tap-spec", 9 | "prebuild": "rm -rf dist", 10 | "build": "mkdir dist && babel lib --out-dir dist" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "https://github.com/wwayne/es6-enum" 15 | }, 16 | "keywords": [ 17 | "es6", 18 | "enum", 19 | "javascript", 20 | "data structure" 21 | ], 22 | "engines": { 23 | "node": ">=0.12.0" 24 | }, 25 | "author": "wwayne", 26 | "license": "MIT", 27 | "bugs": { 28 | "url": "https://github.com/wwayne/es6-enum/issues" 29 | }, 30 | "devDependencies": { 31 | "babel-cli": "^6.16.0", 32 | "babel-preset-es2015": "^6.16.0", 33 | "snazzy": "^2.0.1", 34 | "standard": "^5.4.1", 35 | "tap-spec": "^4.1.1", 36 | "tape": "^4.2.0" 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Wang Zixiao 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # es6-enum 2 | 3 | [![Version](http://img.shields.io/npm/v/es6-enum.svg)](https://www.npmjs.org/package/es6-enum) 4 | [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://github.com/feross/standard) 5 | [![npm download][download-image]][download-url] 6 | [![Circle CI](https://circleci.com/gh/wwayne/es6-enum/tree/master.svg?style=svg)](https://circleci.com/gh/wwayne/es6-enum/tree/master) 7 | 8 | [download-image]: https://img.shields.io/npm/dm/es6-enum.svg?style=flat-square 9 | [download-url]: https://npmjs.org/package/es6-enum 10 | 11 | ### Installation 12 | 13 | ```sh 14 | npm install es6-enum 15 | ``` 16 | 17 | ### Usage 18 | 19 | ```javascript 20 | import Enum from "es6-enum" 21 | 22 | const ORIENTATION = Enum("NORTH", "SOUTH", "WEST", "EAST") 23 | const ORIEN = Enum("NORTH", "SOUTH", "WEST", "EAST") 24 | 25 | typeof ORIENTATION.NORTH // "symbol" 26 | 27 | ORIENTATION.NORTH === ORIENTATION.NORTH // true 28 | ORIENTATION.NORTH === ORIEN.NORTH // false 29 | 30 | export ORIENTATION 31 | ``` 32 | 33 | ### In practice 34 | I like use this package to define **constant** in **Redux** 35 | 36 | ```javascript 37 | constant.js 38 | const APPLICATION = Enum('USER') 39 | 40 | action.js 41 | dispatch({ 42 | type: APPLICATION.USER, 43 | data 44 | }) 45 | 46 | reducer.js 47 | [APPLICATION.USER]: (state, action) => { 48 | ... 49 | } 50 | 51 | ``` 52 | 53 | 54 | ### License 55 | 56 | MIT 57 | --------------------------------------------------------------------------------