nhb-toolbox 2.1.2 → 2.2.2
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/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -2
- package/dist/string/convert.d.ts +13 -0
- package/dist/string/convert.d.ts.map +1 -1
- package/dist/string/convert.js +25 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/utils/index.d.ts +31 -0
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +52 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export { capitalizeString, generateRandomID, trimString, truncateString, } from './string/basics';
|
|
2
2
|
export { generateAnagrams } from './string/anagram';
|
|
3
|
-
export { convertStringCase } from './string/convert';
|
|
3
|
+
export { convertStringCase, replaceAllInString } from './string/convert';
|
|
4
4
|
export { convertToDecimal, getRandomNumber } from './number/basics';
|
|
5
5
|
export { numberToWords } from './number/convert';
|
|
6
6
|
export { findPrimeNumbers, isPrime } from './number/prime';
|
|
@@ -18,5 +18,5 @@ export { cloneObject, countObjectFields, generateQueryParams, isEmptyObject, isO
|
|
|
18
18
|
export { extractNewFields, extractUpdatedAndNewFields, extractUpdatedFields, flattenObjectDotNotation, flattenObjectKeyValue, mergeAndFlattenObjects, mergeObjects, } from './object/objectify';
|
|
19
19
|
export { sanitizeData } from './object/sanitize';
|
|
20
20
|
export { convertObjectValues } from './object/convert';
|
|
21
|
-
export { convertArrayToString, isDeepEqual } from './utils';
|
|
21
|
+
export { convertArrayToString, debounceAction, isDeepEqual, throttleAction, } from './utils';
|
|
22
22
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,gBAAgB,EAChB,gBAAgB,EAChB,UAAU,EACV,cAAc,GACd,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAEpD,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,gBAAgB,EAChB,gBAAgB,EAChB,UAAU,EACV,cAAc,GACd,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAEpD,OAAO,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AAEzE,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAEpE,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAEjD,OAAO,EAAE,gBAAgB,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAE3D,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAEnD,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAEvD,OAAO,EACN,2BAA2B,EAC3B,sBAAsB,GACtB,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EACN,gBAAgB,EAChB,iBAAiB,EACjB,iBAAiB,EACjB,eAAe,EACf,eAAe,EACf,iBAAiB,EACjB,iBAAiB,EACjB,eAAe,EACf,eAAe,EACf,iBAAiB,EACjB,iBAAiB,EACjB,eAAe,EACf,eAAe,EACf,gBAAgB,GAChB,MAAM,kBAAkB,CAAC;AAE1B,OAAO,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AAEvC,OAAO,EACN,oBAAoB,EACpB,YAAY,EACZ,iBAAiB,EACjB,YAAY,GACZ,MAAM,gBAAgB,CAAC;AAExB,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAE3C,OAAO,EACN,kBAAkB,EAClB,yBAAyB,GACzB,MAAM,mBAAmB,CAAC;AAE3B,OAAO,EAAE,mBAAmB,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAEtE,OAAO,EAAE,wBAAwB,EAAE,MAAM,kBAAkB,CAAC;AAE5D,OAAO,EACN,WAAW,EACX,iBAAiB,EACjB,mBAAmB,EACnB,aAAa,EACb,QAAQ,GACR,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EACN,gBAAgB,EAChB,0BAA0B,EAC1B,oBAAoB,EACpB,wBAAwB,EACxB,qBAAqB,EACrB,sBAAsB,EACtB,YAAY,GACZ,MAAM,oBAAoB,CAAC;AAE5B,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAEjD,OAAO,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AAEvD,OAAO,EACN,oBAAoB,EACpB,cAAc,EACd,WAAW,EACX,cAAc,GACd,MAAM,SAAS,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
exports.isDeepEqual = exports.convertArrayToString = exports.convertObjectValues = exports.sanitizeData = exports.mergeObjects = exports.mergeAndFlattenObjects = void 0;
|
|
3
|
+
exports.flattenObjectDotNotation = exports.extractUpdatedFields = exports.extractUpdatedAndNewFields = exports.extractNewFields = exports.isObject = exports.isEmptyObject = exports.generateQueryParams = exports.countObjectFields = exports.cloneObject = exports.createControlledFormData = exports.isEmptyFormData = exports.convertIntoFormData = exports.removeDuplicatesFromArray = exports.createOptionsArray = exports.sortAnArray = exports.shuffleArray = exports.isValidEmptyArray = exports.flattenArray = exports.filterArrayOfObjects = exports.Color = exports.convertRgbToRgba = exports.convertRgbToHsl = exports.convertRgbToHex = exports.convertRgbaToHsla = exports.convertRgbaToHex8 = exports.convertHslToRgb = exports.convertHslToHex = exports.convertHslaToRgba = exports.convertHslaToHex8 = exports.convertHexToRgb = exports.convertHexToHsl = exports.convertHex8ToRgba = exports.convertHex8ToHsla = exports.convertColorCode = exports.generateRandomHSLColor = exports.generateRandomColorInHexRGB = exports.getColorForInitial = exports.getNumbersInRange = exports.isPrime = exports.findPrimeNumbers = exports.numberToWords = exports.getRandomNumber = exports.convertToDecimal = exports.replaceAllInString = exports.convertStringCase = exports.generateAnagrams = exports.truncateString = exports.trimString = exports.generateRandomID = exports.capitalizeString = void 0;
|
|
4
|
+
exports.throttleAction = exports.isDeepEqual = exports.debounceAction = exports.convertArrayToString = exports.convertObjectValues = exports.sanitizeData = exports.mergeObjects = exports.mergeAndFlattenObjects = exports.flattenObjectKeyValue = void 0;
|
|
5
5
|
var basics_1 = require("./string/basics");
|
|
6
6
|
Object.defineProperty(exports, "capitalizeString", { enumerable: true, get: function () { return basics_1.capitalizeString; } });
|
|
7
7
|
Object.defineProperty(exports, "generateRandomID", { enumerable: true, get: function () { return basics_1.generateRandomID; } });
|
|
@@ -11,6 +11,7 @@ var anagram_1 = require("./string/anagram");
|
|
|
11
11
|
Object.defineProperty(exports, "generateAnagrams", { enumerable: true, get: function () { return anagram_1.generateAnagrams; } });
|
|
12
12
|
var convert_1 = require("./string/convert");
|
|
13
13
|
Object.defineProperty(exports, "convertStringCase", { enumerable: true, get: function () { return convert_1.convertStringCase; } });
|
|
14
|
+
Object.defineProperty(exports, "replaceAllInString", { enumerable: true, get: function () { return convert_1.replaceAllInString; } });
|
|
14
15
|
var basics_2 = require("./number/basics");
|
|
15
16
|
Object.defineProperty(exports, "convertToDecimal", { enumerable: true, get: function () { return basics_2.convertToDecimal; } });
|
|
16
17
|
Object.defineProperty(exports, "getRandomNumber", { enumerable: true, get: function () { return basics_2.getRandomNumber; } });
|
|
@@ -78,4 +79,6 @@ var convert_5 = require("./object/convert");
|
|
|
78
79
|
Object.defineProperty(exports, "convertObjectValues", { enumerable: true, get: function () { return convert_5.convertObjectValues; } });
|
|
79
80
|
var utils_1 = require("./utils");
|
|
80
81
|
Object.defineProperty(exports, "convertArrayToString", { enumerable: true, get: function () { return utils_1.convertArrayToString; } });
|
|
82
|
+
Object.defineProperty(exports, "debounceAction", { enumerable: true, get: function () { return utils_1.debounceAction; } });
|
|
81
83
|
Object.defineProperty(exports, "isDeepEqual", { enumerable: true, get: function () { return utils_1.isDeepEqual; } });
|
|
84
|
+
Object.defineProperty(exports, "throttleAction", { enumerable: true, get: function () { return utils_1.throttleAction; } });
|
package/dist/string/convert.d.ts
CHANGED
|
@@ -6,4 +6,17 @@
|
|
|
6
6
|
* @returns The formatted string in the specified case format.
|
|
7
7
|
*/
|
|
8
8
|
export declare function convertStringCase(string: string, format: 'camelCase' | 'snake_case' | 'kebab-case'): string;
|
|
9
|
+
/**
|
|
10
|
+
* Replaces all occurrences of a string or pattern in the given input string.
|
|
11
|
+
*
|
|
12
|
+
* - If `find` is a string, it is converted into a global regular expression (`/find/g`).
|
|
13
|
+
* - If `find` is a `RegExp`, the global (`g`) flag is ensured.
|
|
14
|
+
* - Trims the input before performing replacements.
|
|
15
|
+
*
|
|
16
|
+
* @param input - The string in which replacements should be performed.
|
|
17
|
+
* @param find - The substring or regex pattern to search for.
|
|
18
|
+
* @param replace - The string to replace matches with.
|
|
19
|
+
* @returns The modified/refined string with replacements applied.
|
|
20
|
+
*/
|
|
21
|
+
export declare const replaceAllInString: (input: string, find: string | RegExp, replace: string) => string;
|
|
9
22
|
//# sourceMappingURL=convert.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"convert.d.ts","sourceRoot":"","sources":["../../src/string/convert.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,wBAAgB,iBAAiB,CAChC,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,WAAW,GAAG,YAAY,GAAG,YAAY,GAC/C,MAAM,CA8BR"}
|
|
1
|
+
{"version":3,"file":"convert.d.ts","sourceRoot":"","sources":["../../src/string/convert.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,wBAAgB,iBAAiB,CAChC,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,WAAW,GAAG,YAAY,GAAG,YAAY,GAC/C,MAAM,CA8BR;AAED;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,kBAAkB,UACvB,MAAM,QACP,MAAM,GAAG,MAAM,WACZ,MAAM,KACb,MAgBF,CAAC"}
|
package/dist/string/convert.js
CHANGED
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
* @returns The formatted string in the specified case format.
|
|
8
8
|
*/
|
|
9
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.replaceAllInString = void 0;
|
|
10
11
|
exports.convertStringCase = convertStringCase;
|
|
11
12
|
function convertStringCase(string, format) {
|
|
12
13
|
if (!string || typeof string !== 'string')
|
|
@@ -26,3 +27,27 @@ function convertStringCase(string, format) {
|
|
|
26
27
|
return formattedString;
|
|
27
28
|
}
|
|
28
29
|
}
|
|
30
|
+
/**
|
|
31
|
+
* Replaces all occurrences of a string or pattern in the given input string.
|
|
32
|
+
*
|
|
33
|
+
* - If `find` is a string, it is converted into a global regular expression (`/find/g`).
|
|
34
|
+
* - If `find` is a `RegExp`, the global (`g`) flag is ensured.
|
|
35
|
+
* - Trims the input before performing replacements.
|
|
36
|
+
*
|
|
37
|
+
* @param input - The string in which replacements should be performed.
|
|
38
|
+
* @param find - The substring or regex pattern to search for.
|
|
39
|
+
* @param replace - The string to replace matches with.
|
|
40
|
+
* @returns The modified/refined string with replacements applied.
|
|
41
|
+
*/
|
|
42
|
+
const replaceAllInString = (input, find, replace) => {
|
|
43
|
+
if (!input)
|
|
44
|
+
return '';
|
|
45
|
+
const trimmedString = input?.trim();
|
|
46
|
+
if (!trimmedString)
|
|
47
|
+
return '';
|
|
48
|
+
const regex = typeof find === 'string' ?
|
|
49
|
+
new RegExp(find, 'g')
|
|
50
|
+
: new RegExp(find, find.flags.includes('g') ? find.flags : find.flags + 'g');
|
|
51
|
+
return trimmedString?.replace(regex, replace);
|
|
52
|
+
};
|
|
53
|
+
exports.replaceAllInString = replaceAllInString;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -8,5 +8,11 @@ type Brand<B> = {
|
|
|
8
8
|
export type Branded<T, B> = T & Brand<B>;
|
|
9
9
|
/** Union of Primitive Types */
|
|
10
10
|
export type Primitive = string | number | boolean | null | undefined;
|
|
11
|
+
/** Generic function type */
|
|
12
|
+
export type GenericFn = (...args: any[]) => void;
|
|
13
|
+
/** Debounced function type after certain delay */
|
|
14
|
+
export type DelayedFn<T extends GenericFn> = (...args: Parameters<T>) => void;
|
|
15
|
+
/** Throttled function type after specific delay */
|
|
16
|
+
export type ThrottledFn<T extends GenericFn> = (...args: Parameters<T>) => void;
|
|
11
17
|
export {};
|
|
12
18
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAAA,6CAA6C;AAC7C,MAAM,MAAM,eAAe,GAAG,GAAG,CAAC;AAElC,OAAO,CAAC,MAAM,OAAO,EAAE,OAAO,MAAM,CAAC;AACrC,KAAK,KAAK,CAAC,CAAC,IAAI;IAAE,CAAC,OAAO,CAAC,EAAE,CAAC,CAAA;CAAE,CAAC;AAEjC,6BAA6B;AAC7B,MAAM,MAAM,OAAO,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;AAEzC,+BAA+B;AAC/B,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,GAAG,SAAS,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAAA,6CAA6C;AAC7C,MAAM,MAAM,eAAe,GAAG,GAAG,CAAC;AAElC,OAAO,CAAC,MAAM,OAAO,EAAE,OAAO,MAAM,CAAC;AACrC,KAAK,KAAK,CAAC,CAAC,IAAI;IAAE,CAAC,OAAO,CAAC,EAAE,CAAC,CAAA;CAAE,CAAC;AAEjC,6BAA6B;AAC7B,MAAM,MAAM,OAAO,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;AAEzC,+BAA+B;AAC/B,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,GAAG,SAAS,CAAC;AAErE,4BAA4B;AAC5B,MAAM,MAAM,SAAS,GAAG,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAC;AAEjD,kDAAkD;AAClD,MAAM,MAAM,SAAS,CAAC,CAAC,SAAS,SAAS,IAAI,CAAC,GAAG,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC;AAE9E,mDAAmD;AACnD,MAAM,MAAM,WAAW,CAAC,CAAC,SAAS,SAAS,IAAI,CAAC,GAAG,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC"}
|
package/dist/utils/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { DelayedFn, GenericFn, ThrottledFn } from '../types';
|
|
1
2
|
/**
|
|
2
3
|
* * Deeply compare two values (arrays, objects, or primitive values).
|
|
3
4
|
*
|
|
@@ -14,4 +15,34 @@ export declare const isDeepEqual: <T>(a: T, b: T) => boolean;
|
|
|
14
15
|
* @returns Converted array in string format with the separator.
|
|
15
16
|
*/
|
|
16
17
|
export declare const convertArrayToString: <T>(array: T[], separator?: string) => string;
|
|
18
|
+
/**
|
|
19
|
+
* * A generic debounce function that delays the execution of a callback.
|
|
20
|
+
*
|
|
21
|
+
* @param callback - The function to debounce.
|
|
22
|
+
* @param delay - The delay in milliseconds. Default is `300ms`.
|
|
23
|
+
* @returns A debounced version of the callback function.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* const debouncedSearch = debounceAction((query: string) => {
|
|
27
|
+
* console.log(`Searching for: ${query}`);
|
|
28
|
+
* }, 300);
|
|
29
|
+
*
|
|
30
|
+
* debouncedSearch('laptop'); // Executes after 300ms of inactivity.
|
|
31
|
+
*/
|
|
32
|
+
export declare const debounceAction: <T extends GenericFn>(callback: T, delay?: number) => DelayedFn<T>;
|
|
33
|
+
/**
|
|
34
|
+
* * A generic throttle function that ensures a callback is executed at most once per specified interval.
|
|
35
|
+
*
|
|
36
|
+
* @param callback - The function to throttle.
|
|
37
|
+
* @param delay - The delay in milliseconds. Default is `150ms`.
|
|
38
|
+
* @returns A throttled version of the callback function.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* const throttledResize = throttleAction(() => {
|
|
42
|
+
* console.log('Resized');
|
|
43
|
+
* }, 300);
|
|
44
|
+
*
|
|
45
|
+
* window.addEventListener('resize', throttledResize);
|
|
46
|
+
*/
|
|
47
|
+
export declare const throttleAction: <T extends GenericFn>(callback: T, delay?: number) => ThrottledFn<T>;
|
|
17
48
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,SAAS,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAElE;;;;;;GAMG;AACH,eAAO,MAAM,WAAW,GAAI,CAAC,KAAK,CAAC,KAAK,CAAC,KAAG,OA6B3C,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,oBAAoB,GAAI,CAAC,SAC9B,CAAC,EAAE,cACC,MAAM,KACf,MAKF,CAAC;AAEF;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,cAAc,GAAI,CAAC,SAAS,SAAS,YACvC,CAAC,qBAET,SAAS,CAAC,CAAC,CAYb,CAAC;AAEF;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,cAAc,GAAI,CAAC,SAAS,SAAS,YACvC,CAAC,qBAET,WAAW,CAAC,CAAC,CAWf,CAAC"}
|
package/dist/utils/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.convertArrayToString = exports.isDeepEqual = void 0;
|
|
3
|
+
exports.throttleAction = exports.debounceAction = exports.convertArrayToString = exports.isDeepEqual = void 0;
|
|
4
4
|
const basics_1 = require("../array/basics");
|
|
5
5
|
/**
|
|
6
6
|
* * Deeply compare two values (arrays, objects, or primitive values).
|
|
@@ -50,3 +50,54 @@ const convertArrayToString = (array, separator = ',') => {
|
|
|
50
50
|
return array.join(separator);
|
|
51
51
|
};
|
|
52
52
|
exports.convertArrayToString = convertArrayToString;
|
|
53
|
+
/**
|
|
54
|
+
* * A generic debounce function that delays the execution of a callback.
|
|
55
|
+
*
|
|
56
|
+
* @param callback - The function to debounce.
|
|
57
|
+
* @param delay - The delay in milliseconds. Default is `300ms`.
|
|
58
|
+
* @returns A debounced version of the callback function.
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* const debouncedSearch = debounceAction((query: string) => {
|
|
62
|
+
* console.log(`Searching for: ${query}`);
|
|
63
|
+
* }, 300);
|
|
64
|
+
*
|
|
65
|
+
* debouncedSearch('laptop'); // Executes after 300ms of inactivity.
|
|
66
|
+
*/
|
|
67
|
+
const debounceAction = (callback, delay = 300) => {
|
|
68
|
+
let timeoutId = undefined;
|
|
69
|
+
return (...args) => {
|
|
70
|
+
// Clear the previous timeout
|
|
71
|
+
clearTimeout(timeoutId);
|
|
72
|
+
// Set a new timeout
|
|
73
|
+
timeoutId = setTimeout(() => {
|
|
74
|
+
callback(...args);
|
|
75
|
+
}, delay);
|
|
76
|
+
};
|
|
77
|
+
};
|
|
78
|
+
exports.debounceAction = debounceAction;
|
|
79
|
+
/**
|
|
80
|
+
* * A generic throttle function that ensures a callback is executed at most once per specified interval.
|
|
81
|
+
*
|
|
82
|
+
* @param callback - The function to throttle.
|
|
83
|
+
* @param delay - The delay in milliseconds. Default is `150ms`.
|
|
84
|
+
* @returns A throttled version of the callback function.
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* const throttledResize = throttleAction(() => {
|
|
88
|
+
* console.log('Resized');
|
|
89
|
+
* }, 300);
|
|
90
|
+
*
|
|
91
|
+
* window.addEventListener('resize', throttledResize);
|
|
92
|
+
*/
|
|
93
|
+
const throttleAction = (callback, delay = 150) => {
|
|
94
|
+
let lastCall = 0;
|
|
95
|
+
return (...args) => {
|
|
96
|
+
const now = Date.now();
|
|
97
|
+
if (now - lastCall >= delay) {
|
|
98
|
+
lastCall = now;
|
|
99
|
+
callback(...args);
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
};
|
|
103
|
+
exports.throttleAction = throttleAction;
|
package/package.json
CHANGED