From c156693778d928dc03d98171259881671b515b7b Mon Sep 17 00:00:00 2001 From: SheetJS Date: Thu, 3 Apr 2014 01:11:44 -0700 Subject: [PATCH] version bump 0.6.4: alternate forms with literal - Some versions of Excel render formats with literal hyphens, like `00000\-0000` that should be properly handled --- package.json | 2 +- ssf.js | 14 +++++++------- ssf.md | 17 +++++++++-------- test/oddities.json | 3 +++ 4 files changed, 20 insertions(+), 16 deletions(-) diff --git a/package.json b/package.json index eae8327..a19154f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ssf", - "version": "0.6.3", + "version": "0.6.4", "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 c97a6d3..2d15abe 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.6.3'; +SSF.version = '0.6.4'; /* Options */ var opts_fmt = {}; function fixopts(o){for(var y in opts_fmt) if(o[y]===undefined) o[y]=opts_fmt[y];} @@ -263,15 +263,15 @@ var write_num = function(type, fmt, val) { return val < 0 ? "-" + write_num(type, fmt, -val) : commaify(String(Math.floor(val))) + "." + pad(rr,r[1].length,0); } if((r = fmt.match(/^#,#*,#0/))) return write_num(type,fmt.replace(/^#,#*,/,""),val); - if((r = fmt.match(/^([0#]+)-([0#]+)$/))) { - ff = write_num(type, fmt.replace(/-/,""), val); + if((r = fmt.match(/^([0#]+)\\?-([0#]+)$/))) { + ff = write_num(type, fmt.replace(/[\\-]/g,""), val); return ff.substr(0,ff.length - r[2].length) + "-" + ff.substr(ff.length-r[2].length); } - if((r = fmt.match(/^([0#]+)-([0#]+)-([0#]+)$/))) { - ff = write_num(type, fmt.replace(/-/g,""), val); + if((r = fmt.match(/^([0#]+)\\?-([0#]+)\\?-([0#]+)$/))) { + ff = write_num(type, fmt.replace(/[\\-]/g,""), val); return ff.substr(0,ff.length - r[2].length - r[3].length) + "-" + ff.substr(ff.length-r[2].length - r[3].length, r[2].length) + "-" + ff.substr(ff.length-r[3].length); } - if(fmt == "(###) ###-####") { + if(fmt.match(/\(###\) ###\\?-####/)) { ff = write_num(type, "##########", val); return "(" + ff.substr(0,3) + ") " + ff.substr(3, 3) + "-" + ff.substr(6); } @@ -370,7 +370,7 @@ function eval_fmt(fmt, v, opts, flen) { break; /* Numbers */ case '0': case '#': case '.': - o = c; while("0#?.,E+-%".indexOf(c=fmt[++i]) > -1) o += c; + o = c; while("0#?.,E+-%".indexOf(c=fmt[++i]) > -1 || c=='\\' && fmt[i+1] == "-" && "0#".indexOf(fmt[i+2])>-1) o += c; out.push({t:'n', v:o}); break; case '?': o = fmt[i]; while(fmt[++i] === c) o+=c; diff --git a/ssf.md b/ssf.md index 78ba8e6..0aba79d 100644 --- a/ssf.md +++ b/ssf.md @@ -470,12 +470,12 @@ The next few simplifications ignore leading optional sigils (`#`): The `Zip Code + 4` format needs to treat an interstitial hyphen as a character: ``` - if((r = fmt.match(/^([0#]+)-([0#]+)$/))) { - ff = write_num(type, fmt.replace(/-/,""), val); + if((r = fmt.match(/^([0#]+)\\?-([0#]+)$/))) { + ff = write_num(type, fmt.replace(/[\\-]/g,""), val); return ff.substr(0,ff.length - r[2].length) + "-" + ff.substr(ff.length-r[2].length); } - if((r = fmt.match(/^([0#]+)-([0#]+)-([0#]+)$/))) { - ff = write_num(type, fmt.replace(/-/g,""), val); + if((r = fmt.match(/^([0#]+)\\?-([0#]+)\\?-([0#]+)$/))) { + ff = write_num(type, fmt.replace(/[\\-]/g,""), val); return ff.substr(0,ff.length - r[2].length - r[3].length) + "-" + ff.substr(ff.length-r[2].length - r[3].length, r[2].length) + "-" + ff.substr(ff.length-r[3].length); } ``` @@ -484,7 +484,7 @@ There's a better way to generalize the phone number and other formats in terms of first drawing the digits, but this selection allows for more nuance: ``` - if(fmt == "(###) ###-####") { + if(fmt.match(/\(###\) ###\\?-####/)) { ff = write_num(type, "##########", val); return "(" + ff.substr(0,3) + ") " + ff.substr(3, 3) + "-" + ff.substr(6); } @@ -669,12 +669,13 @@ pseudo-type `Z` is used to capture absolute time blocks: break; ``` -Number blocks (following the general pattern `[0#?][0#?.,E+-%]*`) are grouped together: +Number blocks (following the general pattern `[0#?][0#?.,E+-%]*`) are grouped +together. Literal hyphens are swallowed as well: ``` /* Numbers */ case '0': case '#': case '.': - o = c; while("0#?.,E+-%".indexOf(c=fmt[++i]) > -1) o += c; + o = c; while("0#?.,E+-%".indexOf(c=fmt[++i]) > -1 || c=='\\' && fmt[i+1] == "-" && "0#".indexOf(fmt[i+2])>-1) o += c; out.push({t:'n', v:o}); break; ``` @@ -1139,7 +1140,7 @@ coveralls: ```json>package.json { "name": "ssf", - "version": "0.6.3", + "version": "0.6.4", "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 bcb2354..6595f88 100644 --- a/test/oddities.json +++ b/test/oddities.json @@ -73,7 +73,10 @@ ], ["00000-0000", [941051630, "94105-1630"]], ["000-00-0000", [123456789, "123-45-6789"]], + ["00000\\-0000", [941051630, "94105-1630"]], + ["000\\-00\\-0000", [123456789, "123-45-6789"]], ["[<=9999999]###-####;(###) ###-####", [8675309, "867-5309"],[2813308004, "(281) 330-8004"]], + ["[<=9999999]###\\-####;(###) ###\\-####", [8675309, "867-5309"],[2813308004, "(281) 330-8004"]], ["[Red][<-25]General;[Blue][>25]General;[Green]General;[Yellow]General", [50, "50"],[26, "26"],[25,"25"],[1,"1"],[0,"0"],[-1,"-1"],[-25,"-25"],[-26,"26","#"],[-50,"50","#"], ["foo","foo"],["bar","bar"]], ["[Red][<=-25]General;[Blue][>=25]General;[Green]General;[Yellow]General", [50, "50"],[26, "26"],[25,"25"],[1,"1"],[0,"0"],[-1,"-1"],[-25,"-25"],[-26,"26","#"],[-50,"50","#"], ["foo","foo"],["bar","bar"]], ["[Red]General ;[Blue]General\\ ;[Green]Generalp;[Yellow]General'", [50, "50 "],[0,"0p"],[-25,"-25 "],["foo","foo'"]],