Skip to content

Commit 2bd2a79

Browse files
committed
Implement string enums
0 parents  commit 2bd2a79

11 files changed

+2838
-0
lines changed

.gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Created by https://www.gitignore.io/api/visualstudiocode
2+
3+
### VisualStudioCode ###
4+
.vscode/
5+
6+
dist/
7+
node_modules/
8+
npm-debug.log

.travis.yml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
language: node_js
2+
node_js:
3+
- "node"

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 David Philipson
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.

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# TypeScript String Enums
2+
3+
Typesafe string enums in TypeScript.
4+
5+
[![Build Status](https://travis-ci.org/dphilipson/redux-typescript-reducers.svg?branch=master)](https://travis-ci.org/dphilipson/redux-typescript-reducers)
6+
7+
## Installation
8+
```
9+
npm install --save typescript-string-enums
10+
```
11+
12+
Copyright © 2017 David Philipson

__tests__/test.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Enum } from "../src/index";
2+
3+
describe("Enum", () => {
4+
it("should return an object with values equal to keys", () => {
5+
expect(Enum("BLACK", "WHITE")).toEqual({ BLACK: "BLACK", WHITE: "WHITE" });
6+
});
7+
});

package.json

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{
2+
"name": "typescript-string-enums",
3+
"version": "0.1.0",
4+
"description": "Typesafe string enums in TypeScript.",
5+
"main": "dist/index.js",
6+
"types": "dist/index",
7+
"files": [
8+
"dist/"
9+
],
10+
"repository": {
11+
"type": "git",
12+
"url": "git://github.com/dphilipson/typescript-string-enums.git"
13+
},
14+
"keywords": [
15+
"typescript",
16+
"string",
17+
"enum"
18+
],
19+
"author": "David Philipson <[email protected]> (http://dphil.me)",
20+
"license": "MIT",
21+
"bugs": {
22+
"url": "https://github.com/dphilipson/typescript-string-enums/issues"
23+
},
24+
"homepage": "https://github.com/dphilipson/typescript-string-enums#readme",
25+
"scripts": {
26+
"clean": "rm -rf dist",
27+
"build": "yarn run clean && tsc",
28+
"jest": "jest",
29+
"jest-watch": "yarn run jest -- --watch",
30+
"lint": "tslint --project .",
31+
"prepublish": "yarn run build",
32+
"test": "yarn run lint && tsc --noEmit && yarn run jest",
33+
"watch": "yarn run clean && tsc --watch"
34+
},
35+
"devDependencies": {
36+
"@types/jest": "^16.0.4",
37+
"jest": "^18.1.0",
38+
"ts-jest": "^18.0.1",
39+
"tslint": "^4.3.1",
40+
"typescript": "^2.1.5"
41+
},
42+
"jest": {
43+
"transform": {
44+
".(ts|tsx)": "<rootDir>/node_modules/ts-jest/preprocessor.js"
45+
},
46+
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
47+
"moduleFileExtensions": [
48+
"ts",
49+
"tsx",
50+
"js"
51+
]
52+
}
53+
}

settings.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"typescript.tsdk": "./node_modules/typescript/lib"
3+
}

src/index.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export function Enum<V extends string>(...values: V[]): { [K in V]: K } {
2+
const result: any = {};
3+
values.forEach((value) => result[value] = value);
4+
return result;
5+
}
6+
7+
export type Enum<T> = T[keyof T];

tsconfig.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"compilerOptions": {
3+
"declaration": true,
4+
"module": "commonjs",
5+
"noFallthroughCasesInSwitch": true,
6+
"noImplicitAny": true,
7+
"noImplicitReturns": true,
8+
"noImplicitThis": true,
9+
"noUnusedLocals": true,
10+
"noUnusedParameters": true,
11+
"outDir": "dist/",
12+
"skipLibCheck": true,
13+
"sourceMap": true,
14+
"strictNullChecks": true,
15+
"target": "es5"
16+
},
17+
"files": [
18+
"./src/index.ts"
19+
]
20+
}

tslint.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "tslint:recommended",
3+
"rules": {
4+
"interface-name": [
5+
true, "never-prefix"
6+
],
7+
"object-literal-sort-keys": false
8+
}
9+
}

0 commit comments

Comments
 (0)