@sohanemon/utils 4.1.7 → 4.1.9

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
+ }
@@ -47,6 +47,17 @@ export declare function useTimeout(callback: () => void, delay?: number | null):
47
47
  * @param options - Options for the event listener.
48
48
  */
49
49
  export declare function useWindowEvent<K extends keyof WindowEventMap>(type: K, listener: (this: Window, ev: WindowEventMap[K]) => void, options?: boolean | AddEventListenerOptions): void;
50
+ /**
51
+ * Tuple type for session storage value and updater.
52
+ */
53
+ type SessionStorageValue<T> = [T, React.Dispatch<React.SetStateAction<T>>];
54
+ /**
55
+ * Hook to persist state in session storage.
56
+ * @param key - The key for session storage.
57
+ * @param defaultValue - The default value if no value is found in session storage.
58
+ * @returns A tuple of the stored value and an updater function.
59
+ */
60
+ export declare const useSessionStorage: <T extends Record<string, any>>(key: string, defaultValue: T) => SessionStorageValue<T>;
50
61
  /**
51
62
  * Tuple type for local storage value and updater.
52
63
  */
@@ -140,6 +140,33 @@ export function useWindowEvent(type, listener, options) {
140
140
  return () => window.removeEventListener(type, listener, options);
141
141
  }, [type, listener, options]);
142
142
  }
143
+ /**
144
+ * Hook to persist state in session storage.
145
+ * @param key - The key for session storage.
146
+ * @param defaultValue - The default value if no value is found in session storage.
147
+ * @returns A tuple of the stored value and an updater function.
148
+ */
149
+ export const useSessionStorage = (key, defaultValue) => {
150
+ const [storedValue, setStoredValue] = React.useState(defaultValue);
151
+ React.useEffect(() => {
152
+ const value = sessionStorage.getItem(key);
153
+ if (value) {
154
+ setStoredValue(JSON.parse(value));
155
+ }
156
+ }, [key]);
157
+ const updateStoredValue = (valueOrFn) => {
158
+ let newValue;
159
+ if (typeof valueOrFn === 'function') {
160
+ newValue = valueOrFn(storedValue);
161
+ }
162
+ else {
163
+ newValue = valueOrFn;
164
+ }
165
+ sessionStorage.setItem(key, JSON.stringify(newValue));
166
+ setStoredValue(newValue);
167
+ };
168
+ return [storedValue, updateStoredValue];
169
+ };
143
170
  /**
144
171
  * Hook to persist state in local storage.
145
172
  * @param key - The key for local storage.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sohanemon/utils",
3
- "version": "4.1.7",
3
+ "version": "4.1.9",
4
4
  "author": "Sohan Emon <sohanemon@outlook.com>",
5
5
  "description": "",
6
6
  "type": "module",