@envind/ts-enum-utils 1.0.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Cuzeac Florin
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.
package/README.md ADDED
@@ -0,0 +1,135 @@
1
+ # ts-enum-utils
2
+
3
+ [![npm version](https://img.shields.io/npm/v/ts-enum-utils.svg)](https://www.npmjs.com/package/ts-enum-utils)
4
+ [![TypeScript](https://img.shields.io/badge/TypeScript-5.0%2B-blue)](https://www.typescriptlang.org/)
5
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
6
+ [![Bundle Size](https://img.shields.io/bundlephobia/minzip/ts-enum-utils)](https://bundlephobia.com/package/ts-enum-utils)
7
+
8
+ Type-safe runtime enums with built-in utilities for TypeScript.
9
+
10
+ ## Why?
11
+
12
+ TypeScript's native enums have issues:
13
+
14
+ - Not iterable without hacks
15
+ - Weird reverse mapping behavior
16
+ - No built-in validation
17
+
18
+ This library gives you **type-safe enums** with **runtime utilities** in ~30 lines.
19
+
20
+ ## Installation
21
+
22
+ ```bash
23
+ npm install ts-enum-utils
24
+ # or
25
+ pnpm add ts-enum-utils
26
+ # or
27
+ yarn add ts-enum-utils
28
+ ```
29
+
30
+ ## Usage
31
+
32
+ ```typescript
33
+ import { createEnum, type EnumValue } from "ts-enum-utils";
34
+
35
+ const Status = createEnum(["pending", "active", "archived"] as const);
36
+
37
+ // Direct value access (with autocomplete)
38
+ Status.pending; // "pending"
39
+ Status.active; // "active"
40
+ Status.archived; // "archived"
41
+
42
+ // Get all values
43
+ Status.values; // ["pending", "active", "archived"]
44
+ ```
45
+
46
+ ### Type Guard
47
+
48
+ ```typescript
49
+ const userInput: unknown = "pending";
50
+
51
+ if (Status.is(userInput)) {
52
+ // userInput is now typed as "pending" | "active" | "archived"
53
+ console.log(userInput);
54
+ }
55
+ ```
56
+
57
+ ### Validation with Error
58
+
59
+ ```typescript
60
+ // Throws if invalid
61
+ const validated = Status.assert(untrustedData);
62
+ // Returns the value if valid, throws otherwise
63
+ ```
64
+
65
+ ### Extract the Type
66
+
67
+ ```typescript
68
+ type Status = EnumValue<typeof Status>;
69
+ // "pending" | "active" | "archived"
70
+
71
+ const updateStatus = (id: string, status: Status) => {
72
+ // ...
73
+ };
74
+ ```
75
+
76
+ ### Utilities
77
+
78
+ ```typescript
79
+ // Random value (great for tests/mocks)
80
+ Status.random(); // "pending" | "active" | "archived"
81
+
82
+ // Get index
83
+ Status.indexOf("active"); // 1
84
+
85
+ // Get by index (supports negative)
86
+ Status.at(0); // "pending"
87
+ Status.at(-1); // "archived"
88
+ ```
89
+
90
+ ## API
91
+
92
+ ### `createEnum(values)`
93
+
94
+ Creates a type-safe enum object.
95
+
96
+ | Method | Description |
97
+ | ----------------- | ------------------------------------------ |
98
+ | `.values` | Readonly array of all enum values |
99
+ | `.is(value)` | Type guard that checks if value is valid |
100
+ | `.assert(value)` | Returns value if valid, throws otherwise |
101
+ | `.random()` | Returns a random enum value |
102
+ | `.indexOf(value)` | Returns the index of a value |
103
+ | `.at(index)` | Returns value at index (supports negative) |
104
+
105
+ ### `EnumValue<E>`
106
+
107
+ Utility type to extract the union type from an enum.
108
+
109
+ ```typescript
110
+ type Status = EnumValue<typeof Status>;
111
+ // "pending" | "active" | "archived"
112
+ ```
113
+
114
+ ## Comparison
115
+
116
+ | Feature | Native Enum | ts-enum-utils |
117
+ | ----------- | ----------- | -------------- |
118
+ | Type-safe | ✅ | ✅ |
119
+ | Iterable | ❌ | ✅ `.values` |
120
+ | Type guard | ❌ | ✅ `.is()` |
121
+ | Validation | ❌ | ✅ `.assert()` |
122
+ | Random | ❌ | ✅ `.random()` |
123
+ | Bundle size | - | ~500B |
124
+
125
+ ## License
126
+
127
+ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
128
+
129
+ <div align="center">
130
+
131
+ **[⭐ Star us on GitHub](https://github.com/envindavsorg/ts-enum-utils)** • **[📦 NPM Package](https://www.npmjs.com/package/ts-enum-utils)** • **[📚 Documentation](https://github.com/envindavsorg/ts-enum-utils#readme)**
132
+
133
+ Made with ❤️ by Cuzeac Florin in Paris.
134
+
135
+ </div>
package/dist/index.cjs ADDED
@@ -0,0 +1,2 @@
1
+ 'use strict';var s=e=>{if(e.length===0)throw new Error("Enum must have at least one value");if(new Set(e).size!==e.length)throw new Error("Enum values must be unique");let r=new Set(e),t={values:e,is:n=>typeof n=="string"&&r.has(n),assert:n=>{if(typeof n=="string"&&r.has(n))return n;throw new Error(`Invalid enum value: "${n}". Expected one of: ${e.join(", ")}`)},random:()=>{let n=Math.floor(Math.random()*e.length);return e[n]},indexOf:n=>e.indexOf(n),at:n=>e.at(n)},u=Object.fromEntries(e.map(n=>[n,n]));return Object.freeze({...t,...u})};exports.createEnum=s;//# sourceMappingURL=index.cjs.map
2
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"names":["createEnum","values","valueSet","base","value","index","members","v"],"mappings":"aAiBO,IAAMA,CAAAA,CAAiDC,CAAAA,EAAuB,CACnF,GAAIA,CAAAA,CAAO,MAAA,GAAW,CAAA,CACpB,MAAM,IAAI,KAAA,CAAM,mCAAmC,CAAA,CAIrD,GADqB,IAAI,GAAA,CAAIA,CAAM,CAAA,CAClB,IAAA,GAASA,CAAAA,CAAO,MAAA,CAC/B,MAAM,IAAI,KAAA,CAAM,4BAA4B,CAAA,CAG9C,IAAMC,CAAAA,CAAW,IAAI,GAAA,CAAYD,CAAM,CAAA,CAEjCE,CAAAA,CAAoB,CACxB,MAAA,CAAAF,CAAAA,CAEA,EAAA,CAAKG,CAAAA,EAAuC,OAAOA,CAAAA,EAAU,QAAA,EAAYF,CAAAA,CAAS,GAAA,CAAIE,CAAK,CAAA,CAE3F,MAAA,CAASA,CAAAA,EAA8B,CACrC,GAAI,OAAOA,CAAAA,EAAU,QAAA,EAAYF,CAAAA,CAAS,GAAA,CAAIE,CAAK,CAAA,CACjD,OAAOA,CAAAA,CAET,MAAM,IAAI,KAAA,CAAM,CAAA,qBAAA,EAAwBA,CAAK,CAAA,oBAAA,EAAuBH,CAAAA,CAAO,IAAA,CAAK,IAAI,CAAC,CAAA,CAAE,CACzF,CAAA,CAEA,MAAA,CAAQ,IAAiB,CACvB,IAAMI,CAAAA,CAAQ,IAAA,CAAK,KAAA,CAAM,IAAA,CAAK,MAAA,EAAO,CAAIJ,CAAAA,CAAO,MAAM,CAAA,CACtD,OAAOA,CAAAA,CAAOI,CAAK,CACrB,CAAA,CAEA,OAAA,CAAUD,CAAAA,EAA6BH,CAAAA,CAAO,OAAA,CAAQG,CAAK,CAAA,CAE3D,EAAA,CAAKC,CAAAA,EAAyCJ,CAAAA,CAAO,EAAA,CAAGI,CAAK,CAC/D,CAAA,CAEMC,CAAAA,CAAU,MAAA,CAAO,WAAA,CAAYL,CAAAA,CAAO,GAAA,CAAKM,CAAAA,EAAM,CAACA,CAAAA,CAAGA,CAAC,CAAC,CAAC,CAAA,CAE5D,OAAO,MAAA,CAAO,MAAA,CAAO,CAAE,GAAGJ,CAAAA,CAAM,GAAGG,CAAQ,CAAC,CAC9C","file":"index.cjs","sourcesContent":["type EnumBase<T extends readonly string[]> = {\n readonly values: T;\n is: (value: unknown) => value is T[number];\n assert: (value: unknown) => T[number];\n random: () => T[number];\n indexOf: (value: T[number]) => number;\n at: (index: number) => T[number] | undefined;\n};\n\ntype EnumMembers<T extends readonly string[]> = {\n readonly [K in T[number]]: K;\n};\n\nexport type Enum<T extends readonly string[]> = EnumBase<T> & EnumMembers<T>;\n\nexport type EnumValue<E extends { values: readonly string[] }> = E[\"values\"][number];\n\nexport const createEnum = <const T extends readonly string[]>(values: T): Enum<T> => {\n if (values.length === 0) {\n throw new Error(\"Enum must have at least one value\");\n }\n\n const uniqueValues = new Set(values);\n if (uniqueValues.size !== values.length) {\n throw new Error(\"Enum values must be unique\");\n }\n\n const valueSet = new Set<string>(values);\n\n const base: EnumBase<T> = {\n values,\n\n is: (value: unknown): value is T[number] => typeof value === \"string\" && valueSet.has(value),\n\n assert: (value: unknown): T[number] => {\n if (typeof value === \"string\" && valueSet.has(value)) {\n return value as T[number];\n }\n throw new Error(`Invalid enum value: \"${value}\". Expected one of: ${values.join(\", \")}`);\n },\n\n random: (): T[number] => {\n const index = Math.floor(Math.random() * values.length);\n return values[index] as T[number];\n },\n\n indexOf: (value: T[number]): number => values.indexOf(value),\n\n at: (index: number): T[number] | undefined => values.at(index) as T[number] | undefined,\n };\n\n const members = Object.fromEntries(values.map((v) => [v, v])) as EnumMembers<T>;\n\n return Object.freeze({ ...base, ...members }) as Enum<T>;\n};\n"]}
@@ -0,0 +1,18 @@
1
+ type EnumBase<T extends readonly string[]> = {
2
+ readonly values: T;
3
+ is: (value: unknown) => value is T[number];
4
+ assert: (value: unknown) => T[number];
5
+ random: () => T[number];
6
+ indexOf: (value: T[number]) => number;
7
+ at: (index: number) => T[number] | undefined;
8
+ };
9
+ type EnumMembers<T extends readonly string[]> = {
10
+ readonly [K in T[number]]: K;
11
+ };
12
+ type Enum<T extends readonly string[]> = EnumBase<T> & EnumMembers<T>;
13
+ type EnumValue<E extends {
14
+ values: readonly string[];
15
+ }> = E["values"][number];
16
+ declare const createEnum: <const T extends readonly string[]>(values: T) => Enum<T>;
17
+
18
+ export { type Enum, type EnumValue, createEnum };
@@ -0,0 +1,18 @@
1
+ type EnumBase<T extends readonly string[]> = {
2
+ readonly values: T;
3
+ is: (value: unknown) => value is T[number];
4
+ assert: (value: unknown) => T[number];
5
+ random: () => T[number];
6
+ indexOf: (value: T[number]) => number;
7
+ at: (index: number) => T[number] | undefined;
8
+ };
9
+ type EnumMembers<T extends readonly string[]> = {
10
+ readonly [K in T[number]]: K;
11
+ };
12
+ type Enum<T extends readonly string[]> = EnumBase<T> & EnumMembers<T>;
13
+ type EnumValue<E extends {
14
+ values: readonly string[];
15
+ }> = E["values"][number];
16
+ declare const createEnum: <const T extends readonly string[]>(values: T) => Enum<T>;
17
+
18
+ export { type Enum, type EnumValue, createEnum };
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ var s=e=>{if(e.length===0)throw new Error("Enum must have at least one value");if(new Set(e).size!==e.length)throw new Error("Enum values must be unique");let r=new Set(e),t={values:e,is:n=>typeof n=="string"&&r.has(n),assert:n=>{if(typeof n=="string"&&r.has(n))return n;throw new Error(`Invalid enum value: "${n}". Expected one of: ${e.join(", ")}`)},random:()=>{let n=Math.floor(Math.random()*e.length);return e[n]},indexOf:n=>e.indexOf(n),at:n=>e.at(n)},u=Object.fromEntries(e.map(n=>[n,n]));return Object.freeze({...t,...u})};export{s as createEnum};//# sourceMappingURL=index.js.map
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"names":["createEnum","values","valueSet","base","value","index","members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,CAE5D,OAAO,MAAA,CAAO,MAAA,CAAO,CAAE,GAAGJ,CAAAA,CAAM,GAAGG,CAAQ,CAAC,CAC9C","file":"index.js","sourcesContent":["type EnumBase<T extends readonly string[]> = {\n readonly values: T;\n is: (value: unknown) => value is T[number];\n assert: (value: unknown) => T[number];\n random: () => T[number];\n indexOf: (value: T[number]) => number;\n at: (index: number) => T[number] | undefined;\n};\n\ntype EnumMembers<T extends readonly string[]> = {\n readonly [K in T[number]]: K;\n};\n\nexport type Enum<T extends readonly string[]> = EnumBase<T> & EnumMembers<T>;\n\nexport type EnumValue<E extends { values: readonly string[] }> = E[\"values\"][number];\n\nexport const createEnum = <const T extends readonly string[]>(values: T): Enum<T> => {\n if (values.length === 0) {\n throw new Error(\"Enum must have at least one value\");\n }\n\n const uniqueValues = new Set(values);\n if (uniqueValues.size !== values.length) {\n throw new Error(\"Enum values must be unique\");\n }\n\n const valueSet = new Set<string>(values);\n\n const base: EnumBase<T> = {\n values,\n\n is: (value: unknown): value is T[number] => typeof value === \"string\" && valueSet.has(value),\n\n assert: (value: unknown): T[number] => {\n if (typeof value === \"string\" && valueSet.has(value)) {\n return value as T[number];\n }\n throw new Error(`Invalid enum value: \"${value}\". Expected one of: ${values.join(\", \")}`);\n },\n\n random: (): T[number] => {\n const index = Math.floor(Math.random() * values.length);\n return values[index] as T[number];\n },\n\n indexOf: (value: T[number]): number => values.indexOf(value),\n\n at: (index: number): T[number] | undefined => values.at(index) as T[number] | undefined,\n };\n\n const members = Object.fromEntries(values.map((v) => [v, v])) as EnumMembers<T>;\n\n return Object.freeze({ ...base, ...members }) as Enum<T>;\n};\n"]}
package/package.json ADDED
@@ -0,0 +1,63 @@
1
+ {
2
+ "name": "@envind/ts-enum-utils",
3
+ "version": "1.0.0",
4
+ "description": "Type-safe runtime enums with built-in utilities for TypeScript",
5
+ "author": "Florin Cuzeac",
6
+ "license": "MIT",
7
+ "type": "module",
8
+ "main": "./dist/index.cjs",
9
+ "module": "./dist/index.js",
10
+ "types": "./dist/index.d.ts",
11
+ "exports": {
12
+ ".": {
13
+ "import": {
14
+ "types": "./dist/index.d.ts",
15
+ "default": "./dist/index.js"
16
+ },
17
+ "require": {
18
+ "types": "./dist/index.d.cts",
19
+ "default": "./dist/index.cjs"
20
+ }
21
+ }
22
+ },
23
+ "files": [
24
+ "dist"
25
+ ],
26
+ "scripts": {
27
+ "build": "tsup",
28
+ "dev": "tsup --watch",
29
+ "test": "vitest run",
30
+ "test:watch": "vitest",
31
+ "lint": "biome check .",
32
+ "lint:fix": "biome check --write .",
33
+ "format": "biome format --write .",
34
+ "prepublishOnly": "pnpm run lint && pnpm run test && pnpm run build"
35
+ },
36
+ "devDependencies": {
37
+ "@biomejs/biome": "^1.9.0",
38
+ "@types/node": "^22.0.0",
39
+ "tsup": "^8.3.0",
40
+ "typescript": "^5.6.0",
41
+ "vitest": "^2.1.0"
42
+ },
43
+ "keywords": [
44
+ "typescript",
45
+ "enum",
46
+ "type-safe",
47
+ "runtime",
48
+ "validation",
49
+ "utilities"
50
+ ],
51
+ "repository": {
52
+ "type": "git",
53
+ "url": "git+https://github.com/envindavsorg/ts-enum-utils.git"
54
+ },
55
+ "bugs": {
56
+ "url": "https://github.com/envindavsorg/ts-enum-utils/issues"
57
+ },
58
+ "homepage": "https://github.com/envindavsorg/ts-enum-utils#readme",
59
+ "engines": {
60
+ "node": ">=18"
61
+ },
62
+ "packageManager": "pnpm@10.25.0"
63
+ }