Skip to content
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 .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ The placeholders in the format string are marked by "%" and are followed by one
* e — print a float as scientific notation
* u — print an integer as an unsigned decimal number
* f — print a float as is
* j — print JSON of an object
* o — print an integer as an octal number
* O — print JSON of an object
* s — print a string as is
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
"test": "test"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "mocha"
},
"repository": {
"type": "git",
"url": "https://github.com/jontra/sprintf-fast.js"
},
"author": "Alexandru Marasteanu <hello@alexei.ro> (http://alexei.ro/)",
"license": "BSD",
"readmeFilename": "README.md"
"readmeFilename": "README.md",
"devDependencies": {
"mocha": "^2.1.0"
}
}
10 changes: 6 additions & 4 deletions src/sprintf.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ sprintf.chr_repeat = function(chr, n){
if (r && r.length>=n)
return r.slice(0, n);
r = chr;
for (; r.length<n; r += r);
for (; r.length<n; r += r) {}
chr_repeat_cache[chr] = r;
return r.slice(0, n);
};
Expand All @@ -47,7 +47,7 @@ sprintf.parse = function(fmt){
else if ((match = /^%%/.exec(_fmt)))
f += 'out += "%";\n';
else if ((match =
/^%(?:([1-9]\d*)\$|\(([^\)]+)\))?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([bcdefoOsuxX])/
/^%(?:([1-9]\d*)\$|\(([^\)]+)\))?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([bcdefjoOsuxX])/
.exec(_fmt)))
{
var positional = match[1], keyword = match[2], sign = match[3];
Expand Down Expand Up @@ -88,7 +88,7 @@ sprintf.parse = function(fmt){
f += 'arg = argv['+positional+'];\n';
else /* positional argument (implicit) */
f += 'arg = argv['+(cursor++)+'];\n';
if (/[^sO]/.test(conversion))
if (/[^jsO]/.test(conversion))
f += 'arg = +arg;\n';
switch (conversion)
{
Expand All @@ -107,8 +107,10 @@ sprintf.parse = function(fmt){
else
f += 'arg_s = ""+arg;\n';
break;
case 'j':
case 'O':
f += 'arg_s = JSON.stringify(arg);\n'; break;
case 'o': f += 'arg_s = arg.toString(8);\n'; break;
case 'O': f += 'arg_s = JSON.stringify(arg);\n'; break;
case 'u': f += 'arg = arg >>> 0; arg_s = ""+arg;\n'; break;
case 'x': f += 'arg_s = arg.toString(16);\n'; break;
case 'X': f += 'arg_s = arg.toString(16).toUpperCase();\n'; break;
Expand Down
3 changes: 3 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ describe('sprintf', function(){
t('%O', [1], '1');
t('%O', ["1"], '"1"');
t('%O', [{a: 1, b: 2}], '{"a":1,"b":2}');
t('%j', [1], '1');
t('%j', ["1"], '"1"');
t('%j', [{a: 1, b: 2}], '{"a":1,"b":2}');
t('%(aa)s', [{aa: "AA"}], 'AA');
t('%(aa[1])s', [{aa: [1, 2]}], '2');
t('%(aa[1].b.c[0][1])s', [{aa: [0, {b: {c: [[4, 5]]}}]}], '5');
Expand Down