From 0b3732c4437d3a75e7af413a22b6c8aa2a3c5d57 Mon Sep 17 00:00:00 2001 From: SheetJS Date: Tue, 18 Mar 2014 20:58:45 -0700 Subject: [PATCH] version bump 0.5.9: strip leading # before 0 --- package.json | 2 +- ssf.js | 7 ++++--- ssf.md | 12 +++++++++--- test/oddities.json | 1 + 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 7c6d0b6..d548e61 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ssf", - "version": "0.5.8", + "version": "0.5.9", "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 829f3eb..626f7a9 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.8'; +SSF.version = '0.5.9'; /* Options */ var opts_fmt = {}; function fixopts(o){for(var y in opts_fmt) if(o[y]===undefined) o[y]=opts_fmt[y];} @@ -242,9 +242,10 @@ var write_num = function(type, fmt, val) { o = Math.round(val * Math.pow(10,r[1].length)); return String(o/Math.pow(10,r[1].length)).replace(/^([^\.]+)$/,"$1."+r[1]).replace(/\.$/,"."+r[1]).replace(/\.([0-9]*)$/,function($$, $1) { return "." + $1 + fill("0", r[1].length-$1.length); }); } + fmt = fmt.replace(/^#+0/, "0"); if((r = fmt.match(/^(0*)\.(#*)$/))) { - o = Math.round(val*Math.pow(10,r[2].length)); - return String(o * Math.pow(10,-r[2].length)).replace(/\.(\d*[1-9])0*$/,".$1").replace(/^([-]?\d*)$/,"$1.").replace(/^0\./,r[1].length?"0.":"."); + o = Math.round(aval*Math.pow(10,r[2].length)); + return sign + String(o / Math.pow(10,r[2].length)).replace(/\.(\d*[1-9])0*$/,".$1").replace(/^([-]?\d*)$/,"$1.").replace(/^0\./,r[1].length?"0.":"."); } if((r = fmt.match(/^#,##0([.]?)$/))) return sign + commaify(String(Math.round(aval))); if((r = fmt.match(/^#,##0\.([#0]*0)$/))) { diff --git a/ssf.md b/ssf.md index c61c856..d7bf29b 100644 --- a/ssf.md +++ b/ssf.md @@ -431,9 +431,15 @@ A few special general cases can be handled in a very dumb manner: o = Math.round(val * Math.pow(10,r[1].length)); return String(o/Math.pow(10,r[1].length)).replace(/^([^\.]+)$/,"$1."+r[1]).replace(/\.$/,"."+r[1]).replace(/\.([0-9]*)$/,function($$, $1) { return "." + $1 + fill("0", r[1].length-$1.length); }); } +``` + +The next few simplifications ignore leading optional sigils (`#`): + +``` + fmt = fmt.replace(/^#+0/, "0"); if((r = fmt.match(/^(0*)\.(#*)$/))) { - o = Math.round(val*Math.pow(10,r[2].length)); - return String(o * Math.pow(10,-r[2].length)).replace(/\.(\d*[1-9])0*$/,".$1").replace(/^([-]?\d*)$/,"$1.").replace(/^0\./,r[1].length?"0.":"."); + o = Math.round(aval*Math.pow(10,r[2].length)); + return sign + String(o / Math.pow(10,r[2].length)).replace(/\.(\d*[1-9])0*$/,".$1").replace(/^([-]?\d*)$/,"$1.").replace(/^0\./,r[1].length?"0.":"."); } if((r = fmt.match(/^#,##0([.]?)$/))) return sign + commaify(String(Math.round(aval))); if((r = fmt.match(/^#,##0\.([#0]*0)$/))) { @@ -996,7 +1002,7 @@ coveralls: ```json>package.json { "name": "ssf", - "version": "0.5.8", + "version": "0.5.9", "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 f277e6c..46cd7dd 100644 --- a/test/oddities.json +++ b/test/oddities.json @@ -19,6 +19,7 @@ ["((;@", [1,"(("], [0,"(("], ["foo","foo"]], ["\\r", [1, "r"], [-1, "r"], [0, "r"], ["TODO","TODO"]], ["_($* #,##0_);_($* (#,##0);_($* \"-\"_);_(@_)", [1, " $1 "], [-1, " $(1)"], [0," $- "], ["TODO", " TODO "], ["",""]], + ["#0.#", [0,"0."], [1,"1."], [12,"12."], [12.34, "12.3"], [-1.23, "-1.2"]], ["#,##0.0", [1,"1.0"], [-1,"-1.0"], [0,"0.0"], ["TODO","TODO"]], ["#,##0.00", [1,"1.00"], [-1,"-1.00"], [0,"0.00"], ["TODO","TODO"]], ["#,##0.000", [1,"1.000"], [-1,"-1.000"], [0,"0.000"], ["TODO","TODO"]],