1.4.3 • Published 2 years ago
python-format-js v1.4.3
Overview
String formatting like Python's .format()
Obs:The result expected is the same as Python
Install
Node
Install:
npm install python-format-jsor
yarn add python-format-jsRequire:
// Module and prototype const format = require("python-format-js");or
// Only prototype require("python-format-js");
Tests
Test:
npm testor
yarn test
You Can Do
- Basic formatting
- Padding and aligning strings
- Truncating long strings
- Combining truncating and padding
- Signed numbers
- Named placeholdersSupported Parameter Values
- '<' - Left aligns the result (within the available space)
- '>' - Right aligns the result (within the available space)
- '^' - Center aligns the result (within the available space)
- '=' - Places the sign to the left most position
- '+' - Use a plus sign to indicate if the result is positive or negative
- '-' - Use a minus sign for negative values only
- ' ' - Use a leading space for positive numbers
- ',' - Use a comma as a thousand separator
- '_' - Use a underscore as a thousand separator
- 'b' - Binary format
- 'c' - Converts the value into the corresponding unicode character
- 'd' - Decimal format
- 'e' - Scientific format, with a lower case e
- 'E' - Scientific format, with an upper case E
- 'f' - Fix point number format
- 'F' - Fix point number format, upper case
- 'g' - General format
- 'G' - General format (using a upper case E for scientific notations)
- 'o' - Octal format
- 'x' - Hex format, lower case
- 'X' - Hex format, upper case
- 'n' - Number format
- '%' - Percentage format
- '#' - Makes the format include the 0b prefix in (Octal,Hex,Binary)
Examples
Obs:The result expected is the same as Python
- Please report any bug
- Simples Change:"{} {}".format("Jhon", "Mart");
("Jhon Mart");- Advance Change Array:"My name is {0} and i have {1} years old!".format(["Jônatas", 21]);
("My name is Jônatas and i have 21 years old!");- Advance Change Object:"Your name is {name} and you have {age} years old!".format({
name: "Jônatas",
age: 21,
});
("My name is Jônatas and i have 21 years old!");- One Argument type Number:"{} ".format(2);
("2 ");- One Argument type Float: "{} ".format(3.14))
("3.14 ");- One Argument type Boolean: "{} ".format(true))
("true ")- Multiple type Argument:"{} {} {}".format(2, 3.14, true);
("2 3.14 true");- Overflow String Align Right:"{:^3}".format("Gustavo");
("Gustavo");- Overflow String Align Center:"{:^3}".format("Gustavo");
("Gustavo");- Align Left:"{:<6}".format("oii");
("oii ");- Align Right:"{:>6}".format("oii");
(" oii");- Align Center Incomplete:"{:^6}".format("oii");
(" oii ");- Align Center Complete:"{:^7}".format("oii");
(" oii ");- Crop:"{:.7}".format("Jonatas Martins");
("Jonatas");- Size String:"{:10}".format("test");
("test ");- Char Append Left:"{:_<7}".format("Jhon");
("Jhon___");- Char Append Right:"{:_>7}".format("Jhon");
("___Jhon");- Char Append Center Incomplete:"{:_^7}".format("Jhon");
("_Jhon__");- String and param left align:"Olá {:<8}".format("Jhon");
("Olá Jhon ");- String and param right align:"Olá {:>8}".format("Jhon");
("Olá Jhon");- String and param center align:"Olá {:^8}".format("Jhon");
("Olá Jhon ");- Float:"{:f}; {:f}".format(3.14, -3.14);
("3.140000; -3.140000");- Float Space:"{: f}; {: f}".format(3.14, -3.14);
(" 3.140000; -3.140000");- Float Align:"{:<15f}; {: f}".format(3.14, -3.14);
("3.140000 ; -3.140000");- Float Plus:"{:+f}; {:+f}".format(3.14, -3.14);
("+3.140000; -3.140000");- Float Less:"{:-f}; {:-f}".format(3.14, -3.14);
("3.140000; -3.140000");- Number Simple:"{:n} é maior que {:n} ".format(3.14, 21);
("3.14 é maior que 21 ");- Binary:"{:b}".format(42);
("101010");- Binary Align:"{:>4b}".format(5);
(" 101");- Binary Mask:"{:#b}".format(42);
("0b101010");- Octal:"{:o}".format(42);
("52");- Octal Mask:"{:#o}".format(42);
("0o52");- Octal Mask Sign:"{:-o}".format(42);
("+52");- Octal Mask Space:"{: o}".format(42);
(" 52");- Number Octal Positive:"{:+#o}".format(4233);
("+0o10211");- Number Octal Negative:"{:-#o}".format(-4233);
("-0o10211");- Hexadecimal:"{:x}".format(42);
("2a");- Hexadecimal Mask:"{:#x}".format(42);
("0x2a");- Hexadecimal Mask Upper Case:"{:#X}".format(42);
("0X2A");- Decimal Number:"{:d}".format(42);
("42");- Exp:"{:e}".format(4233);
("4.233e+3");- Exp Upper Case:"{:E}".format(4233);
("4.233E+3");- Exp Size Over:"{:<15e}".format(4233);
("4.233e+3 ");- Percent:"{:%}".format(0.065);
("6.500000%");- All data:"{:g}".format("Hello World");
("Hello World");- Align All:"{:<5g}".format("T");
("T ");
- Thousands Separator:
```javascript
"{:,}".format(1234567890);
("1,234,567,890");Help us
1.4.3
2 years ago
1.4.2
2 years ago
1.4.1
4 years ago
1.4.0
4 years ago
1.3.9
4 years ago
1.3.8
6 years ago
1.3.7
6 years ago
1.3.6
6 years ago
1.3.5
6 years ago
1.3.4
6 years ago
1.3.3
6 years ago
1.3.2
6 years ago
1.3.1
6 years ago
1.3.0
6 years ago
1.2.0
6 years ago
1.1.1
6 years ago
1.1.0
6 years ago
1.0.9
6 years ago
1.0.6
6 years ago
1.0.5
6 years ago
1.0.4
6 years ago
1.0.3
6 years ago
1.0.2
6 years ago
1.0.1
6 years ago
1.0.0
6 years ago
