Skip to content

Commit

Permalink
version bump 0.6.4: alternate forms with literal -
Browse files Browse the repository at this point in the history
Some versions of Excel render formats with literal hyphens, like `00000\-0000` that
should be properly handled
  • Loading branch information
SheetJSDev committed Apr 3, 2014
1 parent be19bcd commit c156693
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 16 deletions.
2 changes: 1 addition & 1 deletion 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" ],
Expand Down
14 changes: 7 additions & 7 deletions ssf.js
Expand Up @@ -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];}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down
17 changes: 9 additions & 8 deletions ssf.md
Expand Up @@ -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);
}
```
Expand All @@ -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);
}
Expand Down Expand Up @@ -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;

```
Expand Down Expand Up @@ -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" ],
Expand Down
3 changes: 3 additions & 0 deletions test/oddities.json
Expand Up @@ -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'"]],
Expand Down

0 comments on commit c156693

Please sign in to comment.