python-format-js
v1.4.3
Published
String formatting like Python's .format()
Downloads
1,114
Readme
Overview
String formatting like Python's .format()
Obs:The result expected is the same as Python
Install
Node
Install:
npm install python-format-js
or
yarn add python-format-js
Require:
// Module and prototype const format = require("python-format-js");
or
// Only prototype require("python-format-js");
Tests
Test:
npm test
or
yarn test
You Can Do
- Basic formatting
- Padding and aligning strings
- Truncating long strings
- Combining truncating and padding
- Signed numbers
- Named placeholders
Supported Parameter Values
- [x] '<' - Left aligns the result (within the available space)
- [x] '>' - Right aligns the result (within the available space)
- [x] '^' - Center aligns the result (within the available space)
- [x] '=' - Places the sign to the left most position
- [x] '+' - Use a plus sign to indicate if the result is positive or negative
- [x] '-' - Use a minus sign for negative values only
- [x] ' ' - Use a leading space for positive numbers
- [x] ',' - Use a comma as a thousand separator
- [x] '_' - Use a underscore as a thousand separator
- [x] 'b' - Binary format
- [x] 'c' - Converts the value into the corresponding unicode character
- [x] 'd' - Decimal format
- [x] 'e' - Scientific format, with a lower case e
- [x] 'E' - Scientific format, with an upper case E
- [x] 'f' - Fix point number format
- [x] 'F' - Fix point number format, upper case
- [x] 'g' - General format
- [x] 'G' - General format (using a upper case E for scientific notations)
- [x] 'o' - Octal format
- [x] 'x' - Hex format, lower case
- [x] 'X' - Hex format, upper case
- [x] 'n' - Number format
- [x] '%' - Percentage format
- [x] '#' - 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");