@@ -11,7 +11,7 @@ A lightweight JavaScript library implements C-style `printf` functionality.
1111
1212## 🌟 Features
1313
14- - Fully functional ` printf ` , ` sprint ` , and ` vsprint ` methods.
14+ - Fully functional ` printf ` , ` sprintf ` , and ` vsprintf ` methods.
1515- C-style format specifiers support:
1616 - ` %d ` , ` %f ` , ` %s ` , ` %x ` , ` %b ` , ` %o ` , ` %u ` , ` %c ` , ` %e ` , ` %g ` , ` %G ` , etc.
1717- Padding, alignment, width, and precision controls
@@ -51,7 +51,7 @@ Below are some of the most common ways to include printfy.
5151** CDN Link**
5252
5353``` html
54- <script src =" https://cdn.jsdelivr.net/npm/printfy@2.0.1 /dist/printfy.min.js" ></script >
54+ <script src =" https://cdn.jsdelivr.net/npm/printfy@2.0.2 /dist/printfy.min.js" ></script >
5555```
5656
5757You can add the script manually to your project:
@@ -64,15 +64,15 @@ You can add the script manually to your project:
6464There are several ways to use [ Webpack] ( https://webpack.js.org/ ) , [ Browserify] ( http://browserify.org/ ) or [ Babel] ( https://babeljs.io/ ) . For more information on using these tools, please refer to the corresponding project's documentation. In the script, including printfy will usually look like this:
6565
6666``` js
67- import {sprint , printf , vsprint } from " printfy" ;
67+ import {sprintf , printf , vprintf , vsprintf } from " printfy" ;
6868```
6969
7070### Node.js
7171
7272Fully compatible with [ Node.js] ( https://nodejs.org/ ) , this library lets you use familiar C-style printf formatting in server-side code—ideal for CLI tools, logging, and backend output formatting.
7373
7474``` js
75- const {sprint , printf , vsprint } = require (" printfy" );
75+ const {sprintf , printf , vprintf , vsprintf } = require (" printfy" );
7676```
7777
7878---
@@ -87,19 +87,27 @@ printf("Hello %s, you have %d new messages", "Alice", 5);
8787// Output: Hello Alice, you have 5 new messages
8888```
8989
90- ### ` sprint(format, ...args) `
90+ ### ` vprintf(format, args[]) `
91+ Logs the formatted output to the console.
92+
93+ ``` js
94+ printf (" Hello %s, you have %d new messages" , [ " Alice" , 5 ]);
95+ // Output: Hello Alice, you have 5 new messages
96+ ```
97+
98+ ### ` sprintf(format, ...args) `
9199Returns a formatted string (like ` sprintf ` in C).
92100
93101``` js
94- let result = sprint (" Value: %08.2f" , 3.14 );
102+ let result = sprintf (" Value: %08.2f" , 3.14 );
95103console .log (result); // Output: Value: 00003.14
96104```
97105
98- ### ` vsprint (format, args[])`
99- Same as ` sprint ` , but accepts arguments as an array.
106+ ### ` vsprintf (format, args[])`
107+ Same as ` sprintf ` , but accepts arguments as an array.
100108
101109``` js
102- vsprint (" User: %s, Score: %d" , [" Bob" , 100 ]);
110+ vsprintf (" User: %s, Score: %d" , [" Bob" , 100 ]);
103111// Output: User: Bob, Score: 100
104112```
105113
@@ -109,7 +117,7 @@ vsprint("User: %s, Score: %d", ["Bob", 100]);
109117
110118| Specifier | Meaning | Example |
111119| -----------| ---------------------------------------| ---------------------------------|
112- | ` %s ` | String | ` sprint ("%s", "abc")` → ` "abc" ` |
120+ | ` %s ` | String | ` sprintf ("%s", "abc")` → ` "abc" ` |
113121| ` %S ` | Uppercase string | ` "abc" ` → ` "ABC" ` |
114122| ` %d ` | Integer (decimal) | ` 42 ` |
115123| ` %u ` | Unsigned integer (64-bit) | ` -1 ` → ` "18446744073709551615" ` |
@@ -137,15 +145,81 @@ vsprint("User: %s, Score: %d", ["Bob", 100]);
137145## 🧪 Examples
138146
139147``` js
140- sprint (" Binary: %08b" , 5 ); // Binary: 00000101
141- sprint (" Hex: %#x" , 255 ); // Hex: ff
142- sprint (" Char: %c" , 65 ); // Char: A
143- sprint (" Padded: %10s" , " text" ); // Padded: text
144- sprint (" Left: %-10s!" , " text" ); // Left: text !
145- sprint (" Float: %.2f" , 3.14159 ); // Float: 3.14
146- sprint (" Scientific: %.2e" , 1200 ); // Scientific: 1.20e+3
148+ sprintf (" Binary: %08b" , 5 ); // Binary: 00000101
149+ sprintf (" Hex: %#x" , 255 ); // Hex: ff
150+ sprintf (" Char: %c" , 65 ); // Char: A
151+ sprintf (" Padded: %10s" , " text" ); // Padded: text
152+ sprintf (" Left: %-10s!" , " text" ); // Left: text !
153+ sprintf (" Float: %.2f" , 3.14159 ); // Float: 3.14
154+ sprintf (" Scientific: %.2e" , 1200 ); // Scientific: 1.20e+3
155+ ```
156+
157+ ** [ index$] – Argument Indexing**
158+ ``` js
159+ sprintf (' %2$s is %1$d years old.' , 22 , ' Modassir' );
160+ // Output: Modassir is 22 years old.
147161```
148162
163+ ** [ padding] – Custom Padding Character**
164+ ``` js
165+ sprintf (" %'~5d" , 42 );
166+ // Output: ~~~42
167+
168+ sprintf (" %'~-5d" , 42 );
169+ // Output: 42~~~
170+ ```
171+
172+ ** [ flag] – Format Flags**
173+
174+ Support flags ` + ` and ` - ` only.
175+
176+ ``` js
177+ sprintf (" %+d" , 42 );
178+ // Output: +45
179+
180+ sprintf (" %-d" , 42 );
181+ // Output: -45
182+ ```
183+
184+ ** [ width] – Custom Width**
185+ ``` js
186+ sprintf (" %6s" , " JS" );
187+ // Output: " JS"
188+
189+ sprintf (" lang%6s" , " JS" );
190+ // Output: "lang JS"
191+
192+ sprintf (" %-6s" , " JS" );
193+ // Output: "JS "
194+
195+ sprintf (" %-6slib" , " JS" );
196+ // Output: "JS lib"
197+ ```
198+
199+ ** [ .precision] – Precision (floating-point or string truncation)**
200+ ``` js
201+ sprintf (" %.2f" , 3.14159 );
202+ // Output: 3.14
203+
204+ sprintf (" %.4s" , " OpenAI" );
205+ // Output: Open
206+ ```
207+
208+ ** Full Format**
209+ ``` js
210+ sprintf (' %2$\' #-+10.2f and %1$\' *_10s' , " JS" , 3.14159 );
211+ // Output: +3.14#####JS*******
212+ ```
213+
214+ 🔹 Explanation:
215+ - ` %2$ ` : Second argument (` 3.14159 ` )
216+ - ` '# ` : padding character ` # `
217+ - ` - ` : left align
218+ - ` + ` : show sign
219+ - ` 10 ` : total width 10
220+ - ` .2f ` : 2 decimal places
221+ - ` %1$'*_10s ` : First argument (` JS ` ), padded with ` * ` to width 10
222+
149223---
150224
151225## ⚠️ Errors & Warnings
@@ -160,13 +234,13 @@ sprint("Scientific: %.2e", 1200); // Scientific: 1.20e+3
160234## 🔄 Comparison with Other Libraries
161235
162236| Feature / Library | printfy ✅ | sprintf-js 🟡 | fast-printf 🟢 | printf (npm) 🔵 |
163- | ---------------------- | ------------------ | ------------- | -------------- | --- ----------------- |
237+ | ---------------------- | ------------------ | --------------- | --------------- | ----------------- |
164238| C-style specifiers | ✅ Full | ✅ Full | ✅ Partial | ✅ Partial |
165239| %2\$ style arg index | ✅ Yes | ✅ Yes | ❌ No | ❌ No |
166240| BigInt support | ✅ Yes | ❌ No | ❌ No | ❌ No |
167241| %f vs %F (locale) | ✅ Separate | ❌ Combined | ❌ Combined | ❌ No |
168242| Custom padding ('x) | ✅ ` 'x ` , ` 0 ` , etc. | ❌ No | ❌ No | ❌ No |
169- | String return (sprint) | ✅ Yes | ✅ Yes | ✅ Yes | ❌ No |
243+ | String return (sprintf) | ✅ Yes | ✅ Yes | ✅ Yes | ❌ No |
170244| Lightweight / No deps | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes |
171245| Unicode emoji safe | Partial | Partial | ❌ No | ❌ No |
172246| Performance | Good | Good | Best | Good |
0 commit comments