@reharik/smart-enum 0.4.6 → 0.4.8
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 +56 -0
- package/package.json +2 -1
package/README.md
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# @reharik/smart-enum
|
|
2
|
+
|
|
3
|
+
Type-safe, feature-rich enumerations for TypeScript. Every member is a frozen object that carries its own wire value, display label, ordering, and any custom fields you give it — with runtime lookup, iteration, serialization, and database revival built in.
|
|
4
|
+
|
|
5
|
+
📖 **Full documentation:** https://reharik.github.io/smart-enums/
|
|
6
|
+
|
|
7
|
+
## The idea
|
|
8
|
+
|
|
9
|
+
Have you ever followed one value — an order status, say — through a codebase and counted how many places had to know about it? The database stores `'ACTIVE'`, the API ships `'ACTIVE'`, a `<select>` uses it, a labels map turns it into `'Active'`, an array lists every option, and a few `if`s compare it. Add `'ARCHIVED'` and there are five places to update. Miss one and it's not carelessness — those places were never connected. The language gave you a bare `string`, so the concept scattered.
|
|
10
|
+
|
|
11
|
+
A smart enum lets that concept be **one object that knows everything about itself**:
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { enumeration, type Enumeration } from '@reharik/smart-enum';
|
|
15
|
+
|
|
16
|
+
const Status = enumeration('Status', {
|
|
17
|
+
input: ['pending', 'active', 'completed'] as const,
|
|
18
|
+
});
|
|
19
|
+
type Status = Enumeration<typeof Status>;
|
|
20
|
+
|
|
21
|
+
Status.active; // { key: 'active', value: 'ACTIVE', display: 'Active', index: 1 }
|
|
22
|
+
Status.active.display; // 'Active' — the label lives with the value
|
|
23
|
+
Status.fromValue('ACTIVE'); // Status.active — runtime lookup, type-narrowed
|
|
24
|
+
Status.items(); // every member, in order — your dropdown options
|
|
25
|
+
Status.values(); // ['PENDING','ACTIVE','COMPLETED'] — your validator set
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
The options list, wire value, label, valid-set, and ordering are all the same object, defined once. There's no fifth place to forget.
|
|
29
|
+
|
|
30
|
+
## Why people reach for it
|
|
31
|
+
|
|
32
|
+
- **Metadata that travels with the value** — attach `status`, `retryable`, `column`, anything, and read it off the member at runtime instead of from a parallel map.
|
|
33
|
+
- **Lookup without boilerplate** — `fromValue` / `fromKey` (and `try*` variants), all type-narrowed to the enum's members.
|
|
34
|
+
- **Survives the boundary** — members serialize to self-describing JSON and revive into the *same* instances across a network, a database, or a full GraphQL stack. A value that left as `Status.active` comes back knowing it is.
|
|
35
|
+
- **Tiny and lock-in-free** — ~600 bytes full, ~149 for just `enumeration` via [entry points](https://reharik.github.io/smart-enums/core/guards-and-entry-points); output is plain frozen objects and ordinary JSON.
|
|
36
|
+
|
|
37
|
+
## Install
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
npm install @reharik/smart-enum
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Then read the [five-minute quick start](https://reharik.github.io/smart-enums/guide/quick-start), or [Coming from TypeScript enums](https://reharik.github.io/smart-enums/guide/coming-from-enums) if you're migrating.
|
|
44
|
+
|
|
45
|
+
## The ecosystem
|
|
46
|
+
|
|
47
|
+
| Package | Purpose |
|
|
48
|
+
| --- | --- |
|
|
49
|
+
| [`@reharik/smart-enum-knex`](https://www.npmjs.com/package/@reharik/smart-enum-knex) | Knex query-level enum revival via `postProcessResponse` |
|
|
50
|
+
| [`@reharik/graphql-codegen-smart-enum`](https://www.npmjs.com/package/@reharik/graphql-codegen-smart-enum) | Generate smart-enum definitions from GraphQL schema enums |
|
|
51
|
+
| [`@reharik/graphql-codegen-smart-enum-type-policies`](https://www.npmjs.com/package/@reharik/graphql-codegen-smart-enum-type-policies) | Apollo `typePolicies` for client-side enum rehydration |
|
|
52
|
+
| [`@reharik/graphql-codegen-smart-enum-preset`](https://www.npmjs.com/package/@reharik/graphql-codegen-smart-enum-preset) | Codegen preset that wires the whole stack with zero per-enum config |
|
|
53
|
+
|
|
54
|
+
## License
|
|
55
|
+
|
|
56
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@reharik/smart-enum",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.8",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -40,6 +40,7 @@
|
|
|
40
40
|
},
|
|
41
41
|
"scripts": {
|
|
42
42
|
"clean": "rm -rf dist",
|
|
43
|
+
"prepack": "cp ../../README.md README.md",
|
|
43
44
|
"build": "npm run clean && tsup",
|
|
44
45
|
"prepublishOnly": "npm run build",
|
|
45
46
|
"test": "jest --config jest.config.cjs",
|