Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
tiny-sprintf
Advanced tools
Tiny but complete.
s
) is 519B minified, full build (all of php) is 989B minified, bare build is 422B (only s
, not extendable).npm install tiny-sprintf
Then, in nodejs:
var sprintf = require('tiny-sprintf');
The repo provides several builds that are updated with each release. They are found in the folder dist
. Each build will have a develop and minified version.
Currently, the collection consists of:
sprintf
s
require("tiny-sprintf/dist/sprintf")
same as require("tiny-sprintf")
require("tiny-sprintf/dist/sprintf.min")
sprintf.all
bcdeEfFgGosxX
require("tiny-sprintf/dist/sprintf.all")
require("tiny-sprintf/dist/sprintf.all.min")
sprintf.bare
s
require("tiny-sprintf/dist/sprintf.bare")
require("tiny-sprintf/dist/sprintf.bare.min")
Custom builds are possible, read 'Custom build' below.
It makes it easy to create monospace text with indenting and alignment.
Stuff like this:
name type description
===== =========== ========================
a value...... contains nothing
ab property... is important
abc method..... deprecated
test method..... unused
toolo method..... too long
metho method..... method with cut off name
Possible with this (for the values of each line):
sprintf(" %-5.5s %-'.-11.11s %-25.25s", name, type, description);
So if you want that stuff in the console or anywhere in your application, you can use sprintf.
The sprintf method will receive a string containing sprintf tokens, and any additional arguments you wish to insert at the place of the tokens. Which argument is used depends on the order of arguments vs the order of valid tokens. By default, the first token will get argument 1, the second token argument 2 etc. If you explicitly request an argument using the index$
notation (see 'Format elements' below), it is still counted as a valid token for what the argument selection is concerned.
The full token format (where []
means optional element) is:
"%[+][index$]['padchar][-][minWidth][.maxWidth]type"
The simplest and most straightforward token is "%s"
, as it inputs whatever argument at that place in the text.
Examples:
sprintf("Place %s into %s", {}, "trash"); // "Place [object Object] into trash"
sprintf("Place %2$s into %1$s", {}, "trash"); // "Place trash into [object Object]"
sprintf("%1$s, %2$s, %2$s, %1$s!", 'left', 'right'); // 'left, right, right, left!'
Formatting with minWidth
and maxWidth
edits the value, which is the result of typecasting the selected argument to string, right before it is inserted in the text. minWidth
fills up the value with padchar
s until it is at least as long as minWidth
dictates. maxWidth
will cut off any part of the value that is too large.
Whether this appending or cutting happens on the left or the right of the value, depends on the -
: default is left, which gives you a 'right align' effect. Adding the -
will cause manipulations on the right, which gives you a 'left align' effect.
Examples:
// Extend length
sprintf("%5s", 'a'); // ' a'
sprintf("%5s", 'abcdefg'); // 'abcdefg'
sprintf("%-5s", 'a'); // 'a '
sprintf("%-5s", 'abcdefg'); // 'abcdefg'
// Limit length
sprintf("%.5s", 'abcdefg'); // 'cdefg'
sprintf("%-.5s", 'abcdefg'); // 'abcde'
// Force exact size
sprintf("%5.5s", 'abc'); // ' abc'
sprintf("%5.5s", 'abcdefg'); // 'cdefg'
sprintf("%-5.5s", 'abc'); // 'abc '
sprintf("%-5.5s", 'abcdefg'); // 'abcde'
// Use pad chars
sprintf("%04s", 10); // "0010"
sprintf("%'#4s", 10); // "##10"
"%[+][index$]['padchar][-][minWidth][.maxWidth]type"
%
+
d
and positive, prepend value with a +
character. Not implemented by default.index$
0$
will point to the string to replace, and you probably don't want that.'padchar
'
)." "
0
), you can leave out the quote.-
minWidth
or maxWidth
defined.minWidth
index$
) in string form is shorter than this, the rest gets filled up with the padchar. It's added on the left or right of value, depending on the alignment setting (see -
)..maxWidth
type
s
or whatever of the full collection of type conversions was included in the build (see type conversions below).s
Each type conversion does something to the value it is given. If a character sequence is formatted as a sprintf token but its character does not yield a type conversion method (or result), the character sequence is 'escaped' instead. Which means, one %
is removed.
Examples:
// Type casting...
sprintf('%s', 10); // '10'
sprintf('%s', 'abc'); // 'abc'
sprintf('%d', 12); // '12'
sprintf('%d', 'abc'); // 'NaN'
// Escape anything else
sprintf('%%', 1); // '%'
sprintf('%T', 'abc'); // 'T'
Explanation of type conversions:
b
42
becomes "101010"
Number.prototype.toString(2)
B
) does not work.c
88
becomes "X"
(see ASCII table dec to char)String.fromCharCode()
C
) does not work.d
"abc"
becomes "NaN"
D
) does not work.e
10000
becomes "1e+4"
Number.prototype.toExponential()
E
) will turn the e
in the string to E
f
1234567.89
may turn into "1.234.567,89"
depending on where and what runs the javascript. Node does not seem to do anything special.Number.prototype.toLocaleString()
F
) will do the same.g
e
and f
which conversion yields the shortest string, and outputs that string. When equal in length, f
is favored.100000000
is shorter as "1e+8"
, but 100000001
is not and (maybe) becomes "100,000,001"
.G
) will use E
and F
instead.o
64
becomes "100"
.Number.prototype.toString(8)
O
) will not work.s
S
) will not work.x
255
becomes "ff"
Number.prototype.toString(16)
X
) will turn any letters in the string to upper case ("ff"
becomes "FF"
).Punch functions into the sprintf
function under the same property as the type character. Lowercase characters only, which will receive both lower- and uppercase. Expect args value, caps, plusChar
and return the value in the desired format, or undefined
to reject. You don't have to convert the value to string (unless it is undefined
).
Arguments:
{*} value
- the value for the match, which is an argument passed to sprintf{Boolean} caps
- whether or not the type character is in caps (/[A-Z]/
){String|undefined} plusChar
- the plus character, or undefined
Return:
{*|undefined}
- if anything else but undefined
, the match is accepted and the value is sprintf formatted. If undefined
, the match is rejected, and escaped like any other 'invalid' character.For example, the method of d
(typecast to Number, add plus if desired, rejects caps) is:
sprintf.d = function(value, caps, plusChar) {
return caps ? undefined : (plusChar || '') + (+value);
};
Enter the sprintf directory, run npm install
. Make sure you have Grunt installed (globally: npm install -g grunt-cli
). Run grunt
for a quick overview.
Use grunt build
to create a custom build. You can define a subset of the available types, or (re)build the bare build if you made changes to the source code.
Full format (where []
means optional):
grunt build[:types][:destPath][:doMinify]
Optional arguments:
types
"bare"
."bare"
will trigger the bare build, which contains only s
.destPath
./dist/sprintf.custom.js
sprintf.custom.js
will be useddoMinify
"false"
, "0"
and "no"
means false
. Everything else means true
, and will cause the build to be minified.true
.u
and A
to Z
are not used as global vars. In return, commonly occurring large property or method names are automatically swapped with vars, notably shrinking the minified build. I am not aware of any uglify2 solution to do this.Run grunt nodeunit
or npm test
. Watch the results.
FAQs
Tiny but complete implementation
The npm package tiny-sprintf receives a total of 1,383 weekly downloads. As such, tiny-sprintf popularity was classified as popular.
We found that tiny-sprintf demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.