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
This commit is contained in:
SheetJS 2014-04-03 01:11:44 -07:00
parent be19bcd01e
commit c156693778
4 changed files with 20 additions and 16 deletions

@ -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" ],

14
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;

17
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" ],

@ -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'"]],