@envind/ts-enum-utils 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +36 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,7 +1,15 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
1
7
|
# ts-enum-utils
|
|
2
8
|
|
|
3
9
|
Type-safe runtime enums with built-in utilities for TypeScript.
|
|
4
10
|
|
|
11
|
+
|
|
12
|
+
|
|
5
13
|
## Why?
|
|
6
14
|
|
|
7
15
|
TypeScript's native enums have issues:
|
|
@@ -12,6 +20,8 @@ TypeScript's native enums have issues:
|
|
|
12
20
|
|
|
13
21
|
This library gives you **type-safe enums** with **runtime utilities** in ~30 lines.
|
|
14
22
|
|
|
23
|
+
|
|
24
|
+
|
|
15
25
|
## Installation
|
|
16
26
|
|
|
17
27
|
```bash
|
|
@@ -22,6 +32,8 @@ pnpm add ts-enum-utils
|
|
|
22
32
|
yarn add ts-enum-utils
|
|
23
33
|
```
|
|
24
34
|
|
|
35
|
+
|
|
36
|
+
|
|
25
37
|
## Usage
|
|
26
38
|
|
|
27
39
|
```typescript
|
|
@@ -38,6 +50,8 @@ Status.archived; // "archived"
|
|
|
38
50
|
Status.values; // ["pending", "active", "archived"]
|
|
39
51
|
```
|
|
40
52
|
|
|
53
|
+
|
|
54
|
+
|
|
41
55
|
### Type Guard
|
|
42
56
|
|
|
43
57
|
```typescript
|
|
@@ -49,6 +63,8 @@ if (Status.is(userInput)) {
|
|
|
49
63
|
}
|
|
50
64
|
```
|
|
51
65
|
|
|
66
|
+
|
|
67
|
+
|
|
52
68
|
### Validation with Error
|
|
53
69
|
|
|
54
70
|
```typescript
|
|
@@ -57,6 +73,9 @@ const validated = Status.assert(untrustedData);
|
|
|
57
73
|
// Returns the value if valid, throws otherwise
|
|
58
74
|
```
|
|
59
75
|
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
|
|
60
79
|
### Extract the Type
|
|
61
80
|
|
|
62
81
|
```typescript
|
|
@@ -68,6 +87,8 @@ const updateStatus = (id: string, status: Status) => {
|
|
|
68
87
|
};
|
|
69
88
|
```
|
|
70
89
|
|
|
90
|
+
|
|
91
|
+
|
|
71
92
|
### Utilities
|
|
72
93
|
|
|
73
94
|
```typescript
|
|
@@ -82,8 +103,12 @@ Status.at(0); // "pending"
|
|
|
82
103
|
Status.at(-1); // "archived"
|
|
83
104
|
```
|
|
84
105
|
|
|
106
|
+
|
|
107
|
+
|
|
85
108
|
## API
|
|
86
109
|
|
|
110
|
+
|
|
111
|
+
|
|
87
112
|
### `createEnum(values)`
|
|
88
113
|
|
|
89
114
|
Creates a type-safe enum object.
|
|
@@ -97,6 +122,8 @@ Creates a type-safe enum object.
|
|
|
97
122
|
| `.indexOf(value)` | Returns the index of a value |
|
|
98
123
|
| `.at(index)` | Returns value at index (supports negative) |
|
|
99
124
|
|
|
125
|
+
|
|
126
|
+
|
|
100
127
|
### `EnumValue<E>`
|
|
101
128
|
|
|
102
129
|
Utility type to extract the union type from an enum.
|
|
@@ -106,6 +133,8 @@ type Status = EnumValue<typeof Status>;
|
|
|
106
133
|
// "pending" | "active" | "archived"
|
|
107
134
|
```
|
|
108
135
|
|
|
136
|
+
|
|
137
|
+
|
|
109
138
|
## Comparison
|
|
110
139
|
|
|
111
140
|
| Feature | Native Enum | ts-enum-utils |
|
|
@@ -117,14 +146,17 @@ type Status = EnumValue<typeof Status>;
|
|
|
117
146
|
| Random | ❌ | ✅ `.random()` |
|
|
118
147
|
| Bundle size | - | ~500B |
|
|
119
148
|
|
|
149
|
+
|
|
150
|
+
|
|
120
151
|
## License
|
|
121
152
|
|
|
122
153
|
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
|
123
154
|
|
|
124
|
-
<div align="center">
|
|
125
155
|
|
|
126
|
-
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
**[⭐ Star me on GitHub](https://github.com/envindavsorg/ts-enum-utils)** • **[📦 NPM Package](https://www.npmjs.com/package/@envind/ts-enum-utils)** • **[📚 Documentation](https://github.com/envindavsorg/ts-enum-utils#readme)**
|
|
127
159
|
|
|
128
|
-
|
|
160
|
+
|
|
129
161
|
|
|
130
|
-
|
|
162
|
+
Made with ❤️ by Florin Cuzeac
|