version bump 0.6.1: ** #,###,#00,000.00,**

Due to IEEE754 overflow, there are slight differences in one order of magnitude.
This commit is contained in:
SheetJS 2014-03-28 14:28:39 -07:00
parent e2b0e6e0de
commit 5c12b601fa
5 changed files with 46 additions and 7 deletions

@ -1,6 +1,6 @@
{
"name": "ssf",
"version": "0.6.0",
"version": "0.6.1",
"author": "SheetJS",
"description": "pure-JS library to format data using ECMA-376 spreadsheet Format Codes",
"keywords": [ "format", "sprintf", "spreadsheet" ],

9
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.0';
SSF.version = '0.6.1';
/* Options */
var opts_fmt = {};
function fixopts(o){for(var y in opts_fmt) if(o[y]===undefined) o[y]=opts_fmt[y];}
@ -259,6 +259,7 @@ var write_num = function(type, fmt, val) {
rr = Math.round((val-Math.floor(val))*Math.pow(10,r[1].length));
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(/^([?]+)([ ]?)\/([ ]?)([?]+)/))) {
rr = Math.min(Math.max(r[1].length, r[4].length),7);
ff = frac(aval, Math.pow(10,rr)-1, false);
@ -269,6 +270,10 @@ var write_num = function(type, fmt, val) {
ff = frac(aval, Math.pow(10,rr)-1, true);
return sign + (ff[0]||(ff[1] ? "" : "0")) + " " + (ff[1] ? pad(ff[1],rr," ") + r[2] + "/" + r[3] + rpad(ff[2],rr," "): fill(" ", 2*rr+1 + r[2].length + r[3].length));
}
if((r = fmt.match(/^00,000\.([#0]*0)$/))) {
rr = val == Math.floor(val) ? 0 : Math.round((val-Math.floor(val))*Math.pow(10,r[1].length));
return val < 0 ? "-" + write_num(type, fmt, -val) : commaify(String(Math.floor(val))).replace(/^\d,\d{3}$/,"0$&").replace(/^\d*$/,function($$) { return "00," + ($$.length < 3 ? pad(0,3-$$.length) : "") + $$; }) + "." + pad(rr,r[1].length,0);
}
switch(fmt) {
case "0": case "#0": return Math.round(val);
case "#,###": var x = commaify(String(Math.round(aval))); return x !== "0" ? sign + x : "";
@ -350,7 +355,7 @@ function eval_fmt(fmt, v, opts, flen) {
case '?':
o = fmt[i]; while(fmt[++i] === c) o+=c;
q={t:c, v:o}; out.push(q); lst = c; break;
case '*': ++i; if(fmt[i] == ' ') ++i; break; // **
case '*': ++i; if(fmt[i] == ' ' || fmt[i] == '*') ++i; break; // **
case '(': case ')': out.push({t:(flen===1?'t':c),v:c}); ++i; break;
case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
o = fmt[i]; while("0123456789".indexOf(fmt[++i]) > -1) o+=fmt[i];

16
ssf.md

@ -464,6 +464,7 @@ The next few simplifications ignore leading optional sigils (`#`):
rr = Math.round((val-Math.floor(val))*Math.pow(10,r[1].length));
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);
```
The frac helper function is used for fraction formats (defined below).
@ -484,6 +485,15 @@ The frac helper function is used for fraction formats (defined below).
The default cases are hard-coded. TODO: actually parse them
```js>tmp/60_number.js
if((r = fmt.match(/^00,000\.([#0]*0)$/))) {
rr = val == Math.floor(val) ? 0 : Math.round((val-Math.floor(val))*Math.pow(10,r[1].length));
```
Note that this is technically incorrect
```
return val < 0 ? "-" + write_num(type, fmt, -val) : commaify(String(Math.floor(val))).replace(/^\d,\d{3}$/,"0$&").replace(/^\d*$/,function($$) { return "00," + ($$.length < 3 ? pad(0,3-$$.length) : "") + $$; }) + "." + pad(rr,r[1].length,0);
}
switch(fmt) {
case "0": case "#0": return Math.round(val);
case "#,###": var x = commaify(String(Math.round(aval))); return x !== "0" ? sign + x : "";
@ -648,7 +658,7 @@ Due to how the CSV generation works, asterisk characters are discarded. TODO:
communicate this somehow, possibly with an option
```
case '*': ++i; if(fmt[i] == ' ') ++i; break; // **
case '*': ++i; if(fmt[i] == ' ' || fmt[i] == '*') ++i; break; // **
```
@ -1061,7 +1071,7 @@ coveralls:
```json>package.json
{
"name": "ssf",
"version": "0.6.0",
"version": "0.6.1",
"author": "SheetJS",
"description": "pure-JS library to format data using ECMA-376 spreadsheet Format Codes",
"keywords": [ "format", "sprintf", "spreadsheet" ],
@ -1249,7 +1259,7 @@ describe('oddities', function() {
if(d[j].length == 2) {
var expected = d[j][1], actual = SSF.format(d[0], d[j][0], {});
assert.equal(actual, expected);
} else assert.throws(function() { SSF.format(d[0], d[j][0]); });
} else if(d[j][2] !== "#") assert.throws(function() { SSF.format(d[0], d[j][0]); });
}
});
});

@ -10,7 +10,7 @@ describe('oddities', function() {
if(d[j].length == 2) {
var expected = d[j][1], actual = SSF.format(d[0], d[j][0], {});
assert.equal(actual, expected);
} else assert.throws(function() { SSF.format(d[0], d[j][0]); });
} else if(d[j][2] !== "#") assert.throws(function() { SSF.format(d[0], d[j][0]); });
}
});
});

@ -47,5 +47,29 @@
["e", [0.7, "1900"]],
["123", [0.7, "123"], [0, "123"], ["sheetjs", "sheetjs"]],
["0.##", [1,"1."], [-1,"-1."], [0, "0."], [1.1, "1.1"], [-1.2, "-1.2"], [1000000000000.01, "1000000000000.01"], [-1000.01, "-1000.01"], [0.1, "0.1"], [1.007, "1.01"], [-1.008, "-1.01"]],
["** #,###,#00,000.00,**",
[1.2345, " 00,000.00"],
[12.345, " 00,000.01"],
[123.45, " 00,000.12"],
[1234.56, " 00,001.23"],
[12345.67, " 00,012.35"],
[123456.78, " 00,123.46"],
[1234567.89, " 01,234.57"],
[12345681.9, " 12,345.68"],
[123456822, " 123,456.82"],
[1234568223, " 1,234,568.22"],
[12345682233, " 12,345,682.23"],
[123456822333, " 123,456,822.33"],
[1234568223333, " 1,234,568,223.33"],
[12345682233333, " 12,345,682,233.33"],
[123456822333333, " 123,456,822,333.33"],
[1234568223333330, " 1,234,568,223,333.33"],
[12345682233333300, " 12,345,682,233,333.30"],
[123456822333333000, " 123,456,822,333,333.00", "#"],
[1234568223333330000, " 1,234,568,223,333,330.00"],
[12345682233333300000, " 12,345,682,233,333,300.00"],
[123456822333333000000, " 123,456,822,333,333,000.00"],
[1234568223333330000000, " 1,234,568,223,333,330,000.00"]
],
["\"foo\";\"bar\";\"baz\";\"qux\";\"foobar\"", [1], [0], [-1], ["sheetjs"]]
]