@salespark/toolkit 2.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.
@@ -0,0 +1,500 @@
1
+ /******************************************************
2
+ * ##: Unique by Key
3
+ * Returns unique items in an array based on a computed key
4
+ * @param {Array<T>} arr - The array to process
5
+ * @param {Function} key - Function to compute the key for uniqueness
6
+ * History:
7
+ * 21-08-2025: Created
8
+ ****************************************************/
9
+ declare function uniqBy<T, K>(arr: T[], key: (v: T) => K): T[];
10
+ /******************************************************
11
+ * ##: Array Chunk
12
+ * Splits an array into equally sized pieces
13
+ * @param {Array<T>} arr - The array to chunk
14
+ * @param {Number} size - Size of each chunk
15
+ * History:
16
+ * 21-08-2025: Created
17
+ ****************************************************/
18
+ declare function chunk<T>(arr: T[], size: number): T[][];
19
+ /******************************************************
20
+ * ##: Deep Array Equality
21
+ * Deeply compares two arrays for equality, ignoring element order and property order in nested objects
22
+ *
23
+ * Notes:
24
+ * - Uses JSON.stringify as the final canonical representation.
25
+ * - Will return false on non-serializable inputs (e.g., circular structures).
26
+ * - For primitives like NaN/Infinity, JSON.stringify follows JS semantics (NaN -> null).
27
+ * @param {Array<T>} arr1 - First array to compare
28
+ * @param {Array<T>} arr2 - Second array to compare
29
+ * History:
30
+ * 21-08-2025: Created
31
+ ****************************************************/
32
+ declare function areArraysEqual<T = unknown>(arr1: T[], arr2: T[]): boolean;
33
+ /**
34
+ * @deprecated Use `areArraysEqual` instead.
35
+ */
36
+ declare const areArraysDeepEqualUnordered: typeof areArraysEqual;
37
+ /******************************************************
38
+ * ##: Unique Values
39
+ * Removes duplicate values from an array
40
+ * @param {Array<T>} arr - The array to process
41
+ * History:
42
+ * 21-08-2025: Created
43
+ ****************************************************/
44
+ declare function uniq<T>(arr: T[]): T[];
45
+ /******************************************************
46
+ * ##: Flatten Array
47
+ * Flattens nested arrays into a single array (1 level deep)
48
+ * @param {Array<any>} arr - The array to flatten
49
+ * History:
50
+ * 21-08-2025: Created
51
+ ****************************************************/
52
+ declare function flatten<T>(arr: any[]): T[];
53
+ /******************************************************
54
+ * ##: Group By
55
+ * Groups array items by a key or function
56
+ * @param {Array<T>} arr - The array to group
57
+ * @param {Function|String} key - Key or function to group by
58
+ * History:
59
+ * 21-08-2025: Created
60
+ ****************************************************/
61
+ declare function groupBy<T, K extends keyof any>(arr: T[], key: ((item: T) => K) | K): Record<string, T[]>;
62
+ /******************************************************
63
+ * ##: Sort By
64
+ * Sorts array by a key or function
65
+ * @param {Array<T>} arr - The array to sort
66
+ * @param {Function|String} key - Key or function to sort by
67
+ * History:
68
+ * 21-08-2025: Created
69
+ ****************************************************/
70
+ declare function sortBy<T>(arr: T[], key: ((item: T) => any) | keyof T): T[];
71
+ /******************************************************
72
+ * ##: Array Difference
73
+ * Returns elements in arr1 not present in arr2
74
+ * @param {Array<T>} arr1 - First array
75
+ * @param {Array<T>} arr2 - Second array
76
+ * History:
77
+ * 21-08-2025: Created
78
+ ****************************************************/
79
+ declare function difference<T>(arr1: T[], arr2: T[]): T[];
80
+ /******************************************************
81
+ * ##: Array Intersection
82
+ * Returns elements common to both arrays
83
+ * @param {Array<T>} arr1 - First array
84
+ * @param {Array<T>} arr2 - Second array
85
+ * History:
86
+ * 21-08-2025: Created
87
+ ****************************************************/
88
+ declare function intersection<T>(arr1: T[], arr2: T[]): T[];
89
+ /******************************************************
90
+ * ##: Compact Array
91
+ * Removes falsy values from array
92
+ * @param {Array<T>} arr - The array to compact
93
+ * History:
94
+ * 21-08-2025: Created
95
+ ****************************************************/
96
+ declare function compact<T>(arr: T[]): T[];
97
+ /******************************************************
98
+ * ##: Pluck Property
99
+ * Extracts a property from each object in array
100
+ * @param {Array<T>} arr - The array of objects
101
+ * @param {String} key - Property name to extract
102
+ * History:
103
+ * 21-08-2025: Created
104
+ ****************************************************/
105
+ declare function pluck<T, K extends keyof T>(arr: T[], key: K): Array<T[K]>;
106
+ /******************************************************
107
+ * ##: Shuffle Array
108
+ * Shuffles array elements randomly
109
+ * @param {Array<T>} arr - The array to shuffle
110
+ * History:
111
+ * 21-08-2025: Created
112
+ ****************************************************/
113
+ declare function shuffle<T>(arr: T[]): T[];
114
+ /**
115
+ ****************************************************
116
+ * ##: Flattenable Value Checker
117
+ * Checks if a value can be flattened (array, arguments, or marked spreadable)
118
+ *
119
+ * Notes:
120
+ * Mirrors lodash's behavior: checks Array.isArray, arguments object, and Symbol.isConcatSpreadable.
121
+ * @param {unknown} value - Value to check for flattenability
122
+ * History:
123
+ * 21-08-2025: Created
124
+ ****************************************************/
125
+ declare function isFlattenable(value: unknown): value is readonly unknown[];
126
+ /**
127
+ ****************************************************
128
+ * ##: Array Push All
129
+ * Appends all values into array in-place (similar to lodash arrayPush)
130
+ * @param {Array<T>} array - Target array
131
+ * @param {Array<T>} values - Values to append
132
+ * History:
133
+ * 21-08-2025: Created
134
+ ****************************************************/
135
+ declare function pushAll<T>(array: T[], values: readonly T[]): T[];
136
+ /**
137
+ ****************************************************
138
+ * ##: Base Array Flatten
139
+ * Base flatten routine with configurable depth and predicate
140
+ *
141
+ * Notes:
142
+ * Allows control of depth, predicate, and strict mode. Used internally for flattening.
143
+ * @param {Array} array - Input array
144
+ * @param {Number} depth - Maximum recursion depth
145
+ * @param {Function} predicate - Function to determine if value is flattenable
146
+ * @param {Boolean} isStrict - If true, only flattenable values are kept
147
+ * @param {Array} result - Optional accumulator (internal)
148
+ * @returns {Array} New flattened array
149
+ * History:
150
+ * 21-08-2025: Created
151
+ ****************************************************/
152
+ declare function flattenDepthBase<T>(array: readonly unknown[], depth: number, predicate?: (v: unknown) => boolean, isStrict?: boolean, result?: T[]): T[];
153
+ /**
154
+ ****************************************************
155
+ * ##: Flatten Array Once
156
+ * Flattens array a single level deep (equivalent to lodash _.flatten)
157
+ *
158
+ * Notes:
159
+ * Example: flattenOnce([1, [2, [3, [4]], 5]]) => [1, 2, [3, [4]], 5]
160
+ * @param {Array} array - Array to flatten
161
+ * @returns {Array} Flattened array (1 level)
162
+ * History:
163
+ * 21-08-2025: Created
164
+ ****************************************************/
165
+ declare function flattenOnce<T>(array: ReadonlyArray<T | ReadonlyArray<T>>): T[];
166
+ /******************************************************
167
+ * ##: Flatten Array to Depth
168
+ * Flattens array up to the specified depth (friendly wrapper, default 1)
169
+ *
170
+ * Notes:
171
+ * Example: flattenDepth([1, [2, [3, [4]], 5]], 2) => [1, 2, 3, [4], 5]
172
+ * @param {Array} array - Array to flatten
173
+ * @param {Number} depth - Maximum depth
174
+ * @param {Object} options - Options: predicate, isStrict
175
+ * @returns {Array} Flattened array up to depth
176
+ * History:
177
+ * 21-08-2025: Created
178
+ ****************************************************/
179
+ declare function flattenDepth<T = unknown>(array: readonly unknown[], depth?: number, options?: {
180
+ predicate?: (v: unknown) => boolean;
181
+ isStrict?: boolean;
182
+ }): T[];
183
+
184
+ /**
185
+ ****************************************************
186
+ * ##: Object Property Picker
187
+ * Picks specified properties from an object and returns a new object
188
+ * @param {Object} obj - Source object
189
+ * @param {Array} keys - Array of keys to pick
190
+ * History:
191
+ * 21-08-2025: Created
192
+ ****************************************************/
193
+ declare function pick<T extends object, K extends keyof T>(obj: T, keys: K[]): Pick<T, K>;
194
+ /**
195
+ ****************************************************
196
+ * ##: Object Property Omitter
197
+ * Omits specified properties from an object and returns a new object
198
+ * @param {Object} obj - Source object
199
+ * @param {Array} keys - Array of keys to omit
200
+ * History:
201
+ * 21-08-2025: Created
202
+ ****************************************************/
203
+ declare function omit<T extends object, K extends keyof T>(obj: T, keys: K[]): Omit<T, K>;
204
+ /**
205
+ ****************************************************
206
+ * ##: Object to Clean String
207
+ * Converts an object or value to a clean string without JSON braces and quotes
208
+ *
209
+ * Notes:
210
+ * Examples: { a: 1, b: "x" } -> "a=1_b=x", null -> "", 42 -> "42"
211
+ * @param {unknown} obj - Object or value to convert
212
+ * History:
213
+ * 21-08-2025: Created
214
+ ****************************************************/
215
+ declare function objectToString(obj: unknown): string;
216
+ declare function cleanObject<T = unknown>(obj: T): any;
217
+
218
+ /******************************************************
219
+ * ##: Slugify String
220
+ * Converts a string to a URL-friendly slug (basic ASCII, keeps numbers and dashes)
221
+ * @param {String} input - String to slugify
222
+ * History:
223
+ * 21-08-2025: Created
224
+ ****************************************************/
225
+ declare function slugify(input: string): string;
226
+ /******************************************************
227
+ * ##: Simple String Template Filler
228
+ * Fills a template string with values (e.g., "Hello, {name}!")
229
+ * @param {String} template - Template string
230
+ * @param {Object} values - Key-value pairs to fill
231
+ * History:
232
+ * 21-08-2025: Created
233
+ ****************************************************/
234
+ declare function fill(template: string, values: Record<string, string | number>): string;
235
+ /******************************************************
236
+ * ##: Remove Diacritics
237
+ * Removes diacritic marks from a string using NFD normalization
238
+ *
239
+ * Notes:
240
+ * Examples: "ação" -> "acao", "coração" -> "coracao", "résumé" -> "resume", "naïve" -> "naive"
241
+ * @param {String} str - String to deburr
242
+ * History:
243
+ * 21-08-2025: Created
244
+ ****************************************************/
245
+ declare function deburr(str: string): string;
246
+ /**
247
+ * @deprecated Use `deburr` instead.
248
+ */
249
+ declare const removeDiacritics: typeof deburr;
250
+ /******************************************************
251
+ * ##: Basic String Sanitizer
252
+ * Sanitizes input by removing control chars, HTML/script/style/iframe blocks, dangerous URL schemes, and keeps letters/numbers/spaces/punctuation
253
+ *
254
+ * Notes:
255
+ * Non-string inputs return "". This is a light, generic sanitizer (not a full HTML sanitizer).
256
+ * @param {unknown} input - Input to sanitize
257
+ * @param {Number} maxLength - Optional max length for output
258
+ * History:
259
+ * 21-08-2025: Created
260
+ ****************************************************/
261
+ declare function sanitize(input: unknown, maxLength?: number): string;
262
+ /**
263
+ * @deprecated Use `sanitize` instead.
264
+ */
265
+ declare const basicSanitize: typeof sanitize;
266
+
267
+ /******************************************************
268
+ * ##: Clamp Number
269
+ * Restricts a number to be within the min and max bounds
270
+ * @param {Number} n - Number to clamp
271
+ * @param {Number} min - Minimum value
272
+ * @param {Number} max - Maximum value
273
+ * History:
274
+ * 21-08-2025: Created
275
+ ****************************************************/
276
+ declare const clamp: (n: number, min: number, max: number) => number;
277
+ /******************************************************
278
+ * ##: Fixed Decimal Rounding
279
+ * Rounds a number to a fixed number of decimals without floating point surprises
280
+ * @param {Number} n - Number to round
281
+ * @param {Number} decimals - Number of decimal places
282
+ * History:
283
+ * 21-08-2025: Created
284
+ ****************************************************/
285
+ declare function round(n: number, decimals?: number): number;
286
+ /******************************************************
287
+ * ##: Safe Integer Conversion
288
+ * Safely converts a value to an integer. Returns defaultValue if parsing fails or results in NaN.
289
+ *
290
+ * Notes:
291
+ * Examples: toInteger("42") -> 42, toInteger("abc", 10) -> 10, toInteger(undefined) -> 0, toInteger(3.9) -> 3
292
+ * @param {unknown} value - Value to convert
293
+ * @param {Number} defaultValue - Default value if parsing fails
294
+ * History:
295
+ * 21-08-2025: Created
296
+ ****************************************************/
297
+ declare function toInteger(value: unknown, defaultValue?: number): number;
298
+ /******************************************************
299
+ * ##: Safe Parse Int (Alias)
300
+ * Alias for safe integer conversion (for discoverability/backward naming)
301
+ * @param {unknown} value - Value to convert
302
+ * @param {Number} defaultValue - Default value if parsing fails
303
+ * History:
304
+ * 21-08-2025: Created
305
+ ****************************************************/
306
+ declare const safeParseInt: typeof toInteger;
307
+ /******************************************************
308
+ * ##: Safe Number Conversion
309
+ * Safely parses a value into a number with optional decimal precision
310
+ *
311
+ * Notes:
312
+ * Handles commas as decimal/thousands separators. Returns 0 for null/undefined/empty string or invalid parsing.
313
+ * Examples: toNumber("123.45") -> 123.45, toNumber("123,45") -> 123.45, toNumber("1,234.56") -> 1234.56, toNumber("abc", 2) -> 0, toNumber(42) -> 42
314
+ * @param {unknown} value - Value to convert
315
+ * @param {Number} decimals - Number of decimal places
316
+ * History:
317
+ * 21-08-2025: Created
318
+ ****************************************************/
319
+ declare function toNumber(value: unknown, decimals?: number): number;
320
+ /**
321
+ * @deprecated Use `toNumber` instead.
322
+ */
323
+ declare const parseToNumber: typeof toNumber;
324
+ /******************************************************
325
+ * ##: Random Digits Generator
326
+ * Generates a random string of digits with secure randomness when available
327
+ *
328
+ * Notes:
329
+ * Options: length (default 6), charset (default "0123456789"), noLeadingZero (if true, first char not "0"). Returns a string to preserve leading zeros. Uses Web Crypto when possible; otherwise falls back to Math.random().
330
+ * @param {Number} length - Number of digits
331
+ * @param {Object} options - Options: charset, noLeadingZero
332
+ * History:
333
+ * 21-08-2025: Created
334
+ ****************************************************/
335
+ declare function randomDigits(length?: number, options?: {
336
+ charset?: string;
337
+ noLeadingZero?: boolean;
338
+ }): string;
339
+ /**
340
+ * @deprecated Use `randomDigits` instead.
341
+ */
342
+ declare const otp: typeof randomDigits;
343
+
344
+ /******************************************************
345
+ * ##: Debounce Function
346
+ * Returns a debounced version of a function that delays execution until after wait ms
347
+ * @param {Function} fn - Function to debounce
348
+ * @param {Number} wait - Delay in milliseconds
349
+ * History:
350
+ * 21-08-2025: Created
351
+ ****************************************************/
352
+ declare function debounce<T extends (...args: any[]) => any>(fn: T, wait?: number): T;
353
+ /******************************************************
354
+ * ##: Throttle Function
355
+ * Returns a throttled version of a function that only executes once per wait ms
356
+ * @param {Function} fn - Function to throttle
357
+ * @param {Number} wait - Delay in milliseconds
358
+ * History:
359
+ * 21-08-2025: Created
360
+ ****************************************************/
361
+ declare function throttle<T extends (...args: any[]) => any>(fn: T, wait?: number): T;
362
+ /******************************************************
363
+ * ##: Nil Value Check
364
+ * Checks if a value is null or undefined (Type Guard)
365
+ * @param {unknown} value - Value to check
366
+ * History:
367
+ * 21-08-2025: Created
368
+ ****************************************************/
369
+ declare const isNil: (value: unknown) => value is null | undefined;
370
+ /******************************************************
371
+ * ##: Nil or Nil Text Check
372
+ * Checks if value is null/undefined or the text "null"/"undefined"
373
+ * @param {unknown} value - Value to check
374
+ * History:
375
+ * 21-08-2025: Created
376
+ ****************************************************/
377
+ declare const isNilText: (value: unknown) => boolean;
378
+ /******************************************************
379
+ * ##: Nil or Empty String Check
380
+ * Checks if value is null/undefined or an empty string ""
381
+ * @param {unknown} value - Value to check
382
+ * History:
383
+ * 21-08-2025: Created
384
+ ****************************************************/
385
+ declare const isNilOrEmpty: (value: unknown) => boolean;
386
+ /******************************************************
387
+ * ##: Array Nil or Empty Element Check
388
+ * Checks if any element in array is nil or empty (""). Returns true if input is not an array.
389
+ * @param {unknown} array - Array to check
390
+ * History:
391
+ * 21-08-2025: Created
392
+ ****************************************************/
393
+ declare const hasNilOrEmpty: (array: unknown) => boolean;
394
+ /******************************************************
395
+ * ##: Nil, Empty, or Zero Length Check
396
+ * Checks if value is nil, empty string, or has zero length (applies to arrays, strings, typed arrays, buffers)
397
+ * @param {unknown} value - Value to check
398
+ * History:
399
+ * 21-08-2025: Created
400
+ ****************************************************/
401
+ declare const isNilEmptyOrZeroLen: (value: unknown) => boolean;
402
+ /******************************************************
403
+ * ##: Nil or Zero Length Check
404
+ * Checks if value is nil or has zero length (.length === 0). Does NOT treat "" specially.
405
+ * @param {unknown} value - Value to check
406
+ * History:
407
+ * 21-08-2025: Created
408
+ ****************************************************/
409
+ declare const isNilOrZeroLen: (value: unknown) => boolean;
410
+ /******************************************************
411
+ * ##: Nil or NaN Check
412
+ * Checks if value is nil OR NaN (coercive, matches global isNaN)
413
+ *
414
+ * Notes:
415
+ * Uses global isNaN for parity with JS behavior (coerces strings). Example: isNaN("foo") === true.
416
+ * @param {unknown} value - Value to check
417
+ * History:
418
+ * 21-08-2025: Created
419
+ ****************************************************/
420
+ declare const isNilOrNaN: (value: unknown) => boolean;
421
+ /**
422
+ * @deprecated Use `isNil` instead.
423
+ */
424
+ declare const isNullOrUndefined: (value: unknown) => value is null | undefined;
425
+ /**
426
+ * @deprecated Use `isNilText` instead.
427
+ */
428
+ declare const isNullOrUndefinedTextInc: (value: unknown) => boolean;
429
+ /**
430
+ * @deprecated Use `isNilOrEmpty` instead.
431
+ */
432
+ declare const isNullUndefinedOrEmpty: (value: unknown) => boolean;
433
+ /**
434
+ * @deprecated Use `hasNilOrEmpty` instead.
435
+ */
436
+ declare const isNullOrUndefinedInArray: (array: unknown) => boolean;
437
+ /**
438
+ * @deprecated Use `isNilEmptyOrZeroLen` instead.
439
+ */
440
+ declare const isNullOrUndefinedEmptyOrZero: (value: unknown) => boolean;
441
+ /**
442
+ * @deprecated Use `isNilOrZeroLen` instead.
443
+ */
444
+ declare const isNullUndefinedOrZero: (value: unknown) => boolean;
445
+ /**
446
+ * @deprecated Use `isNilOrNaN` instead.
447
+ */
448
+ declare const isNullOrUndefinedOrNaN: (value: unknown) => boolean;
449
+ /******************************************************
450
+ * ##: Human-Readable Byte Formatter
451
+ * Formats bytes as human-readable text (e.g., kB, MB, KiB, MiB)
452
+ *
453
+ * Notes:
454
+ * SI (kB, MB, ...) uses 1000; IEC (KiB, MiB, ...) uses 1024. Negative values supported.
455
+ * @param {Number} bytes - Number of bytes to format
456
+ * @param {Boolean} si - True for metric (SI, base 1000), false for binary (IEC, base 1024)
457
+ * @param {Number} dp - Decimal places
458
+ * History:
459
+ * 21-08-2025: Created
460
+ ****************************************************/
461
+ declare const formatBytes: (bytes: number, si?: boolean, dp?: number) => string;
462
+ /**
463
+ * @deprecated Use `formatBytes` instead.
464
+ */
465
+ declare const humanFileSize: (bytes: number, si?: boolean, dp?: number) => string;
466
+ /******************************************************
467
+ * ##: String Similarity
468
+ * Returns the similarity between two strings (0..1)
469
+ *
470
+ * Notes:
471
+ * Similarity = (|longer|-levenshtein(longer, shorter)) / |longer| ∈ [0, 1]. Safe for empty strings; if both empty => 1.0
472
+ * @param {String} s1 - First string
473
+ * @param {String} s2 - Second string
474
+ * History:
475
+ * 21-08-2025: Created
476
+ ****************************************************/
477
+ declare const stringSimilarity: (s1: string, s2: string) => number;
478
+ /**
479
+ * @deprecated Use `stringSimilarity` instead.
480
+ */
481
+ declare const getStringSimilarity: (s1: string, s2: string) => number;
482
+ /******************************************************
483
+ * ##: Thousand Separator Formatter
484
+ * Adds spaces between thousands in a number (e.g., 1234567 -> "1 234 567")
485
+ * @param {Number|String} value - Number or string to format
486
+ * @returns {String} Formatted string with spaces as thousand separators
487
+ * History:
488
+ * 21-08-2025: Created
489
+ ****************************************************/
490
+ declare const addThousandsSpace: (value: number | string) => string;
491
+ /**
492
+ * @deprecated Use `addThousandsSpace` instead.
493
+ */
494
+ declare const addSpaceBetweenNumbers: (value: number | string) => string;
495
+
496
+ /** Environment helpers (runtime-agnostic checks) */
497
+ declare const isBrowser: boolean;
498
+ declare const isNode: boolean;
499
+
500
+ export { addSpaceBetweenNumbers, addThousandsSpace, areArraysDeepEqualUnordered, areArraysEqual, basicSanitize, chunk, clamp, cleanObject, compact, debounce, deburr, difference, fill, flatten, flattenDepth, flattenDepthBase, flattenOnce, formatBytes, getStringSimilarity, groupBy, hasNilOrEmpty, humanFileSize, intersection, isBrowser, isFlattenable, isNil, isNilEmptyOrZeroLen, isNilOrEmpty, isNilOrNaN, isNilOrZeroLen, isNilText, isNode, isNullOrUndefined, isNullOrUndefinedEmptyOrZero, isNullOrUndefinedInArray, isNullOrUndefinedOrNaN, isNullOrUndefinedTextInc, isNullUndefinedOrEmpty, isNullUndefinedOrZero, objectToString, omit, otp, parseToNumber, pick, pluck, pushAll, randomDigits, removeDiacritics, round, safeParseInt, sanitize, shuffle, slugify, sortBy, stringSimilarity, throttle, toInteger, toNumber, uniq, uniqBy };