@tolki/enum 0.0.0 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # Tolki JS Enum Package
2
2
 
3
- This package provides enum utility functions similar to Laravel's Enum class facade.
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
- # Enum Utilities Installation
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
- # Tolki Enum Utilities List
31
+ ## Tolki Enum Utilities List
32
32
 
33
- ## Enum Utilities List
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
- ## Enum Utilities Details
39
+ ### Enum Utilities Details
40
40
 
41
- ### cases
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
- ### defineEnum
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
- ### from
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
- ### tryFrom
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,26 +95,26 @@ 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
- # Tolki Enum Laravel TypeScript Publisher Vite Plugin
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
- ## Usage
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
 
108
108
  ```javascript
109
109
  import { defineConfig } from "vite";
110
- import { laravelTsPublish } from "@tolki/enum";
110
+ import { laravelTsPublish } from "@tolki/enum/vite";
111
111
 
112
112
  export default defineConfig({
113
113
  plugins: [laravelTsPublish()],
114
114
  });
115
115
  ```
116
116
 
117
- ## Default Functionality
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
- ## Plugin Options
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
 
@@ -132,7 +132,7 @@ Below are the available options with a description and default values:
132
132
 
133
133
  ```javascript
134
134
  import { defineConfig } from "vite";
135
- import { laravelTsPublish } from "@tolki/enum";
135
+ import { laravelTsPublish } from "@tolki/enum/vite";
136
136
 
137
137
  export default defineConfig({
138
138
  plugins: [
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
  */
@@ -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;;;;;;;;;;;;;;;GAeG;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,CA2C5D;AAED;;;;;;;;GAQG;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;;;;;;;GAOG;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;;;;;;;;;;;GAWG;AACH,wBAAgB,UAAU,CAAC,KAAK,CAAC,KAAK,SAAS,SAAS,EACpD,OAAO,EAAE,KAAK,GACf,gBAAgB,CAAC,KAAK,CAAC,CA2BzB"}
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 c(e, t) {
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
- const o = { value: t }, h = new Set(e._methods), a = e._helpers, f = new Set(
14
- Array.isArray(a) ? a : []
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, i] of Object.entries(e))
22
- l.has(s) || n.has(s) || f.has(s) || (h.has(s) ? o[s] = i[r] : o[s] = i);
23
- return o;
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 c(e, t);
29
+ return a(e, t);
28
30
  } catch {
29
31
  return null;
30
32
  }
31
33
  }
32
- function _(e) {
34
+ function p(e) {
33
35
  return e._cases.map(
34
- (t) => c(e, e[t])
36
+ (t) => a(e, e[t])
35
37
  );
36
38
  }
37
- function p(e) {
39
+ function u(e) {
38
40
  const t = {
39
- from: (r) => c(e, r),
41
+ from: (r) => a(e, r),
40
42
  tryFrom: (r) => y(e, r),
41
- cases: () => _(e)
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
- _ as cases,
54
- p as defineEnum,
55
- c as from,
55
+ p as cases,
56
+ u as defineEnum,
57
+ a as from,
56
58
  y as tryFrom
57
59
  };
package/dist/index.d.ts CHANGED
@@ -1,3 +1,2 @@
1
1
  export * from './enums';
2
- export * from './vite-plugin';
3
2
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,eAAe,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC"}
package/dist/index.js CHANGED
@@ -1,9 +1,7 @@
1
- import { cases as o, defineEnum as m, from as f, tryFrom as s } from "./enums.js";
2
- import { laravelTsPublish as l } from "./vite-plugin.js";
1
+ import { cases as m, defineEnum as o, from as f, tryFrom as n } from "./enums.js";
3
2
  export {
4
- o as cases,
5
- m as defineEnum,
3
+ m as cases,
4
+ o as defineEnum,
6
5
  f as from,
7
- l as laravelTsPublish,
8
- s as tryFrom
6
+ n as tryFrom
9
7
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tolki/enum",
3
- "version": "0.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.0"
26
+ "@tolki/types": "^1.1.1"
27
27
  },
28
28
  "peerDependencies": {
29
29
  "vite": "^7.0.0"
@@ -32,6 +32,23 @@
32
32
  "files": [
33
33
  "dist"
34
34
  ],
35
+ "exports": {
36
+ ".": {
37
+ "types": "./dist/index.d.ts",
38
+ "import": "./dist/index.js",
39
+ "require": "./dist/index.js"
40
+ },
41
+ "./enums": {
42
+ "types": "./dist/enums.d.ts",
43
+ "import": "./dist/enums.js",
44
+ "default": "./dist/enums.js"
45
+ },
46
+ "./vite": {
47
+ "types": "./dist/vite-plugin.d.ts",
48
+ "import": "./dist/vite-plugin.js",
49
+ "default": "./dist/vite-plugin.js"
50
+ }
51
+ },
35
52
  "tolki": {
36
53
  "docs": [
37
54
  "enums/index.md",
@@ -43,20 +60,5 @@
43
60
  "build": "vite build",
44
61
  "readme:build": "ts-node ../../scripts/readme-build.ts --filter @tolki/enum"
45
62
  },
46
- "types": "dist/index.d.ts",
47
- "exports": {
48
- ".": {
49
- "import": "./dist/index.js",
50
- "require": "./dist/index.js",
51
- "types": "./dist/index.d.ts"
52
- },
53
- "./enums": {
54
- "import": "./dist/enums/index.js",
55
- "types": "./dist/enums/index.d.ts"
56
- },
57
- "./vite": {
58
- "import": "./dist/vite-plugin/index.js",
59
- "types": "./dist/vite-plugin/index.d.ts"
60
- }
61
- }
63
+ "types": "dist/index.d.ts"
62
64
  }