@sohanemon/utils 4.1.6 → 4.1.8

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.
@@ -165,3 +165,28 @@ export declare function throttle<F extends (...args: any[]) => any>(function_: F
165
165
  leading?: boolean;
166
166
  trailing?: boolean;
167
167
  }): ThrottledFunction<F>;
168
+ /**
169
+ * Formats a string by replacing each '%s' placeholder with the corresponding argument.
170
+ * This function mimics the basic behavior of C's printf for %s substitution.
171
+ *
172
+ * It supports both calls like `printf(format, ...args)` and `printf(format, argsArray)`.
173
+ *
174
+ * @param format - The format string containing '%s' placeholders.
175
+ * @param args - The values to substitute into the placeholders, either as separate arguments or as a single array.
176
+ * @returns The formatted string with all '%s' replaced by the provided arguments.
177
+ *
178
+ * @example
179
+ * ```ts
180
+ * const message = printf("%s love %s", "I", "Bangladesh");
181
+ * // message === "I love Bangladesh"
182
+ *
183
+ * const arr = ["I", "Bangladesh"];
184
+ * const message2 = printf("%s love %s", arr);
185
+ * // message2 === "I love Bangladesh"
186
+ *
187
+ * // If there are too few arguments:
188
+ * const incomplete = printf("Bangladesh is %s %s", "beautiful");
189
+ * // incomplete === "Bangladesh is beautiful"
190
+ * ```
191
+ */
192
+ export declare function printf(format: string, ...args: unknown[]): string;
@@ -311,3 +311,35 @@ export function throttle(function_, wait = 100, options) {
311
311
  });
312
312
  return throttled;
313
313
  }
314
+ /**
315
+ * Formats a string by replacing each '%s' placeholder with the corresponding argument.
316
+ * This function mimics the basic behavior of C's printf for %s substitution.
317
+ *
318
+ * It supports both calls like `printf(format, ...args)` and `printf(format, argsArray)`.
319
+ *
320
+ * @param format - The format string containing '%s' placeholders.
321
+ * @param args - The values to substitute into the placeholders, either as separate arguments or as a single array.
322
+ * @returns The formatted string with all '%s' replaced by the provided arguments.
323
+ *
324
+ * @example
325
+ * ```ts
326
+ * const message = printf("%s love %s", "I", "Bangladesh");
327
+ * // message === "I love Bangladesh"
328
+ *
329
+ * const arr = ["I", "Bangladesh"];
330
+ * const message2 = printf("%s love %s", arr);
331
+ * // message2 === "I love Bangladesh"
332
+ *
333
+ * // If there are too few arguments:
334
+ * const incomplete = printf("Bangladesh is %s %s", "beautiful");
335
+ * // incomplete === "Bangladesh is beautiful"
336
+ * ```
337
+ */
338
+ export function printf(format, ...args) {
339
+ const replacements = args.length === 1 && Array.isArray(args[0]) ? args[0] : args;
340
+ let idx = 0;
341
+ return format.replace(/%s/g, () => {
342
+ const arg = replacements[idx++];
343
+ return arg === undefined ? '' : String(arg);
344
+ });
345
+ }
@@ -303,7 +303,7 @@ export const useDomCalculation = ({ blockIds = [], margin = 0, substract = true,
303
303
  height: 500,
304
304
  width: 500,
305
305
  });
306
- const handleCalculation = () => {
306
+ const handleCalculation = React.useCallback(() => {
307
307
  const blocksHeight = blockIds.reduce((prevHeight, id) => prevHeight + (document.getElementById(id)?.clientHeight || 0), 0);
308
308
  const blocksWidth = blockIds.reduce((prevWidth, id) => prevWidth + (document.getElementById(id)?.clientWidth || 0), 0);
309
309
  const height = substract
@@ -312,28 +312,80 @@ export const useDomCalculation = ({ blockIds = [], margin = 0, substract = true,
312
312
  const width = substract
313
313
  ? window.innerWidth - blocksWidth - margin
314
314
  : blocksWidth + margin;
315
- const newDimensions = { height, width };
316
- setDimensions(newDimensions);
315
+ setDimensions((prev) => {
316
+ // Only update state if dimensions have actually changed
317
+ if (prev.height === height && prev.width === width) {
318
+ return prev;
319
+ }
320
+ return { height, width };
321
+ });
317
322
  onChange?.({
318
323
  blocksWidth,
319
324
  blocksHeight,
320
325
  remainingWidth: width,
321
326
  remainingHeight: height,
322
327
  });
323
- };
328
+ }, [blockIds, margin, substract, onChange]);
324
329
  useIsomorphicEffect(() => {
325
330
  handleCalculation();
326
- if (!dynamic)
327
- return;
328
- if (typeof dynamic === 'string') {
329
- const resizableElement = document.getElementById(dynamic);
330
- const resizeObserver = new ResizeObserver(() => handleCalculation());
331
- if (resizableElement)
332
- resizeObserver.observe(resizableElement);
333
- return () => resizeObserver.disconnect();
331
+ const cleanups = [];
332
+ if (blockIds.length > 0) {
333
+ const observer = new MutationObserver((mutations) => {
334
+ let shouldRecalculate = false;
335
+ for (const mutation of mutations) {
336
+ for (const node of mutation.addedNodes) {
337
+ if (node instanceof Element) {
338
+ if (blockIds.includes(node.id) ||
339
+ blockIds.some((id) => node.querySelector(`#${CSS.escape(id)}`))) {
340
+ shouldRecalculate = true;
341
+ break;
342
+ }
343
+ }
344
+ }
345
+ if (shouldRecalculate)
346
+ break;
347
+ for (const node of mutation.removedNodes) {
348
+ if (node instanceof Element) {
349
+ if (blockIds.includes(node.id) ||
350
+ blockIds.some((id) => node.querySelector(`#${CSS.escape(id)}`))) {
351
+ shouldRecalculate = true;
352
+ break;
353
+ }
354
+ }
355
+ }
356
+ if (shouldRecalculate)
357
+ break;
358
+ }
359
+ if (shouldRecalculate) {
360
+ handleCalculation();
361
+ }
362
+ });
363
+ observer.observe(document.body, {
364
+ childList: true,
365
+ subtree: true,
366
+ });
367
+ cleanups.push(() => observer.disconnect());
334
368
  }
335
- window.addEventListener('resize', handleCalculation);
336
- return () => window.removeEventListener('resize', handleCalculation);
337
- }, []);
369
+ if (dynamic) {
370
+ if (typeof dynamic === 'string') {
371
+ const resizableElement = document.getElementById(dynamic);
372
+ if (resizableElement) {
373
+ const resizeObserver = new ResizeObserver(handleCalculation);
374
+ resizeObserver.observe(resizableElement);
375
+ cleanups.push(() => resizeObserver.unobserve(resizableElement));
376
+ cleanups.push(() => resizeObserver.disconnect());
377
+ }
378
+ }
379
+ else {
380
+ window.addEventListener('resize', handleCalculation);
381
+ cleanups.push(() => window.removeEventListener('resize', handleCalculation));
382
+ }
383
+ }
384
+ return () => {
385
+ for (const cleanup of cleanups) {
386
+ cleanup();
387
+ }
388
+ };
389
+ }, [handleCalculation, dynamic, blockIds.join(',')]);
338
390
  return dimensions;
339
391
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sohanemon/utils",
3
- "version": "4.1.6",
3
+ "version": "4.1.8",
4
4
  "author": "Sohan Emon <sohanemon@outlook.com>",
5
5
  "description": "",
6
6
  "type": "module",