@xivdyetools/core 1.3.7
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/LICENSE +37 -0
- package/README.md +400 -0
- package/dist/constants/index.d.ts +56 -0
- package/dist/constants/index.d.ts.map +1 -0
- package/dist/constants/index.js +103 -0
- package/dist/constants/index.js.map +1 -0
- package/dist/data/colors_xiv.json +3130 -0
- package/dist/data/locales/de.json +231 -0
- package/dist/data/locales/en.json +231 -0
- package/dist/data/locales/fr.json +231 -0
- package/dist/data/locales/ja.json +231 -0
- package/dist/data/locales/ko.json +233 -0
- package/dist/data/locales/zh.json +233 -0
- package/dist/data/presets.json +390 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +18 -0
- package/dist/index.js.map +1 -0
- package/dist/services/APIService.d.ts +246 -0
- package/dist/services/APIService.d.ts.map +1 -0
- package/dist/services/APIService.js +499 -0
- package/dist/services/APIService.js.map +1 -0
- package/dist/services/ColorService.d.ts +146 -0
- package/dist/services/ColorService.d.ts.map +1 -0
- package/dist/services/ColorService.js +209 -0
- package/dist/services/ColorService.js.map +1 -0
- package/dist/services/DyeService.d.ts +230 -0
- package/dist/services/DyeService.d.ts.map +1 -0
- package/dist/services/DyeService.js +326 -0
- package/dist/services/DyeService.js.map +1 -0
- package/dist/services/LocalizationService.d.ts +338 -0
- package/dist/services/LocalizationService.d.ts.map +1 -0
- package/dist/services/LocalizationService.js +449 -0
- package/dist/services/LocalizationService.js.map +1 -0
- package/dist/services/PaletteService.d.ts +137 -0
- package/dist/services/PaletteService.d.ts.map +1 -0
- package/dist/services/PaletteService.js +349 -0
- package/dist/services/PaletteService.js.map +1 -0
- package/dist/services/PresetService.d.ts +196 -0
- package/dist/services/PresetService.d.ts.map +1 -0
- package/dist/services/PresetService.js +261 -0
- package/dist/services/PresetService.js.map +1 -0
- package/dist/services/color/ColorAccessibility.d.ts +39 -0
- package/dist/services/color/ColorAccessibility.d.ts.map +1 -0
- package/dist/services/color/ColorAccessibility.js +71 -0
- package/dist/services/color/ColorAccessibility.js.map +1 -0
- package/dist/services/color/ColorConverter.d.ts +146 -0
- package/dist/services/color/ColorConverter.d.ts.map +1 -0
- package/dist/services/color/ColorConverter.js +393 -0
- package/dist/services/color/ColorConverter.js.map +1 -0
- package/dist/services/color/ColorManipulator.d.ts +36 -0
- package/dist/services/color/ColorManipulator.d.ts.map +1 -0
- package/dist/services/color/ColorManipulator.js +56 -0
- package/dist/services/color/ColorManipulator.js.map +1 -0
- package/dist/services/color/ColorblindnessSimulator.d.ts +35 -0
- package/dist/services/color/ColorblindnessSimulator.d.ts.map +1 -0
- package/dist/services/color/ColorblindnessSimulator.js +110 -0
- package/dist/services/color/ColorblindnessSimulator.js.map +1 -0
- package/dist/services/dye/DyeDatabase.d.ts +131 -0
- package/dist/services/dye/DyeDatabase.d.ts.map +1 -0
- package/dist/services/dye/DyeDatabase.js +367 -0
- package/dist/services/dye/DyeDatabase.js.map +1 -0
- package/dist/services/dye/DyeSearch.d.ts +55 -0
- package/dist/services/dye/DyeSearch.d.ts.map +1 -0
- package/dist/services/dye/DyeSearch.js +196 -0
- package/dist/services/dye/DyeSearch.js.map +1 -0
- package/dist/services/dye/HarmonyGenerator.d.ts +110 -0
- package/dist/services/dye/HarmonyGenerator.d.ts.map +1 -0
- package/dist/services/dye/HarmonyGenerator.js +221 -0
- package/dist/services/dye/HarmonyGenerator.js.map +1 -0
- package/dist/services/localization/LocaleLoader.d.ts +38 -0
- package/dist/services/localization/LocaleLoader.d.ts.map +1 -0
- package/dist/services/localization/LocaleLoader.js +83 -0
- package/dist/services/localization/LocaleLoader.js.map +1 -0
- package/dist/services/localization/LocaleRegistry.d.ts +73 -0
- package/dist/services/localization/LocaleRegistry.d.ts.map +1 -0
- package/dist/services/localization/LocaleRegistry.js +84 -0
- package/dist/services/localization/LocaleRegistry.js.map +1 -0
- package/dist/services/localization/TranslationProvider.d.ts +157 -0
- package/dist/services/localization/TranslationProvider.d.ts.map +1 -0
- package/dist/services/localization/TranslationProvider.js +289 -0
- package/dist/services/localization/TranslationProvider.js.map +1 -0
- package/dist/types/index.d.ts +409 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +87 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/logger.d.ts +84 -0
- package/dist/types/logger.d.ts.map +1 -0
- package/dist/types/logger.js +54 -0
- package/dist/types/logger.js.map +1 -0
- package/dist/utils/index.d.ts +441 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +577 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/kd-tree.d.ts +76 -0
- package/dist/utils/kd-tree.d.ts.map +1 -0
- package/dist/utils/kd-tree.js +195 -0
- package/dist/utils/kd-tree.js.map +1 -0
- package/dist/version.d.ts +11 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +11 -0
- package/dist/version.js.map +1 -0
- package/package.json +84 -0
|
@@ -0,0 +1,441 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @xivdyetools/core - Shared Utilities
|
|
3
|
+
*
|
|
4
|
+
* Reusable utility functions (environment-agnostic)
|
|
5
|
+
*
|
|
6
|
+
* @module utils
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Clamp a number between min and max values
|
|
10
|
+
*
|
|
11
|
+
* @param value - The value to clamp
|
|
12
|
+
* @param min - Minimum allowed value
|
|
13
|
+
* @param max - Maximum allowed value
|
|
14
|
+
* @returns Clamped value between min and max
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* clamp(150, 0, 100) // Returns 100
|
|
19
|
+
* clamp(-10, 0, 100) // Returns 0
|
|
20
|
+
* clamp(50, 0, 100) // Returns 50
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* Edge cases:
|
|
24
|
+
* - NaN values return NaN
|
|
25
|
+
* - Infinity is clamped to max
|
|
26
|
+
* - -Infinity is clamped to min
|
|
27
|
+
*/
|
|
28
|
+
export declare function clamp(value: number, min: number, max: number): number;
|
|
29
|
+
/**
|
|
30
|
+
* Linear interpolation between two values
|
|
31
|
+
*
|
|
32
|
+
* @param a - Start value
|
|
33
|
+
* @param b - End value
|
|
34
|
+
* @param t - Interpolation factor (0 = a, 1 = b, 0.5 = midpoint)
|
|
35
|
+
* @returns Interpolated value
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```typescript
|
|
39
|
+
* lerp(0, 100, 0.5) // Returns 50
|
|
40
|
+
* lerp(0, 100, 0) // Returns 0
|
|
41
|
+
* lerp(0, 100, 1) // Returns 100
|
|
42
|
+
* lerp(10, 20, 0.25) // Returns 12.5
|
|
43
|
+
* ```
|
|
44
|
+
*
|
|
45
|
+
* Edge cases:
|
|
46
|
+
* - t can be outside [0, 1] for extrapolation
|
|
47
|
+
* - NaN values return NaN
|
|
48
|
+
* - Handles Infinity correctly
|
|
49
|
+
*/
|
|
50
|
+
export declare function lerp(a: number, b: number, t: number): number;
|
|
51
|
+
/**
|
|
52
|
+
* Round a number to a specific decimal place
|
|
53
|
+
*
|
|
54
|
+
* @param value - The number to round
|
|
55
|
+
* @param decimals - Number of decimal places (default: 0)
|
|
56
|
+
* @returns Rounded number
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```typescript
|
|
60
|
+
* round(3.14159, 2) // Returns 3.14
|
|
61
|
+
* round(2.5) // Returns 3 (rounds to nearest integer)
|
|
62
|
+
* round(123.456, 1) // Returns 123.5
|
|
63
|
+
* round(-2.5) // Returns -2
|
|
64
|
+
* ```
|
|
65
|
+
*
|
|
66
|
+
* Edge cases:
|
|
67
|
+
* - NaN returns NaN
|
|
68
|
+
* - Infinity returns Infinity/-Infinity
|
|
69
|
+
* - Negative decimals round to left of decimal point
|
|
70
|
+
*/
|
|
71
|
+
export declare function round(value: number, decimals?: number): number;
|
|
72
|
+
/**
|
|
73
|
+
* Calculate Euclidean distance between two points in 2D space
|
|
74
|
+
*
|
|
75
|
+
* @param x1 - X coordinate of first point
|
|
76
|
+
* @param y1 - Y coordinate of first point
|
|
77
|
+
* @param x2 - X coordinate of second point
|
|
78
|
+
* @param y2 - Y coordinate of second point
|
|
79
|
+
* @returns Distance between the two points (always >= 0)
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```typescript
|
|
83
|
+
* distance(0, 0, 3, 4) // Returns 5 (Pythagorean theorem)
|
|
84
|
+
* distance(0, 0, 0, 0) // Returns 0 (same point)
|
|
85
|
+
* distance(1, 1, 4, 5) // Returns 5
|
|
86
|
+
* ```
|
|
87
|
+
*
|
|
88
|
+
* Edge cases:
|
|
89
|
+
* - NaN values return NaN
|
|
90
|
+
* - Infinity values may return Infinity
|
|
91
|
+
* - Always returns non-negative value
|
|
92
|
+
*/
|
|
93
|
+
export declare function distance(x1: number, y1: number, x2: number, y2: number): number;
|
|
94
|
+
/**
|
|
95
|
+
* Get unique values from an array
|
|
96
|
+
*
|
|
97
|
+
* @param array - Input array (may contain duplicates)
|
|
98
|
+
* @returns New array with only unique values (preserves first occurrence order)
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* ```typescript
|
|
102
|
+
* unique([1, 2, 2, 3, 1]) // Returns [1, 2, 3]
|
|
103
|
+
* unique(['a', 'b', 'a']) // Returns ['a', 'b']
|
|
104
|
+
* unique([]) // Returns []
|
|
105
|
+
* ```
|
|
106
|
+
*
|
|
107
|
+
* Edge cases:
|
|
108
|
+
* - Empty array returns empty array
|
|
109
|
+
* - Uses Set equality (NaN === NaN, +0 === -0)
|
|
110
|
+
* - Preserves object/array references
|
|
111
|
+
*/
|
|
112
|
+
export declare function unique<T>(array: T[]): T[];
|
|
113
|
+
/**
|
|
114
|
+
* Group array items by a key function
|
|
115
|
+
*
|
|
116
|
+
* @param array - Input array to group
|
|
117
|
+
* @param keyFn - Function that extracts grouping key from each item
|
|
118
|
+
* @returns Object with keys as groups and values as arrays of items
|
|
119
|
+
*
|
|
120
|
+
* @example
|
|
121
|
+
* ```typescript
|
|
122
|
+
* const items = [
|
|
123
|
+
* { type: 'fruit', name: 'apple' },
|
|
124
|
+
* { type: 'fruit', name: 'banana' },
|
|
125
|
+
* { type: 'vegetable', name: 'carrot' }
|
|
126
|
+
* ];
|
|
127
|
+
* groupBy(items, item => item.type)
|
|
128
|
+
* // Returns { fruit: [...], vegetable: [...] }
|
|
129
|
+
* ```
|
|
130
|
+
*
|
|
131
|
+
* Edge cases:
|
|
132
|
+
* - Empty array returns empty object
|
|
133
|
+
* - Handles undefined/null keys as string keys
|
|
134
|
+
*/
|
|
135
|
+
export declare function groupBy<T, K extends string | number>(array: T[], keyFn: (item: T) => K): Record<K, T[]>;
|
|
136
|
+
/**
|
|
137
|
+
* Sort array by property value
|
|
138
|
+
*
|
|
139
|
+
* @param array - Input array to sort
|
|
140
|
+
* @param property - Property key to sort by
|
|
141
|
+
* @param order - Sort order ('asc' or 'desc', default: 'asc')
|
|
142
|
+
* @returns New sorted array (does not mutate original)
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* ```typescript
|
|
146
|
+
* const items = [{ age: 30 }, { age: 20 }, { age: 25 }];
|
|
147
|
+
* sortByProperty(items, 'age') // Sorted by age ascending
|
|
148
|
+
* sortByProperty(items, 'age', 'desc') // Sorted by age descending
|
|
149
|
+
* ```
|
|
150
|
+
*
|
|
151
|
+
* Edge cases:
|
|
152
|
+
* - Returns shallow copy of array
|
|
153
|
+
* - Handles undefined properties (sorted to end)
|
|
154
|
+
* - Stable sort (preserves relative order of equal elements)
|
|
155
|
+
*/
|
|
156
|
+
export declare function sortByProperty<T>(array: T[], property: keyof T, order?: 'asc' | 'desc'): T[];
|
|
157
|
+
/**
|
|
158
|
+
* Filter array items, removing null and undefined values
|
|
159
|
+
*
|
|
160
|
+
* @param array - Input array with possibly null/undefined items
|
|
161
|
+
* @returns New array with null and undefined values removed
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* ```typescript
|
|
165
|
+
* filterNulls([1, null, 2, undefined, 3]) // Returns [1, 2, 3]
|
|
166
|
+
* filterNulls([null, undefined]) // Returns []
|
|
167
|
+
* filterNulls([0, false, '']) // Returns [0, false, ''] (keeps falsy values)
|
|
168
|
+
* ```
|
|
169
|
+
*
|
|
170
|
+
* Edge cases:
|
|
171
|
+
* - Keeps falsy values (0, false, empty string)
|
|
172
|
+
* - Type guard ensures return type doesn't include null|undefined
|
|
173
|
+
*/
|
|
174
|
+
export declare function filterNulls<T>(array: (T | null | undefined)[]): T[];
|
|
175
|
+
/**
|
|
176
|
+
* Validate a hexadecimal color string
|
|
177
|
+
*
|
|
178
|
+
* @param hex - Hex color string to validate
|
|
179
|
+
* @returns true if valid hex color, false otherwise
|
|
180
|
+
*
|
|
181
|
+
* @example
|
|
182
|
+
* ```typescript
|
|
183
|
+
* isValidHexColor('#FF0000') // Returns true
|
|
184
|
+
* isValidHexColor('#F00') // Returns true (shorthand)
|
|
185
|
+
* isValidHexColor('FF0000') // Returns false (missing #)
|
|
186
|
+
* isValidHexColor('#GGGGGG') // Returns false (invalid characters)
|
|
187
|
+
* isValidHexColor('') // Returns false
|
|
188
|
+
* ```
|
|
189
|
+
*
|
|
190
|
+
* Accepts:
|
|
191
|
+
* - Full format: #RRGGBB (e.g., #FF0000)
|
|
192
|
+
* - Shorthand format: #RGB (e.g., #F00)
|
|
193
|
+
* - Case insensitive (A-F or a-f)
|
|
194
|
+
*/
|
|
195
|
+
export declare function isValidHexColor(hex: string): boolean;
|
|
196
|
+
/**
|
|
197
|
+
* Validate RGB color values
|
|
198
|
+
*
|
|
199
|
+
* @param r - Red value
|
|
200
|
+
* @param g - Green value
|
|
201
|
+
* @param b - Blue value
|
|
202
|
+
* @returns true if all values are valid (0-255, finite, not NaN), false otherwise
|
|
203
|
+
*
|
|
204
|
+
* @example
|
|
205
|
+
* ```typescript
|
|
206
|
+
* isValidRGB(255, 0, 0) // Returns true
|
|
207
|
+
* isValidRGB(0, 128, 255) // Returns true
|
|
208
|
+
* isValidRGB(256, 0, 0) // Returns false (r > 255)
|
|
209
|
+
* isValidRGB(-1, 0, 0) // Returns false (r < 0)
|
|
210
|
+
* isValidRGB(NaN, 0, 0) // Returns false
|
|
211
|
+
* isValidRGB(Infinity, 0, 0) // Returns false
|
|
212
|
+
* ```
|
|
213
|
+
*
|
|
214
|
+
* Valid range: 0-255 (inclusive) for all channels
|
|
215
|
+
*/
|
|
216
|
+
export declare function isValidRGB(r: number, g: number, b: number): boolean;
|
|
217
|
+
/**
|
|
218
|
+
* Validate HSV color values
|
|
219
|
+
*
|
|
220
|
+
* @param h - Hue value (0-360)
|
|
221
|
+
* @param s - Saturation value (0-100)
|
|
222
|
+
* @param v - Value/brightness (0-100)
|
|
223
|
+
* @returns true if all values are valid (finite, not NaN, within ranges), false otherwise
|
|
224
|
+
*
|
|
225
|
+
* @example
|
|
226
|
+
* ```typescript
|
|
227
|
+
* isValidHSV(180, 50, 100) // Returns true
|
|
228
|
+
* isValidHSV(0, 0, 0) // Returns true
|
|
229
|
+
* isValidHSV(360, 100, 100) // Returns true (edge of range)
|
|
230
|
+
* isValidHSV(361, 50, 50) // Returns false (h > 360)
|
|
231
|
+
* isValidHSV(180, -1, 50) // Returns false (s < 0)
|
|
232
|
+
* isValidHSV(NaN, 50, 50) // Returns false
|
|
233
|
+
* ```
|
|
234
|
+
*
|
|
235
|
+
* Valid ranges:
|
|
236
|
+
* - Hue: 0-360 (degrees)
|
|
237
|
+
* - Saturation: 0-100 (percent)
|
|
238
|
+
* - Value: 0-100 (percent)
|
|
239
|
+
*/
|
|
240
|
+
export declare function isValidHSV(h: number, s: number, v: number): boolean;
|
|
241
|
+
/**
|
|
242
|
+
* Type guard: Check if a value is a string
|
|
243
|
+
*
|
|
244
|
+
* @param value - Value to check
|
|
245
|
+
* @returns true if value is a string, false otherwise
|
|
246
|
+
*
|
|
247
|
+
* @example
|
|
248
|
+
* ```typescript
|
|
249
|
+
* isString('hello') // Returns true
|
|
250
|
+
* isString('') // Returns true (empty string)
|
|
251
|
+
* isString(123) // Returns false
|
|
252
|
+
* isString(new String('')) // Returns false (String object, not primitive)
|
|
253
|
+
* ```
|
|
254
|
+
*/
|
|
255
|
+
export declare function isString(value: unknown): value is string;
|
|
256
|
+
/**
|
|
257
|
+
* Type guard: Check if a value is a finite number (excludes NaN and Infinity)
|
|
258
|
+
*
|
|
259
|
+
* @param value - Value to check
|
|
260
|
+
* @returns true if value is a number and finite (not NaN, not Infinity), false otherwise
|
|
261
|
+
*
|
|
262
|
+
* @example
|
|
263
|
+
* ```typescript
|
|
264
|
+
* isNumber(42) // Returns true
|
|
265
|
+
* isNumber(0) // Returns true
|
|
266
|
+
* isNumber(-3.14) // Returns true
|
|
267
|
+
* isNumber(NaN) // Returns false
|
|
268
|
+
* isNumber(Infinity) // Returns false
|
|
269
|
+
* isNumber('123') // Returns false (string)
|
|
270
|
+
* isNumber(new Number(5)) // Returns false (Number object)
|
|
271
|
+
* ```
|
|
272
|
+
*
|
|
273
|
+
* Note: This excludes NaN and Infinity for safer numeric operations
|
|
274
|
+
*/
|
|
275
|
+
export declare function isNumber(value: unknown): value is number;
|
|
276
|
+
/**
|
|
277
|
+
* Type guard: Check if a value is an array
|
|
278
|
+
*
|
|
279
|
+
* @param value - Value to check
|
|
280
|
+
* @returns true if value is an array, false otherwise
|
|
281
|
+
*
|
|
282
|
+
* @example
|
|
283
|
+
* ```typescript
|
|
284
|
+
* isArray([1, 2, 3]) // Returns true
|
|
285
|
+
* isArray([]) // Returns true
|
|
286
|
+
* isArray('not array') // Returns false
|
|
287
|
+
* isArray({ length: 0 }) // Returns false (array-like object)
|
|
288
|
+
* ```
|
|
289
|
+
*/
|
|
290
|
+
export declare function isArray<T = unknown>(value: unknown): value is T[];
|
|
291
|
+
/**
|
|
292
|
+
* Type guard: Check if a value is a plain object (not array, not null)
|
|
293
|
+
*
|
|
294
|
+
* @param value - Value to check
|
|
295
|
+
* @returns true if value is a plain object, false otherwise
|
|
296
|
+
*
|
|
297
|
+
* @example
|
|
298
|
+
* ```typescript
|
|
299
|
+
* isObject({ a: 1 }) // Returns true
|
|
300
|
+
* isObject({}) // Returns true
|
|
301
|
+
* isObject([]) // Returns false (array)
|
|
302
|
+
* isObject(null) // Returns false
|
|
303
|
+
* isObject(new Date()) // Returns true (object instance)
|
|
304
|
+
* ```
|
|
305
|
+
*/
|
|
306
|
+
export declare function isObject(value: unknown): value is Record<string, unknown>;
|
|
307
|
+
/**
|
|
308
|
+
* Type guard: Check if a value is null or undefined
|
|
309
|
+
*
|
|
310
|
+
* @param value - Value to check
|
|
311
|
+
* @returns true if value is null or undefined, false otherwise
|
|
312
|
+
*
|
|
313
|
+
* @example
|
|
314
|
+
* ```typescript
|
|
315
|
+
* isNullish(null) // Returns true
|
|
316
|
+
* isNullish(undefined) // Returns true
|
|
317
|
+
* isNullish(0) // Returns false
|
|
318
|
+
* isNullish('') // Returns false
|
|
319
|
+
* isNullish(false) // Returns false
|
|
320
|
+
* ```
|
|
321
|
+
*
|
|
322
|
+
* Note: More precise than falsy check - only null/undefined, not 0/false/''
|
|
323
|
+
*/
|
|
324
|
+
export declare function isNullish(value: unknown): value is null | undefined;
|
|
325
|
+
/**
|
|
326
|
+
* Sleep for a specified duration (async delay)
|
|
327
|
+
*
|
|
328
|
+
* @param ms - Milliseconds to sleep (must be non-negative)
|
|
329
|
+
* @returns Promise that resolves after the specified delay
|
|
330
|
+
*
|
|
331
|
+
* @example
|
|
332
|
+
* ```typescript
|
|
333
|
+
* await sleep(1000); // Wait 1 second
|
|
334
|
+
* await sleep(0); // Immediate next tick
|
|
335
|
+
*
|
|
336
|
+
* // Use with async/await
|
|
337
|
+
* async function delayed() {
|
|
338
|
+
* console.log('Start');
|
|
339
|
+
* await sleep(2000);
|
|
340
|
+
* console.log('After 2 seconds');
|
|
341
|
+
* }
|
|
342
|
+
* ```
|
|
343
|
+
*
|
|
344
|
+
* Note: Negative values are clamped to 0 (immediate resolution)
|
|
345
|
+
*/
|
|
346
|
+
export declare function sleep(ms: number): Promise<void>;
|
|
347
|
+
/**
|
|
348
|
+
* Check if an error is an AbortError (from AbortController timeout)
|
|
349
|
+
*
|
|
350
|
+
* @param error - Error to check
|
|
351
|
+
* @returns true if error is an AbortError, false otherwise
|
|
352
|
+
*
|
|
353
|
+
* @example
|
|
354
|
+
* ```typescript
|
|
355
|
+
* try {
|
|
356
|
+
* await fetch(url, { signal: controller.signal });
|
|
357
|
+
* } catch (error) {
|
|
358
|
+
* if (isAbortError(error)) {
|
|
359
|
+
* console.log('Request timed out or was aborted');
|
|
360
|
+
* }
|
|
361
|
+
* }
|
|
362
|
+
* ```
|
|
363
|
+
*/
|
|
364
|
+
export declare function isAbortError(error: unknown): boolean;
|
|
365
|
+
/**
|
|
366
|
+
* Retry a function multiple times with exponential backoff
|
|
367
|
+
*
|
|
368
|
+
* @param fn - Async function to retry
|
|
369
|
+
* @param maxAttempts - Maximum number of attempts (default: 3, min: 1)
|
|
370
|
+
* @param delayMs - Initial delay in milliseconds (default: 1000, doubles each retry)
|
|
371
|
+
* @returns Promise resolving to function result
|
|
372
|
+
* @throws Last error if all attempts fail
|
|
373
|
+
*
|
|
374
|
+
* @example
|
|
375
|
+
* ```typescript
|
|
376
|
+
* // Retry API call up to 3 times
|
|
377
|
+
* const data = await retry(
|
|
378
|
+
* () => fetch('https://api.example.com/data').then(r => r.json()),
|
|
379
|
+
* 3,
|
|
380
|
+
* 1000
|
|
381
|
+
* );
|
|
382
|
+
* // Delays: 0ms (try 1), 1000ms (try 2), 2000ms (try 3)
|
|
383
|
+
*
|
|
384
|
+
* // Custom retry logic
|
|
385
|
+
* const result = await retry(
|
|
386
|
+
* async () => {
|
|
387
|
+
* const response = await riskyOperation();
|
|
388
|
+
* if (!response.ok) throw new Error('Not OK');
|
|
389
|
+
* return response;
|
|
390
|
+
* },
|
|
391
|
+
* 5,
|
|
392
|
+
* 500
|
|
393
|
+
* );
|
|
394
|
+
* ```
|
|
395
|
+
*
|
|
396
|
+
* Backoff schedule:
|
|
397
|
+
* - Attempt 1: Immediate (no delay)
|
|
398
|
+
* - Attempt 2: delayMs * 2^0 = delayMs
|
|
399
|
+
* - Attempt 3: delayMs * 2^1 = delayMs * 2
|
|
400
|
+
* - Attempt 4: delayMs * 2^2 = delayMs * 4
|
|
401
|
+
* - etc.
|
|
402
|
+
*
|
|
403
|
+
* @remarks
|
|
404
|
+
* Retries on all errors including AbortError (timeout), allowing
|
|
405
|
+
* transient network issues to be recovered from.
|
|
406
|
+
*/
|
|
407
|
+
export declare function retry<T>(fn: () => Promise<T>, maxAttempts?: number, delayMs?: number): Promise<T>;
|
|
408
|
+
/**
|
|
409
|
+
* Generate a simple checksum for data integrity checking
|
|
410
|
+
*
|
|
411
|
+
* Uses a non-cryptographic hash function (djb2-like algorithm)
|
|
412
|
+
* Suitable for cache validation and detecting data corruption
|
|
413
|
+
*
|
|
414
|
+
* @param data - Any JSON-serializable data
|
|
415
|
+
* @returns Base-36 encoded hash string
|
|
416
|
+
* @throws Error if data contains circular references or cannot be stringified
|
|
417
|
+
*
|
|
418
|
+
* @example
|
|
419
|
+
* ```typescript
|
|
420
|
+
* const checksum1 = generateChecksum({ a: 1, b: 2 }); // "abc123"
|
|
421
|
+
* const checksum2 = generateChecksum({ a: 1, b: 2 }); // "abc123" (same)
|
|
422
|
+
* const checksum3 = generateChecksum({ a: 1, b: 3 }); // "xyz789" (different)
|
|
423
|
+
*
|
|
424
|
+
* // Use for cache validation
|
|
425
|
+
* const cachedData = { checksum: "abc123", data: {...} };
|
|
426
|
+
* const computedChecksum = generateChecksum(cachedData.data);
|
|
427
|
+
* if (computedChecksum !== cachedData.checksum) {
|
|
428
|
+
* console.warn('Cache corruption detected!');
|
|
429
|
+
* }
|
|
430
|
+
* ```
|
|
431
|
+
*
|
|
432
|
+
* Important notes:
|
|
433
|
+
* - NOT cryptographically secure (do not use for security)
|
|
434
|
+
* - Deterministic: same input always produces same output
|
|
435
|
+
* - Fast and lightweight
|
|
436
|
+
* - Collision-resistant for typical cache validation use cases
|
|
437
|
+
* - Throws on circular references (by JSON.stringify)
|
|
438
|
+
* - Per Issue #7: Uses |0 to properly convert to 32-bit integer
|
|
439
|
+
*/
|
|
440
|
+
export declare function generateChecksum(data: unknown): string;
|
|
441
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAkBH;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAKrE;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,IAAI,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAK5D;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,GAAE,MAAU,GAAG,MAAM,CASjE;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,QAAQ,CAAC,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,MAAM,CAO/E;AAMD;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,MAAM,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,GAAG,CAAC,EAAE,CAEzC;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,OAAO,CAAC,CAAC,EAAE,CAAC,SAAS,MAAM,GAAG,MAAM,EAClD,KAAK,EAAE,CAAC,EAAE,EACV,KAAK,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,GACpB,MAAM,CAA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