Those who write in C # know very well and often use the String.Format mechanism, which is strongly lacking in JavaScript. Despite its simplicity and convenience, there is not much to dig up on the web, mainly variations on the topic of sprintf (hello to the speakers). For a long time, a script was written that allowed formatting strings in JavaScript and was similar to String.Format C #. The formatting was used by my colleagues quite tightly in the scripts and I decided to brush the code a bit and publish it for those who want to get String.Format in JavaScript.
So, the main features:
- Markers in C #:
{0}
- You can set the formatting function to use:
{0:d}
- You can pass the parameters of the formatting function:
{0:n(,2)}
- You can register your formatting functions.
- Small size - 3.5KB packed
- Works fast
- Works in IE, Chrome, Firefox (verified), theoretically, nothing prevents to work in server-side JavaScript
- Already implemented and built-in formatting features:
- Array Formatting
- Formatting numbers
- Date and time formatting
Using
You can use 2 ways:
1. As in C #:
var s = String. Format (format, arg0 [ , arg1 [ , arg2 [ ... ] ] ] );
2. As a function of any line:
var s = 'format string {0}' . format (arg0 [ , arg1 [ , arg2 [ ... ] ] ] );
Rules for markers
{0}
- the value will be converted to a string using JavaScript rules
{0:f}
- the value will be converted to a string using the function registered under the name f
{0:f(p1,p2)}
- the value will be converted to a string using a function registered as f and this function will be passed the parameters p1 and p2 in the array, the number and rules for the parameters depend on the function itself, however there are several common rules:
- Parameters are separated by commas.
- All signs in parentheses are meaningful. for
{0:f(p1,p2)}
will be transmitted ['p1', 'p2'], and in the case of {0:f(p1, p2)}
will be transmitted ['p1', 'p2'] - To mask the comma and the closing parenthesis, you need to use a slash:
{0:f(p1\, p2)}
will be transferred ['p1, p2'] - Parameters can be skipped: for
{0:f(,p2)}
will be transferred ['', 'p2'] - You can use nested markers: for
{0:f({1})}
will be transferred ['value_ from_parameter_in_index_1'], in this case formatting is not allowed, and the value is transferred the same as was passed to the format function
Formatting functions
All formatting functions receive 2 parameters: the value to be formatted and an array of parameters. This is the recommended way to register formatting functions:
( function (format)<br/> { <br/> // <br/> // name - <br/> // v - , , format <br/> // params - , , <br/> // , <br/> format. add (name, function (v, params)<br/> { <br/> return ...;<br/> } );<br/> } )(String.prototype.format);
Built-in formatting features
The script already has built-in formatting functions for numbers, arrays, and dates.
{0:n}
- formatting a number, if the function did not receive a number, NaN will be displayed. The type of numbers 1.11111111e + 20 will be converted to normal: 111111111000000000000. You can send lines with the number: '1.67' or '123.456e + 2' - 1.67 and 12345.6 will be inserted, respectively.
{0:n([i][,f])}
- formatting of the number with zero filling up to the required number of digits.
i is the number of digits for the integer part, if there are more digits in the integer part, then they remain in place, if it is less, the necessary number of zeros will be inserted at the beginning.
f - the number of digits for the fractional part, the extra numbers will be discarded.
Parameters can be skipped: {0: n (, 2)} - display a number with 2 characters in the decimal part.
')
{0:df([f])}
- arbitrary date formatting, f - substitutional format string, possible substitutions:
- yy or yyyy - Year, always 4 characters are displayed.
- M or MM - Month, 1 or 2 characters
- d or dd - Day, 1 or 2 characters
- H or HH - Clock, 1 or 2 digits in 24-hour format
- m or mm - Minutes, 1 or 2 digits
- s or ss - Seconds, 1 or 2 characters
- f ... ffff - Milliseconds, from 1 to 4 characters
More complete documentation where to download the script
Actually everything is there: a
full description of the built-in formats, a test script, source codes and a packaged versionThe script does not require any additional libraries and is divided into 3 parts:
- Actually implementation of the main formatting code
- Formatting numbers
- Date / Time Formatting
To reduce the script, you can remove the second and / or third parts from there.
In the source there are enough comments to understand how it works.
