@sohanemon/utils 4.1.7 → 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
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sohanemon/utils",
3
- "version": "4.1.7",
3
+ "version": "4.1.8",
4
4
  "author": "Sohan Emon <sohanemon@outlook.com>",
5
5
  "description": "",
6
6
  "type": "module",