@sohanemon/utils 4.0.32 → 4.1.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.
@@ -60,3 +60,97 @@ export declare const copyToClipboard: (value: string, onSuccess?: () => void) =>
60
60
  * @returns - Normal Case
61
61
  */
62
62
  export declare function convertToNormalCase(inputString: string): string;
63
+ /**
64
+ * Checks if the code is running in a server-side environment.
65
+ *
66
+ * @returns - True if the code is executed in SSR (Server-Side Rendering) context, false otherwise
67
+ */
68
+ export declare const isSSR: boolean;
69
+ /**
70
+ * Converts an SVG string to a Base64-encoded string.
71
+ *
72
+ * @param str - The SVG string to encode
73
+ * @returns - Base64-encoded string representation of the SVG
74
+ */
75
+ export declare const svgToBase64: (str: string) => string;
76
+ /**
77
+ * Pauses execution for the specified time.
78
+ *
79
+ * @param time - Time in milliseconds to sleep (default is 1000ms)
80
+ * @returns - A Promise that resolves after the specified time
81
+ */
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
+ };
87
+ /**
88
+ * Creates a debounced function that delays invoking the provided function until
89
+ * after the specified `wait` time has elapsed since the last invocation.
90
+ *
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.
115
+ */
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
+ };
123
+ /**
124
+ * Creates a throttled function that invokes the provided function at most once
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.
147
+ *
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.
152
+ */
153
+ export declare function throttle<F extends (...args: any[]) => any>(function_: F, wait?: number, options?: {
154
+ leading?: boolean;
155
+ trailing?: boolean;
156
+ }): ThrottledFunction<F>;
@@ -105,7 +105,176 @@ export const copyToClipboard = (value, onSuccess = () => { }) => {
105
105
  export function convertToNormalCase(inputString) {
106
106
  const splittedString = inputString.split('.').pop();
107
107
  const string = splittedString || inputString;
108
- const words = string.replace(/([a-z])([A-Z])/g, '$1 $2').split(/[-_|\s]+/);
108
+ const words = string.replace(/([a-z])([A-Z])/g, '$1 $2').split(/[-_|​\s]+/);
109
109
  const capitalizedWords = words.map((word) => word.charAt(0).toUpperCase() + word.slice(1));
110
110
  return capitalizedWords.join(' ');
111
111
  }
112
+ /**
113
+ * Checks if the code is running in a server-side environment.
114
+ *
115
+ * @returns - True if the code is executed in SSR (Server-Side Rendering) context, false otherwise
116
+ */
117
+ export const isSSR = typeof window === 'undefined';
118
+ /**
119
+ * Converts an SVG string to a Base64-encoded string.
120
+ *
121
+ * @param str - The SVG string to encode
122
+ * @returns - Base64-encoded string representation of the SVG
123
+ */
124
+ export const svgToBase64 = (str) => isSSR ? Buffer.from(str).toString('base64') : window.btoa(str);
125
+ /**
126
+ * Pauses execution for the specified time.
127
+ *
128
+ * @param time - Time in milliseconds to sleep (default is 1000ms)
129
+ * @returns - A Promise that resolves after the specified time
130
+ */
131
+ export const sleep = (time = 1000) => new Promise((resolve) => setTimeout(resolve, time));
132
+ /**
133
+ * Creates a debounced function that delays invoking the provided function until
134
+ * after the specified `wait` time has elapsed since the last invocation.
135
+ *
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.
160
+ */
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;
190
+ };
191
+ Object.defineProperty(debounced, 'isPending', {
192
+ get() {
193
+ return timeoutId !== undefined;
194
+ },
195
+ });
196
+ return debounced;
197
+ }
198
+ /**
199
+ * Creates a throttled function that invokes the provided function at most once
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.
206
+ *
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.
227
+ */
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);
271
+ }
272
+ return result;
273
+ };
274
+ Object.defineProperty(throttled, 'isPending', {
275
+ get() {
276
+ return timeoutId !== undefined;
277
+ },
278
+ });
279
+ return throttled;
280
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sohanemon/utils",
3
- "version": "4.0.32",
3
+ "version": "4.1.0",
4
4
  "author": "Sohan Emon <sohanemon@outlook.com>",
5
5
  "description": "",
6
6
  "type": "module",