@khgtrn/lib 1.0.0 → 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 +19 -9
- package/dist/cjs/base-enum.d.ts +5 -5
- package/dist/cjs/base-enum.js +4 -4
- package/dist/esm/base-enum.d.ts +5 -5
- package/dist/esm/base-enum.js +4 -4
- package/package.json +2 -3
package/README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Library for Typescript/Javascript
|
|
2
2
|
|
|
3
|
-
[](https://www.npmjs.com/package/@khgtrn/lib)
|
|
4
|
+
[](https://www.npmjs.com/package/@khgtrn/lib)
|
|
5
|
+
[](https://github.com/khgtrn/ts-lib/blob/main/LICENSE)
|
|
6
6
|
|
|
7
7
|
Shared TypeScript utility library: Java-style enums, common helper functions, number-to-words conversion (Vietnamese/English), and floating-point-safe rounding.
|
|
8
8
|
|
|
@@ -11,7 +11,7 @@ Built as both ESM (`dist/esm`) and CJS (`dist/cjs`), with full type declarations
|
|
|
11
11
|
## Installation
|
|
12
12
|
|
|
13
13
|
```bash
|
|
14
|
-
pnpm add @khgtrn/
|
|
14
|
+
pnpm add @khgtrn/lib
|
|
15
15
|
```
|
|
16
16
|
|
|
17
17
|
Requires Node.js >= 19 at runtime (needed for `crypto.getRandomValues` as a global — see `engines` in [package.json](package.json)). The shipped `.d.ts` files are compatible down to **TypeScript 2.7**, and the compiled JS itself avoids syntax (`?.`, `??`) that older bundlers can't parse — no separate install/config needed to consume this package from a legacy toolchain (e.g. Angular 6).
|
|
@@ -21,7 +21,7 @@ Requires Node.js >= 19 at runtime (needed for `crypto.getRandomValues` as a glob
|
|
|
21
21
|
`BaseEnum` is a base class for simulating Java enums: each constant is a singleton instance, and subclasses only need to `extends` and declare `static readonly` fields — no constructor to write.
|
|
22
22
|
|
|
23
23
|
```ts
|
|
24
|
-
import { BaseEnum } from '@khgtrn/
|
|
24
|
+
import { BaseEnum } from '@khgtrn/lib';
|
|
25
25
|
|
|
26
26
|
class Role extends BaseEnum<number> {
|
|
27
27
|
static readonly Admin = new Role(1, 'Administrator');
|
|
@@ -43,7 +43,17 @@ Role.Admin.name(); // 'Admin'
|
|
|
43
43
|
new Role(2, 'x'); // compile error — constructor is protected
|
|
44
44
|
```
|
|
45
45
|
|
|
46
|
-
The constructor also accepts an optional
|
|
46
|
+
The constructor also accepts an optional third parameter, `opt?: O`, for storing arbitrary extra data per constant. Pass the second type argument on `BaseEnum<T, O>` to get accurate type hints on `opt`:
|
|
47
|
+
|
|
48
|
+
```ts
|
|
49
|
+
class Role extends BaseEnum<number, { icon: string }> {
|
|
50
|
+
static readonly Admin = new Role(1, 'Administrator', { icon: 'shield' });
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
Role.Admin.opt?.icon; // typed as `string | undefined`
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Without an explicit `O`, it defaults to `any`.
|
|
47
57
|
|
|
48
58
|
## Utility functions (`func.ts`)
|
|
49
59
|
|
|
@@ -76,7 +86,7 @@ See the JSDoc in [src/func.ts](src/func.ts) for full parameter/return details.
|
|
|
76
86
|
## numberToWords — spelling out numbers
|
|
77
87
|
|
|
78
88
|
```ts
|
|
79
|
-
import { numberToWords } from '@khgtrn/
|
|
89
|
+
import { numberToWords } from '@khgtrn/lib';
|
|
80
90
|
|
|
81
91
|
numberToWords(1005); // "một nghìn không trăm linh năm" (Vietnamese by default)
|
|
82
92
|
numberToWords(1005, 'en'); // "one thousand five"
|
|
@@ -92,7 +102,7 @@ numberToWords(-123, 'en'); // "negative one hundred twenty-three"
|
|
|
92
102
|
## round — floating-point-safe rounding
|
|
93
103
|
|
|
94
104
|
```ts
|
|
95
|
-
import { round } from '@khgtrn/
|
|
105
|
+
import { round } from '@khgtrn/lib';
|
|
96
106
|
|
|
97
107
|
round(491.66999999999996); // 491.67 (default precision = 10)
|
|
98
108
|
round(1.23456, 2); // 1.23
|
package/dist/cjs/base-enum.d.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Base class for simulating Java-style enums in TypeScript.
|
|
3
3
|
*
|
|
4
4
|
* Subclasses only need to `extends BaseEnum<...>` and declare constants as
|
|
5
|
-
* `static readonly Xxx = new SubClass(value, label,
|
|
5
|
+
* `static readonly Xxx = new SubClass(value, label, opt?)`, without
|
|
6
6
|
* redeclaring a constructor. Because `BaseEnum`'s constructor is `protected`,
|
|
7
7
|
* subclasses inherit it while keeping the same protection — instances can't
|
|
8
8
|
* be `new`-ed from outside the class, which preserves enum singleton/identity
|
|
@@ -10,10 +10,10 @@
|
|
|
10
10
|
*
|
|
11
11
|
* @typeParam T - Type of the `value` field (defaults to `number`).
|
|
12
12
|
*/
|
|
13
|
-
export declare abstract class BaseEnum<T = number> {
|
|
13
|
+
export declare abstract class BaseEnum<T = number, O = any> {
|
|
14
14
|
readonly value: T;
|
|
15
15
|
readonly label: string;
|
|
16
|
-
readonly
|
|
16
|
+
readonly opt?: O | undefined;
|
|
17
17
|
/**
|
|
18
18
|
* Registry of every instance created, keyed per subclass and per `value`.
|
|
19
19
|
* The outer key is the subclass constructor (so each subclass has its own
|
|
@@ -28,9 +28,9 @@ export declare abstract class BaseEnum<T = number> {
|
|
|
28
28
|
*
|
|
29
29
|
* @param value - Identifying value of the constant (used by `fromValue()`, `equals()`).
|
|
30
30
|
* @param label - Display label/description of the constant.
|
|
31
|
-
* @param
|
|
31
|
+
* @param opt - Optional extra data, freely defined by the subclass as needed.
|
|
32
32
|
*/
|
|
33
|
-
protected constructor(value: T, label: string,
|
|
33
|
+
protected constructor(value: T, label: string, opt?: O | undefined);
|
|
34
34
|
/**
|
|
35
35
|
* Returns the names of the `static readonly` fields declared on the
|
|
36
36
|
* subclass, in declaration order. Mirrors the idea of an enum constant's
|
package/dist/cjs/base-enum.js
CHANGED
|
@@ -5,7 +5,7 @@ exports.BaseEnum = void 0;
|
|
|
5
5
|
* Base class for simulating Java-style enums in TypeScript.
|
|
6
6
|
*
|
|
7
7
|
* Subclasses only need to `extends BaseEnum<...>` and declare constants as
|
|
8
|
-
* `static readonly Xxx = new SubClass(value, label,
|
|
8
|
+
* `static readonly Xxx = new SubClass(value, label, opt?)`, without
|
|
9
9
|
* redeclaring a constructor. Because `BaseEnum`'s constructor is `protected`,
|
|
10
10
|
* subclasses inherit it while keeping the same protection — instances can't
|
|
11
11
|
* be `new`-ed from outside the class, which preserves enum singleton/identity
|
|
@@ -21,12 +21,12 @@ class BaseEnum {
|
|
|
21
21
|
*
|
|
22
22
|
* @param value - Identifying value of the constant (used by `fromValue()`, `equals()`).
|
|
23
23
|
* @param label - Display label/description of the constant.
|
|
24
|
-
* @param
|
|
24
|
+
* @param opt - Optional extra data, freely defined by the subclass as needed.
|
|
25
25
|
*/
|
|
26
|
-
constructor(value, label,
|
|
26
|
+
constructor(value, label, opt) {
|
|
27
27
|
this.value = value;
|
|
28
28
|
this.label = label;
|
|
29
|
-
this.
|
|
29
|
+
this.opt = opt;
|
|
30
30
|
let map = BaseEnum.registry.get(this.constructor);
|
|
31
31
|
if (!map) {
|
|
32
32
|
map = new Map();
|
package/dist/esm/base-enum.d.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Base class for simulating Java-style enums in TypeScript.
|
|
3
3
|
*
|
|
4
4
|
* Subclasses only need to `extends BaseEnum<...>` and declare constants as
|
|
5
|
-
* `static readonly Xxx = new SubClass(value, label,
|
|
5
|
+
* `static readonly Xxx = new SubClass(value, label, opt?)`, without
|
|
6
6
|
* redeclaring a constructor. Because `BaseEnum`'s constructor is `protected`,
|
|
7
7
|
* subclasses inherit it while keeping the same protection — instances can't
|
|
8
8
|
* be `new`-ed from outside the class, which preserves enum singleton/identity
|
|
@@ -10,10 +10,10 @@
|
|
|
10
10
|
*
|
|
11
11
|
* @typeParam T - Type of the `value` field (defaults to `number`).
|
|
12
12
|
*/
|
|
13
|
-
export declare abstract class BaseEnum<T = number> {
|
|
13
|
+
export declare abstract class BaseEnum<T = number, O = any> {
|
|
14
14
|
readonly value: T;
|
|
15
15
|
readonly label: string;
|
|
16
|
-
readonly
|
|
16
|
+
readonly opt?: O | undefined;
|
|
17
17
|
/**
|
|
18
18
|
* Registry of every instance created, keyed per subclass and per `value`.
|
|
19
19
|
* The outer key is the subclass constructor (so each subclass has its own
|
|
@@ -28,9 +28,9 @@ export declare abstract class BaseEnum<T = number> {
|
|
|
28
28
|
*
|
|
29
29
|
* @param value - Identifying value of the constant (used by `fromValue()`, `equals()`).
|
|
30
30
|
* @param label - Display label/description of the constant.
|
|
31
|
-
* @param
|
|
31
|
+
* @param opt - Optional extra data, freely defined by the subclass as needed.
|
|
32
32
|
*/
|
|
33
|
-
protected constructor(value: T, label: string,
|
|
33
|
+
protected constructor(value: T, label: string, opt?: O | undefined);
|
|
34
34
|
/**
|
|
35
35
|
* Returns the names of the `static readonly` fields declared on the
|
|
36
36
|
* subclass, in declaration order. Mirrors the idea of an enum constant's
|
package/dist/esm/base-enum.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Base class for simulating Java-style enums in TypeScript.
|
|
3
3
|
*
|
|
4
4
|
* Subclasses only need to `extends BaseEnum<...>` and declare constants as
|
|
5
|
-
* `static readonly Xxx = new SubClass(value, label,
|
|
5
|
+
* `static readonly Xxx = new SubClass(value, label, opt?)`, without
|
|
6
6
|
* redeclaring a constructor. Because `BaseEnum`'s constructor is `protected`,
|
|
7
7
|
* subclasses inherit it while keeping the same protection — instances can't
|
|
8
8
|
* be `new`-ed from outside the class, which preserves enum singleton/identity
|
|
@@ -18,12 +18,12 @@ export class BaseEnum {
|
|
|
18
18
|
*
|
|
19
19
|
* @param value - Identifying value of the constant (used by `fromValue()`, `equals()`).
|
|
20
20
|
* @param label - Display label/description of the constant.
|
|
21
|
-
* @param
|
|
21
|
+
* @param opt - Optional extra data, freely defined by the subclass as needed.
|
|
22
22
|
*/
|
|
23
|
-
constructor(value, label,
|
|
23
|
+
constructor(value, label, opt) {
|
|
24
24
|
this.value = value;
|
|
25
25
|
this.label = label;
|
|
26
|
-
this.
|
|
26
|
+
this.opt = opt;
|
|
27
27
|
let map = BaseEnum.registry.get(this.constructor);
|
|
28
28
|
if (!map) {
|
|
29
29
|
map = new Map();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@khgtrn/lib",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Library for Typescript",
|
|
5
5
|
"main": "./dist/cjs/index.js",
|
|
6
6
|
"module": "./dist/esm/index.js",
|
|
@@ -22,8 +22,7 @@
|
|
|
22
22
|
"typescript",
|
|
23
23
|
"ts",
|
|
24
24
|
"lib",
|
|
25
|
-
"library"
|
|
26
|
-
"klib"
|
|
25
|
+
"library"
|
|
27
26
|
],
|
|
28
27
|
"author": "khgtrn",
|
|
29
28
|
"license": "MIT",
|