version bump 0.5.10: support trailing commas

This commit is contained in:
SheetJS 2014-03-25 00:42:14 -07:00
parent 0b3732c443
commit b2940bac21
5 changed files with 57 additions and 9 deletions

View File

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

11
ssf.js
View File

@ -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.9';
SSF.version = '0.5.10';
/* Options */
var opts_fmt = {};
function fixopts(o){for(var y in opts_fmt) if(o[y]===undefined) o[y]=opts_fmt[y];}
@ -205,6 +205,8 @@ var write_num = function(type, fmt, val) {
var mul = 0, o;
fmt = fmt.replace(/%/g,function(x) { mul++; return ""; });
if(mul !== 0) return write_num(type, fmt, val * Math.pow(10,2*mul)) + fill("%",mul);
fmt = fmt.replace(/(\.0+)(,+)$/g,function($$,$1,$2) { mul=$2.length; return $1; });
if(mul !== 0) return write_num(type, fmt, val / Math.pow(10,3*mul));
if(fmt.indexOf("E") > -1) {
var idx = fmt.indexOf("E") - fmt.indexOf(".") - 1;
if(fmt.match(/^#+0.0E\+0$/)) {
@ -238,11 +240,12 @@ var write_num = function(type, fmt, val) {
if(fmt.match(/^#+0+$/)) fmt = fmt.replace(/#/g,"");
if(fmt.match(/^00+$/)) return (val<0?"-":"")+pad(Math.round(aval),fmt.length);
if(fmt.match(/^[#?]+$/)) return String(Math.round(val)).replace(/^0$/,"");
if((r = fmt.match(/^#*0+\.(0+)/))) {
if((r = fmt.match(/^#*0*\.(0+)/))) {
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); });
rr = 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); });
return fmt.match(/0\./) ? rr : rr.replace(/^0\./,".");
}
fmt = fmt.replace(/^#+0/, "0");
fmt = fmt.replace(/^#+([0.])/, "$1");
if((r = fmt.match(/^(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.":".");

17
ssf.md
View File

@ -366,6 +366,14 @@ Percentage values should be physically shifted:
if(mul !== 0) return write_num(type, fmt, val * Math.pow(10,2*mul)) + fill("%",mul);
```
Formats with multiple commas after the decimal point should be shifted by the
appropiate multiple of 1000 (more magic):
```js>tmp/60_number.js
fmt = fmt.replace(/(\.0+)(,+)$/g,function($$,$1,$2) { mul=$2.length; return $1; });
if(mul !== 0) return write_num(type, fmt, val / Math.pow(10,3*mul));
```
For exponents, get the exponent and mantissa and format them separately:
```
@ -427,16 +435,17 @@ A few special general cases can be handled in a very dumb manner:
if(fmt.match(/^#+0+$/)) fmt = fmt.replace(/#/g,"");
if(fmt.match(/^00+$/)) return (val<0?"-":"")+pad(Math.round(aval),fmt.length);
if(fmt.match(/^[#?]+$/)) return String(Math.round(val)).replace(/^0$/,"");
if((r = fmt.match(/^#*0+\.(0+)/))) {
if((r = fmt.match(/^#*0*\.(0+)/))) {
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); });
rr = 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); });
return fmt.match(/0\./) ? rr : rr.replace(/^0\./,".");
}
```
The next few simplifications ignore leading optional sigils (`#`):
```
fmt = fmt.replace(/^#+0/, "0");
fmt = fmt.replace(/^#+([0.])/, "$1");
if((r = fmt.match(/^(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.":".");
@ -1002,7 +1011,7 @@ coveralls:
```json>package.json
{
"name": "ssf",
"version": "0.5.9",
"version": "0.5.10",
"author": "SheetJS",
"description": "pure-JS library to format data using ECMA-376 spreadsheet Format Codes",
"keywords": [ "format", "sprintf", "spreadsheet" ],

21
test/comma.js Normal file
View File

@ -0,0 +1,21 @@
/* vim: set ts=2: */
/*jshint loopfunc:true */
var SSF = require('../');
var fs = require('fs'), assert = require('assert');
var data = fs.readFileSync('./test/comma.tsv','utf8').split("\n");
function doit(w, headers) {
it(headers[w], function() {
for(var j=1;j<data.length;++j) {
if(!data[j]) continue;
var d = data[j].replace(/#{255}/g,"").split("\t");
var expected = d[w].replace("|", ""), actual;
try { actual = SSF.format(headers[w], Number(d[0]), {}); } catch(e) { }
if(actual != expected && d[w][0] !== "|") throw [actual, expected, w, headers[w],d[0],d].join("|");
}
});
}
describe('comma formats', function() {
var headers = data[0].split("\t");
for(var w = 1; w < headers.length; ++w) doit(w, headers);
});

15
test/comma.tsv Normal file
View File

@ -0,0 +1,15 @@
value #.0000,,, #.0000,, #.0000,
1.2345 .0000 .0000 .0012
12.345 .0000 .0000 .0123
123.456 .0000 .0001 .1235
1234 .0000 .0012 1.2340
12345 .0000 .0123 12.3450
123456 .0001 .1235 123.4560
1234567 .0012 1.2346 1234.5670
12345678 .0123 12.3457 12345.6780
123456789 .1235 123.4568 123456.7890
1234567890 1.2346 1234.5679 1234567.8900
12345678901 12.3457 12345.6789 12345678.9010
123456789012 123.4568 123456.7890 123456789.0120
4321 .0000 .0043 4.3210
4321234 .0043 4.3212 4321.2340
1 value #.0000,,, #.0000,, #.0000,
2 1.2345 .0000 .0000 .0012
3 12.345 .0000 .0000 .0123
4 123.456 .0000 .0001 .1235
5 1234 .0000 .0012 1.2340
6 12345 .0000 .0123 12.3450
7 123456 .0001 .1235 123.4560
8 1234567 .0012 1.2346 1234.5670
9 12345678 .0123 12.3457 12345.6780
10 123456789 .1235 123.4568 123456.7890
11 1234567890 1.2346 1234.5679 1234567.8900
12 12345678901 12.3457 12345.6789 12345678.9010
13 123456789012 123.4568 123456.7890 123456789.0120
14 4321 .0000 .0043 4.3210
15 4321234 .0043 4.3212 4321.2340