@kikiutils/shared 10.2.1 → 10.4.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/dist/buffer.cjs +43 -0
- package/dist/buffer.cjs.map +1 -0
- package/dist/buffer.d.ts +34 -0
- package/dist/buffer.d.ts.map +1 -0
- package/dist/buffer.mjs +41 -0
- package/dist/buffer.mjs.map +1 -0
- package/dist/clipboard.cjs +4 -4
- package/dist/clipboard.cjs.map +1 -1
- package/dist/clipboard.d.ts +3 -3
- package/dist/clipboard.mjs +4 -4
- package/dist/clipboard.mjs.map +1 -1
- package/dist/datetime.cjs +13 -13
- package/dist/datetime.cjs.map +1 -1
- package/dist/datetime.d.ts +12 -12
- package/dist/datetime.mjs +13 -13
- package/dist/datetime.mjs.map +1 -1
- package/dist/element-plus.cjs +6 -6
- package/dist/element-plus.cjs.map +1 -1
- package/dist/element-plus.d.ts +6 -6
- package/dist/element-plus.mjs +6 -6
- package/dist/element-plus.mjs.map +1 -1
- package/dist/enum.cjs +70 -25
- package/dist/enum.cjs.map +1 -1
- package/dist/enum.d.ts +61 -10
- package/dist/enum.d.ts.map +1 -1
- package/dist/enum.mjs +70 -26
- package/dist/enum.mjs.map +1 -1
- package/dist/env.cjs +4 -4
- package/dist/env.cjs.map +1 -1
- package/dist/env.d.ts +4 -4
- package/dist/env.mjs +4 -4
- package/dist/env.mjs.map +1 -1
- package/dist/general.cjs.map +1 -1
- package/dist/general.d.ts +5 -5
- package/dist/general.mjs.map +1 -1
- package/dist/math.cjs +4 -4
- package/dist/math.cjs.map +1 -1
- package/dist/math.d.ts +4 -4
- package/dist/math.mjs +4 -4
- package/dist/math.mjs.map +1 -1
- package/dist/number.cjs +3 -3
- package/dist/number.cjs.map +1 -1
- package/dist/number.d.ts +3 -3
- package/dist/number.mjs +3 -3
- package/dist/number.mjs.map +1 -1
- package/dist/object.cjs +4 -4
- package/dist/object.cjs.map +1 -1
- package/dist/object.d.ts +4 -4
- package/dist/object.mjs +4 -4
- package/dist/object.mjs.map +1 -1
- package/dist/random.cjs +7 -7
- package/dist/random.cjs.map +1 -1
- package/dist/random.d.ts +7 -7
- package/dist/random.mjs +7 -7
- package/dist/random.mjs.map +1 -1
- package/dist/storage/enhanced/local/core.cjs +11 -11
- package/dist/storage/enhanced/local/core.cjs.map +1 -1
- package/dist/storage/enhanced/local/core.d.ts +9 -9
- package/dist/storage/enhanced/local/core.mjs +11 -11
- package/dist/storage/enhanced/local/core.mjs.map +1 -1
- package/dist/storage/enhanced/local/keyed-store.cjs +3 -3
- package/dist/storage/enhanced/local/keyed-store.cjs.map +1 -1
- package/dist/storage/enhanced/local/keyed-store.d.ts +3 -3
- package/dist/storage/enhanced/local/keyed-store.mjs +3 -3
- package/dist/storage/enhanced/local/keyed-store.mjs.map +1 -1
- package/dist/storage/enhanced/redis/core.cjs +18 -18
- package/dist/storage/enhanced/redis/core.cjs.map +1 -1
- package/dist/storage/enhanced/redis/core.d.ts +16 -16
- package/dist/storage/enhanced/redis/core.mjs +18 -18
- package/dist/storage/enhanced/redis/core.mjs.map +1 -1
- package/dist/storage/enhanced/redis/keyed-store.cjs +4 -4
- package/dist/storage/enhanced/redis/keyed-store.cjs.map +1 -1
- package/dist/storage/enhanced/redis/keyed-store.d.ts +4 -4
- package/dist/storage/enhanced/redis/keyed-store.mjs +4 -4
- package/dist/storage/enhanced/redis/keyed-store.mjs.map +1 -1
- package/dist/storage/lru/keyed-store.cjs +3 -3
- package/dist/storage/lru/keyed-store.cjs.map +1 -1
- package/dist/storage/lru/keyed-store.d.ts +3 -3
- package/dist/storage/lru/keyed-store.mjs +3 -3
- package/dist/storage/lru/keyed-store.mjs.map +1 -1
- package/dist/string.cjs +5 -5
- package/dist/string.cjs.map +1 -1
- package/dist/string.d.ts +4 -4
- package/dist/string.mjs +5 -5
- package/dist/string.mjs.map +1 -1
- package/dist/url.cjs +3 -3
- package/dist/url.cjs.map +1 -1
- package/dist/url.d.ts +3 -3
- package/dist/url.mjs +3 -3
- package/dist/url.mjs.map +1 -1
- package/dist/vue.cjs +6 -6
- package/dist/vue.cjs.map +1 -1
- package/dist/vue.d.ts +6 -6
- package/dist/vue.mjs +6 -6
- package/dist/vue.mjs.map +1 -1
- package/dist/web.cjs +4 -4
- package/dist/web.cjs.map +1 -1
- package/dist/web.d.ts +4 -4
- package/dist/web.mjs +4 -4
- package/dist/web.mjs.map +1 -1
- package/package.json +9 -5
- package/src/buffer.ts +37 -0
- package/src/clipboard.ts +4 -4
- package/src/datetime.ts +13 -13
- package/src/element-plus.ts +6 -6
- package/src/enum.ts +68 -24
- package/src/env.ts +4 -4
- package/src/general.ts +5 -5
- package/src/math.ts +4 -4
- package/src/number.ts +3 -3
- package/src/object.ts +4 -4
- package/src/random.ts +7 -7
- package/src/storage/enhanced/local/core.ts +11 -11
- package/src/storage/enhanced/local/keyed-store.ts +3 -3
- package/src/storage/enhanced/redis/core.ts +18 -18
- package/src/storage/enhanced/redis/keyed-store.ts +4 -4
- package/src/storage/lru/keyed-store.ts +3 -3
- package/src/string.ts +5 -5
- package/src/url.ts +3 -3
- package/src/vue.ts +6 -6
- package/src/web.ts +4 -4
package/dist/enum.cjs
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* Extracts the numeric values from an enumeration-like object.
|
|
4
|
+
* Extracts only the numeric values from an enumeration-like object.
|
|
5
5
|
*
|
|
6
|
-
* @
|
|
7
|
-
* The keys can be numbers or strings, and the values can be numbers or strings.
|
|
6
|
+
* @template T - The type of the enum object
|
|
8
7
|
*
|
|
9
|
-
* @
|
|
8
|
+
* @param {T} enumObject - The enumeration-like object to extract numeric values from,
|
|
9
|
+
* the object can contain numeric values, string values, or both.
|
|
10
|
+
*
|
|
11
|
+
* @returns {Extract<T[keyof T], number>[]} An array of numeric values extracted from the enum object
|
|
10
12
|
*
|
|
11
13
|
* @example
|
|
12
14
|
* ```typescript
|
|
@@ -21,16 +23,18 @@
|
|
|
21
23
|
* console.log(getEnumNumberValues(RecordType)); // [0, 1]
|
|
22
24
|
* ```
|
|
23
25
|
*/
|
|
24
|
-
function getEnumNumberValues(
|
|
25
|
-
return
|
|
26
|
+
function getEnumNumberValues(enumObject) {
|
|
27
|
+
return getEnumValues(enumObject).filter((value) => typeof value === 'number');
|
|
26
28
|
}
|
|
27
29
|
/**
|
|
28
|
-
* Extracts the string values from an enumeration-like object.
|
|
30
|
+
* Extracts only the string values from an enumeration-like object.
|
|
31
|
+
*
|
|
32
|
+
* @template T - The type of the enum object
|
|
29
33
|
*
|
|
30
|
-
* @param {
|
|
31
|
-
*
|
|
34
|
+
* @param {T} enumObject - The enumeration-like object to extract string values from,
|
|
35
|
+
* the object can contain numeric values, string values, or both.
|
|
32
36
|
*
|
|
33
|
-
* @returns {string[]} An array of string values extracted from the object
|
|
37
|
+
* @returns {Extract<T[keyof T], string>[]} An array of string values extracted from the enum object
|
|
34
38
|
*
|
|
35
39
|
* @example
|
|
36
40
|
* ```typescript
|
|
@@ -45,23 +49,64 @@ function getEnumNumberValues(data) {
|
|
|
45
49
|
* console.log(getEnumStringValues(RecordType)); // ['unknown']
|
|
46
50
|
* ```
|
|
47
51
|
*/
|
|
48
|
-
function getEnumStringValues(
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
52
|
+
function getEnumStringValues(enumObject) {
|
|
53
|
+
return getEnumValues(enumObject).filter((value) => typeof value === 'string');
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Extracts all values from an enumeration-like object.
|
|
57
|
+
*
|
|
58
|
+
* This function handles TypeScript enums correctly by accounting for the reverse mapping
|
|
59
|
+
* that occurs with numeric enums. It works with pure numeric enums, pure string enums,
|
|
60
|
+
* and mixed enums.
|
|
61
|
+
*
|
|
62
|
+
* @template T - The type of the enum object
|
|
63
|
+
*
|
|
64
|
+
* @param {T} enumObject - The enumeration-like object to extract values from,
|
|
65
|
+
* the object can contain numeric values, string values, or both.
|
|
66
|
+
*
|
|
67
|
+
* @returns {(T[keyof T])[]} An array containing all the values from the enum object
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```typescript
|
|
71
|
+
* import { getEnumValues } from '@kikiutils/shared/enum';
|
|
72
|
+
*
|
|
73
|
+
* // Numeric enum
|
|
74
|
+
* enum Status {
|
|
75
|
+
* Active = 0,
|
|
76
|
+
* Inactive = 1,
|
|
77
|
+
* Pending = 2
|
|
78
|
+
* }
|
|
79
|
+
*
|
|
80
|
+
* console.log(getEnumValues(Status)); // [0, 1, 2]
|
|
81
|
+
*
|
|
82
|
+
* // String enum
|
|
83
|
+
* enum Color {
|
|
84
|
+
* Red = 'RED',
|
|
85
|
+
* Green = 'GREEN',
|
|
86
|
+
* Blue = 'BLUE'
|
|
87
|
+
* }
|
|
88
|
+
*
|
|
89
|
+
* console.log(getEnumValues(Color)); // ['RED', 'GREEN', 'BLUE']
|
|
90
|
+
*
|
|
91
|
+
* // Mixed enum
|
|
92
|
+
* enum RecordType {
|
|
93
|
+
* Receive = 0,
|
|
94
|
+
* Send = 1,
|
|
95
|
+
* Unknown = 'unknown'
|
|
96
|
+
* }
|
|
97
|
+
*
|
|
98
|
+
* console.log(getEnumValues(RecordType)); // [0, 1, 'unknown']
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
function getEnumValues(enumObject) {
|
|
102
|
+
const values = Object.values(enumObject);
|
|
103
|
+
const hasNumberValues = values.some((value) => typeof value === 'number');
|
|
104
|
+
if (!hasNumberValues)
|
|
105
|
+
return values;
|
|
106
|
+
return Object.keys(enumObject).filter((key) => Number.isNaN(Number(key))).map((key) => enumObject[key]);
|
|
63
107
|
}
|
|
64
108
|
|
|
65
109
|
exports.getEnumNumberValues = getEnumNumberValues;
|
|
66
110
|
exports.getEnumStringValues = getEnumStringValues;
|
|
111
|
+
exports.getEnumValues = getEnumValues;
|
|
67
112
|
//# sourceMappingURL=enum.cjs.map
|
package/dist/enum.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"enum.cjs","sources":["../src/enum.ts"],"sourcesContent":["/**\n * Extracts the numeric values from an enumeration-like object.\n *\n * @
|
|
1
|
+
{"version":3,"file":"enum.cjs","sources":["../src/enum.ts"],"sourcesContent":["/**\n * Extracts only the numeric values from an enumeration-like object.\n *\n * @template T - The type of the enum object\n *\n * @param {T} enumObject - The enumeration-like object to extract numeric values from,\n * the object can contain numeric values, string values, or both.\n *\n * @returns {Extract<T[keyof T], number>[]} An array of numeric values extracted from the enum object\n *\n * @example\n * ```typescript\n * import { getEnumNumberValues } from '@kikiutils/shared/enum';\n *\n * enum RecordType {\n * Receive = 0,\n * Send = 1,\n * Unknown = 'unknown'\n * }\n *\n * console.log(getEnumNumberValues(RecordType)); // [0, 1]\n * ```\n */\nexport function getEnumNumberValues<T extends Record<string, any>>(enumObject: T): Extract<T[keyof T], number>[] {\n return getEnumValues(enumObject).filter((value) => typeof value === 'number');\n}\n\n/**\n * Extracts only the string values from an enumeration-like object.\n *\n * @template T - The type of the enum object\n *\n * @param {T} enumObject - The enumeration-like object to extract string values from,\n * the object can contain numeric values, string values, or both.\n *\n * @returns {Extract<T[keyof T], string>[]} An array of string values extracted from the enum object\n *\n * @example\n * ```typescript\n * import { getEnumStringValues } from '@kikiutils/shared/enum';\n *\n * enum RecordType {\n * Receive = 0,\n * Send = 1,\n * Unknown = 'unknown'\n * }\n *\n * console.log(getEnumStringValues(RecordType)); // ['unknown']\n * ```\n */\nexport function getEnumStringValues<T extends Record<string, any>>(enumObject: T): Extract<T[keyof T], string>[] {\n return getEnumValues(enumObject).filter((value) => typeof value === 'string');\n}\n\n/**\n * Extracts all values from an enumeration-like object.\n *\n * This function handles TypeScript enums correctly by accounting for the reverse mapping\n * that occurs with numeric enums. It works with pure numeric enums, pure string enums,\n * and mixed enums.\n *\n * @template T - The type of the enum object\n *\n * @param {T} enumObject - The enumeration-like object to extract values from,\n * the object can contain numeric values, string values, or both.\n *\n * @returns {(T[keyof T])[]} An array containing all the values from the enum object\n *\n * @example\n * ```typescript\n * import { getEnumValues } from '@kikiutils/shared/enum';\n *\n * // Numeric enum\n * enum Status {\n * Active = 0,\n * Inactive = 1,\n * Pending = 2\n * }\n *\n * console.log(getEnumValues(Status)); // [0, 1, 2]\n *\n * // String enum\n * enum Color {\n * Red = 'RED',\n * Green = 'GREEN',\n * Blue = 'BLUE'\n * }\n *\n * console.log(getEnumValues(Color)); // ['RED', 'GREEN', 'BLUE']\n *\n * // Mixed enum\n * enum RecordType {\n * Receive = 0,\n * Send = 1,\n * Unknown = 'unknown'\n * }\n *\n * console.log(getEnumValues(RecordType)); // [0, 1, 'unknown']\n * ```\n */\nexport function getEnumValues<T extends Record<string, any>>(enumObject: T): (T[keyof T])[] {\n const values = Object.values(enumObject);\n const hasNumberValues = values.some((value) => typeof value === 'number');\n if (!hasNumberValues) return values;\n return Object.keys(enumObject).filter((key) => Number.isNaN(Number(key))).map((key) => enumObject[key]);\n}\n"],"names":[],"mappings":";;AAAA;;;;;;;;;;;;;;;;;;;;;;AAsBG;AACG,SAAU,mBAAmB,CAAgC,UAAa,EAAA;AAC5E,IAAA,OAAO,aAAa,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,CAAC,KAAK,KAAK,OAAO,KAAK,KAAK,QAAQ,CAAC;AACjF;AAEA;;;;;;;;;;;;;;;;;;;;;;AAsBG;AACG,SAAU,mBAAmB,CAAgC,UAAa,EAAA;AAC5E,IAAA,OAAO,aAAa,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,CAAC,KAAK,KAAK,OAAO,KAAK,KAAK,QAAQ,CAAC;AACjF;AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6CG;AACG,SAAU,aAAa,CAAgC,UAAa,EAAA;IACtE,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC;AACxC,IAAA,MAAM,eAAe,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC,KAAK,KAAK,OAAO,KAAK,KAAK,QAAQ,CAAC;AACzE,IAAA,IAAI,CAAC,eAAe;AAAE,QAAA,OAAO,MAAM;AACnC,IAAA,OAAO,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,CAAC,GAAG,KAAK,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,KAAK,UAAU,CAAC,GAAG,CAAC,CAAC;AAC3G;;;;;;"}
|
package/dist/enum.d.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Extracts the numeric values from an enumeration-like object.
|
|
2
|
+
* Extracts only the numeric values from an enumeration-like object.
|
|
3
3
|
*
|
|
4
|
-
* @
|
|
5
|
-
* The keys can be numbers or strings, and the values can be numbers or strings.
|
|
4
|
+
* @template T - The type of the enum object
|
|
6
5
|
*
|
|
7
|
-
* @
|
|
6
|
+
* @param {T} enumObject - The enumeration-like object to extract numeric values from,
|
|
7
|
+
* the object can contain numeric values, string values, or both.
|
|
8
|
+
*
|
|
9
|
+
* @returns {Extract<T[keyof T], number>[]} An array of numeric values extracted from the enum object
|
|
8
10
|
*
|
|
9
11
|
* @example
|
|
10
12
|
* ```typescript
|
|
@@ -19,14 +21,16 @@
|
|
|
19
21
|
* console.log(getEnumNumberValues(RecordType)); // [0, 1]
|
|
20
22
|
* ```
|
|
21
23
|
*/
|
|
22
|
-
export declare function getEnumNumberValues
|
|
24
|
+
export declare function getEnumNumberValues<T extends Record<string, any>>(enumObject: T): Extract<T[keyof T], number>[];
|
|
23
25
|
/**
|
|
24
|
-
* Extracts the string values from an enumeration-like object.
|
|
26
|
+
* Extracts only the string values from an enumeration-like object.
|
|
27
|
+
*
|
|
28
|
+
* @template T - The type of the enum object
|
|
25
29
|
*
|
|
26
|
-
* @param {
|
|
27
|
-
*
|
|
30
|
+
* @param {T} enumObject - The enumeration-like object to extract string values from,
|
|
31
|
+
* the object can contain numeric values, string values, or both.
|
|
28
32
|
*
|
|
29
|
-
* @returns {string[]} An array of string values extracted from the object
|
|
33
|
+
* @returns {Extract<T[keyof T], string>[]} An array of string values extracted from the enum object
|
|
30
34
|
*
|
|
31
35
|
* @example
|
|
32
36
|
* ```typescript
|
|
@@ -41,5 +45,52 @@ export declare function getEnumNumberValues(data: Record<number | string, number
|
|
|
41
45
|
* console.log(getEnumStringValues(RecordType)); // ['unknown']
|
|
42
46
|
* ```
|
|
43
47
|
*/
|
|
44
|
-
export declare function getEnumStringValues
|
|
48
|
+
export declare function getEnumStringValues<T extends Record<string, any>>(enumObject: T): Extract<T[keyof T], string>[];
|
|
49
|
+
/**
|
|
50
|
+
* Extracts all values from an enumeration-like object.
|
|
51
|
+
*
|
|
52
|
+
* This function handles TypeScript enums correctly by accounting for the reverse mapping
|
|
53
|
+
* that occurs with numeric enums. It works with pure numeric enums, pure string enums,
|
|
54
|
+
* and mixed enums.
|
|
55
|
+
*
|
|
56
|
+
* @template T - The type of the enum object
|
|
57
|
+
*
|
|
58
|
+
* @param {T} enumObject - The enumeration-like object to extract values from,
|
|
59
|
+
* the object can contain numeric values, string values, or both.
|
|
60
|
+
*
|
|
61
|
+
* @returns {(T[keyof T])[]} An array containing all the values from the enum object
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```typescript
|
|
65
|
+
* import { getEnumValues } from '@kikiutils/shared/enum';
|
|
66
|
+
*
|
|
67
|
+
* // Numeric enum
|
|
68
|
+
* enum Status {
|
|
69
|
+
* Active = 0,
|
|
70
|
+
* Inactive = 1,
|
|
71
|
+
* Pending = 2
|
|
72
|
+
* }
|
|
73
|
+
*
|
|
74
|
+
* console.log(getEnumValues(Status)); // [0, 1, 2]
|
|
75
|
+
*
|
|
76
|
+
* // String enum
|
|
77
|
+
* enum Color {
|
|
78
|
+
* Red = 'RED',
|
|
79
|
+
* Green = 'GREEN',
|
|
80
|
+
* Blue = 'BLUE'
|
|
81
|
+
* }
|
|
82
|
+
*
|
|
83
|
+
* console.log(getEnumValues(Color)); // ['RED', 'GREEN', 'BLUE']
|
|
84
|
+
*
|
|
85
|
+
* // Mixed enum
|
|
86
|
+
* enum RecordType {
|
|
87
|
+
* Receive = 0,
|
|
88
|
+
* Send = 1,
|
|
89
|
+
* Unknown = 'unknown'
|
|
90
|
+
* }
|
|
91
|
+
*
|
|
92
|
+
* console.log(getEnumValues(RecordType)); // [0, 1, 'unknown']
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
export declare function getEnumValues<T extends Record<string, any>>(enumObject: T): (T[keyof T])[];
|
|
45
96
|
//# sourceMappingURL=enum.d.ts.map
|
package/dist/enum.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"enum.d.ts","sourceRoot":"","sources":["../src/enum.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"enum.d.ts","sourceRoot":"","sources":["../src/enum.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,mBAAmB,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,UAAU,EAAE,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,EAAE,MAAM,CAAC,EAAE,CAE/G;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,mBAAmB,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,UAAU,EAAE,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,EAAE,MAAM,CAAC,EAAE,CAE/G;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6CG;AACH,wBAAgB,aAAa,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,UAAU,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAK1F"}
|
package/dist/enum.mjs
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Extracts the numeric values from an enumeration-like object.
|
|
2
|
+
* Extracts only the numeric values from an enumeration-like object.
|
|
3
3
|
*
|
|
4
|
-
* @
|
|
5
|
-
* The keys can be numbers or strings, and the values can be numbers or strings.
|
|
4
|
+
* @template T - The type of the enum object
|
|
6
5
|
*
|
|
7
|
-
* @
|
|
6
|
+
* @param {T} enumObject - The enumeration-like object to extract numeric values from,
|
|
7
|
+
* the object can contain numeric values, string values, or both.
|
|
8
|
+
*
|
|
9
|
+
* @returns {Extract<T[keyof T], number>[]} An array of numeric values extracted from the enum object
|
|
8
10
|
*
|
|
9
11
|
* @example
|
|
10
12
|
* ```typescript
|
|
@@ -19,16 +21,18 @@
|
|
|
19
21
|
* console.log(getEnumNumberValues(RecordType)); // [0, 1]
|
|
20
22
|
* ```
|
|
21
23
|
*/
|
|
22
|
-
function getEnumNumberValues(
|
|
23
|
-
return
|
|
24
|
+
function getEnumNumberValues(enumObject) {
|
|
25
|
+
return getEnumValues(enumObject).filter((value) => typeof value === 'number');
|
|
24
26
|
}
|
|
25
27
|
/**
|
|
26
|
-
* Extracts the string values from an enumeration-like object.
|
|
28
|
+
* Extracts only the string values from an enumeration-like object.
|
|
29
|
+
*
|
|
30
|
+
* @template T - The type of the enum object
|
|
27
31
|
*
|
|
28
|
-
* @param {
|
|
29
|
-
*
|
|
32
|
+
* @param {T} enumObject - The enumeration-like object to extract string values from,
|
|
33
|
+
* the object can contain numeric values, string values, or both.
|
|
30
34
|
*
|
|
31
|
-
* @returns {string[]} An array of string values extracted from the object
|
|
35
|
+
* @returns {Extract<T[keyof T], string>[]} An array of string values extracted from the enum object
|
|
32
36
|
*
|
|
33
37
|
* @example
|
|
34
38
|
* ```typescript
|
|
@@ -43,22 +47,62 @@ function getEnumNumberValues(data) {
|
|
|
43
47
|
* console.log(getEnumStringValues(RecordType)); // ['unknown']
|
|
44
48
|
* ```
|
|
45
49
|
*/
|
|
46
|
-
function getEnumStringValues(
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
50
|
+
function getEnumStringValues(enumObject) {
|
|
51
|
+
return getEnumValues(enumObject).filter((value) => typeof value === 'string');
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Extracts all values from an enumeration-like object.
|
|
55
|
+
*
|
|
56
|
+
* This function handles TypeScript enums correctly by accounting for the reverse mapping
|
|
57
|
+
* that occurs with numeric enums. It works with pure numeric enums, pure string enums,
|
|
58
|
+
* and mixed enums.
|
|
59
|
+
*
|
|
60
|
+
* @template T - The type of the enum object
|
|
61
|
+
*
|
|
62
|
+
* @param {T} enumObject - The enumeration-like object to extract values from,
|
|
63
|
+
* the object can contain numeric values, string values, or both.
|
|
64
|
+
*
|
|
65
|
+
* @returns {(T[keyof T])[]} An array containing all the values from the enum object
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```typescript
|
|
69
|
+
* import { getEnumValues } from '@kikiutils/shared/enum';
|
|
70
|
+
*
|
|
71
|
+
* // Numeric enum
|
|
72
|
+
* enum Status {
|
|
73
|
+
* Active = 0,
|
|
74
|
+
* Inactive = 1,
|
|
75
|
+
* Pending = 2
|
|
76
|
+
* }
|
|
77
|
+
*
|
|
78
|
+
* console.log(getEnumValues(Status)); // [0, 1, 2]
|
|
79
|
+
*
|
|
80
|
+
* // String enum
|
|
81
|
+
* enum Color {
|
|
82
|
+
* Red = 'RED',
|
|
83
|
+
* Green = 'GREEN',
|
|
84
|
+
* Blue = 'BLUE'
|
|
85
|
+
* }
|
|
86
|
+
*
|
|
87
|
+
* console.log(getEnumValues(Color)); // ['RED', 'GREEN', 'BLUE']
|
|
88
|
+
*
|
|
89
|
+
* // Mixed enum
|
|
90
|
+
* enum RecordType {
|
|
91
|
+
* Receive = 0,
|
|
92
|
+
* Send = 1,
|
|
93
|
+
* Unknown = 'unknown'
|
|
94
|
+
* }
|
|
95
|
+
*
|
|
96
|
+
* console.log(getEnumValues(RecordType)); // [0, 1, 'unknown']
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
function getEnumValues(enumObject) {
|
|
100
|
+
const values = Object.values(enumObject);
|
|
101
|
+
const hasNumberValues = values.some((value) => typeof value === 'number');
|
|
102
|
+
if (!hasNumberValues)
|
|
103
|
+
return values;
|
|
104
|
+
return Object.keys(enumObject).filter((key) => Number.isNaN(Number(key))).map((key) => enumObject[key]);
|
|
61
105
|
}
|
|
62
106
|
|
|
63
|
-
export { getEnumNumberValues, getEnumStringValues };
|
|
107
|
+
export { getEnumNumberValues, getEnumStringValues, getEnumValues };
|
|
64
108
|
//# sourceMappingURL=enum.mjs.map
|
package/dist/enum.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"enum.mjs","sources":["../src/enum.ts"],"sourcesContent":["/**\n * Extracts the numeric values from an enumeration-like object.\n *\n * @
|
|
1
|
+
{"version":3,"file":"enum.mjs","sources":["../src/enum.ts"],"sourcesContent":["/**\n * Extracts only the numeric values from an enumeration-like object.\n *\n * @template T - The type of the enum object\n *\n * @param {T} enumObject - The enumeration-like object to extract numeric values from,\n * the object can contain numeric values, string values, or both.\n *\n * @returns {Extract<T[keyof T], number>[]} An array of numeric values extracted from the enum object\n *\n * @example\n * ```typescript\n * import { getEnumNumberValues } from '@kikiutils/shared/enum';\n *\n * enum RecordType {\n * Receive = 0,\n * Send = 1,\n * Unknown = 'unknown'\n * }\n *\n * console.log(getEnumNumberValues(RecordType)); // [0, 1]\n * ```\n */\nexport function getEnumNumberValues<T extends Record<string, any>>(enumObject: T): Extract<T[keyof T], number>[] {\n return getEnumValues(enumObject).filter((value) => typeof value === 'number');\n}\n\n/**\n * Extracts only the string values from an enumeration-like object.\n *\n * @template T - The type of the enum object\n *\n * @param {T} enumObject - The enumeration-like object to extract string values from,\n * the object can contain numeric values, string values, or both.\n *\n * @returns {Extract<T[keyof T], string>[]} An array of string values extracted from the enum object\n *\n * @example\n * ```typescript\n * import { getEnumStringValues } from '@kikiutils/shared/enum';\n *\n * enum RecordType {\n * Receive = 0,\n * Send = 1,\n * Unknown = 'unknown'\n * }\n *\n * console.log(getEnumStringValues(RecordType)); // ['unknown']\n * ```\n */\nexport function getEnumStringValues<T extends Record<string, any>>(enumObject: T): Extract<T[keyof T], string>[] {\n return getEnumValues(enumObject).filter((value) => typeof value === 'string');\n}\n\n/**\n * Extracts all values from an enumeration-like object.\n *\n * This function handles TypeScript enums correctly by accounting for the reverse mapping\n * that occurs with numeric enums. It works with pure numeric enums, pure string enums,\n * and mixed enums.\n *\n * @template T - The type of the enum object\n *\n * @param {T} enumObject - The enumeration-like object to extract values from,\n * the object can contain numeric values, string values, or both.\n *\n * @returns {(T[keyof T])[]} An array containing all the values from the enum object\n *\n * @example\n * ```typescript\n * import { getEnumValues } from '@kikiutils/shared/enum';\n *\n * // Numeric enum\n * enum Status {\n * Active = 0,\n * Inactive = 1,\n * Pending = 2\n * }\n *\n * console.log(getEnumValues(Status)); // [0, 1, 2]\n *\n * // String enum\n * enum Color {\n * Red = 'RED',\n * Green = 'GREEN',\n * Blue = 'BLUE'\n * }\n *\n * console.log(getEnumValues(Color)); // ['RED', 'GREEN', 'BLUE']\n *\n * // Mixed enum\n * enum RecordType {\n * Receive = 0,\n * Send = 1,\n * Unknown = 'unknown'\n * }\n *\n * console.log(getEnumValues(RecordType)); // [0, 1, 'unknown']\n * ```\n */\nexport function getEnumValues<T extends Record<string, any>>(enumObject: T): (T[keyof T])[] {\n const values = Object.values(enumObject);\n const hasNumberValues = values.some((value) => typeof value === 'number');\n if (!hasNumberValues) return values;\n return Object.keys(enumObject).filter((key) => Number.isNaN(Number(key))).map((key) => enumObject[key]);\n}\n"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;AAsBG;AACG,SAAU,mBAAmB,CAAgC,UAAa,EAAA;AAC5E,IAAA,OAAO,aAAa,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,CAAC,KAAK,KAAK,OAAO,KAAK,KAAK,QAAQ,CAAC;AACjF;AAEA;;;;;;;;;;;;;;;;;;;;;;AAsBG;AACG,SAAU,mBAAmB,CAAgC,UAAa,EAAA;AAC5E,IAAA,OAAO,aAAa,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,CAAC,KAAK,KAAK,OAAO,KAAK,KAAK,QAAQ,CAAC;AACjF;AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6CG;AACG,SAAU,aAAa,CAAgC,UAAa,EAAA;IACtE,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC;AACxC,IAAA,MAAM,eAAe,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC,KAAK,KAAK,OAAO,KAAK,KAAK,QAAQ,CAAC;AACzE,IAAA,IAAI,CAAC,eAAe;AAAE,QAAA,OAAO,MAAM;AACnC,IAAA,OAAO,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,CAAC,GAAG,KAAK,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,KAAK,UAAU,CAAC,GAAG,CAAC,CAAC;AAC3G;;;;"}
|
package/dist/env.cjs
CHANGED
|
@@ -12,7 +12,7 @@ class EnvironmentNotFoundError extends Error {
|
|
|
12
12
|
/**
|
|
13
13
|
* Creates a new EnvironmentNotFoundError.
|
|
14
14
|
*
|
|
15
|
-
* @param {string} key - The missing environment variable key
|
|
15
|
+
* @param {string} key - The missing environment variable key
|
|
16
16
|
*/
|
|
17
17
|
constructor(key) {
|
|
18
18
|
super(`Missing environment variable: ${key}`);
|
|
@@ -27,11 +27,11 @@ class EnvironmentNotFoundError extends Error {
|
|
|
27
27
|
* Only checks for `process.env[key] === undefined`. An empty string (e.g. '') or any falsy string
|
|
28
28
|
* value like `'0'` or `'false'` is considered a valid (defined) value.
|
|
29
29
|
*
|
|
30
|
-
* @param {string} key - The environment variable key to retrieve
|
|
30
|
+
* @param {string} key - The environment variable key to retrieve
|
|
31
31
|
*
|
|
32
|
-
* @returns {string} The value of the environment variable
|
|
32
|
+
* @returns {string} The value of the environment variable
|
|
33
33
|
*
|
|
34
|
-
* @throws {EnvironmentNotFoundError} If the environment variable is not defined
|
|
34
|
+
* @throws {EnvironmentNotFoundError} If the environment variable is not defined
|
|
35
35
|
*
|
|
36
36
|
* @example
|
|
37
37
|
* ```typescript
|
package/dist/env.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"env.cjs","sources":["../src/env.ts"],"sourcesContent":["/**\n * Custom error class for handling missing environment variables.\n *\n * Extends the built-in `Error` class and includes the missing key.\n *\n * @extends {Error}\n */\nexport class EnvironmentNotFoundError extends Error {\n readonly key: string;\n\n /**\n * Creates a new EnvironmentNotFoundError.\n *\n * @param {string} key - The missing environment variable key
|
|
1
|
+
{"version":3,"file":"env.cjs","sources":["../src/env.ts"],"sourcesContent":["/**\n * Custom error class for handling missing environment variables.\n *\n * Extends the built-in `Error` class and includes the missing key.\n *\n * @extends {Error}\n */\nexport class EnvironmentNotFoundError extends Error {\n readonly key: string;\n\n /**\n * Creates a new EnvironmentNotFoundError.\n *\n * @param {string} key - The missing environment variable key\n */\n constructor(key: string) {\n super(`Missing environment variable: ${key}`);\n this.key = key;\n this.name = this.constructor.name;\n Error.captureStackTrace?.(this, this.constructor);\n }\n}\n\n/**\n * Retrieves the value of an environment variable, or throws an error if it is not defined.\n *\n * Only checks for `process.env[key] === undefined`. An empty string (e.g. '') or any falsy string\n * value like `'0'` or `'false'` is considered a valid (defined) value.\n *\n * @param {string} key - The environment variable key to retrieve\n *\n * @returns {string} The value of the environment variable\n *\n * @throws {EnvironmentNotFoundError} If the environment variable is not defined\n *\n * @example\n * ```typescript\n * process.env.API_KEY = '';\n * checkAndGetEnvValue('API_KEY'); // ✅ Returns '' (still considered \"defined\")\n *\n * delete process.env.API_KEY;\n * checkAndGetEnvValue('API_KEY'); // ❌ Throws EnvironmentNotFoundError\n * ```\n */\nexport function checkAndGetEnvValue(key: string): string {\n if (process.env[key] === undefined) throw new EnvironmentNotFoundError(key);\n return process.env[key];\n}\n"],"names":[],"mappings":";;AAAA;;;;;;AAMG;AACG,MAAO,wBAAyB,SAAQ,KAAK,CAAA;AACtC,IAAA,GAAG;AAEZ;;;;AAIG;AACH,IAAA,WAAA,CAAY,GAAW,EAAA;AACnB,QAAA,KAAK,CAAC,CAAA,8BAAA,EAAiC,GAAG,CAAA,CAAE,CAAC;AAC7C,QAAA,IAAI,CAAC,GAAG,GAAG,GAAG;QACd,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,WAAW,CAAC,IAAI;QACjC,KAAK,CAAC,iBAAiB,GAAG,IAAI,EAAE,IAAI,CAAC,WAAW,CAAC;IACrD;AACH;AAED;;;;;;;;;;;;;;;;;;;;AAoBG;AACG,SAAU,mBAAmB,CAAC,GAAW,EAAA;AAC3C,IAAA,IAAI,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,KAAK,SAAS;AAAE,QAAA,MAAM,IAAI,wBAAwB,CAAC,GAAG,CAAC;AAC3E,IAAA,OAAO,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC;AAC3B;;;;;"}
|
package/dist/env.d.ts
CHANGED
|
@@ -10,7 +10,7 @@ export declare class EnvironmentNotFoundError extends Error {
|
|
|
10
10
|
/**
|
|
11
11
|
* Creates a new EnvironmentNotFoundError.
|
|
12
12
|
*
|
|
13
|
-
* @param {string} key - The missing environment variable key
|
|
13
|
+
* @param {string} key - The missing environment variable key
|
|
14
14
|
*/
|
|
15
15
|
constructor(key: string);
|
|
16
16
|
}
|
|
@@ -20,11 +20,11 @@ export declare class EnvironmentNotFoundError extends Error {
|
|
|
20
20
|
* Only checks for `process.env[key] === undefined`. An empty string (e.g. '') or any falsy string
|
|
21
21
|
* value like `'0'` or `'false'` is considered a valid (defined) value.
|
|
22
22
|
*
|
|
23
|
-
* @param {string} key - The environment variable key to retrieve
|
|
23
|
+
* @param {string} key - The environment variable key to retrieve
|
|
24
24
|
*
|
|
25
|
-
* @returns {string} The value of the environment variable
|
|
25
|
+
* @returns {string} The value of the environment variable
|
|
26
26
|
*
|
|
27
|
-
* @throws {EnvironmentNotFoundError} If the environment variable is not defined
|
|
27
|
+
* @throws {EnvironmentNotFoundError} If the environment variable is not defined
|
|
28
28
|
*
|
|
29
29
|
* @example
|
|
30
30
|
* ```typescript
|
package/dist/env.mjs
CHANGED
|
@@ -10,7 +10,7 @@ class EnvironmentNotFoundError extends Error {
|
|
|
10
10
|
/**
|
|
11
11
|
* Creates a new EnvironmentNotFoundError.
|
|
12
12
|
*
|
|
13
|
-
* @param {string} key - The missing environment variable key
|
|
13
|
+
* @param {string} key - The missing environment variable key
|
|
14
14
|
*/
|
|
15
15
|
constructor(key) {
|
|
16
16
|
super(`Missing environment variable: ${key}`);
|
|
@@ -25,11 +25,11 @@ class EnvironmentNotFoundError extends Error {
|
|
|
25
25
|
* Only checks for `process.env[key] === undefined`. An empty string (e.g. '') or any falsy string
|
|
26
26
|
* value like `'0'` or `'false'` is considered a valid (defined) value.
|
|
27
27
|
*
|
|
28
|
-
* @param {string} key - The environment variable key to retrieve
|
|
28
|
+
* @param {string} key - The environment variable key to retrieve
|
|
29
29
|
*
|
|
30
|
-
* @returns {string} The value of the environment variable
|
|
30
|
+
* @returns {string} The value of the environment variable
|
|
31
31
|
*
|
|
32
|
-
* @throws {EnvironmentNotFoundError} If the environment variable is not defined
|
|
32
|
+
* @throws {EnvironmentNotFoundError} If the environment variable is not defined
|
|
33
33
|
*
|
|
34
34
|
* @example
|
|
35
35
|
* ```typescript
|
package/dist/env.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"env.mjs","sources":["../src/env.ts"],"sourcesContent":["/**\n * Custom error class for handling missing environment variables.\n *\n * Extends the built-in `Error` class and includes the missing key.\n *\n * @extends {Error}\n */\nexport class EnvironmentNotFoundError extends Error {\n readonly key: string;\n\n /**\n * Creates a new EnvironmentNotFoundError.\n *\n * @param {string} key - The missing environment variable key
|
|
1
|
+
{"version":3,"file":"env.mjs","sources":["../src/env.ts"],"sourcesContent":["/**\n * Custom error class for handling missing environment variables.\n *\n * Extends the built-in `Error` class and includes the missing key.\n *\n * @extends {Error}\n */\nexport class EnvironmentNotFoundError extends Error {\n readonly key: string;\n\n /**\n * Creates a new EnvironmentNotFoundError.\n *\n * @param {string} key - The missing environment variable key\n */\n constructor(key: string) {\n super(`Missing environment variable: ${key}`);\n this.key = key;\n this.name = this.constructor.name;\n Error.captureStackTrace?.(this, this.constructor);\n }\n}\n\n/**\n * Retrieves the value of an environment variable, or throws an error if it is not defined.\n *\n * Only checks for `process.env[key] === undefined`. An empty string (e.g. '') or any falsy string\n * value like `'0'` or `'false'` is considered a valid (defined) value.\n *\n * @param {string} key - The environment variable key to retrieve\n *\n * @returns {string} The value of the environment variable\n *\n * @throws {EnvironmentNotFoundError} If the environment variable is not defined\n *\n * @example\n * ```typescript\n * process.env.API_KEY = '';\n * checkAndGetEnvValue('API_KEY'); // ✅ Returns '' (still considered \"defined\")\n *\n * delete process.env.API_KEY;\n * checkAndGetEnvValue('API_KEY'); // ❌ Throws EnvironmentNotFoundError\n * ```\n */\nexport function checkAndGetEnvValue(key: string): string {\n if (process.env[key] === undefined) throw new EnvironmentNotFoundError(key);\n return process.env[key];\n}\n"],"names":[],"mappings":"AAAA;;;;;;AAMG;AACG,MAAO,wBAAyB,SAAQ,KAAK,CAAA;AACtC,IAAA,GAAG;AAEZ;;;;AAIG;AACH,IAAA,WAAA,CAAY,GAAW,EAAA;AACnB,QAAA,KAAK,CAAC,CAAA,8BAAA,EAAiC,GAAG,CAAA,CAAE,CAAC;AAC7C,QAAA,IAAI,CAAC,GAAG,GAAG,GAAG;QACd,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,WAAW,CAAC,IAAI;QACjC,KAAK,CAAC,iBAAiB,GAAG,IAAI,EAAE,IAAI,CAAC,WAAW,CAAC;IACrD;AACH;AAED;;;;;;;;;;;;;;;;;;;;AAoBG;AACG,SAAU,mBAAmB,CAAC,GAAW,EAAA;AAC3C,IAAA,IAAI,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,KAAK,SAAS;AAAE,QAAA,MAAM,IAAI,wBAAwB,CAAC,GAAG,CAAC;AAC3E,IAAA,OAAO,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC;AAC3B;;;;"}
|
package/dist/general.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"general.cjs","sources":["../src/general.ts"],"sourcesContent":["/* eslint-disable jsdoc/check-param-names */\n\n/**\n * Extracts the first value from an array or returns the value itself if it's not an array.\n *\n * - If `value` is an array, returns the first element.\n * - If `value` is not an array, returns `value` directly.\n * - If the result is `null` or `undefined`, and `defaultValue` is provided, returns `defaultValue` instead.\n *\n * @template T - The type of the input value(s)
|
|
1
|
+
{"version":3,"file":"general.cjs","sources":["../src/general.ts"],"sourcesContent":["/* eslint-disable jsdoc/check-param-names */\n\n/**\n * Extracts the first value from an array or returns the value itself if it's not an array.\n *\n * - If `value` is an array, returns the first element.\n * - If `value` is not an array, returns `value` directly.\n * - If the result is `null` or `undefined`, and `defaultValue` is provided, returns `defaultValue` instead.\n *\n * @template T - The type of the input value(s)\n * @template D - The type of the default value (if provided)\n *\n * @param {T | T[]} value - A single value or an array of values\n * @param {D} [defaultValue] - A fallback value if the result is `null` or `undefined`\n *\n * @returns {T | D | undefined} The first value or the fallback\n *\n * @example\n * ```typescript\n * import { extractFirstValue } from '@kikiutils/shared/general';\n *\n * console.log(extractFirstValue([1, 2, 3])); // 1\n * console.log(extractFirstValue('hello')); // hello\n * console.log(extractFirstValue([], 'default')); // default\n * console.log(extractFirstValue(undefined, 'fallback')); // fallback\n * ```\n */\nexport function extractFirstValue<T>(value: T | T[]): T | undefined;\nexport function extractFirstValue<T, D>(value: T | T[], defaultValue: D): D | NonNullable<T>;\nexport function extractFirstValue<T, D>(value: T | T[], defaultValue?: D) {\n return (Array.isArray(value) ? value[0] : value) ?? defaultValue;\n}\n\n/* eslint-enable jsdoc/check-param-names */\n"],"names":[],"mappings":";;AAAA;AA6BM,SAAU,iBAAiB,CAAO,KAAc,EAAE,YAAgB,EAAA;IACpE,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,GAAG,KAAK,KAAK,YAAY;AACpE;AAEA;;;;"}
|
package/dist/general.d.ts
CHANGED
|
@@ -5,13 +5,13 @@
|
|
|
5
5
|
* - If `value` is not an array, returns `value` directly.
|
|
6
6
|
* - If the result is `null` or `undefined`, and `defaultValue` is provided, returns `defaultValue` instead.
|
|
7
7
|
*
|
|
8
|
-
* @template T - The type of the input value(s)
|
|
9
|
-
* @template D - The type of the default value (if provided)
|
|
8
|
+
* @template T - The type of the input value(s)
|
|
9
|
+
* @template D - The type of the default value (if provided)
|
|
10
10
|
*
|
|
11
|
-
* @param {T | T[]} value - A single value or an array of values
|
|
12
|
-
* @param {D} [defaultValue] - A fallback value if the result is `null` or `undefined
|
|
11
|
+
* @param {T | T[]} value - A single value or an array of values
|
|
12
|
+
* @param {D} [defaultValue] - A fallback value if the result is `null` or `undefined`
|
|
13
13
|
*
|
|
14
|
-
* @returns {T | D | undefined} The first value or the fallback
|
|
14
|
+
* @returns {T | D | undefined} The first value or the fallback
|
|
15
15
|
*
|
|
16
16
|
* @example
|
|
17
17
|
* ```typescript
|
package/dist/general.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"general.mjs","sources":["../src/general.ts"],"sourcesContent":["/* eslint-disable jsdoc/check-param-names */\n\n/**\n * Extracts the first value from an array or returns the value itself if it's not an array.\n *\n * - If `value` is an array, returns the first element.\n * - If `value` is not an array, returns `value` directly.\n * - If the result is `null` or `undefined`, and `defaultValue` is provided, returns `defaultValue` instead.\n *\n * @template T - The type of the input value(s)
|
|
1
|
+
{"version":3,"file":"general.mjs","sources":["../src/general.ts"],"sourcesContent":["/* eslint-disable jsdoc/check-param-names */\n\n/**\n * Extracts the first value from an array or returns the value itself if it's not an array.\n *\n * - If `value` is an array, returns the first element.\n * - If `value` is not an array, returns `value` directly.\n * - If the result is `null` or `undefined`, and `defaultValue` is provided, returns `defaultValue` instead.\n *\n * @template T - The type of the input value(s)\n * @template D - The type of the default value (if provided)\n *\n * @param {T | T[]} value - A single value or an array of values\n * @param {D} [defaultValue] - A fallback value if the result is `null` or `undefined`\n *\n * @returns {T | D | undefined} The first value or the fallback\n *\n * @example\n * ```typescript\n * import { extractFirstValue } from '@kikiutils/shared/general';\n *\n * console.log(extractFirstValue([1, 2, 3])); // 1\n * console.log(extractFirstValue('hello')); // hello\n * console.log(extractFirstValue([], 'default')); // default\n * console.log(extractFirstValue(undefined, 'fallback')); // fallback\n * ```\n */\nexport function extractFirstValue<T>(value: T | T[]): T | undefined;\nexport function extractFirstValue<T, D>(value: T | T[], defaultValue: D): D | NonNullable<T>;\nexport function extractFirstValue<T, D>(value: T | T[], defaultValue?: D) {\n return (Array.isArray(value) ? value[0] : value) ?? defaultValue;\n}\n\n/* eslint-enable jsdoc/check-param-names */\n"],"names":[],"mappings":"AAAA;AA6BM,SAAU,iBAAiB,CAAO,KAAc,EAAE,YAAgB,EAAA;IACpE,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,GAAG,KAAK,KAAK,YAAY;AACpE;AAEA;;;;"}
|
package/dist/math.cjs
CHANGED
|
@@ -9,11 +9,11 @@ const decimal_js = require('decimal.js');
|
|
|
9
9
|
* - Supports custom decimal places and optional percentage symbol.
|
|
10
10
|
* - Returns `'0.00%'` if result is `NaN` or division is invalid.
|
|
11
11
|
*
|
|
12
|
-
* @param {CalculableValue} molecular - The numerator of the fraction
|
|
13
|
-
* @param {CalculableValue} denominator - The denominator of the fraction
|
|
14
|
-
* @param {ToPercentageStringOptions} [options] - Optional output settings
|
|
12
|
+
* @param {CalculableValue} molecular - The numerator of the fraction
|
|
13
|
+
* @param {CalculableValue} denominator - The denominator of the fraction
|
|
14
|
+
* @param {ToPercentageStringOptions} [options] - Optional output settings
|
|
15
15
|
*
|
|
16
|
-
* @returns {string} Formatted percentage string
|
|
16
|
+
* @returns {string} Formatted percentage string
|
|
17
17
|
*
|
|
18
18
|
* @example
|
|
19
19
|
* ```typescript
|
package/dist/math.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"math.cjs","sources":["../src/math.ts"],"sourcesContent":["import { Decimal } from 'decimal.js';\n\ntype CalculableValue = Decimal.Value | { toString: () => string };\n\n/**\n * Options for configuring the output of `toPercentageString`.\n */\nexport interface ToPercentageStringOptions {\n /**\n * Number of decimal places to include in the result.\n *\n * @default 2\n */\n decimalPlaces?: number;\n\n /**\n * Whether to include the '%' symbol in the result.\n *\n * @default true\n */\n withSymbol?: boolean;\n}\n\n/**\n * Converts a fraction (numerator / denominator) into a percentage string.\n *\n * - Uses `decimal.js` for precise decimal calculations.\n * - Supports custom decimal places and optional percentage symbol.\n * - Returns `'0.00%'` if result is `NaN` or division is invalid.\n *\n * @param {CalculableValue} molecular - The numerator of the fraction
|
|
1
|
+
{"version":3,"file":"math.cjs","sources":["../src/math.ts"],"sourcesContent":["import { Decimal } from 'decimal.js';\n\ntype CalculableValue = Decimal.Value | { toString: () => string };\n\n/**\n * Options for configuring the output of `toPercentageString`.\n */\nexport interface ToPercentageStringOptions {\n /**\n * Number of decimal places to include in the result.\n *\n * @default 2\n */\n decimalPlaces?: number;\n\n /**\n * Whether to include the '%' symbol in the result.\n *\n * @default true\n */\n withSymbol?: boolean;\n}\n\n/**\n * Converts a fraction (numerator / denominator) into a percentage string.\n *\n * - Uses `decimal.js` for precise decimal calculations.\n * - Supports custom decimal places and optional percentage symbol.\n * - Returns `'0.00%'` if result is `NaN` or division is invalid.\n *\n * @param {CalculableValue} molecular - The numerator of the fraction\n * @param {CalculableValue} denominator - The denominator of the fraction\n * @param {ToPercentageStringOptions} [options] - Optional output settings\n *\n * @returns {string} Formatted percentage string\n *\n * @example\n * ```typescript\n * import { toPercentageString } from '@kikiutils/shared/math';\n *\n * console.log(toPercentageString(50, 200)); // 25.00%\n * console.log(toPercentageString(50, 200, { withSymbol: false })); // 25.00\n * console.log(toPercentageString(50, 200, { decimalPlaces: 1 })); // 25.0%\n * ```\n */\nexport function toPercentageString(\n molecular: CalculableValue,\n denominator: CalculableValue,\n options?: ToPercentageStringOptions,\n) {\n const molecularDecimal = new Decimal(molecular.toString());\n const denominatorDecimal = new Decimal(denominator.toString());\n const calculationResult = molecularDecimal.div(denominatorDecimal);\n const result = calculationResult.isNaN()\n ? '0.00'\n : calculationResult.times(100).toFixed(options?.decimalPlaces ?? 2);\n\n return options?.withSymbol ?? true ? `${result}%` : result;\n}\n"],"names":["Decimal"],"mappings":";;;;AAuBA;;;;;;;;;;;;;;;;;;;;;AAqBG;SACa,kBAAkB,CAC9B,SAA0B,EAC1B,WAA4B,EAC5B,OAAmC,EAAA;IAEnC,MAAM,gBAAgB,GAAG,IAAIA,kBAAO,CAAC,SAAS,CAAC,QAAQ,EAAE,CAAC;IAC1D,MAAM,kBAAkB,GAAG,IAAIA,kBAAO,CAAC,WAAW,CAAC,QAAQ,EAAE,CAAC;IAC9D,MAAM,iBAAiB,GAAG,gBAAgB,CAAC,GAAG,CAAC,kBAAkB,CAAC;AAClE,IAAA,MAAM,MAAM,GAAG,iBAAiB,CAAC,KAAK;AAClC,UAAE;AACF,UAAE,iBAAiB,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,OAAO,EAAE,aAAa,IAAI,CAAC,CAAC;AAEvE,IAAA,OAAO,OAAO,EAAE,UAAU,IAAI,IAAI,GAAG,CAAA,EAAG,MAAM,CAAA,CAAA,CAAG,GAAG,MAAM;AAC9D;;;;"}
|
package/dist/math.d.ts
CHANGED
|
@@ -26,11 +26,11 @@ export interface ToPercentageStringOptions {
|
|
|
26
26
|
* - Supports custom decimal places and optional percentage symbol.
|
|
27
27
|
* - Returns `'0.00%'` if result is `NaN` or division is invalid.
|
|
28
28
|
*
|
|
29
|
-
* @param {CalculableValue} molecular - The numerator of the fraction
|
|
30
|
-
* @param {CalculableValue} denominator - The denominator of the fraction
|
|
31
|
-
* @param {ToPercentageStringOptions} [options] - Optional output settings
|
|
29
|
+
* @param {CalculableValue} molecular - The numerator of the fraction
|
|
30
|
+
* @param {CalculableValue} denominator - The denominator of the fraction
|
|
31
|
+
* @param {ToPercentageStringOptions} [options] - Optional output settings
|
|
32
32
|
*
|
|
33
|
-
* @returns {string} Formatted percentage string
|
|
33
|
+
* @returns {string} Formatted percentage string
|
|
34
34
|
*
|
|
35
35
|
* @example
|
|
36
36
|
* ```typescript
|
package/dist/math.mjs
CHANGED
|
@@ -7,11 +7,11 @@ import { Decimal } from 'decimal.js';
|
|
|
7
7
|
* - Supports custom decimal places and optional percentage symbol.
|
|
8
8
|
* - Returns `'0.00%'` if result is `NaN` or division is invalid.
|
|
9
9
|
*
|
|
10
|
-
* @param {CalculableValue} molecular - The numerator of the fraction
|
|
11
|
-
* @param {CalculableValue} denominator - The denominator of the fraction
|
|
12
|
-
* @param {ToPercentageStringOptions} [options] - Optional output settings
|
|
10
|
+
* @param {CalculableValue} molecular - The numerator of the fraction
|
|
11
|
+
* @param {CalculableValue} denominator - The denominator of the fraction
|
|
12
|
+
* @param {ToPercentageStringOptions} [options] - Optional output settings
|
|
13
13
|
*
|
|
14
|
-
* @returns {string} Formatted percentage string
|
|
14
|
+
* @returns {string} Formatted percentage string
|
|
15
15
|
*
|
|
16
16
|
* @example
|
|
17
17
|
* ```typescript
|