1.1.0 • Published 6 years ago

format-strings v1.1.0

Weekly downloads
-
License
MIT
Repository
-
Last release
6 years ago

Format-Strings

An advanced JavaScript library,
that adds python-like format function as a standalone function and also to a String prototype

How to install

If you use NPM package manager

npm install format-strings --save

or

If you use YARN package manager

yarn add format-strings

How to start using it

There are 2 ways to use it:

Using it as a String.prototype function:

require('format-strings') // It automatically adds to a String.prototype
// So, you can use it like here:
"An {example} string".format({
    example: "example advanced" // formatting parameters (see below)
})
// ↑↑↑ Results: "An example advanced string" ↑↑↑

or,

Using it as a standalone function:

let format = require('format-strings')
// It also avialable as String.prototype function too,
// but now it can also be used as a standalone function, so:
format("An {example} string", {
    example: "example advanced" // formatting parameters (see below)
})
// ↑↑↑ Results: "An example advanced string" again ↑↑↑

Formatting styles

Here I rely on the fact that you have already imported the library

A simple one-parameter formatting

If format's parameter is only a one string, it replaces target's template expressions with it

"{} or {something}, or even {5} - doesn't matter".format("Result")

Object-like formatting

If format's parameter is object, it replaces template expressions with object data (can be array)

"A {mdn} {0} {1} - cool {1}".format({
    mdn: "modern",
    0: "simple",
    1: "formatting"
})
// ↑↑↑ Results: "A modern simple formatting - cool formatting" ↑↑↑

Array-like formatting

This is like Object-like formatting, but with array

"A {2} {0} {1} - cool {1}".format([
    "simple",
    "formatting",
    "modern"
])
// ↑↑↑ Results: "A modern simple formatting - cool formatting" ↑↑↑

Empty template-style

If template's value is empty (its default doesn't count), it replaces with the next element of format parameter array

"Testing {}, {}, {1} again, and {2:third}".format([
    "first",
    "second"
]);
// ↑↑↑ Results: "Testing first, second, second again, and third" ↑↑↑

As you can see, next {}'s value depends only on its previous usage, and it doesn't on other templates with value.


Default values

You can set the default value for your template-expressions with ":"

"Let {someone:me} {0:oh, no!} the data{c:comma} {please}".format({
    0: "format",
    c: ", "
})
// ↑↑↑ Results: "Let someone format the data, " ↑↑↑

As you can see, the default value only puts when no usual value passed in the format parameter object And, if there is no usual value passed and no default value, the template expression becomes just empty


Escaping brackets and colons

There is a possibility to escape :, { and } or whole expression in your string, so the template expressions will work normally.

Using backslash method

Just put \\ before them in a string, or \ if it is user input (because of JS's own string-symbols-escaping ability, just Google about it)

"Here you can put \\{any\\} {:\\\\\\:\\{value\\}\\:\\\\}".format({})
// ↑↑↑ Results: "Here you can put {any} \:{value}:\" ↑↑↑
Using alternative method

Just use expressions below, if you do not want to use backslashes: | Expression | Meaning | Output | | ---------- | ------------ | ------ | | <<lb>> | LeftBracket | { | | <<rb>> | RightBracket | } | | <<cn>> | Colon | : | | <<bs>> | BackSlash | \ | Here's an example of using this method instead of default:

"Here you can put <<lb>>any<<rb>> {:<<bs>><<cn>><<lb>>value<<rb>><<cn>><<bs>>}".format({})
// ↑↑↑ Results: "Here you can put {any} \:{value}:\" ↑↑↑

Thanks for your attention. If you have ideas to improve the tool, or this readme (I'm not good at English), contact me: @Wynell at Telegram, hs#5588 at Discord, p3trukh1n at VK

1.1.0

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