npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

python-format-js

v1.4.3

Published

String formatting like Python's .format()

Downloads

1,114

Readme

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

  • [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

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