📜 ⬆️ ⬇️

String.Format

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:



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:
  1. Parameters are separated by commas.
  2. 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']
  3. To mask the comma and the closing parenthesis, you need to use a slash: {0:f(p1\, p2)} will be transferred ['p1, p2']
  4. Parameters can be skipped: for {0:f(,p2)} will be transferred ['', 'p2']
  5. 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:

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 version

The script does not require any additional libraries and is divided into 3 parts:
  1. Actually implementation of the main formatting code
  2. Formatting numbers
  3. 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.
All code is colored with DmSyntax

Source: https://habr.com/ru/post/83185/


All Articles