diff --git a/package.json b/package.json index 65f3c71..316b65f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ssf", - "version": "0.5.4", + "version": "0.5.5", "author": "SheetJS", "description": "pure-JS library to format data using ECMA-376 spreadsheet Format Codes", "keywords": [ "format", "sprintf", "spreadsheet" ], diff --git a/ssf.js b/ssf.js index 34c770a..da25f77 100644 --- a/ssf.js +++ b/ssf.js @@ -5,7 +5,7 @@ var _strrev = function(x) { return String(x).split("").reverse().join("");}; function fill(c,l) { return new Array(l+1).join(c); } function pad(v,d,c){var t=String(v);return t.length>=d?t:(fill(c||0,d-t.length)+t);} function rpad(v,d,c){var t=String(v);return t.length>=d?t:(t+fill(c||0,d-t.length));} -SSF.version = '0.5.4'; +SSF.version = '0.5.5'; /* Options */ var opts_fmt = {}; function fixopts(o){for(var y in opts_fmt) if(o[y]===undefined) o[y]=opts_fmt[y];} @@ -371,7 +371,7 @@ function eval_fmt(fmt, v, opts, flen) { out[i].v += out[jj].v; delete out[jj]; ++jj; } - out[i].v = write_num(out[i].t, out[i].v, v); + out[i].v = write_num(out[i].t, out[i].v, (flen >1 && v < 0 && i>0 && out[i-1].v == "-" ? -v:v)); out[i].t = 't'; i = jj-1; break; case 'G': out[i].t = 't'; out[i].v = general_fmt(v,opts); break; @@ -385,6 +385,7 @@ function choose_fmt(fmt, v, o) { if(typeof fmt === 'number') fmt = ((o&&o.table) ? o.table : table_fmt)[fmt]; if(typeof fmt === "string") fmt = split_fmt(fmt); var l = fmt.length; + if(l<4 && fmt[l-1].indexOf("@")>-1) --l; switch(fmt.length) { case 1: fmt = fmt[0].indexOf("@")>-1 ? ["General", "General", "General", fmt[0]] : [fmt[0], fmt[0], fmt[0], "@"]; break; case 2: fmt = fmt[1].indexOf("@")>-1 ? [fmt[0], fmt[0], fmt[0], fmt[1]] : [fmt[0], fmt[1], fmt[0], "@"]; break; @@ -402,7 +403,7 @@ var format = function format(fmt,v,o) { var f = choose_fmt(fmt, v, o); if(f[1].toLowerCase() === "general") return general_fmt(v,o); if(v === true) v = "TRUE"; if(v === false) v = "FALSE"; - if(v === "" || typeof v === "undefined") return ""; + if(v === "" || typeof v === "undefined") return ""; return eval_fmt(f[1], v, o, f[0]); }; diff --git a/ssf.md b/ssf.md index 77aec3b..6b00563 100644 --- a/ssf.md +++ b/ssf.md @@ -666,7 +666,13 @@ The default magic characters are listed in subsubsections 18.8.30-31 of ECMA376: out[i].v += out[jj].v; delete out[jj]; ++jj; } - out[i].v = write_num(out[i].t, out[i].v, v); +``` + +The magic in the next line is to ensure that the negative number is passed as +positive when there is an explicit hyphen before it (e.g. `#,##0.0;-#,##0.0`): + +``` + out[i].v = write_num(out[i].t, out[i].v, (flen >1 && v < 0 && i>0 && out[i-1].v == "-" ? -v:v)); out[i].t = 't'; i = jj-1; break; case 'G': out[i].t = 't'; out[i].v = general_fmt(v,opts); break; @@ -767,6 +773,7 @@ function choose_fmt(fmt, v, o) { if(typeof fmt === 'number') fmt = ((o&&o.table) ? o.table : table_fmt)[fmt]; if(typeof fmt === "string") fmt = split_fmt(fmt); var l = fmt.length; + if(l<4 && fmt[l-1].indexOf("@")>-1) --l; switch(fmt.length) { ``` @@ -816,7 +823,7 @@ The boolean TRUE and FALSE are formatted as if they are the uppercase text: Empty string should always emit empty, even if there are other characters: ``` - if(v === "" || typeof v === "undefined") return ""; + if(v === "" || typeof v === "undefined") return ""; return eval_fmt(f[1], v, o, f[0]); }; @@ -953,7 +960,7 @@ coveralls: ```json>package.json { "name": "ssf", - "version": "0.5.4", + "version": "0.5.5", "author": "SheetJS", "description": "pure-JS library to format data using ECMA-376 spreadsheet Format Codes", "keywords": [ "format", "sprintf", "spreadsheet" ], diff --git a/test/oddities.json b/test/oddities.json index f2c8cce..0f8575a 100644 --- a/test/oddities.json +++ b/test/oddities.json @@ -16,7 +16,7 @@ ["[Blue]G3neral", [1], [-1], [0], ["TODO","TODO"]], ["A\"TODO\"", [1, "ATODO"], [-1, "ATODO"], [0, "ATODO"], ["TODO","TODO"]], ["r", [1], [-1], [0], ["TODO","TODO"]], - ["((;@", [1], [-1], [0], ["TODO","TODO"]], + ["((;@", [1,"(("], [0,"(("], ["foo","foo"]], ["\\r", [1, "r"], [-1, "r"], [0, "r"], ["TODO","TODO"]], ["_($* #,##0_);_($* (#,##0);_($* \"-\"_);_(@_)", [1, " $1 "], [-1, " $(1)"], [0," $- "], ["TODO", " TODO "], ["",""]], ["#,##0.0", [1,"1.0"], [-1,"-1.0"], [0,"0.0"], ["TODO","TODO"]],