@tolki/enum 0.0.1 → 0.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 +13 -13
- package/dist/enums.d.ts +7 -0
- package/dist/enums.d.ts.map +1 -1
- package/dist/enums.js +19 -17
- package/package.json +8 -6
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Tolki JS Enum Package
|
|
2
2
|
|
|
3
|
-
This package provides enum utility functions similar to
|
|
3
|
+
This package provides enum utility functions similar to a PHP enum class.
|
|
4
4
|
|
|
5
5
|
## Documentation
|
|
6
6
|
|
|
@@ -8,7 +8,7 @@ The full documentation for the enum utilities can be found at [https://tolki.abe
|
|
|
8
8
|
|
|
9
9
|
<!-- AUTO-GENERATED-DOCS:START -->
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
## Enum Utilities Installation
|
|
12
12
|
|
|
13
13
|
The [`@tolki/enum`](https://www.npmjs.com/package/@tolki/enum) package provides a variety of enum manipulation utilities inspired by PHP's enum utilities like [from](https://www.php.net/manual/en/backedenum.from.php), [tryFrom](https://www.php.net/manual/en/backedenum.tryfrom.php), and [cases](https://www.php.net/manual/en/unitenum.cases.php).
|
|
14
14
|
|
|
@@ -28,17 +28,17 @@ yarn add @tolki/enum
|
|
|
28
28
|
pnpm add @tolki/enum
|
|
29
29
|
```
|
|
30
30
|
|
|
31
|
-
|
|
31
|
+
## Tolki Enum Utilities List
|
|
32
32
|
|
|
33
|
-
|
|
33
|
+
### Enum Utilities List
|
|
34
34
|
|
|
35
35
|
As mentioned in the [Enum Utilities Installation](./index.md) page, the `@tolki/enum` package is not meant to be used as standalone package as it works with the [Laravel TypeScript Publisher](https://github.com/abetwothree/laravel-ts-publish) package to transform PHP enums into TypeScript enums. The functions below are being listed out for reference, not necessarily for direct use.
|
|
36
36
|
|
|
37
37
|
[cases](#cases) [defineEnum](#defineenum) [from](#from) [tryFrom](#tryfrom)
|
|
38
38
|
|
|
39
|
-
|
|
39
|
+
### Enum Utilities Details
|
|
40
40
|
|
|
41
|
-
|
|
41
|
+
#### cases
|
|
42
42
|
|
|
43
43
|
Similar to the PHP [cases](https://www.php.net/manual/en/unitenum.cases.php) method, this function returns an array of all the cases of the given enum.
|
|
44
44
|
|
|
@@ -49,7 +49,7 @@ import { Status } from "@js/types/enums";
|
|
|
49
49
|
const result = cases(Status); // result is an array with an enum instance for each case in the Status enum
|
|
50
50
|
```
|
|
51
51
|
|
|
52
|
-
|
|
52
|
+
#### defineEnum
|
|
53
53
|
|
|
54
54
|
This is a factory function that is automatically applied by the Laravel TypeScript Publisher package when it transforms PHP enums into TypeScript enums. It automatically adds the `cases`, `from`, and `tryFrom` methods to the transformed TypeScript enums.
|
|
55
55
|
|
|
@@ -71,7 +71,7 @@ Status.from("active"); // result is the enum instance for the ACTIVE case
|
|
|
71
71
|
Status.tryFrom("non-valid-key"); // result null
|
|
72
72
|
```
|
|
73
73
|
|
|
74
|
-
|
|
74
|
+
#### from
|
|
75
75
|
|
|
76
76
|
Similar to the PHP [from](https://www.php.net/manual/en/backedenum.from.php) method, this function returns the enum instance for the given value. If the value does not exist in the enum, it throws an error.
|
|
77
77
|
|
|
@@ -83,7 +83,7 @@ const result = from(Status, "active"); // result is the enum instance for the AC
|
|
|
83
83
|
const result2 = from(Status, "non-valid-key"); // throws an error because "non-valid-key" is not a valid value
|
|
84
84
|
```
|
|
85
85
|
|
|
86
|
-
|
|
86
|
+
#### tryFrom
|
|
87
87
|
|
|
88
88
|
Similar to the PHP [tryFrom](https://www.php.net/manual/en/backedenum.tryfrom.php) method, this function returns the enum instance for the given value. If the value does not exist in the enum, it returns null instead of throwing an error.
|
|
89
89
|
|
|
@@ -95,13 +95,13 @@ const result = tryFrom(Status, "active"); // result is the enum instance for the
|
|
|
95
95
|
const result2 = tryFrom(Status, "non-valid-key"); // result is null because "non-valid-key" is not a valid value
|
|
96
96
|
```
|
|
97
97
|
|
|
98
|
-
|
|
98
|
+
## Tolki Enum Laravel TypeScript Publisher Vite Plugin
|
|
99
99
|
|
|
100
100
|
The `@tolki/enum` package provides a Vite plugin to automatically watch for changes of the transformed PHP files by the [Laravel TypeScript Publisher](https://github.com/abetwothree/laravel-ts-publish) package.
|
|
101
101
|
|
|
102
102
|
The Laravel TypeScript Publisher package can publish a JSON file list of transformed PHP files. This Vite plugin uses that JSON file list to watch for changes in those PHP files and automatically call the `php artisan ts:publish` command to transform the changed PHP files into TypeScript files.
|
|
103
103
|
|
|
104
|
-
|
|
104
|
+
### Usage
|
|
105
105
|
|
|
106
106
|
To use the Vite plugin, you need to add it to your Vite configuration file. Below is an example of how to add the plugin to your Vite configuration file:
|
|
107
107
|
|
|
@@ -114,7 +114,7 @@ export default defineConfig({
|
|
|
114
114
|
});
|
|
115
115
|
```
|
|
116
116
|
|
|
117
|
-
|
|
117
|
+
### Default Functionality
|
|
118
118
|
|
|
119
119
|
By default, the plugin will work in the following way:
|
|
120
120
|
|
|
@@ -124,7 +124,7 @@ By default, the plugin will work in the following way:
|
|
|
124
124
|
4. It will call the publish command on `vite build` before bundling.
|
|
125
125
|
5. It will throw an error if the publish command fails on `vite build`.
|
|
126
126
|
|
|
127
|
-
|
|
127
|
+
### Plugin Options
|
|
128
128
|
|
|
129
129
|
The plugin accepts an options object to customize its behavior. It is recommended to use `.env` config settings to sync settings between the PHP side and the Vite plugin for the `filename` and `directory` options.
|
|
130
130
|
|
package/dist/enums.d.ts
CHANGED
|
@@ -9,6 +9,7 @@ import { CaseValue, DefineEnumResult, EnumConst, ToEnumResult } from '../../type
|
|
|
9
9
|
* The enumObj is the published enum from the Laravel TypeScript Publish package.
|
|
10
10
|
*
|
|
11
11
|
* @see https://github.com/abetwothree/laravel-ts-publish#enum-metadata--tolki-enum-package
|
|
12
|
+
* @see https://tolki.abe.dev/enums/enum-utilities-list.html#from
|
|
12
13
|
*
|
|
13
14
|
* @param enumObj - The enum const object to resolve from.
|
|
14
15
|
* @param value - The case value to resolve. Must be one of the values defined in the enum const's cases.
|
|
@@ -21,6 +22,8 @@ export declare function from<TEnum extends EnumConst, const TValue extends CaseV
|
|
|
21
22
|
*
|
|
22
23
|
* Mirrors PHP's `BackedEnum::tryFrom()`.
|
|
23
24
|
*
|
|
25
|
+
* @see https://tolki.abe.dev/enums/enum-utilities-list.html#tryfrom
|
|
26
|
+
*
|
|
24
27
|
* @param enumObj - The enum const object to resolve from.
|
|
25
28
|
* @param value - The case value to resolve.
|
|
26
29
|
* @returns The resolved enum case, or null if the value does not match any case.
|
|
@@ -31,6 +34,8 @@ export declare function tryFrom<TEnum extends EnumConst, const TValue extends Ca
|
|
|
31
34
|
*
|
|
32
35
|
* Mirrors PHP's `BackedEnum::cases()`, returning one resolved instance per case defined in the enum const.
|
|
33
36
|
*
|
|
37
|
+
* @see https://tolki.abe.dev/enums/enum-utilities-list.html#cases
|
|
38
|
+
*
|
|
34
39
|
* @param enumObj - The enum const object to resolve all cases from.
|
|
35
40
|
* @returns An array of resolved enum instances, one for each case.
|
|
36
41
|
*/
|
|
@@ -44,6 +49,8 @@ export declare function cases<TEnum extends EnumConst>(enumObj: TEnum): Array<To
|
|
|
44
49
|
*
|
|
45
50
|
* The original enum data is preserved and the PHP like enum methods are attached directly to the returned object.
|
|
46
51
|
*
|
|
52
|
+
* @see https://tolki.abe.dev/enums/enum-utilities-list.html#defineenum
|
|
53
|
+
*
|
|
47
54
|
* @param enumObj - The raw enum const object to enrich.
|
|
48
55
|
* @returns The enum object with `from`, `tryFrom`, and `cases` methods bound to it.
|
|
49
56
|
*/
|
package/dist/enums.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"enums.d.ts","sourceRoot":"","sources":["../src/enums.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACR,SAAS,EACT,gBAAgB,EAChB,SAAS,EACT,YAAY,EACf,MAAM,cAAc,CAAC;AAEtB
|
|
1
|
+
{"version":3,"file":"enums.d.ts","sourceRoot":"","sources":["../src/enums.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACR,SAAS,EACT,gBAAgB,EAChB,SAAS,EACT,YAAY,EACf,MAAM,cAAc,CAAC;AAEtB;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,IAAI,CAChB,KAAK,SAAS,SAAS,EACvB,KAAK,CAAC,MAAM,SAAS,SAAS,CAAC,KAAK,CAAC,EACvC,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,GAAG,YAAY,CAAC,KAAK,EAAE,MAAM,CAAC,CA4C5D;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,OAAO,CACnB,KAAK,SAAS,SAAS,EACvB,KAAK,CAAC,MAAM,SAAS,SAAS,CAAC,KAAK,CAAC,EACvC,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,GAAG,YAAY,CAAC,KAAK,EAAE,MAAM,CAAC,GAAG,IAAI,CAMnE;AAED;;;;;;;;;GASG;AACH,wBAAgB,KAAK,CAAC,KAAK,SAAS,SAAS,EACzC,OAAO,EAAE,KAAK,GACf,KAAK,CAAC,YAAY,CAAC,KAAK,EAAE,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC,CAI9C;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,UAAU,CAAC,KAAK,CAAC,KAAK,SAAS,SAAS,EACpD,OAAO,EAAE,KAAK,GACf,gBAAgB,CAAC,KAAK,CAAC,CA2BzB"}
|
package/dist/enums.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
function
|
|
1
|
+
function a(e, t) {
|
|
2
2
|
const n = new Set(e._cases);
|
|
3
3
|
let r;
|
|
4
4
|
for (const s of n)
|
|
@@ -6,39 +6,41 @@ function c(e, t) {
|
|
|
6
6
|
r = s;
|
|
7
7
|
break;
|
|
8
8
|
}
|
|
9
|
-
if (!r)
|
|
9
|
+
if (!r) {
|
|
10
|
+
const s = Array.from(n).map((o) => e[o]);
|
|
10
11
|
throw new Error(
|
|
11
|
-
`Value "${String(t)}" does not match any case in the enum. Cases: ${[...n].join(", ")}`
|
|
12
|
+
`Value "${String(t)}" does not match any case in the enum. Values: ${s.join(", ")}, Cases: ${[...n].join(", ")}`
|
|
12
13
|
);
|
|
13
|
-
|
|
14
|
-
|
|
14
|
+
}
|
|
15
|
+
const c = { value: t }, h = new Set(e._methods), i = e._helpers, f = new Set(
|
|
16
|
+
Array.isArray(i) ? i : []
|
|
15
17
|
), l = /* @__PURE__ */ new Set([
|
|
16
18
|
"_cases",
|
|
17
19
|
"_methods",
|
|
18
20
|
"_helpers",
|
|
19
21
|
"_static"
|
|
20
22
|
]);
|
|
21
|
-
for (const [s,
|
|
22
|
-
l.has(s) || n.has(s) || f.has(s) || (h.has(s) ?
|
|
23
|
-
return
|
|
23
|
+
for (const [s, o] of Object.entries(e))
|
|
24
|
+
l.has(s) || n.has(s) || f.has(s) || (h.has(s) ? c[s] = o[r] : c[s] = o);
|
|
25
|
+
return c;
|
|
24
26
|
}
|
|
25
27
|
function y(e, t) {
|
|
26
28
|
try {
|
|
27
|
-
return
|
|
29
|
+
return a(e, t);
|
|
28
30
|
} catch {
|
|
29
31
|
return null;
|
|
30
32
|
}
|
|
31
33
|
}
|
|
32
|
-
function
|
|
34
|
+
function p(e) {
|
|
33
35
|
return e._cases.map(
|
|
34
|
-
(t) =>
|
|
36
|
+
(t) => a(e, e[t])
|
|
35
37
|
);
|
|
36
38
|
}
|
|
37
|
-
function
|
|
39
|
+
function u(e) {
|
|
38
40
|
const t = {
|
|
39
|
-
from: (r) =>
|
|
41
|
+
from: (r) => a(e, r),
|
|
40
42
|
tryFrom: (r) => y(e, r),
|
|
41
|
-
cases: () =>
|
|
43
|
+
cases: () => p(e)
|
|
42
44
|
}, n = Object.keys(t);
|
|
43
45
|
return Object.assign(
|
|
44
46
|
/* @__PURE__ */ Object.create(null),
|
|
@@ -50,8 +52,8 @@ function p(e) {
|
|
|
50
52
|
);
|
|
51
53
|
}
|
|
52
54
|
export {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
55
|
+
p as cases,
|
|
56
|
+
u as defineEnum,
|
|
57
|
+
a as from,
|
|
56
58
|
y as tryFrom
|
|
57
59
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tolki/enum",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2",
|
|
4
4
|
"description": "A set of enum utilities for the @tolki packages.",
|
|
5
5
|
"author": "Abraham Arango <https://github.com/abetwothree>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"utilities"
|
|
24
24
|
],
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@tolki/types": "^1.1.
|
|
26
|
+
"@tolki/types": "^1.1.1"
|
|
27
27
|
},
|
|
28
28
|
"peerDependencies": {
|
|
29
29
|
"vite": "^7.0.0"
|
|
@@ -39,12 +39,14 @@
|
|
|
39
39
|
"require": "./dist/index.js"
|
|
40
40
|
},
|
|
41
41
|
"./enums": {
|
|
42
|
-
"types": "./dist/enums
|
|
43
|
-
"import": "./dist/enums
|
|
42
|
+
"types": "./dist/enums.d.ts",
|
|
43
|
+
"import": "./dist/enums.js",
|
|
44
|
+
"default": "./dist/enums.js"
|
|
44
45
|
},
|
|
45
46
|
"./vite": {
|
|
46
|
-
"types": "./dist/vite-plugin
|
|
47
|
-
"import": "./dist/vite-plugin
|
|
47
|
+
"types": "./dist/vite-plugin.d.ts",
|
|
48
|
+
"import": "./dist/vite-plugin.js",
|
|
49
|
+
"default": "./dist/vite-plugin.js"
|
|
48
50
|
}
|
|
49
51
|
},
|
|
50
52
|
"tolki": {
|