@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/src/enum.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,17 +21,19 @@
|
|
|
19
21
|
* console.log(getEnumNumberValues(RecordType)); // [0, 1]
|
|
20
22
|
* ```
|
|
21
23
|
*/
|
|
22
|
-
export function getEnumNumberValues(
|
|
23
|
-
return
|
|
24
|
+
export function getEnumNumberValues<T extends Record<string, any>>(enumObject: T): Extract<T[keyof T], number>[] {
|
|
25
|
+
return getEnumValues(enumObject).filter((value) => typeof value === 'number');
|
|
24
26
|
}
|
|
25
27
|
|
|
26
28
|
/**
|
|
27
|
-
* Extracts the string values from an enumeration-like object.
|
|
29
|
+
* Extracts only the string values from an enumeration-like object.
|
|
30
|
+
*
|
|
31
|
+
* @template T - The type of the enum object
|
|
28
32
|
*
|
|
29
|
-
* @param {
|
|
30
|
-
*
|
|
33
|
+
* @param {T} enumObject - The enumeration-like object to extract string values from,
|
|
34
|
+
* the object can contain numeric values, string values, or both.
|
|
31
35
|
*
|
|
32
|
-
* @returns {string[]} An array of string values extracted from the object
|
|
36
|
+
* @returns {Extract<T[keyof T], string>[]} An array of string values extracted from the enum object
|
|
33
37
|
*
|
|
34
38
|
* @example
|
|
35
39
|
* ```typescript
|
|
@@ -44,19 +48,59 @@ export function getEnumNumberValues(data: Record<number | string, number | strin
|
|
|
44
48
|
* console.log(getEnumStringValues(RecordType)); // ['unknown']
|
|
45
49
|
* ```
|
|
46
50
|
*/
|
|
47
|
-
export function getEnumStringValues
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
const values: any[] = [];
|
|
51
|
-
Object.entries(data).forEach(([key, value]) => {
|
|
52
|
-
keys.push(key);
|
|
53
|
-
values.push(value);
|
|
54
|
-
if (typeof value !== 'string') return;
|
|
55
|
-
keysCount[key] = (keysCount[key] ?? 0) + 1;
|
|
56
|
-
keysCount[value] = (keysCount[value] ?? 0) + 1;
|
|
57
|
-
});
|
|
51
|
+
export function getEnumStringValues<T extends Record<string, any>>(enumObject: T): Extract<T[keyof T], string>[] {
|
|
52
|
+
return getEnumValues(enumObject).filter((value) => typeof value === 'string');
|
|
53
|
+
}
|
|
58
54
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
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
|
+
export function getEnumValues<T extends Record<string, any>>(enumObject: T): (T[keyof T])[] {
|
|
102
|
+
const values = Object.values(enumObject);
|
|
103
|
+
const hasNumberValues = values.some((value) => typeof value === 'number');
|
|
104
|
+
if (!hasNumberValues) return values;
|
|
105
|
+
return Object.keys(enumObject).filter((key) => Number.isNaN(Number(key))).map((key) => enumObject[key]);
|
|
62
106
|
}
|
package/src/env.ts
CHANGED
|
@@ -11,7 +11,7 @@ export class EnvironmentNotFoundError extends Error {
|
|
|
11
11
|
/**
|
|
12
12
|
* Creates a new EnvironmentNotFoundError.
|
|
13
13
|
*
|
|
14
|
-
* @param {string} key - The missing environment variable key
|
|
14
|
+
* @param {string} key - The missing environment variable key
|
|
15
15
|
*/
|
|
16
16
|
constructor(key: string) {
|
|
17
17
|
super(`Missing environment variable: ${key}`);
|
|
@@ -27,11 +27,11 @@ export 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/src/general.ts
CHANGED
|
@@ -7,13 +7,13 @@
|
|
|
7
7
|
* - If `value` is not an array, returns `value` directly.
|
|
8
8
|
* - If the result is `null` or `undefined`, and `defaultValue` is provided, returns `defaultValue` instead.
|
|
9
9
|
*
|
|
10
|
-
* @template T - The type of the input value(s)
|
|
11
|
-
* @template D - The type of the default value (if provided)
|
|
10
|
+
* @template T - The type of the input value(s)
|
|
11
|
+
* @template D - The type of the default value (if provided)
|
|
12
12
|
*
|
|
13
|
-
* @param {T | T[]} value - A single value or an array of values
|
|
14
|
-
* @param {D} [defaultValue] - A fallback value if the result is `null` or `undefined
|
|
13
|
+
* @param {T | T[]} value - A single value or an array of values
|
|
14
|
+
* @param {D} [defaultValue] - A fallback value if the result is `null` or `undefined`
|
|
15
15
|
*
|
|
16
|
-
* @returns {T | D | undefined} The first value or the fallback
|
|
16
|
+
* @returns {T | D | undefined} The first value or the fallback
|
|
17
17
|
*
|
|
18
18
|
* @example
|
|
19
19
|
* ```typescript
|
package/src/math.ts
CHANGED
|
@@ -28,11 +28,11 @@ export interface ToPercentageStringOptions {
|
|
|
28
28
|
* - Supports custom decimal places and optional percentage symbol.
|
|
29
29
|
* - Returns `'0.00%'` if result is `NaN` or division is invalid.
|
|
30
30
|
*
|
|
31
|
-
* @param {CalculableValue} molecular - The numerator of the fraction
|
|
32
|
-
* @param {CalculableValue} denominator - The denominator of the fraction
|
|
33
|
-
* @param {ToPercentageStringOptions} [options] - Optional output settings
|
|
31
|
+
* @param {CalculableValue} molecular - The numerator of the fraction
|
|
32
|
+
* @param {CalculableValue} denominator - The denominator of the fraction
|
|
33
|
+
* @param {ToPercentageStringOptions} [options] - Optional output settings
|
|
34
34
|
*
|
|
35
|
-
* @returns {string} Formatted percentage string
|
|
35
|
+
* @returns {string} Formatted percentage string
|
|
36
36
|
*
|
|
37
37
|
* @example
|
|
38
38
|
* ```typescript
|
package/src/number.ts
CHANGED
|
@@ -5,10 +5,10 @@ import { millify } from 'millify';
|
|
|
5
5
|
*
|
|
6
6
|
* Applies lowercase units (e.g. 'k', 'm') and default precision of 2, unless overridden.
|
|
7
7
|
*
|
|
8
|
-
* @param {number} value - The number to format
|
|
9
|
-
* @param {Parameters<typeof millify>[1]} [options] - Optional configuration passed to `millify
|
|
8
|
+
* @param {number} value - The number to format
|
|
9
|
+
* @param {Parameters<typeof millify>[1]} [options] - Optional configuration passed to `millify`
|
|
10
10
|
*
|
|
11
|
-
* @returns {string} The compact number string
|
|
11
|
+
* @returns {string} The compact number string
|
|
12
12
|
*
|
|
13
13
|
* @example
|
|
14
14
|
* ```typescript
|
package/src/object.ts
CHANGED
|
@@ -8,10 +8,10 @@
|
|
|
8
8
|
* It is designed for use cases such as signature generation, cache key construction,
|
|
9
9
|
* or any context requiring consistent and predictable object serialization.
|
|
10
10
|
*
|
|
11
|
-
* @param {Record<string, any>} input - The object to serialize. Can contain nested objects and arrays
|
|
12
|
-
* @param {string} kvSeparator - The string used to separate each key from its value (default: '=')
|
|
13
|
-
* @param {string} pairSeparator - The string used to separate each key-value pair (default: '&')
|
|
14
|
-
* @returns {string} A deterministic string representation of the input object
|
|
11
|
+
* @param {Record<string, any>} input - The object to serialize. Can contain nested objects and arrays
|
|
12
|
+
* @param {string} kvSeparator - The string used to separate each key from its value (default: '=')
|
|
13
|
+
* @param {string} pairSeparator - The string used to separate each key-value pair (default: '&')
|
|
14
|
+
* @returns {string} A deterministic string representation of the input object
|
|
15
15
|
*
|
|
16
16
|
* @example
|
|
17
17
|
* ```typescript
|
package/src/random.ts
CHANGED
|
@@ -8,15 +8,15 @@
|
|
|
8
8
|
*
|
|
9
9
|
* This function supports any return type by using a generic type parameter.
|
|
10
10
|
*
|
|
11
|
-
* @template T - The return type of the generator function
|
|
11
|
+
* @template T - The return type of the generator function
|
|
12
12
|
*
|
|
13
|
-
* @param {(length: number) => T} generator - A function that accepts a length and returns a value of type T
|
|
14
|
-
* @param {number} minMin - Lower bound of the first random range
|
|
15
|
-
* @param {number} minMax - Upper bound of the first random range
|
|
16
|
-
* @param {number} maxMin - Lower bound of the second random range
|
|
17
|
-
* @param {number} maxMax - Upper bound of the second random range
|
|
13
|
+
* @param {(length: number) => T} generator - A function that accepts a length and returns a value of type T
|
|
14
|
+
* @param {number} minMin - Lower bound of the first random range
|
|
15
|
+
* @param {number} minMax - Upper bound of the first random range
|
|
16
|
+
* @param {number} maxMin - Lower bound of the second random range
|
|
17
|
+
* @param {number} maxMax - Upper bound of the second random range
|
|
18
18
|
*
|
|
19
|
-
* @returns {T} The result of the generator function using the computed final length
|
|
19
|
+
* @returns {T} The result of the generator function using the computed final length
|
|
20
20
|
*/
|
|
21
21
|
export function generateWithNestedRandomLength<T = string>(
|
|
22
22
|
generator: (length: number) => T,
|
|
@@ -34,11 +34,11 @@ export const enhancedLocalStorage = Object.freeze({
|
|
|
34
34
|
/**
|
|
35
35
|
* Retrieves a value by key and decodes it using SuperJSON or raw string.
|
|
36
36
|
*
|
|
37
|
-
* @template T - The expected type of the value
|
|
37
|
+
* @template T - The expected type of the value
|
|
38
38
|
*
|
|
39
|
-
* @param {string} key - The key of the value to retrieve
|
|
39
|
+
* @param {string} key - The key of the value to retrieve
|
|
40
40
|
*
|
|
41
|
-
* @returns {null | T} The decoded value or null if not found
|
|
41
|
+
* @returns {null | T} The decoded value or null if not found
|
|
42
42
|
*/
|
|
43
43
|
getItem<T = unknown>(key: string) {
|
|
44
44
|
const rawValue = window.localStorage.getItem(key);
|
|
@@ -47,15 +47,15 @@ export const enhancedLocalStorage = Object.freeze({
|
|
|
47
47
|
/**
|
|
48
48
|
* Checks whether a key exists in localStorage.
|
|
49
49
|
*
|
|
50
|
-
* @param {string} key - The key to check
|
|
50
|
+
* @param {string} key - The key to check
|
|
51
51
|
*
|
|
52
|
-
* @returns {boolean} True if the key exists, false otherwise
|
|
52
|
+
* @returns {boolean} True if the key exists, false otherwise
|
|
53
53
|
*/
|
|
54
54
|
hasItem: (key: string) => window.localStorage.getItem(key) !== null,
|
|
55
55
|
/**
|
|
56
56
|
* Returns the number of items stored in localStorage.
|
|
57
57
|
*
|
|
58
|
-
* @returns {number} The number of items stored in localStorage
|
|
58
|
+
* @returns {number} The number of items stored in localStorage
|
|
59
59
|
*/
|
|
60
60
|
get length() {
|
|
61
61
|
return window.localStorage.length;
|
|
@@ -63,14 +63,14 @@ export const enhancedLocalStorage = Object.freeze({
|
|
|
63
63
|
/**
|
|
64
64
|
* Removes a specific key from localStorage.
|
|
65
65
|
*
|
|
66
|
-
* @param {string} key - The key to remove
|
|
66
|
+
* @param {string} key - The key to remove
|
|
67
67
|
*/
|
|
68
68
|
removeItem: (key: string) => window.localStorage.removeItem(key),
|
|
69
69
|
/**
|
|
70
70
|
* Stores a value in localStorage with automatic serialization.
|
|
71
71
|
*
|
|
72
|
-
* @param {string} key - The key to store the value under
|
|
73
|
-
* @param {any} value - The value to store
|
|
72
|
+
* @param {string} key - The key to store the value under
|
|
73
|
+
* @param {any} value - The value to store
|
|
74
74
|
*/
|
|
75
75
|
setItem: (key: string, value: any) => window.localStorage.setItem(key, encodeToStorageValue(value)),
|
|
76
76
|
});
|
|
@@ -84,11 +84,11 @@ function decodeStorageValue(data: string) {
|
|
|
84
84
|
try {
|
|
85
85
|
return deserialize(JSON.parse(payload));
|
|
86
86
|
} catch {
|
|
87
|
-
throw new Error('[EnhancedLocalStorage] Failed to parse JSON payload
|
|
87
|
+
throw new Error('[EnhancedLocalStorage] Failed to parse JSON payload');
|
|
88
88
|
}
|
|
89
89
|
case StorageValueEncodingType.String: return payload;
|
|
90
90
|
default:
|
|
91
|
-
throw new Error(`[EnhancedLocalStorage] Unknown encoding type: ${type}
|
|
91
|
+
throw new Error(`[EnhancedLocalStorage] Unknown encoding type: ${type}`);
|
|
92
92
|
}
|
|
93
93
|
}
|
|
94
94
|
|
|
@@ -7,9 +7,9 @@ import { enhancedLocalStorage } from './core';
|
|
|
7
7
|
* This utility allows you to abstract away key construction logic and work directly
|
|
8
8
|
* with scoped key-value operations like `getItem`, `setItem`, and `removeItem`.
|
|
9
9
|
*
|
|
10
|
-
* @template D - The value type to store
|
|
10
|
+
* @template D - The value type to store
|
|
11
11
|
*
|
|
12
|
-
* @returns A factory that accepts a key generator function and returns a scoped storage interface
|
|
12
|
+
* @returns A factory that accepts a key generator function and returns a scoped storage interface
|
|
13
13
|
*
|
|
14
14
|
* @example
|
|
15
15
|
* ```typescript
|
|
@@ -28,7 +28,7 @@ export function createKeyedEnhancedLocalStore<D = unknown>() {
|
|
|
28
28
|
/**
|
|
29
29
|
* Resolves the storage key from the given arguments.
|
|
30
30
|
*
|
|
31
|
-
* @returns {string} The final string key used internally
|
|
31
|
+
* @returns {string} The final string key used internally
|
|
32
32
|
*/
|
|
33
33
|
resolveKey: (...args: P) => getKeyFunction(...args),
|
|
34
34
|
setItem: (value: D, ...args: P) => enhancedLocalStorage.setItem(getKeyFunction(...args), value),
|
|
@@ -26,9 +26,9 @@ const customValueHeaderLength = customValueHeader.byteLength + 1;
|
|
|
26
26
|
* This utility provides a typed, serializable key-value store backed by Redis,
|
|
27
27
|
* supporting TTL operations and safe deserialization of complex types (e.g. Date, Map).
|
|
28
28
|
*
|
|
29
|
-
* @param {Redis | string} ioRedisInstanceOrUrl - Either an existing `ioredis` instance or a Redis connection string
|
|
29
|
+
* @param {Redis | string} ioRedisInstanceOrUrl - Either an existing `ioredis` instance or a Redis connection string
|
|
30
30
|
*
|
|
31
|
-
* @returns A frozen object that wraps Redis commands with typed get/set logic and encoding
|
|
31
|
+
* @returns A frozen object that wraps Redis commands with typed get/set logic and encoding
|
|
32
32
|
*
|
|
33
33
|
* @example
|
|
34
34
|
* ```typescript
|
|
@@ -45,11 +45,11 @@ export function createEnhancedRedisStorage(ioRedisInstanceOrUrl: Redis | string)
|
|
|
45
45
|
/**
|
|
46
46
|
* Retrieves a value from Redis and decodes it.
|
|
47
47
|
*
|
|
48
|
-
* @template T - The expected return type
|
|
48
|
+
* @template T - The expected return type
|
|
49
49
|
*
|
|
50
|
-
* @param {string} key - The Redis key
|
|
50
|
+
* @param {string} key - The Redis key
|
|
51
51
|
*
|
|
52
|
-
* @returns {Promise<null | T>} The decoded value or null if not found
|
|
52
|
+
* @returns {Promise<null | T>} The decoded value or null if not found
|
|
53
53
|
*/
|
|
54
54
|
async getItem<T = unknown>(key: string) {
|
|
55
55
|
const rawValue = await instance.getBuffer(key);
|
|
@@ -58,24 +58,24 @@ export function createEnhancedRedisStorage(ioRedisInstanceOrUrl: Redis | string)
|
|
|
58
58
|
/**
|
|
59
59
|
* Gets the remaining TTL (in seconds) for a given key.
|
|
60
60
|
*
|
|
61
|
-
* @param {string} key - The Redis key
|
|
61
|
+
* @param {string} key - The Redis key
|
|
62
62
|
*
|
|
63
|
-
* @returns {Promise<number>} The number of seconds until the key expires, or -1 if no expiration is set
|
|
63
|
+
* @returns {Promise<number>} The number of seconds until the key expires, or -1 if no expiration is set
|
|
64
64
|
*/
|
|
65
65
|
getItemTtl: (key: string) => instance.ttl(key),
|
|
66
66
|
/**
|
|
67
67
|
* Checks whether a key exists in Redis.
|
|
68
68
|
*
|
|
69
|
-
* @param {string} key - The Redis key
|
|
69
|
+
* @param {string} key - The Redis key
|
|
70
70
|
*
|
|
71
|
-
* @returns {Promise<boolean>} True if the key exists, false otherwise
|
|
71
|
+
* @returns {Promise<boolean>} True if the key exists, false otherwise
|
|
72
72
|
*/
|
|
73
73
|
hasItem: async (key: string) => await instance.exists(key) === 1,
|
|
74
74
|
/**
|
|
75
75
|
* The underlying Redis instance, exposed for advanced operations.
|
|
76
76
|
* Use with caution; most use cases should rely on the wrapper methods.
|
|
77
77
|
*
|
|
78
|
-
* @returns {Redis} The underlying Redis instance
|
|
78
|
+
* @returns {Redis} The underlying Redis instance
|
|
79
79
|
*/
|
|
80
80
|
get instance() {
|
|
81
81
|
return instance;
|
|
@@ -83,7 +83,7 @@ export function createEnhancedRedisStorage(ioRedisInstanceOrUrl: Redis | string)
|
|
|
83
83
|
/**
|
|
84
84
|
* Removes a key from Redis.
|
|
85
85
|
*
|
|
86
|
-
* @param {string} key - The Redis key to delete
|
|
86
|
+
* @param {string} key - The Redis key to delete
|
|
87
87
|
*
|
|
88
88
|
* @returns {Promise<boolean>} A Promise that resolves to `true` if the key was removed,
|
|
89
89
|
* or `false` if it did not exist.
|
|
@@ -92,16 +92,16 @@ export function createEnhancedRedisStorage(ioRedisInstanceOrUrl: Redis | string)
|
|
|
92
92
|
/**
|
|
93
93
|
* Stores a value in Redis without expiration.
|
|
94
94
|
*
|
|
95
|
-
* @param {string} key - The Redis key
|
|
96
|
-
* @param {any} value - The value to store. Will be serialized
|
|
95
|
+
* @param {string} key - The Redis key
|
|
96
|
+
* @param {any} value - The value to store. Will be serialized
|
|
97
97
|
*/
|
|
98
98
|
setItem: (key: string, value: any) => instance.set(key, encodeToStorageValue(value)),
|
|
99
99
|
/**
|
|
100
100
|
* Stores a value in Redis with a time-to-live (TTL).
|
|
101
101
|
*
|
|
102
|
-
* @param {string} key - The Redis key
|
|
103
|
-
* @param {number} seconds - Expiration time in seconds
|
|
104
|
-
* @param {any} value - The value to store. Will be serialized
|
|
102
|
+
* @param {string} key - The Redis key
|
|
103
|
+
* @param {number} seconds - Expiration time in seconds
|
|
104
|
+
* @param {any} value - The value to store. Will be serialized
|
|
105
105
|
*/
|
|
106
106
|
setItemWithTtl(key: string, seconds: number, value: any) {
|
|
107
107
|
return instance.setex(key, seconds, encodeToStorageValue(value));
|
|
@@ -119,11 +119,11 @@ function decodeStorageValue(data: Buffer) {
|
|
|
119
119
|
try {
|
|
120
120
|
return deserialize(JSON.parse(payload.toString()));
|
|
121
121
|
} catch {
|
|
122
|
-
throw new Error('[RedisStorage] Failed to parse JSON payload
|
|
122
|
+
throw new Error('[RedisStorage] Failed to parse JSON payload');
|
|
123
123
|
}
|
|
124
124
|
case StorageValueEncodingType.String: return payload.toString();
|
|
125
125
|
default:
|
|
126
|
-
throw new Error(`[RedisStorage] Unknown encoding type: ${type}
|
|
126
|
+
throw new Error(`[RedisStorage] Unknown encoding type: ${type}`);
|
|
127
127
|
}
|
|
128
128
|
}
|
|
129
129
|
|
|
@@ -8,11 +8,11 @@ import type { createEnhancedRedisStorage } from './core';
|
|
|
8
8
|
* to Redis operations such as `getItem`, `setItem`, `setItemWithTtl`, and `getItemTtl`.
|
|
9
9
|
* It is ideal for namespaced data, caching, and session handling.
|
|
10
10
|
*
|
|
11
|
-
* @template D - The value type to store
|
|
11
|
+
* @template D - The value type to store
|
|
12
12
|
*
|
|
13
|
-
* @param {ReturnType<typeof createEnhancedRedisStorage>} storage - The enhanced Redis storage instance
|
|
13
|
+
* @param {ReturnType<typeof createEnhancedRedisStorage>} storage - The enhanced Redis storage instance
|
|
14
14
|
*
|
|
15
|
-
* @returns A factory that accepts a key generator function and returns a scoped Redis storage interface
|
|
15
|
+
* @returns A factory that accepts a key generator function and returns a scoped Redis storage interface
|
|
16
16
|
*
|
|
17
17
|
* @example
|
|
18
18
|
* ```typescript
|
|
@@ -32,7 +32,7 @@ export function createKeyedEnhancedRedisStore<D = unknown>(storage: ReturnType<t
|
|
|
32
32
|
/**
|
|
33
33
|
* Resolves the storage key from the given arguments.
|
|
34
34
|
*
|
|
35
|
-
* @returns {string} The final string key used internally
|
|
35
|
+
* @returns {string} The final string key used internally
|
|
36
36
|
*/
|
|
37
37
|
resolveKey: (...args: P) => getKeyFunction(...args),
|
|
38
38
|
setItem: (value: D, ...args: P) => storage.setItem(getKeyFunction(...args), value),
|
|
@@ -5,11 +5,11 @@ import type { LRUCache } from 'lru-cache';
|
|
|
5
5
|
*
|
|
6
6
|
* This utility allows structured access to cache entries using a dynamic key-generation function.
|
|
7
7
|
*
|
|
8
|
-
* @template D - The specific value type exposed by this store (must extend `V`)
|
|
8
|
+
* @template D - The specific value type exposed by this store (must extend `V`)
|
|
9
9
|
*
|
|
10
|
-
* @param {LRUCache<any, any, any>} lruInstance - An instance of `lru-cache
|
|
10
|
+
* @param {LRUCache<any, any, any>} lruInstance - An instance of `lru-cache`
|
|
11
11
|
*
|
|
12
|
-
* @returns A factory that accepts a key generator and returns a scoped, type-safe LRU-based store
|
|
12
|
+
* @returns A factory that accepts a key generator and returns a scoped, type-safe LRU-based store
|
|
13
13
|
*
|
|
14
14
|
* @example
|
|
15
15
|
* ```typescript
|
package/src/string.ts
CHANGED
|
@@ -23,12 +23,12 @@ const CHARSETS: Record<RandomStringMode, string> = {
|
|
|
23
23
|
/**
|
|
24
24
|
* Generates a random string of a given length using a specified character set.
|
|
25
25
|
*
|
|
26
|
-
* @param {number} length - The length of the string to generate. Must be a positive integer
|
|
27
|
-
* @param {RandomStringMode} [mode] - The character set to use
|
|
26
|
+
* @param {number} length - The length of the string to generate. Must be a positive integer
|
|
27
|
+
* @param {RandomStringMode} [mode] - The character set to use
|
|
28
28
|
*
|
|
29
|
-
* @returns {string} The generated random string
|
|
29
|
+
* @returns {string} The generated random string
|
|
30
30
|
*
|
|
31
|
-
* @throws {Error} If the length is not a positive integer or the mode is unsupported
|
|
31
|
+
* @throws {Error} If the length is not a positive integer or the mode is unsupported
|
|
32
32
|
*
|
|
33
33
|
* @example
|
|
34
34
|
* ```typescript
|
|
@@ -41,7 +41,7 @@ const CHARSETS: Record<RandomStringMode, string> = {
|
|
|
41
41
|
*/
|
|
42
42
|
export function randomString(length: number, mode: RandomStringMode = 'alphabetic') {
|
|
43
43
|
if (!Number.isInteger(length) || length <= 0) {
|
|
44
|
-
throw new Error(`Invalid length: ${length}. Must be a positive integer
|
|
44
|
+
throw new Error(`Invalid length: ${length}. Must be a positive integer`);
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
const charset = CHARSETS[mode];
|
package/src/url.ts
CHANGED
|
@@ -3,10 +3,10 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Typically used to preserve the user's current path for post-login navigation.
|
|
5
5
|
*
|
|
6
|
-
* @param {string} url - The target URL to modify
|
|
7
|
-
* @param {string} redirectPath - The path to use as the redirect destination
|
|
6
|
+
* @param {string} url - The target URL to modify
|
|
7
|
+
* @param {string} redirectPath - The path to use as the redirect destination
|
|
8
8
|
*
|
|
9
|
-
* @returns {string} A new URL string with the `redirect` query parameter
|
|
9
|
+
* @returns {string} A new URL string with the `redirect` query parameter
|
|
10
10
|
*/
|
|
11
11
|
export function appendRedirectParamToUrl(url: string, redirectPath: string) {
|
|
12
12
|
const [base, rawQuery = ''] = url.split('?');
|
package/src/vue.ts
CHANGED
|
@@ -10,9 +10,9 @@ import { appendRedirectParamToUrl } from './url';
|
|
|
10
10
|
/**
|
|
11
11
|
* Appends the current Vue Router route's fullPath as the `redirect` query parameter to the given URL.
|
|
12
12
|
*
|
|
13
|
-
* @param {string} url - The base URL to modify
|
|
13
|
+
* @param {string} url - The base URL to modify
|
|
14
14
|
*
|
|
15
|
-
* @returns {string} A new URL with the current route fullPath as the `redirect` parameter
|
|
15
|
+
* @returns {string} A new URL with the current route fullPath as the `redirect` parameter
|
|
16
16
|
*/
|
|
17
17
|
export function appendRedirectParamFromCurrentRouteToUrl(url: string) {
|
|
18
18
|
return appendRedirectParamToUrl(url, useRoute().fullPath);
|
|
@@ -21,7 +21,7 @@ export function appendRedirectParamFromCurrentRouteToUrl(url: string) {
|
|
|
21
21
|
/**
|
|
22
22
|
* Clears an interval referenced by a Vue ref and sets it to null.
|
|
23
23
|
*
|
|
24
|
-
* @param {Ref<null | ReturnType<typeof setInterval>>} intervalRef - A Vue ref holding a NodeJS.Timeout or null
|
|
24
|
+
* @param {Ref<null | ReturnType<typeof setInterval>>} intervalRef - A Vue ref holding a NodeJS.Timeout or null
|
|
25
25
|
*/
|
|
26
26
|
export function clearIntervalRef(intervalRef: Ref<null | ReturnType<typeof setInterval>>) {
|
|
27
27
|
if (intervalRef.value) clearInterval(intervalRef.value);
|
|
@@ -31,7 +31,7 @@ export function clearIntervalRef(intervalRef: Ref<null | ReturnType<typeof setIn
|
|
|
31
31
|
/**
|
|
32
32
|
* Clears a timeout referenced by a Vue ref and sets it to null.
|
|
33
33
|
*
|
|
34
|
-
* @param {Ref<null | ReturnType<typeof setTimeout>>} timeoutRef - A Vue ref holding a NodeJS.Timeout or null
|
|
34
|
+
* @param {Ref<null | ReturnType<typeof setTimeout>>} timeoutRef - A Vue ref holding a NodeJS.Timeout or null
|
|
35
35
|
*/
|
|
36
36
|
export function clearTimeoutRef(timeoutRef: Ref<null | ReturnType<typeof setTimeout>>) {
|
|
37
37
|
if (timeoutRef.value) clearTimeout(timeoutRef.value);
|
|
@@ -42,9 +42,9 @@ export function clearTimeoutRef(timeoutRef: Ref<null | ReturnType<typeof setTime
|
|
|
42
42
|
* A Vue composition function that remembers and restores scroll position
|
|
43
43
|
* of a scrollable container across route changes and keep-alive activation.
|
|
44
44
|
*
|
|
45
|
-
* @template T - The type of the scrollable element (defaults to HTMLElement)
|
|
45
|
+
* @template T - The type of the scrollable element (defaults to HTMLElement)
|
|
46
46
|
*
|
|
47
|
-
* @param {Ref<null | T>} containerRef - A ref to the scrollable HTML element
|
|
47
|
+
* @param {Ref<null | T>} containerRef - A ref to the scrollable HTML element
|
|
48
48
|
*/
|
|
49
49
|
export function usePreserveScroll<T extends Element = HTMLElement>(containerRef: Ref<null | T>) {
|
|
50
50
|
let scrollLeft = 0;
|
package/src/web.ts
CHANGED
|
@@ -3,9 +3,9 @@ import { appendRedirectParamToUrl } from './url';
|
|
|
3
3
|
/**
|
|
4
4
|
* Appends the current browser URL (including path, query, and hash) as the `redirect` query parameter to the given URL.
|
|
5
5
|
*
|
|
6
|
-
* @param {string} url - The base URL to modify
|
|
6
|
+
* @param {string} url - The base URL to modify
|
|
7
7
|
*
|
|
8
|
-
* @returns {string} A new URL with the current location as the `redirect` parameter
|
|
8
|
+
* @returns {string} A new URL with the current location as the `redirect` parameter
|
|
9
9
|
*/
|
|
10
10
|
export function appendRedirectParamFromCurrentLocationToUrl(url: string) {
|
|
11
11
|
const currentPath = `${window.location.pathname}${window.location.search}${window.location.hash}`;
|
|
@@ -19,8 +19,8 @@ export function appendRedirectParamFromCurrentLocationToUrl(url: string) {
|
|
|
19
19
|
* Useful for redirecting to login or other gateways while preserving
|
|
20
20
|
* the current location for post-auth navigation.
|
|
21
21
|
*
|
|
22
|
-
* @param {string} url - The destination URL to navigate to
|
|
23
|
-
* @param {number} [delayMs] - Optional delay in milliseconds before navigation
|
|
22
|
+
* @param {string} url - The destination URL to navigate to
|
|
23
|
+
* @param {number} [delayMs] - Optional delay in milliseconds before navigation
|
|
24
24
|
*/
|
|
25
25
|
export function assignUrlWithRedirectParamFromCurrentLocation(url: string, delayMs?: number) {
|
|
26
26
|
if (delayMs === undefined) window.location.assign(appendRedirectParamFromCurrentLocationToUrl(url));
|