Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add an n specifier that prints nothing #228

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ The placeholders in the format string are marked by `%` and are followed by one
* `u` — yields an integer as an unsigned decimal number
* `f` — yields a float as is; see notes on precision above
* `g` — yields a float as is; see notes on precision above
* `n` — yields nothing
* `o` — yields an integer as an octal number
* `s` — yields a string as is
* `t` — yields `true` or `false`
Expand Down
11 changes: 9 additions & 2 deletions src/sprintf.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
not_bool: /[^t]/,
not_type: /[^T]/,
not_primitive: /[^v]/,
nothing: /[n]/,
number: /[diefg]/,
numeric_arg: /[bcdiefguxX]/,
json: /[j]/,
not_json: /[^j]/,
text: /^[^\x25]+/,
modulo: /^\x25{2}/,
placeholder: /^\x25(?:([1-9]\d*)\$|\(([^)]+)\))?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-gijostTuvxX])/,
placeholder: /^\x25(?:([1-9]\d*)\$|\(([^)]+)\))?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-gijnostTuvxX])/,
key: /^([a-z_][a-z_\d]*)/i,
key_access: /^\.([a-z_][a-z_\d]*)/i,
index_access: /^\[(\d+)\]/,
Expand Down Expand Up @@ -89,6 +90,9 @@
case 'g':
arg = ph.precision ? String(Number(arg.toPrecision(ph.precision))) : parseFloat(arg)
break
case 'n':
arg = ''
break
case 'o':
arg = (parseInt(arg, 10) >>> 0).toString(8)
break
Expand Down Expand Up @@ -118,7 +122,10 @@
arg = (parseInt(arg, 10) >>> 0).toString(16).toUpperCase()
break
}
if (re.json.test(ph.type)) {
if (re.nothing.test(ph.type)) {
output += arg
}
else if (re.json.test(ph.type)) {
output += arg
}
else {
Expand Down
7 changes: 7 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ describe('sprintfjs', function() {
assert.equal('1,2,3', sprintf('%v', [1, 2, 3]))
assert.equal('[object Object]', sprintf('%v', {foo: 'bar'}))
assert.equal('/<("[^"]*"|\'[^\']*\'|[^\'">])*>/', sprintf('%v', /<("[^"]*"|'[^']*'|[^'">])*>/))

assert.equal('', sprintf('%n', 42))
assert.equal('', sprintf('%2$n', 'foo', 'bar', 'baz'))
assert.equal('Hello !', sprintf('Hello %(name)n!', {name: 'world'}))
})

it('should return formated strings for complex placeholders', function() {
Expand Down Expand Up @@ -104,6 +108,9 @@ describe('sprintfjs', function() {
assert.equal('xxxxx', sprintf('%5.5s', 'xxxxxx'))
assert.equal(' x', sprintf('%5.1s', 'xxxxxx'))

// mixed nothing
assert.equal('', sprintf("%+'#10n", -123))
assert.equal('foobaz', sprintf('%s%n%s', 'foo', 'bar', 'baz'))
})

it('should return formated strings for callbacks', function() {
Expand Down