@kikiutils/shared 12.0.0 → 13.0.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/README.md +1 -1
- package/dist/buffer.d.ts +7 -3
- package/dist/buffer.d.ts.map +1 -1
- package/dist/buffer.js +42 -0
- package/dist/buffer.js.map +1 -0
- package/dist/clipboard.d.ts +8 -6
- package/dist/clipboard.d.ts.map +1 -1
- package/dist/clipboard.js +86 -0
- package/dist/clipboard.js.map +1 -0
- package/dist/consola.d.ts +8 -2
- package/dist/consola.d.ts.map +1 -1
- package/dist/consola.js +32 -0
- package/dist/consola.js.map +1 -0
- package/dist/crypto-hash.d.ts +16 -26
- package/dist/crypto-hash.d.ts.map +1 -1
- package/dist/crypto-hash.js +37 -0
- package/dist/crypto-hash.js.map +1 -0
- package/dist/datetime.d.ts +13 -9
- package/dist/datetime.d.ts.map +1 -1
- package/dist/datetime.js +131 -0
- package/dist/datetime.js.map +1 -0
- package/dist/element-plus.d.ts +8 -4
- package/dist/element-plus.d.ts.map +1 -1
- package/dist/element-plus.js +37 -0
- package/dist/element-plus.js.map +1 -0
- package/dist/enum.d.ts +6 -3
- package/dist/enum.d.ts.map +1 -1
- package/dist/enum.js +108 -0
- package/dist/enum.js.map +1 -0
- package/dist/env.d.ts +12 -9
- package/dist/env.d.ts.map +1 -1
- package/dist/env.js +51 -0
- package/dist/env.js.map +1 -0
- package/dist/general.d.ts +5 -2
- package/dist/general.d.ts.map +1 -1
- package/dist/general.js +8 -0
- package/dist/general.js.map +1 -0
- package/dist/hash.d.ts +7 -4
- package/dist/hash.d.ts.map +1 -1
- package/dist/hash.js +20 -0
- package/dist/hash.js.map +1 -0
- package/dist/math.d.ts +20 -17
- package/dist/math.d.ts.map +1 -1
- package/dist/math.js +36 -0
- package/dist/math.js.map +1 -0
- package/dist/number.d.ts +7 -2
- package/dist/number.d.ts.map +1 -1
- package/dist/number.js +32 -0
- package/dist/number.js.map +1 -0
- package/dist/object.d.ts +4 -1
- package/dist/object.d.ts.map +1 -1
- package/dist/object.js +42 -0
- package/dist/object.js.map +1 -0
- package/dist/pino.d.ts +8 -2
- package/dist/pino.d.ts.map +1 -1
- package/dist/pino.js +40 -0
- package/dist/pino.js.map +1 -0
- package/dist/random.d.ts +4 -1
- package/dist/random.d.ts.map +1 -1
- package/dist/random.js +31 -0
- package/dist/random.js.map +1 -0
- package/dist/storage/enhanced/local/core.d.ts +45 -42
- package/dist/storage/enhanced/local/core.d.ts.map +1 -1
- package/dist/storage/enhanced/local/core.js +63 -0
- package/dist/storage/enhanced/local/core.js.map +1 -0
- package/dist/storage/enhanced/local/index.d.ts +3 -3
- package/dist/storage/enhanced/local/index.js +4 -0
- package/dist/storage/enhanced/local/keyed-store.d.ts +14 -11
- package/dist/storage/enhanced/local/keyed-store.d.ts.map +1 -1
- package/dist/storage/enhanced/local/keyed-store.js +36 -0
- package/dist/storage/enhanced/local/keyed-store.js.map +1 -0
- package/dist/storage/enhanced/redis/core.d.ts +64 -59
- package/dist/storage/enhanced/redis/core.d.ts.map +1 -1
- package/dist/storage/enhanced/redis/core.js +85 -0
- package/dist/storage/enhanced/redis/core.js.map +1 -0
- package/dist/storage/enhanced/redis/index.d.ts +3 -3
- package/dist/storage/enhanced/redis/index.js +4 -0
- package/dist/storage/enhanced/redis/keyed-store.d.ts +19 -14
- package/dist/storage/enhanced/redis/keyed-store.d.ts.map +1 -1
- package/dist/storage/enhanced/redis/keyed-store.js +41 -0
- package/dist/storage/enhanced/redis/keyed-store.js.map +1 -0
- package/dist/storage/lru/keyed-store.d.ts +21 -16
- package/dist/storage/lru/keyed-store.d.ts.map +1 -1
- package/dist/storage/lru/keyed-store.js +40 -0
- package/dist/storage/lru/keyed-store.js.map +1 -0
- package/dist/string.d.ts +5 -2
- package/dist/string.d.ts.map +1 -1
- package/dist/string.js +42 -0
- package/dist/string.js.map +1 -0
- package/dist/url.d.ts +4 -1
- package/dist/url.d.ts.map +1 -1
- package/dist/url.js +21 -0
- package/dist/url.js.map +1 -0
- package/dist/vue.d.ts +10 -5
- package/dist/vue.d.ts.map +1 -1
- package/dist/vue.js +58 -0
- package/dist/vue.js.map +1 -0
- package/dist/web.d.ts +5 -2
- package/dist/web.d.ts.map +1 -1
- package/dist/web.js +32 -0
- package/dist/web.js.map +1 -0
- package/package.json +42 -26
- package/src/vue.ts +1 -1
- package/src/web.ts +1 -1
- package/dist/buffer.cjs +0 -43
- package/dist/buffer.cjs.map +0 -1
- package/dist/buffer.mjs +0 -41
- package/dist/buffer.mjs.map +0 -1
- package/dist/clipboard.cjs +0 -91
- package/dist/clipboard.cjs.map +0 -1
- package/dist/clipboard.mjs +0 -88
- package/dist/clipboard.mjs.map +0 -1
- package/dist/consola.cjs +0 -35
- package/dist/consola.cjs.map +0 -1
- package/dist/consola.mjs +0 -32
- package/dist/consola.mjs.map +0 -1
- package/dist/crypto-hash.cjs +0 -61
- package/dist/crypto-hash.cjs.map +0 -1
- package/dist/crypto-hash.mjs +0 -50
- package/dist/crypto-hash.mjs.map +0 -1
- package/dist/datetime.cjs +0 -134
- package/dist/datetime.cjs.map +0 -1
- package/dist/datetime.mjs +0 -130
- package/dist/datetime.mjs.map +0 -1
- package/dist/element-plus.cjs +0 -37
- package/dist/element-plus.cjs.map +0 -1
- package/dist/element-plus.mjs +0 -35
- package/dist/element-plus.mjs.map +0 -1
- package/dist/enum.cjs +0 -112
- package/dist/enum.cjs.map +0 -1
- package/dist/enum.mjs +0 -108
- package/dist/enum.mjs.map +0 -1
- package/dist/env.cjs +0 -53
- package/dist/env.cjs.map +0 -1
- package/dist/env.mjs +0 -50
- package/dist/env.mjs.map +0 -1
- package/dist/general.cjs +0 -10
- package/dist/general.cjs.map +0 -1
- package/dist/general.mjs +0 -8
- package/dist/general.mjs.map +0 -1
- package/dist/hash.cjs +0 -36
- package/dist/hash.cjs.map +0 -1
- package/dist/hash.mjs +0 -31
- package/dist/hash.mjs.map +0 -1
- package/dist/math.cjs +0 -38
- package/dist/math.cjs.map +0 -1
- package/dist/math.mjs +0 -36
- package/dist/math.mjs.map +0 -1
- package/dist/number.cjs +0 -32
- package/dist/number.cjs.map +0 -1
- package/dist/number.mjs +0 -30
- package/dist/number.mjs.map +0 -1
- package/dist/object.cjs +0 -55
- package/dist/object.cjs.map +0 -1
- package/dist/object.mjs +0 -53
- package/dist/object.mjs.map +0 -1
- package/dist/pino.cjs +0 -42
- package/dist/pino.cjs.map +0 -1
- package/dist/pino.mjs +0 -39
- package/dist/pino.mjs.map +0 -1
- package/dist/random.cjs +0 -31
- package/dist/random.cjs.map +0 -1
- package/dist/random.mjs +0 -29
- package/dist/random.mjs.map +0 -1
- package/dist/storage/enhanced/local/core.cjs +0 -105
- package/dist/storage/enhanced/local/core.cjs.map +0 -1
- package/dist/storage/enhanced/local/core.mjs +0 -103
- package/dist/storage/enhanced/local/core.mjs.map +0 -1
- package/dist/storage/enhanced/local/index.cjs +0 -10
- package/dist/storage/enhanced/local/index.cjs.map +0 -1
- package/dist/storage/enhanced/local/index.d.ts.map +0 -1
- package/dist/storage/enhanced/local/index.mjs +0 -3
- package/dist/storage/enhanced/local/index.mjs.map +0 -1
- package/dist/storage/enhanced/local/keyed-store.cjs +0 -41
- package/dist/storage/enhanced/local/keyed-store.cjs.map +0 -1
- package/dist/storage/enhanced/local/keyed-store.mjs +0 -39
- package/dist/storage/enhanced/local/keyed-store.mjs.map +0 -1
- package/dist/storage/enhanced/redis/core.cjs +0 -144
- package/dist/storage/enhanced/redis/core.cjs.map +0 -1
- package/dist/storage/enhanced/redis/core.mjs +0 -142
- package/dist/storage/enhanced/redis/core.mjs.map +0 -1
- package/dist/storage/enhanced/redis/index.cjs +0 -10
- package/dist/storage/enhanced/redis/index.cjs.map +0 -1
- package/dist/storage/enhanced/redis/index.d.ts.map +0 -1
- package/dist/storage/enhanced/redis/index.mjs +0 -3
- package/dist/storage/enhanced/redis/index.mjs.map +0 -1
- package/dist/storage/enhanced/redis/keyed-store.cjs +0 -46
- package/dist/storage/enhanced/redis/keyed-store.cjs.map +0 -1
- package/dist/storage/enhanced/redis/keyed-store.mjs +0 -44
- package/dist/storage/enhanced/redis/keyed-store.mjs.map +0 -1
- package/dist/storage/lru/keyed-store.cjs +0 -49
- package/dist/storage/lru/keyed-store.cjs.map +0 -1
- package/dist/storage/lru/keyed-store.mjs +0 -47
- package/dist/storage/lru/keyed-store.mjs.map +0 -1
- package/dist/string.cjs +0 -45
- package/dist/string.cjs.map +0 -1
- package/dist/string.mjs +0 -43
- package/dist/string.mjs.map +0 -1
- package/dist/url.cjs +0 -21
- package/dist/url.cjs.map +0 -1
- package/dist/url.mjs +0 -19
- package/dist/url.mjs.map +0 -1
- package/dist/vue.cjs +0 -64
- package/dist/vue.cjs.map +0 -1
- package/dist/vue.mjs +0 -59
- package/dist/vue.mjs.map +0 -1
- package/dist/web.cjs +0 -35
- package/dist/web.cjs.map +0 -1
- package/dist/web.mjs +0 -32
- package/dist/web.mjs.map +0 -1
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
//#region src/element-plus.ts
|
|
2
|
+
/**
|
|
3
|
+
* Creates a reusable Element Plus `<el-form-item>` validation rule with sensible defaults.
|
|
4
|
+
*
|
|
5
|
+
* This utility provides default values for `required`, `trigger`, and `type`,
|
|
6
|
+
* while allowing overrides via the `options` parameter. It simplifies common
|
|
7
|
+
* form validation rule creation and ensures consistency across forms.
|
|
8
|
+
*
|
|
9
|
+
* @param {string} message - The validation message to display when the rule fails
|
|
10
|
+
* @param {FormItemRule} [options] - Optional overrides for the rule fields
|
|
11
|
+
* @param {boolean} [options.required] - Whether the field is required (default: `true`)
|
|
12
|
+
* @param {string | string[]} [options.trigger] - The event(s) that trigger validation (default: `'blur'`)
|
|
13
|
+
* @param {RuleType} [options.type] - The expected type of the field (default: `'string'`)
|
|
14
|
+
*
|
|
15
|
+
* @returns A `FormItemRule` object that can be used in Element Plus form validation
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* import { createElFormItemRuleWithDefaults } from '@kikiutils/shared/element-plus';
|
|
20
|
+
*
|
|
21
|
+
* const rule = createElFormItemRuleWithDefaults('This field is required');
|
|
22
|
+
* const optionalRule = createElFormItemRuleWithDefaults('Optional field', { required: false });
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
function createElFormItemRuleWithDefaults(message, options = {}) {
|
|
26
|
+
return {
|
|
27
|
+
...options,
|
|
28
|
+
message,
|
|
29
|
+
required: options.required ?? true,
|
|
30
|
+
trigger: options.trigger ?? "blur",
|
|
31
|
+
type: options.type ?? "string"
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
//#endregion
|
|
36
|
+
export { createElFormItemRuleWithDefaults };
|
|
37
|
+
//# sourceMappingURL=element-plus.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"element-plus.js","names":[],"sources":["../src/element-plus.ts"],"sourcesContent":["import type { RuleType } from 'async-validator';\nimport type { FormItemRule } from 'element-plus';\n\nexport type DoNotRemoveOrUseThisType = RuleType;\n\n/**\n * Creates a reusable Element Plus `<el-form-item>` validation rule with sensible defaults.\n *\n * This utility provides default values for `required`, `trigger`, and `type`,\n * while allowing overrides via the `options` parameter. It simplifies common\n * form validation rule creation and ensures consistency across forms.\n *\n * @param {string} message - The validation message to display when the rule fails\n * @param {FormItemRule} [options] - Optional overrides for the rule fields\n * @param {boolean} [options.required] - Whether the field is required (default: `true`)\n * @param {string | string[]} [options.trigger] - The event(s) that trigger validation (default: `'blur'`)\n * @param {RuleType} [options.type] - The expected type of the field (default: `'string'`)\n *\n * @returns A `FormItemRule` object that can be used in Element Plus form validation\n *\n * @example\n * ```typescript\n * import { createElFormItemRuleWithDefaults } from '@kikiutils/shared/element-plus';\n *\n * const rule = createElFormItemRuleWithDefaults('This field is required');\n * const optionalRule = createElFormItemRuleWithDefaults('Optional field', { required: false });\n * ```\n */\nexport function createElFormItemRuleWithDefaults(message: string, options: FormItemRule = {}): FormItemRule {\n return {\n ...options,\n message,\n required: options.required ?? true,\n trigger: options.trigger ?? 'blur',\n type: options.type ?? 'string',\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AA4BA,SAAgB,iCAAiC,SAAiB,UAAwB,EAAE,EAAgB;AACxG,QAAO;EACH,GAAG;EACH;EACA,UAAU,QAAQ,YAAY;EAC9B,SAAS,QAAQ,WAAW;EAC5B,MAAM,QAAQ,QAAQ;EACzB"}
|
package/dist/enum.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
//#region src/enum.d.ts
|
|
1
2
|
/**
|
|
2
3
|
* Extracts only the numeric values from an enumeration-like object.
|
|
3
4
|
*
|
|
@@ -21,7 +22,7 @@
|
|
|
21
22
|
* console.log(getEnumNumberValues(RecordType)); // [0, 1]
|
|
22
23
|
* ```
|
|
23
24
|
*/
|
|
24
|
-
|
|
25
|
+
declare function getEnumNumberValues<T extends Record<string, any>>(enumObject: T): Extract<T[keyof T], number>[];
|
|
25
26
|
/**
|
|
26
27
|
* Extracts only the string values from an enumeration-like object.
|
|
27
28
|
*
|
|
@@ -45,7 +46,7 @@ export declare function getEnumNumberValues<T extends Record<string, any>>(enumO
|
|
|
45
46
|
* console.log(getEnumStringValues(RecordType)); // ['unknown']
|
|
46
47
|
* ```
|
|
47
48
|
*/
|
|
48
|
-
|
|
49
|
+
declare function getEnumStringValues<T extends Record<string, any>>(enumObject: T): Extract<T[keyof T], string>[];
|
|
49
50
|
/**
|
|
50
51
|
* Extracts all values from an enumeration-like object.
|
|
51
52
|
*
|
|
@@ -92,5 +93,7 @@ export declare function getEnumStringValues<T extends Record<string, any>>(enumO
|
|
|
92
93
|
* console.log(getEnumValues(RecordType)); // [0, 1, 'unknown']
|
|
93
94
|
* ```
|
|
94
95
|
*/
|
|
95
|
-
|
|
96
|
+
declare function getEnumValues<T extends Record<string, any>>(enumObject: T): (T[keyof T])[];
|
|
97
|
+
//#endregion
|
|
98
|
+
export { getEnumNumberValues, getEnumStringValues, getEnumValues };
|
|
96
99
|
//# sourceMappingURL=enum.d.ts.map
|
package/dist/enum.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"enum.d.ts","
|
|
1
|
+
{"version":3,"file":"enum.d.ts","names":[],"sources":["../src/enum.ts"],"sourcesContent":[],"mappings":";;AAuBA;;;;;;;;AA2BA;;;;;;;;AAkDA;;;;;;AAAuF,iBA7EvE,mBA6EuE,CAAA,UA7EzC,MA6EyC,CAAA,MAAA,EAAA,GAAA,CAAA,CAAA,CAAA,UAAA,EA7ER,CA6EQ,CAAA,EA7EJ,OA6EI,CA7EI,CA6EJ,CAAA,MA7EY,CA6EZ,CAAA,EAAA,MAAA,CAAA,EAAA;;;;;;;;;;;;;;;;;;;;;;;;iBAlDvE,8BAA8B,iCAAiC,IAAI,QAAQ,QAAQ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAkDnF,wBAAwB,iCAAiC,KAAK,QAAQ"}
|
package/dist/enum.js
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
//#region src/enum.ts
|
|
2
|
+
/**
|
|
3
|
+
* Extracts only the numeric values from an enumeration-like object.
|
|
4
|
+
*
|
|
5
|
+
* @template T - The type of the enum object
|
|
6
|
+
*
|
|
7
|
+
* @param {T} enumObject - The enumeration-like object to extract numeric values from,
|
|
8
|
+
* the object can contain numeric values, string values, or both.
|
|
9
|
+
*
|
|
10
|
+
* @returns {Extract<T[keyof T], number>[]} An array of numeric values extracted from the enum object
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* import { getEnumNumberValues } from '@kikiutils/shared/enum';
|
|
15
|
+
*
|
|
16
|
+
* enum RecordType {
|
|
17
|
+
* Receive = 0,
|
|
18
|
+
* Send = 1,
|
|
19
|
+
* Unknown = 'unknown'
|
|
20
|
+
* }
|
|
21
|
+
*
|
|
22
|
+
* console.log(getEnumNumberValues(RecordType)); // [0, 1]
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
function getEnumNumberValues(enumObject) {
|
|
26
|
+
return getEnumValues(enumObject).filter((value) => typeof value === "number");
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Extracts only the string values from an enumeration-like object.
|
|
30
|
+
*
|
|
31
|
+
* @template T - The type of the enum object
|
|
32
|
+
*
|
|
33
|
+
* @param {T} enumObject - The enumeration-like object to extract string values from,
|
|
34
|
+
* the object can contain numeric values, string values, or both.
|
|
35
|
+
*
|
|
36
|
+
* @returns {Extract<T[keyof T], string>[]} An array of string values extracted from the enum object
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```typescript
|
|
40
|
+
* import { getEnumStringValues } from '@kikiutils/shared/enum';
|
|
41
|
+
*
|
|
42
|
+
* enum RecordType {
|
|
43
|
+
* Receive = 0,
|
|
44
|
+
* Send = 1,
|
|
45
|
+
* Unknown = 'unknown'
|
|
46
|
+
* }
|
|
47
|
+
*
|
|
48
|
+
* console.log(getEnumStringValues(RecordType)); // ['unknown']
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
function getEnumStringValues(enumObject) {
|
|
52
|
+
return getEnumValues(enumObject).filter((value) => typeof value === "string");
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Extracts all values from an enumeration-like object.
|
|
56
|
+
*
|
|
57
|
+
* This function handles TypeScript enums correctly by accounting for the reverse mapping
|
|
58
|
+
* that occurs with numeric enums. It works with pure numeric enums, pure string enums,
|
|
59
|
+
* and mixed enums.
|
|
60
|
+
*
|
|
61
|
+
* @template T - The type of the enum object
|
|
62
|
+
*
|
|
63
|
+
* @param {T} enumObject - The enumeration-like object to extract values from,
|
|
64
|
+
* the object can contain numeric values, string values, or both.
|
|
65
|
+
*
|
|
66
|
+
* @returns {(T[keyof T])[]} An array containing all the values from the enum object
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```typescript
|
|
70
|
+
* import { getEnumValues } from '@kikiutils/shared/enum';
|
|
71
|
+
*
|
|
72
|
+
* // Numeric enum
|
|
73
|
+
* enum Status {
|
|
74
|
+
* Active = 0,
|
|
75
|
+
* Inactive = 1,
|
|
76
|
+
* Pending = 2
|
|
77
|
+
* }
|
|
78
|
+
*
|
|
79
|
+
* console.log(getEnumValues(Status)); // [0, 1, 2]
|
|
80
|
+
*
|
|
81
|
+
* // String enum
|
|
82
|
+
* enum Color {
|
|
83
|
+
* Red = 'RED',
|
|
84
|
+
* Green = 'GREEN',
|
|
85
|
+
* Blue = 'BLUE'
|
|
86
|
+
* }
|
|
87
|
+
*
|
|
88
|
+
* console.log(getEnumValues(Color)); // ['RED', 'GREEN', 'BLUE']
|
|
89
|
+
*
|
|
90
|
+
* // Mixed enum
|
|
91
|
+
* enum RecordType {
|
|
92
|
+
* Receive = 0,
|
|
93
|
+
* Send = 1,
|
|
94
|
+
* Unknown = 'unknown'
|
|
95
|
+
* }
|
|
96
|
+
*
|
|
97
|
+
* console.log(getEnumValues(RecordType)); // [0, 1, 'unknown']
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
function getEnumValues(enumObject) {
|
|
101
|
+
const values = Object.values(enumObject);
|
|
102
|
+
if (!values.some((value) => typeof value === "number")) return values;
|
|
103
|
+
return Object.keys(enumObject).filter((key) => Number.isNaN(Number(key))).map((key) => enumObject[key]);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
//#endregion
|
|
107
|
+
export { getEnumNumberValues, getEnumStringValues, getEnumValues };
|
|
108
|
+
//# sourceMappingURL=enum.js.map
|
package/dist/enum.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"enum.js","names":[],"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"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAuBA,SAAgB,oBAAmD,YAA8C;AAC7G,QAAO,cAAc,WAAW,CAAC,QAAQ,UAAU,OAAO,UAAU,SAAS;;;;;;;;;;;;;;;;;;;;;;;;;AA0BjF,SAAgB,oBAAmD,YAA8C;AAC7G,QAAO,cAAc,WAAW,CAAC,QAAQ,UAAU,OAAO,UAAU,SAAS;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiDjF,SAAgB,cAA6C,YAA+B;CACxF,MAAM,SAAS,OAAO,OAAO,WAAW;AAExC,KAAI,CADoB,OAAO,MAAM,UAAU,OAAO,UAAU,SAAS,CACnD,QAAO;AAC7B,QAAO,OAAO,KAAK,WAAW,CAAC,QAAQ,QAAQ,OAAO,MAAM,OAAO,IAAI,CAAC,CAAC,CAAC,KAAK,QAAQ,WAAW,KAAK"}
|
package/dist/env.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
//#region src/env.d.ts
|
|
1
2
|
/**
|
|
2
3
|
* Custom error class for handling missing environment variables.
|
|
3
4
|
*
|
|
@@ -5,14 +6,14 @@
|
|
|
5
6
|
*
|
|
6
7
|
* @extends {Error}
|
|
7
8
|
*/
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
9
|
+
declare class EnvironmentNotFoundError extends Error {
|
|
10
|
+
readonly key: string;
|
|
11
|
+
/**
|
|
12
|
+
* Creates a new EnvironmentNotFoundError.
|
|
13
|
+
*
|
|
14
|
+
* @param {string} key - The missing environment variable key
|
|
15
|
+
*/
|
|
16
|
+
constructor(key: string);
|
|
16
17
|
}
|
|
17
18
|
/**
|
|
18
19
|
* Retrieves the value of an environment variable, or throws an error if it is not defined.
|
|
@@ -35,5 +36,7 @@ export declare class EnvironmentNotFoundError extends Error {
|
|
|
35
36
|
* checkAndGetEnvValue('API_KEY'); // ❌ Throws EnvironmentNotFoundError
|
|
36
37
|
* ```
|
|
37
38
|
*/
|
|
38
|
-
|
|
39
|
+
declare function checkAndGetEnvValue(key: string): string;
|
|
40
|
+
//#endregion
|
|
41
|
+
export { EnvironmentNotFoundError, checkAndGetEnvValue };
|
|
39
42
|
//# sourceMappingURL=env.d.ts.map
|
package/dist/env.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"env.d.ts","
|
|
1
|
+
{"version":3,"file":"env.d.ts","names":[],"sources":["../src/env.ts"],"sourcesContent":[],"mappings":";;AAOA;AAqCA;;;;;cArCa,wBAAA,SAAiC,KAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAqC9B,mBAAA"}
|
package/dist/env.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
//#region src/env.ts
|
|
2
|
+
/**
|
|
3
|
+
* Custom error class for handling missing environment variables.
|
|
4
|
+
*
|
|
5
|
+
* Extends the built-in `Error` class and includes the missing key.
|
|
6
|
+
*
|
|
7
|
+
* @extends {Error}
|
|
8
|
+
*/
|
|
9
|
+
var EnvironmentNotFoundError = class extends Error {
|
|
10
|
+
key;
|
|
11
|
+
/**
|
|
12
|
+
* Creates a new EnvironmentNotFoundError.
|
|
13
|
+
*
|
|
14
|
+
* @param {string} key - The missing environment variable key
|
|
15
|
+
*/
|
|
16
|
+
constructor(key) {
|
|
17
|
+
super(`Missing environment variable: ${key}`);
|
|
18
|
+
this.key = key;
|
|
19
|
+
this.name = this.constructor.name;
|
|
20
|
+
Error.captureStackTrace?.(this, this.constructor);
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Retrieves the value of an environment variable, or throws an error if it is not defined.
|
|
25
|
+
*
|
|
26
|
+
* Only checks for `process.env[key] === undefined`. An empty string (e.g. '') or any falsy string
|
|
27
|
+
* value like `'0'` or `'false'` is considered a valid (defined) value.
|
|
28
|
+
*
|
|
29
|
+
* @param {string} key - The environment variable key to retrieve
|
|
30
|
+
*
|
|
31
|
+
* @returns {string} The value of the environment variable
|
|
32
|
+
*
|
|
33
|
+
* @throws {EnvironmentNotFoundError} If the environment variable is not defined
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```typescript
|
|
37
|
+
* process.env.API_KEY = '';
|
|
38
|
+
* checkAndGetEnvValue('API_KEY'); // ✅ Returns '' (still considered "defined")
|
|
39
|
+
*
|
|
40
|
+
* delete process.env.API_KEY;
|
|
41
|
+
* checkAndGetEnvValue('API_KEY'); // ❌ Throws EnvironmentNotFoundError
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
function checkAndGetEnvValue(key) {
|
|
45
|
+
if (process.env[key] === void 0) throw new EnvironmentNotFoundError(key);
|
|
46
|
+
return process.env[key];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
//#endregion
|
|
50
|
+
export { EnvironmentNotFoundError, checkAndGetEnvValue };
|
|
51
|
+
//# sourceMappingURL=env.js.map
|
package/dist/env.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"env.js","names":[],"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"],"mappings":";;;;;;;;AAOA,IAAa,2BAAb,cAA8C,MAAM;CAChD,AAAS;;;;;;CAOT,YAAY,KAAa;AACrB,QAAM,iCAAiC,MAAM;AAC7C,OAAK,MAAM;AACX,OAAK,OAAO,KAAK,YAAY;AAC7B,QAAM,oBAAoB,MAAM,KAAK,YAAY;;;;;;;;;;;;;;;;;;;;;;;;AAyBzD,SAAgB,oBAAoB,KAAqB;AACrD,KAAI,QAAQ,IAAI,SAAS,OAAW,OAAM,IAAI,yBAAyB,IAAI;AAC3E,QAAO,QAAQ,IAAI"}
|
package/dist/general.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
//#region src/general.d.ts
|
|
1
2
|
/**
|
|
2
3
|
* Extracts the first value from an array or returns the value itself if it's not an array.
|
|
3
4
|
*
|
|
@@ -23,6 +24,8 @@
|
|
|
23
24
|
* console.log(extractFirstValue(undefined, 'fallback')); // fallback
|
|
24
25
|
* ```
|
|
25
26
|
*/
|
|
26
|
-
|
|
27
|
-
|
|
27
|
+
declare function extractFirstValue<T>(value: T | T[]): T | undefined;
|
|
28
|
+
declare function extractFirstValue<T, D>(value: T | T[], defaultValue: D): D | NonNullable<T>;
|
|
29
|
+
//#endregion
|
|
30
|
+
export { extractFirstValue };
|
|
28
31
|
//# sourceMappingURL=general.d.ts.map
|
package/dist/general.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"general.d.ts","
|
|
1
|
+
{"version":3,"file":"general.d.ts","names":[],"sources":["../src/general.ts"],"sourcesContent":[],"mappings":";;AA2BA;;;;;;AACA;;;;;;;;;;;;;;;;;;iBADgB,4BAA4B,IAAI,MAAM;iBACtC,+BAA+B,IAAI,mBAAmB,IAAI,IAAI,YAAY"}
|
package/dist/general.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"general.js","names":[],"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"],"mappings":";AA6BA,SAAgB,kBAAwB,OAAgB,cAAkB;AACtE,SAAQ,MAAM,QAAQ,MAAM,GAAG,MAAM,KAAK,UAAU"}
|
package/dist/hash.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
//#region src/hash.d.ts
|
|
1
2
|
/**
|
|
2
3
|
* This file provides a set of functions for creating SHA-3 hash digests
|
|
3
4
|
* using different bit lengths (224, 256, 384, 512).
|
|
@@ -11,8 +12,10 @@
|
|
|
11
12
|
* console.log(sha3256('test')); // 36f028580bb02cc8272a9a020f4200e346e276ae664e45ee80745574e2f5ab80
|
|
12
13
|
* ```
|
|
13
14
|
*/
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
declare function sha3224(data: string | Uint8Array): string;
|
|
16
|
+
declare function sha3256(data: string | Uint8Array): string;
|
|
17
|
+
declare function sha3384(data: string | Uint8Array): string;
|
|
18
|
+
declare function sha3512(data: string | Uint8Array): string;
|
|
19
|
+
//#endregion
|
|
20
|
+
export { sha3224, sha3256, sha3384, sha3512 };
|
|
18
21
|
//# sourceMappingURL=hash.d.ts.map
|
package/dist/hash.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hash.d.ts","
|
|
1
|
+
{"version":3,"file":"hash.d.ts","names":[],"sources":["../src/hash.ts"],"sourcesContent":[],"mappings":";;AAyBA;AAIA;AAIA;AAIA;;;;;;;;;iBAZgB,OAAA,gBAAuB;iBAIvB,OAAA,gBAAuB;iBAIvB,OAAA,gBAAuB;iBAIvB,OAAA,gBAAuB"}
|
package/dist/hash.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { sha3_224, sha3_256, sha3_384, sha3_512 } from "@noble/hashes/sha3";
|
|
2
|
+
import { bytesToHex, utf8ToBytes } from "@noble/hashes/utils";
|
|
3
|
+
|
|
4
|
+
//#region src/hash.ts
|
|
5
|
+
function sha3224(data) {
|
|
6
|
+
return bytesToHex(sha3_224(typeof data === "string" ? utf8ToBytes(data) : data));
|
|
7
|
+
}
|
|
8
|
+
function sha3256(data) {
|
|
9
|
+
return bytesToHex(sha3_256(typeof data === "string" ? utf8ToBytes(data) : data));
|
|
10
|
+
}
|
|
11
|
+
function sha3384(data) {
|
|
12
|
+
return bytesToHex(sha3_384(typeof data === "string" ? utf8ToBytes(data) : data));
|
|
13
|
+
}
|
|
14
|
+
function sha3512(data) {
|
|
15
|
+
return bytesToHex(sha3_512(typeof data === "string" ? utf8ToBytes(data) : data));
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
//#endregion
|
|
19
|
+
export { sha3224, sha3256, sha3384, sha3512 };
|
|
20
|
+
//# sourceMappingURL=hash.js.map
|
package/dist/hash.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hash.js","names":[],"sources":["../src/hash.ts"],"sourcesContent":["/**\n * This file provides a set of functions for creating SHA-3 hash digests\n * using different bit lengths (224, 256, 384, 512).\n * These functions use the [@noble/hashes](https://github.com/paulmillr/noble-hashes) library to generate the hashes.\n * Can be used in the browser, mainly for Nuxt/Vue and other frameworks compiled and executed in the browser.\n *\n * @example\n * ```typescript\n * import { sha3256 } from '@kikiutils/shared/hash';\n *\n * console.log(sha3256('test')); // 36f028580bb02cc8272a9a020f4200e346e276ae664e45ee80745574e2f5ab80\n * ```\n */\n\nimport {\n sha3_224,\n sha3_256,\n sha3_384,\n sha3_512,\n} from '@noble/hashes/sha3';\nimport {\n bytesToHex,\n utf8ToBytes,\n} from '@noble/hashes/utils';\n\nexport function sha3224(data: string | Uint8Array) {\n return bytesToHex(sha3_224(typeof data === 'string' ? utf8ToBytes(data) : data));\n}\n\nexport function sha3256(data: string | Uint8Array) {\n return bytesToHex(sha3_256(typeof data === 'string' ? utf8ToBytes(data) : data));\n}\n\nexport function sha3384(data: string | Uint8Array) {\n return bytesToHex(sha3_384(typeof data === 'string' ? utf8ToBytes(data) : data));\n}\n\nexport function sha3512(data: string | Uint8Array) {\n return bytesToHex(sha3_512(typeof data === 'string' ? utf8ToBytes(data) : data));\n}\n"],"mappings":";;;;AAyBA,SAAgB,QAAQ,MAA2B;AAC/C,QAAO,WAAW,SAAS,OAAO,SAAS,WAAW,YAAY,KAAK,GAAG,KAAK,CAAC;;AAGpF,SAAgB,QAAQ,MAA2B;AAC/C,QAAO,WAAW,SAAS,OAAO,SAAS,WAAW,YAAY,KAAK,GAAG,KAAK,CAAC;;AAGpF,SAAgB,QAAQ,MAA2B;AAC/C,QAAO,WAAW,SAAS,OAAO,SAAS,WAAW,YAAY,KAAK,GAAG,KAAK,CAAC;;AAGpF,SAAgB,QAAQ,MAA2B;AAC/C,QAAO,WAAW,SAAS,OAAO,SAAS,WAAW,YAAY,KAAK,GAAG,KAAK,CAAC"}
|
package/dist/math.d.ts
CHANGED
|
@@ -1,23 +1,25 @@
|
|
|
1
|
-
import { Decimal } from
|
|
1
|
+
import { Decimal } from "decimal.js";
|
|
2
|
+
|
|
3
|
+
//#region src/math.d.ts
|
|
2
4
|
type CalculableValue = Decimal.Value | {
|
|
3
|
-
|
|
5
|
+
toString: () => string;
|
|
4
6
|
};
|
|
5
7
|
/**
|
|
6
8
|
* Options for configuring the output of `toPercentageString`.
|
|
7
9
|
*/
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
10
|
+
interface ToPercentageStringOptions {
|
|
11
|
+
/**
|
|
12
|
+
* Number of decimal places to include in the result.
|
|
13
|
+
*
|
|
14
|
+
* @default 2
|
|
15
|
+
*/
|
|
16
|
+
decimalPlaces?: number;
|
|
17
|
+
/**
|
|
18
|
+
* Whether to include the '%' symbol in the result.
|
|
19
|
+
*
|
|
20
|
+
* @default true
|
|
21
|
+
*/
|
|
22
|
+
withSymbol?: boolean;
|
|
21
23
|
}
|
|
22
24
|
/**
|
|
23
25
|
* Converts a fraction (numerator / denominator) into a percentage string.
|
|
@@ -41,6 +43,7 @@ export interface ToPercentageStringOptions {
|
|
|
41
43
|
* console.log(toPercentageString(50, 200, { decimalPlaces: 1 })); // 25.0%
|
|
42
44
|
* ```
|
|
43
45
|
*/
|
|
44
|
-
|
|
45
|
-
|
|
46
|
+
declare function toPercentageString(molecular: CalculableValue, denominator: CalculableValue, options?: ToPercentageStringOptions): string;
|
|
47
|
+
//#endregion
|
|
48
|
+
export { ToPercentageStringOptions, toPercentageString };
|
|
46
49
|
//# sourceMappingURL=math.d.ts.map
|
package/dist/math.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"math.d.ts","
|
|
1
|
+
{"version":3,"file":"math.d.ts","names":[],"sources":["../src/math.ts"],"sourcesContent":[],"mappings":";;;KAEK,eAAA,GAAkB,OAAA,CAAQ;;AAFM,CAAA;AAOrC;AAsCA;;AACe,UAvCE,yBAAA,CAuCF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBADC,kBAAA,YACD,8BACE,2BACH"}
|
package/dist/math.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Decimal } from "decimal.js";
|
|
2
|
+
|
|
3
|
+
//#region src/math.ts
|
|
4
|
+
/**
|
|
5
|
+
* Converts a fraction (numerator / denominator) into a percentage string.
|
|
6
|
+
*
|
|
7
|
+
* - Uses `decimal.js` for precise decimal calculations.
|
|
8
|
+
* - Supports custom decimal places and optional percentage symbol.
|
|
9
|
+
* - Returns `'0.00%'` if result is `NaN` or division is invalid.
|
|
10
|
+
*
|
|
11
|
+
* @param {CalculableValue} molecular - The numerator of the fraction
|
|
12
|
+
* @param {CalculableValue} denominator - The denominator of the fraction
|
|
13
|
+
* @param {ToPercentageStringOptions} [options] - Optional output settings
|
|
14
|
+
*
|
|
15
|
+
* @returns {string} Formatted percentage string
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* import { toPercentageString } from '@kikiutils/shared/math';
|
|
20
|
+
*
|
|
21
|
+
* console.log(toPercentageString(50, 200)); // 25.00%
|
|
22
|
+
* console.log(toPercentageString(50, 200, { withSymbol: false })); // 25.00
|
|
23
|
+
* console.log(toPercentageString(50, 200, { decimalPlaces: 1 })); // 25.0%
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
function toPercentageString(molecular, denominator, options) {
|
|
27
|
+
const molecularDecimal = new Decimal(molecular.toString());
|
|
28
|
+
const denominatorDecimal = new Decimal(denominator.toString());
|
|
29
|
+
const calculationResult = molecularDecimal.div(denominatorDecimal);
|
|
30
|
+
const result = calculationResult.isNaN() ? "0.00" : calculationResult.times(100).toFixed(options?.decimalPlaces ?? 2);
|
|
31
|
+
return options?.withSymbol ?? true ? `${result}%` : result;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
//#endregion
|
|
35
|
+
export { toPercentageString };
|
|
36
|
+
//# sourceMappingURL=math.js.map
|
package/dist/math.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"math.js","names":[],"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"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AA6CA,SAAgB,mBACZ,WACA,aACA,SACF;CACE,MAAM,mBAAmB,IAAI,QAAQ,UAAU,UAAU,CAAC;CAC1D,MAAM,qBAAqB,IAAI,QAAQ,YAAY,UAAU,CAAC;CAC9D,MAAM,oBAAoB,iBAAiB,IAAI,mBAAmB;CAClE,MAAM,SAAS,kBAAkB,OAAO,GAClC,SACA,kBAAkB,MAAM,IAAI,CAAC,QAAQ,SAAS,iBAAiB,EAAE;AAEvE,QAAO,SAAS,cAAc,OAAO,GAAG,OAAO,KAAK"}
|
package/dist/number.d.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
import { millify } from
|
|
1
|
+
import { millify } from "millify";
|
|
2
|
+
|
|
3
|
+
//#region src/number.d.ts
|
|
4
|
+
|
|
2
5
|
/**
|
|
3
6
|
* Converts a large number into a compact, human-readable string using `millify`.
|
|
4
7
|
*
|
|
@@ -17,5 +20,7 @@ import { millify } from 'millify';
|
|
|
17
20
|
* console.log(toCompactNumberString(1234567, { precision: 3 })); // 1.235m
|
|
18
21
|
* ```
|
|
19
22
|
*/
|
|
20
|
-
|
|
23
|
+
declare function toCompactNumberString(value: number, options?: Parameters<typeof millify>[1]): string;
|
|
24
|
+
//#endregion
|
|
25
|
+
export { toCompactNumberString };
|
|
21
26
|
//# sourceMappingURL=number.d.ts.map
|
package/dist/number.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"number.d.ts","
|
|
1
|
+
{"version":3,"file":"number.d.ts","names":[],"sources":["../src/number.ts"],"sourcesContent":[],"mappings":";;;;;;AAoBA;;;;;;;;;;;;;;;;iBAAgB,qBAAA,0BAA+C,kBAAkB"}
|
package/dist/number.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { millify } from "millify";
|
|
2
|
+
|
|
3
|
+
//#region src/number.ts
|
|
4
|
+
/**
|
|
5
|
+
* Converts a large number into a compact, human-readable string using `millify`.
|
|
6
|
+
*
|
|
7
|
+
* Applies lowercase units (e.g. 'k', 'm') and default precision of 2, unless overridden.
|
|
8
|
+
*
|
|
9
|
+
* @param {number} value - The number to format
|
|
10
|
+
* @param {Parameters<typeof millify>[1]} [options] - Optional configuration passed to `millify`
|
|
11
|
+
*
|
|
12
|
+
* @returns {string} The compact number string
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* import { toCompactNumberString } from '@kikiutils/shared/number';
|
|
17
|
+
*
|
|
18
|
+
* console.log(toCompactNumberString(1234567)); // 1.23m
|
|
19
|
+
* console.log(toCompactNumberString(1234567, { precision: 3 })); // 1.235m
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
function toCompactNumberString(value, options) {
|
|
23
|
+
return millify(value, {
|
|
24
|
+
lowercase: true,
|
|
25
|
+
precision: 2,
|
|
26
|
+
...options
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
//#endregion
|
|
31
|
+
export { toCompactNumberString };
|
|
32
|
+
//# sourceMappingURL=number.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"number.js","names":[],"sources":["../src/number.ts"],"sourcesContent":["import { millify } from 'millify';\n\n/**\n * Converts a large number into a compact, human-readable string using `millify`.\n *\n * Applies lowercase units (e.g. 'k', 'm') and default precision of 2, unless overridden.\n *\n * @param {number} value - The number to format\n * @param {Parameters<typeof millify>[1]} [options] - Optional configuration passed to `millify`\n *\n * @returns {string} The compact number string\n *\n * @example\n * ```typescript\n * import { toCompactNumberString } from '@kikiutils/shared/number';\n *\n * console.log(toCompactNumberString(1234567)); // 1.23m\n * console.log(toCompactNumberString(1234567, { precision: 3 })); // 1.235m\n * ```\n */\nexport function toCompactNumberString(value: number, options?: Parameters<typeof millify>[1]) {\n return millify(\n value,\n {\n lowercase: true,\n precision: 2,\n ...options,\n },\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAoBA,SAAgB,sBAAsB,OAAe,SAAyC;AAC1F,QAAO,QACH,OACA;EACI,WAAW;EACX,WAAW;EACX,GAAG;EACN,CACJ"}
|
package/dist/object.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
//#region src/object.d.ts
|
|
1
2
|
/**
|
|
2
3
|
* Serializes a nested object into a deterministic, flat string format.
|
|
3
4
|
*
|
|
@@ -21,5 +22,7 @@
|
|
|
21
22
|
* console.log(stringifyObjectDeterministically({ foo: 'bar' }, ':', '|')); // foo:bar
|
|
22
23
|
* ```
|
|
23
24
|
*/
|
|
24
|
-
|
|
25
|
+
declare function stringifyObjectDeterministically(input: Record<string, any>, kvSeparator?: string, pairSeparator?: string): string;
|
|
26
|
+
//#endregion
|
|
27
|
+
export { stringifyObjectDeterministically };
|
|
25
28
|
//# sourceMappingURL=object.d.ts.map
|
package/dist/object.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"object.d.ts","
|
|
1
|
+
{"version":3,"file":"object.d.ts","names":[],"sources":["../src/object.ts"],"sourcesContent":[],"mappings":";;AAuBA;;;;;;;;;;;;;;;;;;;;;;iBAAgB,gCAAA,QACL"}
|
package/dist/object.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
//#region src/object.ts
|
|
2
|
+
/**
|
|
3
|
+
* Serializes a nested object into a deterministic, flat string format.
|
|
4
|
+
*
|
|
5
|
+
* This function recursively traverses the input object (including nested objects and arrays),
|
|
6
|
+
* flattens it into key paths using dot notation (e.g. "a.b.0.c"), sorts all keys,
|
|
7
|
+
* and joins each key-value pair into a string with customizable separators.
|
|
8
|
+
*
|
|
9
|
+
* It is designed for use cases such as signature generation, cache key construction,
|
|
10
|
+
* or any context requiring consistent and predictable object serialization.
|
|
11
|
+
*
|
|
12
|
+
* @param {Record<string, any>} input - The object to serialize. Can contain nested objects and arrays
|
|
13
|
+
* @param {string} kvSeparator - The string used to separate each key from its value (default: '=')
|
|
14
|
+
* @param {string} pairSeparator - The string used to separate each key-value pair (default: '&')
|
|
15
|
+
* @returns {string} A deterministic string representation of the input object
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* import { stringifyObjectDeterministically } from '@kikiutils/shared/object';
|
|
20
|
+
*
|
|
21
|
+
* console.log(stringifyObjectDeterministically({ b: 2, a: { x: 1, y: [3, 4] } })); // a.x=1&a.y.0=3&a.y.1=4&b=2
|
|
22
|
+
* console.log(stringifyObjectDeterministically({ foo: 'bar' }, ':', '|')); // foo:bar
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
function stringifyObjectDeterministically(input, kvSeparator = "=", pairSeparator = "&") {
|
|
26
|
+
const entries = [];
|
|
27
|
+
function walk(object, path = []) {
|
|
28
|
+
if (Array.isArray(object)) object.forEach((value, index) => {
|
|
29
|
+
walk(value, [...path, index.toString()]);
|
|
30
|
+
});
|
|
31
|
+
else if (object !== null && typeof object === "object" && Object.prototype.toString.call(object) === "[object Object]") Object.keys(object).sort().forEach((key) => {
|
|
32
|
+
walk(object[key], [...path, key]);
|
|
33
|
+
});
|
|
34
|
+
else entries.push(`${path.join(".")}${kvSeparator}${String(object)}`);
|
|
35
|
+
}
|
|
36
|
+
walk(input);
|
|
37
|
+
return entries.sort().join(pairSeparator);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
//#endregion
|
|
41
|
+
export { stringifyObjectDeterministically };
|
|
42
|
+
//# sourceMappingURL=object.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"object.js","names":["entries: string[]"],"sources":["../src/object.ts"],"sourcesContent":["/**\n * Serializes a nested object into a deterministic, flat string format.\n *\n * This function recursively traverses the input object (including nested objects and arrays),\n * flattens it into key paths using dot notation (e.g. \"a.b.0.c\"), sorts all keys,\n * and joins each key-value pair into a string with customizable separators.\n *\n * It is designed for use cases such as signature generation, cache key construction,\n * or any context requiring consistent and predictable object serialization.\n *\n * @param {Record<string, any>} input - The object to serialize. Can contain nested objects and arrays\n * @param {string} kvSeparator - The string used to separate each key from its value (default: '=')\n * @param {string} pairSeparator - The string used to separate each key-value pair (default: '&')\n * @returns {string} A deterministic string representation of the input object\n *\n * @example\n * ```typescript\n * import { stringifyObjectDeterministically } from '@kikiutils/shared/object';\n *\n * console.log(stringifyObjectDeterministically({ b: 2, a: { x: 1, y: [3, 4] } })); // a.x=1&a.y.0=3&a.y.1=4&b=2\n * console.log(stringifyObjectDeterministically({ foo: 'bar' }, ':', '|')); // foo:bar\n * ```\n */\nexport function stringifyObjectDeterministically(\n input: Record<string, any>,\n kvSeparator: string = '=',\n pairSeparator: string = '&',\n) {\n const entries: string[] = [];\n\n function walk(object: any, path: string[] = []) {\n if (Array.isArray(object)) {\n object.forEach((value, index) => {\n walk(\n value,\n [\n ...path,\n index.toString(),\n ],\n );\n });\n } else if (\n object !== null\n && typeof object === 'object'\n && Object.prototype.toString.call(object) === '[object Object]'\n ) {\n Object.keys(object).sort().forEach((key) => {\n walk(\n object[key],\n [\n ...path,\n key,\n ],\n );\n });\n } else entries.push(`${path.join('.')}${kvSeparator}${String(object)}`);\n }\n\n walk(input);\n return entries.sort().join(pairSeparator);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAuBA,SAAgB,iCACZ,OACA,cAAsB,KACtB,gBAAwB,KAC1B;CACE,MAAMA,UAAoB,EAAE;CAE5B,SAAS,KAAK,QAAa,OAAiB,EAAE,EAAE;AAC5C,MAAI,MAAM,QAAQ,OAAO,CACrB,QAAO,SAAS,OAAO,UAAU;AAC7B,QACI,OACA,CACI,GAAG,MACH,MAAM,UAAU,CACnB,CACJ;IACH;WAEF,WAAW,QACR,OAAO,WAAW,YAClB,OAAO,UAAU,SAAS,KAAK,OAAO,KAAK,kBAE9C,QAAO,KAAK,OAAO,CAAC,MAAM,CAAC,SAAS,QAAQ;AACxC,QACI,OAAO,MACP,CACI,GAAG,MACH,IACH,CACJ;IACH;MACC,SAAQ,KAAK,GAAG,KAAK,KAAK,IAAI,GAAG,cAAc,OAAO,OAAO,GAAG;;AAG3E,MAAK,MAAM;AACX,QAAO,QAAQ,MAAM,CAAC,KAAK,cAAc"}
|