@arcjet/sprintf
Advanced tools
+18
-1
@@ -1,1 +0,18 @@ | ||
| export default function sprintf(str: string, ...args: unknown[]): string; | ||
| /** | ||
| * Format a string with placeholders using the provided arguments. | ||
| * | ||
| * @param template | ||
| * Template. | ||
| * @param values | ||
| * Values to interpolate. | ||
| * @returns | ||
| * Formatted string. | ||
| */ | ||
| export declare function sprintf(template: string, ...values: unknown[]): string; | ||
| /** | ||
| * Format a string. | ||
| * | ||
| * @deprecated | ||
| * Use `sprintf` instead. | ||
| */ | ||
| export default sprintf; |
+73
-64
@@ -47,49 +47,58 @@ function bigintReplacer(key, value) { | ||
| // SOFTWARE. | ||
| function sprintf(str, ...args) { | ||
| if (typeof str !== "string") { | ||
| /** | ||
| * Format a string with placeholders using the provided arguments. | ||
| * | ||
| * @param template | ||
| * Template. | ||
| * @param values | ||
| * Values to interpolate. | ||
| * @returns | ||
| * Formatted string. | ||
| */ | ||
| function sprintf(template, ...values) { | ||
| if (typeof template !== "string") { | ||
| throw new TypeError("First argument must be a string"); | ||
| } | ||
| const argsLength = args.length; | ||
| if (argsLength === 0) { | ||
| return str; | ||
| if (values.length === 0) { | ||
| return template; | ||
| } | ||
| let output = ""; | ||
| let argIdx = 0; | ||
| let valueIndex = 0; | ||
| let lastPosition = -1; | ||
| const strLength = str.length; | ||
| for (let i = 0; i < strLength;) { | ||
| if (str.charCodeAt(i) === PERCENT_CODE && i + 1 < strLength) { | ||
| for (let index = 0; index < template.length;) { | ||
| if (template.charCodeAt(index) === PERCENT_CODE && | ||
| index + 1 < template.length) { | ||
| lastPosition = lastPosition > -1 ? lastPosition : 0; | ||
| switch (str.charCodeAt(i + 1)) { | ||
| switch (template.charCodeAt(index + 1)) { | ||
| case LOWERCASE_D_CODE: | ||
| case LOWERCASE_F_CODE: { | ||
| if (argIdx >= argsLength) { | ||
| if (valueIndex >= values.length) { | ||
| break; | ||
| } | ||
| const arg = args[argIdx]; | ||
| if (typeof arg !== "number") { | ||
| const value = values[valueIndex]; | ||
| if (typeof value !== "number") { | ||
| break; | ||
| } | ||
| if (lastPosition < i) { | ||
| output += str.slice(lastPosition, i); | ||
| if (lastPosition < index) { | ||
| output += template.slice(lastPosition, index); | ||
| } | ||
| output += arg; | ||
| lastPosition = i + 2; | ||
| i++; | ||
| output += value; | ||
| lastPosition = index + 2; | ||
| index++; | ||
| break; | ||
| } | ||
| case LOWERCASE_I_CODE: { | ||
| if (argIdx >= argsLength) { | ||
| if (valueIndex >= values.length) { | ||
| break; | ||
| } | ||
| const arg = args[argIdx]; | ||
| if (typeof arg !== "number") { | ||
| const value = values[valueIndex]; | ||
| if (typeof value !== "number") { | ||
| break; | ||
| } | ||
| if (lastPosition < i) { | ||
| output += str.slice(lastPosition, i); | ||
| if (lastPosition < index) { | ||
| output += template.slice(lastPosition, index); | ||
| } | ||
| output += Math.floor(arg); | ||
| lastPosition = i + 2; | ||
| i++; | ||
| output += Math.floor(value); | ||
| lastPosition = index + 2; | ||
| index++; | ||
| break; | ||
@@ -100,71 +109,71 @@ } | ||
| case LOWERCASE_J_CODE: { | ||
| if (argIdx >= argsLength) { | ||
| if (valueIndex >= values.length) { | ||
| break; | ||
| } | ||
| const arg = args[argIdx]; | ||
| if (arg === undefined) { | ||
| const value = values[valueIndex]; | ||
| if (value === undefined) { | ||
| break; | ||
| } | ||
| if (lastPosition < i) { | ||
| output += str.slice(lastPosition, i); | ||
| if (lastPosition < index) { | ||
| output += template.slice(lastPosition, index); | ||
| } | ||
| if (typeof arg === "string") { | ||
| output += `'${arg}'`; | ||
| lastPosition = i + 2; | ||
| i++; | ||
| if (typeof value === "string") { | ||
| output += `'${value}'`; | ||
| lastPosition = index + 2; | ||
| index++; | ||
| break; | ||
| } | ||
| if (typeof arg === "bigint") { | ||
| if (typeof value === "bigint") { | ||
| output += `"[BigInt]"`; | ||
| lastPosition = i + 2; | ||
| i++; | ||
| lastPosition = index + 2; | ||
| index++; | ||
| break; | ||
| } | ||
| if (typeof arg === "function") { | ||
| output += arg.name || "<anonymous>"; | ||
| lastPosition = i + 2; | ||
| i++; | ||
| if (typeof value === "function") { | ||
| output += value.name || "<anonymous>"; | ||
| lastPosition = index + 2; | ||
| index++; | ||
| break; | ||
| } | ||
| output += tryStringify(arg); | ||
| lastPosition = i + 2; | ||
| i++; | ||
| output += tryStringify(value); | ||
| lastPosition = index + 2; | ||
| index++; | ||
| break; | ||
| } | ||
| case LOWERCASE_S_CODE: { | ||
| if (argIdx >= argsLength) { | ||
| if (valueIndex >= values.length) { | ||
| break; | ||
| } | ||
| const arg = args[argIdx]; | ||
| if (typeof arg !== "string") { | ||
| const value = values[valueIndex]; | ||
| if (typeof value !== "string") { | ||
| break; | ||
| } | ||
| if (lastPosition < i) { | ||
| output += str.slice(lastPosition, i); | ||
| if (lastPosition < index) { | ||
| output += template.slice(lastPosition, index); | ||
| } | ||
| output += arg; | ||
| lastPosition = i + 2; | ||
| i++; | ||
| output += value; | ||
| lastPosition = index + 2; | ||
| index++; | ||
| break; | ||
| } | ||
| case PERCENT_CODE: { | ||
| if (lastPosition < i) { | ||
| output += str.slice(lastPosition, i); | ||
| if (lastPosition < index) { | ||
| output += template.slice(lastPosition, index); | ||
| } | ||
| output += "%"; | ||
| lastPosition = i + 2; | ||
| i++; | ||
| argIdx--; | ||
| lastPosition = index + 2; | ||
| index++; | ||
| valueIndex--; | ||
| break; | ||
| } | ||
| } | ||
| ++argIdx; | ||
| ++valueIndex; | ||
| } | ||
| ++i; | ||
| ++index; | ||
| } | ||
| if (lastPosition === -1) { | ||
| return str; | ||
| return template; | ||
| } | ||
| if (lastPosition < strLength) { | ||
| output += str.slice(lastPosition); | ||
| if (lastPosition < template.length) { | ||
| output += template.slice(lastPosition); | ||
| } | ||
@@ -174,2 +183,2 @@ return output; | ||
| export { sprintf as default }; | ||
| export { sprintf as default, sprintf }; |
+6
-7
| { | ||
| "name": "@arcjet/sprintf", | ||
| "version": "1.0.0-beta.10", | ||
| "version": "1.0.0-beta.11", | ||
| "description": "Arcjet platform-independent replacement for util.format", | ||
@@ -48,8 +48,7 @@ "keywords": [ | ||
| "devDependencies": { | ||
| "@arcjet/eslint-config": "1.0.0-beta.10", | ||
| "@arcjet/rollup-config": "1.0.0-beta.10", | ||
| "@arcjet/tsconfig": "1.0.0-beta.10", | ||
| "@rollup/wasm-node": "4.46.2", | ||
| "@types/node": "18.18.0", | ||
| "eslint": "9.32.0", | ||
| "@arcjet/eslint-config": "1.0.0-beta.11", | ||
| "@arcjet/rollup-config": "1.0.0-beta.11", | ||
| "@rollup/wasm-node": "4.50.0", | ||
| "@types/node": "24.3.0", | ||
| "eslint": "9.34.0", | ||
| "typescript": "5.9.2" | ||
@@ -56,0 +55,0 @@ }, |
+7
-1
@@ -79,5 +79,11 @@ <a href="https://arcjet.com" target="_arcjet-home"> | ||
| Derivative work based on [`quick-format-unescaped`][quick-format-unescaped] | ||
| licensed under [MIT][quick-format-unescaped-license] © David Mark Clements. | ||
| Our work is more restrictive while maintaining as much compatibility as | ||
| possible. | ||
| [apache-license]: http://www.apache.org/licenses/LICENSE-2.0 | ||
| [arcjet]: https://arcjet.com | ||
| [node-util]: https://nodejs.org/docs/latest-v18.x/api/util.html#utilformatformat-args | ||
| [quick-format-unescaped]: https://github.com/pinojs/quick-format-unescaped/blob/20ebf64c2f2e182f97923a423d468757b9a24a63/index.js | ||
| [quick-format-unescaped-license]: https://github.com/pinojs/quick-format-unescaped/blob/20ebf64/LICENSE | ||
| [quick-format-unescaped]: https://github.com/pinojs/quick-format-unescaped/blob/20ebf64/index.js |
23154
4.58%6
-14.29%198
15.12%89
7.23%