Skip to content

Commit

Permalink
refactor: replace sprintf-js with fast-printf
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Feb 4, 2021
1 parent eaefbf9 commit 929c207
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 79 deletions.
12 changes: 8 additions & 4 deletions i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
'use strict'

// dependencies
var vsprintf = require('sprintf-js').vsprintf
var printf = require('fast-printf').printf
var pkgVersion = require('./package.json').version
var fs = require('fs')
var url = require('url')
Expand Down Expand Up @@ -621,18 +621,22 @@ const i18n = function I18n(_OPTS = false) {

// replace the counter
if (typeof count === 'number') {
msg = vsprintf(msg, [Number(count)])
console.log({
msg,
args: [Number(count)]
})
msg = printf(msg, Number(count))
}

// if the msg string contains {{Mustache}} patterns we render it as a mini tempalate
// if the msg string contains {{Mustache}} patterns we render it as a mini template
if (!mustacheConfig.disable && mustacheRegex.test(msg)) {
msg = Mustache.render(msg, namedValues, {}, mustacheConfig.tags)
}

// if we have extra arguments with values to get replaced,
// an additional substition injects those strings afterwards
if (/%/.test(msg) && args && args.length > 0) {
msg = vsprintf(msg, args)
msg = printf(msg, ...args)
}

return msg
Expand Down
74 changes: 69 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@
},
"dependencies": {
"debug": "^4.1.1",
"fast-printf": "^1.4.1",
"make-plural": "^6.2.2",
"math-interval-parser": "^2.0.1",
"messageformat": "^2.3.0",
"mustache": "^4.0.1",
"sprintf-js": "^1.1.2"
"mustache": "^4.0.1"
},
"devDependencies": {
"async": "^3.2.0",
Expand Down
36 changes: 2 additions & 34 deletions test/i18n.api.global.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,12 +299,14 @@ describe('Module API', function () {
1,
__('tree')
)
console.log('singular', singular)
var plural = __n(
'There is one monkey in the %%s',
'There are %d monkeys in the %%s',
3,
__('tree')
)
console.log('plural', plural)
should.equal(singular, 'There is one monkey in the tree')
should.equal(plural, 'There are 3 monkeys in the tree')

Expand All @@ -325,40 +327,6 @@ describe('Module API', function () {
should.equal(plural, 'Im Baum sitzen 3 Affen')
})

it("won't return substitutions when not masked by an extra % (%% issue #49)", function () {
i18n.setLocale('en')
var singular = __n(
'There is one monkey in the %s',
'There are %d monkeys in the %s',
1,
__('tree')
)
var plural = __n(
'There is one monkey in the %s',
'There are %d monkeys in the %s',
3,
__('tree')
)
should.equal(singular, 'There is one monkey in the 1')
should.equal(plural, 'There are 3 monkeys in the undefined')

i18n.setLocale('de')
singular = __n(
'There is one monkey in the %s',
'There are %d monkeys in the %s',
1,
__('tree')
)
plural = __n(
'There is one monkey in the %s',
'There are %d monkeys in the %s',
3,
__('tree')
)
should.equal(singular, 'There is one monkey in the 1')
should.equal(plural, 'There are 3 monkeys in the undefined')
})

it('should be possible to use an json object as 1st parameter to specifiy a certain locale for that lookup', function () {
var singular, plural

Expand Down
34 changes: 0 additions & 34 deletions test/i18n.api.local.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,40 +439,6 @@ describe('Module API', function () {
should.equal(plural, 'Im Baum sitzen 3 Affen')
})

it("won't return substitutions when not masked by an extra % (%% issue #49)", function () {
i18n.setLocale(req, 'en')
var singular = req.__n(
'There is one monkey in the %s',
'There are %d monkeys in the %s',
1,
req.__('tree')
)
var plural = req.__n(
'There is one monkey in the %s',
'There are %d monkeys in the %s',
3,
req.__('tree')
)
should.equal(singular, 'There is one monkey in the 1')
should.equal(plural, 'There are 3 monkeys in the undefined')

i18n.setLocale(req, 'de')
singular = req.__n(
'There is one monkey in the %s',
'There are %d monkeys in the %s',
1,
req.__('tree')
)
plural = req.__n(
'There is one monkey in the %s',
'There are %d monkeys in the %s',
3,
req.__('tree')
)
should.equal(singular, 'There is one monkey in the 1')
should.equal(plural, 'There are 3 monkeys in the undefined')
})

it('should be possible to use an json object as 1st parameter to specifiy a certain locale for that lookup', function () {
i18n.setLocale(req, 'en')
var singular = req.__n(
Expand Down

0 comments on commit 929c207

Please sign in to comment.