diff --git a/README.md b/README.md index 73cc188..6a6fbe4 100644 --- a/README.md +++ b/README.md @@ -396,12 +396,12 @@ printf("|%-*.*d|", -4, 2, 1); // |01 | width=4 prec=2 value=1 flags='-' A negative precision is discarded: ```C -printf("|%*s|\n", 4, "sheetjs"); // |sheetjs| width=4 -printf("|%*.*s|\n", 4, 3, "sheetjs"); // | she| width=4 prec=3 -printf("|%*.*s|\n", 4, 2, "sheetjs"); // | sh| width=4 prec=2 -printf("|%*.*s|\n", 4, 1, "sheetjs"); // | s| width=4 prec=1 -printf("|%*.*s|\n", 4, 0, "sheetjs"); // | | width=4 prec=0 -printf("|%*.*s|\n", 4, -1, "sheetjs"); // |sheetjs| width=4 (prec ignored) +printf("|%*s|", 4, "sheetjs"); // |sheetjs| width=4 +printf("|%*.*s|", 4, 3, "sheetjs"); // | she| width=4 prec=3 +printf("|%*.*s|", 4, 2, "sheetjs"); // | sh| width=4 prec=2 +printf("|%*.*s|", 4, 1, "sheetjs"); // | s| width=4 prec=1 +printf("|%*.*s|", 4, 0, "sheetjs"); // | | width=4 prec=0 +printf("|%*.*s|", 4, -1, "sheetjs"); // |sheetjs| width=4 (prec ignored) ``` @@ -544,9 +544,9 @@ When a length specifier implies a certain size (such as `hh` for a single-byte integer), the number will be converted before rendering strings. For example: ```C -printf("%1$02hhx %1$02hx %1$02lx %1$02llx\n", 256); // 00 100 100 100 -printf("%1$02hhx %1$02hx %1$02lx %1$02llx\n", 4096); // 00 1000 1000 1000 -printf("%1$02hhx %1$02hx %1$02lx %1$02llx\n", 65536); // 00 00 10000 10000 +printf("%1$02hhx %1$02hx %1$02lx %1$02llx", 256); // |00 100 100 100| +printf("%1$02hhx %1$02hx %1$02lx %1$02llx", 4096); // |00 1000 1000 1000| +printf("%1$02hhx %1$02hx %1$02lx %1$02llx", 65536); // |00 00 10000 10000| ``` Values are restricted by first limiting the result to a specified number of diff --git a/bits/01_version.js b/bits/01_version.js index 908ae42..3633660 100644 --- a/bits/01_version.js +++ b/bits/01_version.js @@ -1 +1 @@ -PRINTJ.version = '1.0.1'; +PRINTJ.version = '1.1.0'; diff --git a/bits/50_doit.js b/bits/50_doit.js index 1a92acf..a359aea 100644 --- a/bits/50_doit.js +++ b/bits/50_doit.js @@ -70,6 +70,8 @@ function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { #include "54_convmisc.js" } + if(width < 0) { width = -width; flags += "-"; } + if(isnum == -1) { #include "60_integer.js" } else if(isnum > 0) { diff --git a/ctest/printj.js b/ctest/printj.js index 7a65d48..626c1c7 100644 --- a/ctest/printj.js +++ b/ctest/printj.js @@ -23,7 +23,7 @@ var PRINTJ; /*jshint ignore:end */ }(function(PRINTJ) { -PRINTJ.version = '1.0.1'; +PRINTJ.version = '1.1.0'; function tokenize(fmt) { var out = []; @@ -314,6 +314,8 @@ function doit(t, args) { } + if(width < 0) { width = -width; flags += "-"; } + if(isnum == -1) { Vnum = Number(arg); diff --git a/ctest/test.js b/ctest/test.js index 546b239..9b1c71c 100644 --- a/ctest/test.js +++ b/ctest/test.js @@ -171,4 +171,23 @@ describe('special cases', function() { assert.equal(sprintf("|%1$b|%1$B|%1$d|%1$D|%1$i|%1$o|%1$O|%1$u|%1$U|%1$x|%1$X|", undefined), "|0|0|0|0|0|0|0|0|0|0|0|"); assert.equal(sprintf("|%1$b|%1$B|%1$d|%1$D|%1$i|%1$o|%1$O|%1$u|%1$U|%1$x|%1$X|", null), "|0|0|0|0|0|0|0|0|0|0|0|"); }); + it('handles dynamic specifiers', function() { + assert.equal(sprintf("|%5s|", "sheetjs"), "|sheetjs|"); + assert.equal(sprintf("|%*s|", 5, "sheetjs"), "|sheetjs|"); + assert.equal(sprintf("|%2$*1$s|", 5, "sheetjs", 10), "|sheetjs|"); + assert.equal(sprintf("|%10s|", "sheetjs"), "| sheetjs|"); + assert.equal(sprintf("|%2$*3$s|", 5, "sheetjs", 10), "| sheetjs|"); + assert.equal(sprintf("|%0*.*d|", 4, 2, 1), "| 01|"); + assert.equal(sprintf("|%1$0*3$.*2$d|", 1, 2, 4), "| 01|"); + assert.equal(sprintf("|%*.*d|", 4, 2, 1), "| 01|"); + assert.equal(sprintf("|%-*.*d|", 4, 2, 1), "|01 |"); + assert.equal(sprintf("|%*.*d|", -4, 2, 1), "|01 |"); + assert.equal(sprintf("|%-*.*d|", -4, 2, 1), "|01 |"); + assert.equal(sprintf("|%*s|", 4, "sheetjs"), "|sheetjs|"); + assert.equal(sprintf("|%*.*s|", 4, 3, "sheetjs"), "| she|"); + assert.equal(sprintf("|%*.*s|", 4, 2, "sheetjs"), "| sh|"); + assert.equal(sprintf("|%*.*s|", 4, 1, "sheetjs"), "| s|"); + assert.equal(sprintf("|%*.*s|", 4, 0, "sheetjs"), "| |"); + assert.equal(sprintf("|%*.*s|", 4, -1, "sheetjs"), "|sheetjs|"); + }); }); diff --git a/package.json b/package.json index ac1663d..ce106dc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "printj", - "version": "1.0.2", + "version": "1.1.0", "author": "sheetjs", "description": "Pure-JS printf", "keywords": [ "printf", "sprintf", "format", "string" ], @@ -12,9 +12,10 @@ "dependencies": { }, "devDependencies": { - "mocha":"", - "@sheetjs/uglify-js":"", - "@types/node":"", + "mocha":"~2.5.3", + "blanket": "~1.2.3", + "@sheetjs/uglify-js":"~2.7.3", + "@types/node":"^8.0.7", "dtslint": "^0.1.2", "typescript": "2.2.0" }, diff --git a/printj.flow.js b/printj.flow.js index 1a4c8b2..539d72d 100644 --- a/printj.flow.js +++ b/printj.flow.js @@ -25,7 +25,7 @@ var PRINTJ/*:PRINTJModule*/; /*jshint ignore:end */ }(function(PRINTJ/*:PRINTJModule*/) { -PRINTJ.version = '1.0.1'; +PRINTJ.version = '1.1.0'; function tokenize(fmt/*:string*/)/*:ParsedFmt*/ { var out/*:ParsedFmt*/ = []; @@ -317,6 +317,8 @@ function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { } + if(width < 0) { width = -width; flags += "-"; } + if(isnum == -1) { Vnum = Number(arg); diff --git a/printj.js b/printj.js index 7a65d48..626c1c7 100644 --- a/printj.js +++ b/printj.js @@ -23,7 +23,7 @@ var PRINTJ; /*jshint ignore:end */ }(function(PRINTJ) { -PRINTJ.version = '1.0.1'; +PRINTJ.version = '1.1.0'; function tokenize(fmt) { var out = []; @@ -314,6 +314,8 @@ function doit(t, args) { } + if(width < 0) { width = -width; flags += "-"; } + if(isnum == -1) { Vnum = Number(arg); diff --git a/test.js b/test.js index 546b239..9b1c71c 100644 --- a/test.js +++ b/test.js @@ -171,4 +171,23 @@ describe('special cases', function() { assert.equal(sprintf("|%1$b|%1$B|%1$d|%1$D|%1$i|%1$o|%1$O|%1$u|%1$U|%1$x|%1$X|", undefined), "|0|0|0|0|0|0|0|0|0|0|0|"); assert.equal(sprintf("|%1$b|%1$B|%1$d|%1$D|%1$i|%1$o|%1$O|%1$u|%1$U|%1$x|%1$X|", null), "|0|0|0|0|0|0|0|0|0|0|0|"); }); + it('handles dynamic specifiers', function() { + assert.equal(sprintf("|%5s|", "sheetjs"), "|sheetjs|"); + assert.equal(sprintf("|%*s|", 5, "sheetjs"), "|sheetjs|"); + assert.equal(sprintf("|%2$*1$s|", 5, "sheetjs", 10), "|sheetjs|"); + assert.equal(sprintf("|%10s|", "sheetjs"), "| sheetjs|"); + assert.equal(sprintf("|%2$*3$s|", 5, "sheetjs", 10), "| sheetjs|"); + assert.equal(sprintf("|%0*.*d|", 4, 2, 1), "| 01|"); + assert.equal(sprintf("|%1$0*3$.*2$d|", 1, 2, 4), "| 01|"); + assert.equal(sprintf("|%*.*d|", 4, 2, 1), "| 01|"); + assert.equal(sprintf("|%-*.*d|", 4, 2, 1), "|01 |"); + assert.equal(sprintf("|%*.*d|", -4, 2, 1), "|01 |"); + assert.equal(sprintf("|%-*.*d|", -4, 2, 1), "|01 |"); + assert.equal(sprintf("|%*s|", 4, "sheetjs"), "|sheetjs|"); + assert.equal(sprintf("|%*.*s|", 4, 3, "sheetjs"), "| she|"); + assert.equal(sprintf("|%*.*s|", 4, 2, "sheetjs"), "| sh|"); + assert.equal(sprintf("|%*.*s|", 4, 1, "sheetjs"), "| s|"); + assert.equal(sprintf("|%*.*s|", 4, 0, "sheetjs"), "| |"); + assert.equal(sprintf("|%*.*s|", 4, -1, "sheetjs"), "|sheetjs|"); + }); });