@sohanemon/utils 4.0.33 → 4.1.1
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/functions/index.d.ts +66 -10
- package/dist/functions/index.js +136 -22
- package/dist/hooks/index.d.ts +2 -1
- package/dist/hooks/index.js +5 -2
- package/package.json +21 -7
|
@@ -80,21 +80,77 @@ export declare const svgToBase64: (str: string) => string;
|
|
|
80
80
|
* @returns - A Promise that resolves after the specified time
|
|
81
81
|
*/
|
|
82
82
|
export declare const sleep: (time?: number) => Promise<unknown>;
|
|
83
|
+
type DebouncedFunction<F extends (...args: any[]) => any> = {
|
|
84
|
+
(...args: Parameters<F>): ReturnType<F> | undefined;
|
|
85
|
+
readonly isPending: boolean;
|
|
86
|
+
};
|
|
83
87
|
/**
|
|
84
88
|
* Creates a debounced function that delays invoking the provided function until
|
|
85
|
-
* after the specified wait time has elapsed since the last invocation.
|
|
89
|
+
* after the specified `wait` time has elapsed since the last invocation.
|
|
86
90
|
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
*
|
|
91
|
+
* If the `immediate` option is set to `true`, the function will be invoked immediately
|
|
92
|
+
* on the leading edge of the wait interval. Subsequent calls during the wait interval
|
|
93
|
+
* will reset the timer but not invoke the function until the interval elapses again.
|
|
94
|
+
*
|
|
95
|
+
* The returned function includes the `isPending` property to check if the debounce
|
|
96
|
+
* timer is currently active.
|
|
97
|
+
*
|
|
98
|
+
* @typeParam F - The type of the function to debounce.
|
|
99
|
+
*
|
|
100
|
+
* @param function_ - The function to debounce.
|
|
101
|
+
* @param wait - The number of milliseconds to delay (default is 100ms).
|
|
102
|
+
* @param options - An optional object with the following properties:
|
|
103
|
+
* - `immediate` (boolean): If `true`, invokes the function on the leading edge
|
|
104
|
+
* of the wait interval instead of the trailing edge.
|
|
105
|
+
*
|
|
106
|
+
* @returns A debounced version of the provided function, enhanced with the `isPending` property.
|
|
107
|
+
*
|
|
108
|
+
* @throws {TypeError} If the first parameter is not a function.
|
|
109
|
+
* @throws {RangeError} If the `wait` parameter is negative.
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* const log = debounce((message: string) => console.log(message), 200);
|
|
113
|
+
* log('Hello'); // Logs "Hello" after 200ms if no other call is made.
|
|
114
|
+
* console.log(log.isPending); // true if the timer is active.
|
|
90
115
|
*/
|
|
91
|
-
export declare function debounce<
|
|
116
|
+
export declare function debounce<F extends (...args: any[]) => any>(function_: F, wait?: number, options?: {
|
|
117
|
+
immediate: boolean;
|
|
118
|
+
}): DebouncedFunction<F>;
|
|
119
|
+
type ThrottledFunction<F extends (...args: any[]) => any> = {
|
|
120
|
+
(...args: Parameters<F>): ReturnType<F> | undefined;
|
|
121
|
+
readonly isPending: boolean;
|
|
122
|
+
};
|
|
92
123
|
/**
|
|
93
124
|
* Creates a throttled function that invokes the provided function at most once
|
|
94
|
-
* every
|
|
125
|
+
* every `wait` milliseconds.
|
|
126
|
+
*
|
|
127
|
+
* If the `leading` option is set to `true`, the function will be invoked immediately
|
|
128
|
+
* on the leading edge of the throttle interval. If the `trailing` option is set to `true`,
|
|
129
|
+
* the function will also be invoked at the end of the throttle interval if additional
|
|
130
|
+
* calls were made during the interval.
|
|
131
|
+
*
|
|
132
|
+
* The returned function includes the `isPending` property to check if the throttle
|
|
133
|
+
* timer is currently active.
|
|
134
|
+
*
|
|
135
|
+
* @typeParam F - The type of the function to throttle.
|
|
136
|
+
*
|
|
137
|
+
* @param function_ - The function to throttle.
|
|
138
|
+
* @param wait - The number of milliseconds to wait between invocations (default is 100ms).
|
|
139
|
+
* @param options - An optional object with the following properties:
|
|
140
|
+
* - `leading` (boolean): If `true`, invokes the function on the leading edge of the interval.
|
|
141
|
+
* - `trailing` (boolean): If `true`, invokes the function on the trailing edge of the interval.
|
|
142
|
+
*
|
|
143
|
+
* @returns A throttled version of the provided function, enhanced with the `isPending` property.
|
|
144
|
+
*
|
|
145
|
+
* @throws {TypeError} If the first parameter is not a function.
|
|
146
|
+
* @throws {RangeError} If the `wait` parameter is negative.
|
|
95
147
|
*
|
|
96
|
-
* @
|
|
97
|
-
*
|
|
98
|
-
*
|
|
148
|
+
* @example
|
|
149
|
+
* const log = throttle((message: string) => console.log(message), 200);
|
|
150
|
+
* log('Hello'); // Logs "Hello" immediately if leading is true.
|
|
151
|
+
* console.log(log.isPending); // true if the timer is active.
|
|
99
152
|
*/
|
|
100
|
-
export declare function throttle<
|
|
153
|
+
export declare function throttle<F extends (...args: any[]) => any>(function_: F, wait?: number, options?: {
|
|
154
|
+
leading?: boolean;
|
|
155
|
+
trailing?: boolean;
|
|
156
|
+
}): ThrottledFunction<F>;
|
package/dist/functions/index.js
CHANGED
|
@@ -131,36 +131,150 @@ export const svgToBase64 = (str) => isSSR ? Buffer.from(str).toString('base64')
|
|
|
131
131
|
export const sleep = (time = 1000) => new Promise((resolve) => setTimeout(resolve, time));
|
|
132
132
|
/**
|
|
133
133
|
* Creates a debounced function that delays invoking the provided function until
|
|
134
|
-
* after the specified wait time has elapsed since the last invocation.
|
|
134
|
+
* after the specified `wait` time has elapsed since the last invocation.
|
|
135
135
|
*
|
|
136
|
-
*
|
|
137
|
-
*
|
|
138
|
-
*
|
|
136
|
+
* If the `immediate` option is set to `true`, the function will be invoked immediately
|
|
137
|
+
* on the leading edge of the wait interval. Subsequent calls during the wait interval
|
|
138
|
+
* will reset the timer but not invoke the function until the interval elapses again.
|
|
139
|
+
*
|
|
140
|
+
* The returned function includes the `isPending` property to check if the debounce
|
|
141
|
+
* timer is currently active.
|
|
142
|
+
*
|
|
143
|
+
* @typeParam F - The type of the function to debounce.
|
|
144
|
+
*
|
|
145
|
+
* @param function_ - The function to debounce.
|
|
146
|
+
* @param wait - The number of milliseconds to delay (default is 100ms).
|
|
147
|
+
* @param options - An optional object with the following properties:
|
|
148
|
+
* - `immediate` (boolean): If `true`, invokes the function on the leading edge
|
|
149
|
+
* of the wait interval instead of the trailing edge.
|
|
150
|
+
*
|
|
151
|
+
* @returns A debounced version of the provided function, enhanced with the `isPending` property.
|
|
152
|
+
*
|
|
153
|
+
* @throws {TypeError} If the first parameter is not a function.
|
|
154
|
+
* @throws {RangeError} If the `wait` parameter is negative.
|
|
155
|
+
*
|
|
156
|
+
* @example
|
|
157
|
+
* const log = debounce((message: string) => console.log(message), 200);
|
|
158
|
+
* log('Hello'); // Logs "Hello" after 200ms if no other call is made.
|
|
159
|
+
* console.log(log.isPending); // true if the timer is active.
|
|
139
160
|
*/
|
|
140
|
-
export function debounce(
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
161
|
+
export function debounce(function_, wait = 100, options) {
|
|
162
|
+
if (typeof function_ !== 'function') {
|
|
163
|
+
throw new TypeError(`Expected the first parameter to be a function, got \`${typeof function_}\`.`);
|
|
164
|
+
}
|
|
165
|
+
if (wait < 0) {
|
|
166
|
+
throw new RangeError('`wait` must not be negative.');
|
|
167
|
+
}
|
|
168
|
+
const immediate = options?.immediate ?? false;
|
|
169
|
+
let timeoutId;
|
|
170
|
+
let lastArgs;
|
|
171
|
+
let lastContext;
|
|
172
|
+
let result;
|
|
173
|
+
function run() {
|
|
174
|
+
result = function_.apply(lastContext, lastArgs);
|
|
175
|
+
lastArgs = undefined;
|
|
176
|
+
lastContext = undefined;
|
|
177
|
+
return result;
|
|
178
|
+
}
|
|
179
|
+
const debounced = function (...args) {
|
|
180
|
+
lastArgs = args;
|
|
181
|
+
lastContext = this;
|
|
182
|
+
if (timeoutId === undefined && immediate) {
|
|
183
|
+
result = run.call(this);
|
|
184
|
+
}
|
|
185
|
+
if (timeoutId !== undefined) {
|
|
186
|
+
clearTimeout(timeoutId);
|
|
187
|
+
}
|
|
188
|
+
timeoutId = setTimeout(run.bind(this), wait);
|
|
189
|
+
return result;
|
|
145
190
|
};
|
|
191
|
+
Object.defineProperty(debounced, 'isPending', {
|
|
192
|
+
get() {
|
|
193
|
+
return timeoutId !== undefined;
|
|
194
|
+
},
|
|
195
|
+
});
|
|
196
|
+
return debounced;
|
|
146
197
|
}
|
|
147
198
|
/**
|
|
148
199
|
* Creates a throttled function that invokes the provided function at most once
|
|
149
|
-
* every
|
|
200
|
+
* every `wait` milliseconds.
|
|
201
|
+
*
|
|
202
|
+
* If the `leading` option is set to `true`, the function will be invoked immediately
|
|
203
|
+
* on the leading edge of the throttle interval. If the `trailing` option is set to `true`,
|
|
204
|
+
* the function will also be invoked at the end of the throttle interval if additional
|
|
205
|
+
* calls were made during the interval.
|
|
150
206
|
*
|
|
151
|
-
*
|
|
152
|
-
*
|
|
153
|
-
*
|
|
207
|
+
* The returned function includes the `isPending` property to check if the throttle
|
|
208
|
+
* timer is currently active.
|
|
209
|
+
*
|
|
210
|
+
* @typeParam F - The type of the function to throttle.
|
|
211
|
+
*
|
|
212
|
+
* @param function_ - The function to throttle.
|
|
213
|
+
* @param wait - The number of milliseconds to wait between invocations (default is 100ms).
|
|
214
|
+
* @param options - An optional object with the following properties:
|
|
215
|
+
* - `leading` (boolean): If `true`, invokes the function on the leading edge of the interval.
|
|
216
|
+
* - `trailing` (boolean): If `true`, invokes the function on the trailing edge of the interval.
|
|
217
|
+
*
|
|
218
|
+
* @returns A throttled version of the provided function, enhanced with the `isPending` property.
|
|
219
|
+
*
|
|
220
|
+
* @throws {TypeError} If the first parameter is not a function.
|
|
221
|
+
* @throws {RangeError} If the `wait` parameter is negative.
|
|
222
|
+
*
|
|
223
|
+
* @example
|
|
224
|
+
* const log = throttle((message: string) => console.log(message), 200);
|
|
225
|
+
* log('Hello'); // Logs "Hello" immediately if leading is true.
|
|
226
|
+
* console.log(log.isPending); // true if the timer is active.
|
|
154
227
|
*/
|
|
155
|
-
export function throttle(
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
228
|
+
export function throttle(function_, wait = 100, options) {
|
|
229
|
+
if (typeof function_ !== 'function') {
|
|
230
|
+
throw new TypeError(`Expected the first parameter to be a function, got \`${typeof function_}\`.`);
|
|
231
|
+
}
|
|
232
|
+
if (wait < 0) {
|
|
233
|
+
throw new RangeError('`wait` must not be negative.');
|
|
234
|
+
}
|
|
235
|
+
const leading = options?.leading ?? true;
|
|
236
|
+
const trailing = options?.trailing ?? true;
|
|
237
|
+
let timeoutId;
|
|
238
|
+
let lastArgs;
|
|
239
|
+
let lastContext;
|
|
240
|
+
let lastCallTime;
|
|
241
|
+
let result;
|
|
242
|
+
function invoke() {
|
|
243
|
+
lastCallTime = Date.now();
|
|
244
|
+
result = function_.apply(lastContext, lastArgs);
|
|
245
|
+
lastArgs = undefined;
|
|
246
|
+
lastContext = undefined;
|
|
247
|
+
}
|
|
248
|
+
function later() {
|
|
249
|
+
timeoutId = undefined;
|
|
250
|
+
if (trailing && lastArgs) {
|
|
251
|
+
invoke();
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
const throttled = function (...args) {
|
|
255
|
+
const now = Date.now();
|
|
256
|
+
const timeSinceLastCall = lastCallTime
|
|
257
|
+
? now - lastCallTime
|
|
258
|
+
: Number.POSITIVE_INFINITY;
|
|
259
|
+
lastArgs = args;
|
|
260
|
+
lastContext = this;
|
|
261
|
+
if (timeSinceLastCall >= wait) {
|
|
262
|
+
if (leading) {
|
|
263
|
+
invoke();
|
|
264
|
+
}
|
|
265
|
+
else {
|
|
266
|
+
timeoutId = setTimeout(later, wait);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
else if (!timeoutId && trailing) {
|
|
270
|
+
timeoutId = setTimeout(later, wait - timeSinceLastCall);
|
|
164
271
|
}
|
|
272
|
+
return result;
|
|
165
273
|
};
|
|
274
|
+
Object.defineProperty(throttled, 'isPending', {
|
|
275
|
+
get() {
|
|
276
|
+
return timeoutId !== undefined;
|
|
277
|
+
},
|
|
278
|
+
});
|
|
279
|
+
return throttled;
|
|
166
280
|
}
|
package/dist/hooks/index.d.ts
CHANGED
|
@@ -98,10 +98,11 @@ type CalculationProps = {
|
|
|
98
98
|
blockIds: string[];
|
|
99
99
|
dynamic?: boolean | string;
|
|
100
100
|
margin?: number;
|
|
101
|
+
substract?: boolean;
|
|
101
102
|
};
|
|
102
103
|
/**
|
|
103
104
|
* Hook to calculate the height of an element based on viewport and other block heights.
|
|
104
105
|
* @param params - Configuration object for height calculation.
|
|
105
106
|
* @returns The calculated height.
|
|
106
107
|
*/
|
|
107
|
-
export declare const useHeightCalculation: ({ blockIds, margin, dynamic, }: CalculationProps) => number;
|
|
108
|
+
export declare const useHeightCalculation: ({ blockIds, margin, substract, dynamic, }: CalculationProps) => number;
|
package/dist/hooks/index.js
CHANGED
|
@@ -264,11 +264,14 @@ export function useCopyToClipboard({ timeout = 2000 }) {
|
|
|
264
264
|
* @param params - Configuration object for height calculation.
|
|
265
265
|
* @returns The calculated height.
|
|
266
266
|
*/
|
|
267
|
-
export const useHeightCalculation = ({ blockIds = [], margin = 0, dynamic = false, }) => {
|
|
267
|
+
export const useHeightCalculation = ({ blockIds = [], margin = 0, substract = true, dynamic = false, }) => {
|
|
268
268
|
const [height, setTableHeight] = React.useState(500);
|
|
269
269
|
const handleResize = () => {
|
|
270
270
|
const blockHeight = blockIds.reduce((prevHeight, id) => prevHeight + (document.getElementById(id)?.clientHeight || 0), 0);
|
|
271
|
-
|
|
271
|
+
const height = substract
|
|
272
|
+
? window.innerHeight - blockHeight - margin
|
|
273
|
+
: blockHeight + margin;
|
|
274
|
+
setTableHeight(height);
|
|
272
275
|
};
|
|
273
276
|
useIsomorphicEffect(() => {
|
|
274
277
|
handleResize();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sohanemon/utils",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.1.1",
|
|
4
4
|
"author": "Sohan Emon <sohanemon@outlook.com>",
|
|
5
5
|
"description": "",
|
|
6
6
|
"type": "module",
|
|
@@ -15,19 +15,33 @@
|
|
|
15
15
|
},
|
|
16
16
|
"typesVersions": {
|
|
17
17
|
"*": {
|
|
18
|
-
"core": [
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
"
|
|
18
|
+
"core": [
|
|
19
|
+
"dist/index.d.ts"
|
|
20
|
+
],
|
|
21
|
+
"types": [
|
|
22
|
+
"dist/types/index.d.ts"
|
|
23
|
+
],
|
|
24
|
+
"hooks": [
|
|
25
|
+
"dist/hooks/index.d.ts"
|
|
26
|
+
],
|
|
27
|
+
"components": [
|
|
28
|
+
"dist/components/index.d.ts"
|
|
29
|
+
]
|
|
22
30
|
}
|
|
23
31
|
},
|
|
24
|
-
"files": [
|
|
32
|
+
"files": [
|
|
33
|
+
"dist",
|
|
34
|
+
"README.md"
|
|
35
|
+
],
|
|
25
36
|
"scripts": {
|
|
26
37
|
"build": "tsc",
|
|
27
38
|
"build:watch": "tsc --watch",
|
|
28
39
|
"export": "tsc && npm publish"
|
|
29
40
|
},
|
|
30
|
-
"keywords": [
|
|
41
|
+
"keywords": [
|
|
42
|
+
"utils",
|
|
43
|
+
"cn"
|
|
44
|
+
],
|
|
31
45
|
"license": "ISC",
|
|
32
46
|
"devDependencies": {
|
|
33
47
|
"@types/node": "^22.4.0",
|