Skip to content

jhonmart/python-format-js

Repository files navigation

Overview

NPM

License: MIT npm version

License: MIT License: MIT License: MIT

String formatting like Python's .format()

Obs:The result expected is the same as Python

Install

Node

  1. Install:

    npm install python-format-js

    or

    yarn add python-format-js
  2. Require:

    // Module and prototype
    const format = require("python-format-js");

    or

    // Only prototype
    require("python-format-js");

Tests

  1. 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

See Documention in Python

Supported 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

More Examples

- 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

CONTRIBUTING

About

String formatting like Python's .format()

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Contributors 5

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy