forked from sheetjs/sheetjs
		
	version bump 0.8.0: performance
- separate i4/r8 code (core functions optimized by v8) - prefer faster methods when convenient - node-based performance tests
This commit is contained in:
		
							parent
							
								
									9f4aa161e6
								
							
						
					
					
						commit
						e32a2894c4
					
				| @ -3,3 +3,5 @@ node_modules/ | ||||
| tmp/ | ||||
| .gitignore | ||||
| .vocrc | ||||
| v8.log | ||||
| perf.log | ||||
|  | ||||
							
								
								
									
										4
									
								
								Makefile
									
									
									
									
									
										
										
										Normal file → Executable file
									
								
							
							
								
								
								
								
								
									
									
								
							
						
						
									
										4
									
								
								Makefile
									
									
									
									
									
										
										
										Normal file → Executable file
									
								
							| @ -13,6 +13,10 @@ lint: | ||||
| 	jshint ssf.js test/ | ||||
| 	jscs ssf.js | ||||
| 
 | ||||
| .PHONY: perf | ||||
| perf: | ||||
| 	bash misc/perf.sh | ||||
| 
 | ||||
| .PHONY: cov | ||||
| cov: tmp/coverage.html | ||||
| 
 | ||||
|  | ||||
							
								
								
									
										77
									
								
								misc/perf.js
									
									
									
									
									
										Normal file
									
								
							
							
								
								
								
								
								
									
									
								
							
						
						
									
										77
									
								
								misc/perf.js
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,77 @@ | ||||
| /* vim: set ts=2: */ | ||||
| /*jshint loopfunc:true */ | ||||
| var SSF = require('../'); | ||||
| var fs = require('fs')//, assert = require('assert');
 | ||||
| var data = JSON.parse(fs.readFileSync('./test/oddities.json','utf8')); | ||||
| var dates = fs.readFileSync('./test/dates.tsv','utf8').split("\n"); | ||||
| var date2 = fs.readFileSync('./test/cal.tsv',  'utf8').split("\n"); | ||||
| var times = fs.readFileSync('./test/times.tsv','utf8').split("\n"); | ||||
| function doit(data) { | ||||
|   /* Prevent Optimization */ | ||||
|   /* Prevent Optimization */ | ||||
|   /* Prevent Optimization */ | ||||
|   /* Prevent Optimization */ | ||||
|   /* Prevent Optimization */ | ||||
|   /* Prevent Optimization */ | ||||
|   /* Prevent Optimization */ | ||||
|   /* Prevent Optimization */ | ||||
|   /* Prevent Optimization */ | ||||
|   /* Prevent Optimization */ | ||||
|   /* Prevent Optimization */ | ||||
|   /* Prevent Optimization */ | ||||
|   /* Prevent Optimization */ | ||||
|   /* Prevent Optimization */ | ||||
|   /* Prevent Optimization */ | ||||
|   /* Prevent Optimization */ | ||||
|   /* Prevent Optimization */ | ||||
|   /* Prevent Optimization */ | ||||
|   /* Prevent Optimization */ | ||||
|   /* Prevent Optimization */ | ||||
|   /* Prevent Optimization */ | ||||
|   var headers = data[0].split("\t"); | ||||
|   for(var k = 1; k <= data.length; ++k) { | ||||
|     if(data[k] == null) return; | ||||
|     var d = data[k].replace(/#{255}/g,"").split("\t"); | ||||
|     for(var w = 1; w < headers.length; ++w) { | ||||
|       var expected = d[w], actual = SSF.format(headers[w], parseFloat(d[0]), {}); | ||||
|     } | ||||
|   } | ||||
| } | ||||
| 
 | ||||
| function testit() { | ||||
|   /* Prevent Optimization */ | ||||
|   /* Prevent Optimization */ | ||||
|   /* Prevent Optimization */ | ||||
|   /* Prevent Optimization */ | ||||
|   /* Prevent Optimization */ | ||||
|   /* Prevent Optimization */ | ||||
|   /* Prevent Optimization */ | ||||
|   /* Prevent Optimization */ | ||||
|   /* Prevent Optimization */ | ||||
|   /* Prevent Optimization */ | ||||
|   /* Prevent Optimization */ | ||||
|   /* Prevent Optimization */ | ||||
|   /* Prevent Optimization */ | ||||
|   /* Prevent Optimization */ | ||||
|   /* Prevent Optimization */ | ||||
|   /* Prevent Optimization */ | ||||
|   /* Prevent Optimization */ | ||||
|   /* Prevent Optimization */ | ||||
|   /* Prevent Optimization */ | ||||
|   /* Prevent Optimization */ | ||||
|   doit(times.slice(0,4000)); | ||||
|   doit(dates.slice(0,4000)); | ||||
|     for(var i = 0; i != 1000; ++i) { | ||||
|     for(var k = 0; k != data.length; ++k) { | ||||
|       var d = data[k]; | ||||
|       for(var j=1;j<d.length;++j) { | ||||
|         if(d[j].length == 2) { | ||||
|           var expected = d[j][1], actual = SSF.format(d[0], d[j][0], {}); | ||||
|           //if(actual != expected) console.log(d[j]);
 | ||||
|         } | ||||
|       } | ||||
|     } | ||||
|   } | ||||
| } | ||||
| 
 | ||||
| testit(); | ||||
							
								
								
									
										13
									
								
								misc/perf.sh
									
									
									
									
									
										Executable file
									
								
							
							
								
								
								
								
								
									
									
								
							
						
						
									
										13
									
								
								misc/perf.sh
									
									
									
									
									
										Executable file
									
								
							| @ -0,0 +1,13 @@ | ||||
| #!/bin/bash | ||||
| ARGS="--trace_opt --trace_deopt --trace_inlining --code_comments" | ||||
| #ARGS="--trace_opt --trace_deopt --code_comments" | ||||
| SCPT=misc/perf.js | ||||
| 
 | ||||
| echo 1 | ||||
| make && jshint --show-non-errors ssf.js && make lint && | ||||
| MINTEST=1 mocha -b && time node $SCPT && { | ||||
| node $ARGS $SCPT > perf.log | ||||
| node --prof $SCPT | ||||
| echo 1; time node $SCPT >/dev/null | ||||
| echo 1; time node $SCPT >/dev/null | ||||
| } && grep disabled perf.log | ||||
| @ -1,13 +1,13 @@ | ||||
| { | ||||
|   "name": "ssf", | ||||
|   "version": "0.7.1", | ||||
|   "version": "0.8.0", | ||||
|   "author": "SheetJS", | ||||
|   "description": "pure-JS library to format data using ECMA-376 spreadsheet Format Codes", | ||||
|   "description": "Format data using ECMA-376 spreadsheet Format Codes", | ||||
|   "keywords": [ "format", "sprintf", "spreadsheet" ], | ||||
|   "main": "ssf.js", | ||||
|   "dependencies": { | ||||
|     "voc":"", | ||||
|     "colors":"", | ||||
|     "colors":"0.6.2", | ||||
|     "frac":"0.3.1" | ||||
|   }, | ||||
|   "devDependencies": { | ||||
|  | ||||
							
								
								
									
										776
									
								
								ssf.js
									
									
									
									
									
								
							
							
								
								
								
								
								
									
									
								
							
						
						
									
										776
									
								
								ssf.js
									
									
									
									
									
								
							| @ -1,18 +1,27 @@ | ||||
| /* ssf.js (C) 2013-2014 SheetJS -- http://sheetjs.com */ | ||||
| /*jshint -W041 */ | ||||
| var SSF = {}; | ||||
| var make_ssf = function(SSF){ | ||||
| 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.7.1'; | ||||
| var make_ssf = function make_ssf(SSF){ | ||||
| SSF.version = '0.8.0'; | ||||
| function _strrev(x) { var o = "", i = x.length-1; while(i>=0) o += x.charAt(i--); return o; } | ||||
| function fill(c,l) { var o = ""; while(o.length < l) o+=c; return o; } | ||||
| function pad0(v,d){var t=""+v; return t.length>=d?t:fill('0',d-t.length)+t;} | ||||
| function pad_(v,d){var t=""+v;return t.length>=d?t:fill(' ',d-t.length)+t;} | ||||
| function rpad_(v,d){var t=""+v; return t.length>=d?t:t+fill(' ',d-t.length);} | ||||
| function pad0r1(v,d){var t=""+Math.round(v); return t.length>=d?t:fill('0',d-t.length)+t;} | ||||
| function pad0r2(v,d){var t=""+v; return t.length>=d?t:fill('0',d-t.length)+t;} | ||||
| var p2_32 = Math.pow(2,32); | ||||
| function pad0r(v,d){if(v>p2_32||v<-p2_32) return pad0r1(v,d); var i = Math.round(v); return pad0r2(i,d); } | ||||
| function isgeneral(s, i) { return s.length >= 7 + i && (s.charCodeAt(i)|32) === 103 && (s.charCodeAt(i+1)|32) === 101 && (s.charCodeAt(i+2)|32) === 110 && (s.charCodeAt(i+3)|32) === 101 && (s.charCodeAt(i+4)|32) === 114 && (s.charCodeAt(i+5)|32) === 97 && (s.charCodeAt(i+6)|32) === 108; } | ||||
| /* Options */ | ||||
| var opts_fmt = { | ||||
|   date1904:0, | ||||
|   output:"", | ||||
|   WTF:false | ||||
| }; | ||||
| function fixopts(o){for(var y in opts_fmt) if(o[y]===undefined) o[y]=opts_fmt[y];} | ||||
| var opts_fmt = [ | ||||
|   ["date1904", 0], | ||||
|   ["output", ""], | ||||
|   ["WTF", false] | ||||
| ]; | ||||
| function fixopts(o){ | ||||
|   for(var y = 0; y != opts_fmt.length; ++y) if(o[opts_fmt[y][0]]===undefined) o[opts_fmt[y][0]]=opts_fmt[y][1]; | ||||
| } | ||||
| SSF.opts = opts_fmt; | ||||
| var table_fmt = { | ||||
|   0:  'General', | ||||
| @ -69,7 +78,7 @@ var months = [ | ||||
|   ['N', 'Nov', 'November'], | ||||
|   ['D', 'Dec', 'December'] | ||||
| ]; | ||||
| var frac = function frac(x, D, mixed) { | ||||
| function frac(x, D, mixed) { | ||||
|   var sgn = x < 0 ? -1 : 1; | ||||
|   var B = x * sgn; | ||||
|   var P_2 = 0, P_1 = 1, P = 0; | ||||
| @ -90,40 +99,58 @@ var frac = function frac(x, D, mixed) { | ||||
|   if(Q===0) throw "Unexpected state: "+P+" "+P_1+" "+P_2+" "+Q+" "+Q_1+" "+Q_2; | ||||
|   var q = Math.floor(sgn * P/Q); | ||||
|   return [q, sgn*P - q*Q, Q]; | ||||
| }; | ||||
| var general_fmt = function(v, opts) { | ||||
|   if(typeof v === 'boolean') return v ? "TRUE" : "FALSE"; | ||||
|   if(typeof v === 'number') { | ||||
|     var o, V = v < 0 ? -v : v; | ||||
|     if(V >= 0.1 && V < 1) o = v.toPrecision(9); | ||||
|     else if(V >= 0.01 && V < 0.1) o = v.toPrecision(8); | ||||
|     else if(V >= 0.001 && V < 0.01) o = v.toPrecision(7); | ||||
|     else if(V >= 0.0001 && V < 0.001) o = v.toPrecision(6); | ||||
|     else if(V >= Math.pow(10,10) && V < Math.pow(10,11)) o = v.toFixed(10).substr(0,12); | ||||
|     else if(V > Math.pow(10,-9) && V < Math.pow(10,11)) { | ||||
|       o = v.toFixed(12).replace(/(\.[0-9]*[1-9])0*$/,"$1").replace(/\.$/,""); | ||||
|       if(o.length > (v<0?12:11)) o = v.toPrecision(10); | ||||
|       if(o.length > (v<0?12:11)) o = v.toExponential(5); | ||||
|     } | ||||
|     else { | ||||
|       o = v.toFixed(11).replace(/(\.[0-9]*[1-9])0*$/,"$1"); | ||||
|       if(o.length > (v<0?12:11)) o = v.toPrecision(6); | ||||
|     } | ||||
|     o = o.replace(/(\.[0-9]*[1-9])0+e/,"$1e").replace(/\.0*e/,"e"); | ||||
|     return o.replace("e","E").replace(/\.0*$/,"").replace(/\.([0-9]*[^0])0*$/,".$1").replace(/(E[+-])([0-9])$/,"$1"+"0"+"$2"); | ||||
| } | ||||
| function general_fmt_int(v, opts) { return ""+v; } | ||||
| SSF._general_int = general_fmt_int; | ||||
| var general_fmt_num = (function make_general_fmt_num() { | ||||
| var gnr1 = /\.(\d*[1-9])0+$/, gnr2 = /\.0*$/, gnr4 = /\.(\d*[1-9])0+/, gnr5 = /\.0*[Ee]/, gnr6 = /(E[+-])(\d)$/; | ||||
| function gfn2(v) { | ||||
|   var w = (v<0?12:11); | ||||
|   var o = gfn5(v.toFixed(12)); if(o.length <= w) return o; | ||||
|   o = v.toPrecision(10); if(o.length <= w) return o; | ||||
|   return v.toExponential(5); | ||||
| } | ||||
| function gfn3(v) { | ||||
|   var o = v.toFixed(11).replace(gnr1,".$1"); | ||||
|   if(o.length > (v<0?12:11)) o = v.toPrecision(6); | ||||
|   return o; | ||||
| } | ||||
| function gfn4(o) { | ||||
|   for(var i = 0; i != o.length; ++i) if((o.charCodeAt(i) | 0x20) === 101) return o.replace(gnr4,".$1").replace(gnr5,"E").replace("e","E").replace(gnr6,"$10$2"); | ||||
|   return o; | ||||
| } | ||||
| function gfn5(o) { | ||||
|   //for(var i = 0; i != o.length; ++i) if(o.charCodeAt(i) === 46) return o.replace(gnr2,"").replace(gnr1,".$1");
 | ||||
|   //return o;
 | ||||
|   return o.indexOf(".") > -1 ? o.replace(gnr2,"").replace(gnr1,".$1") : o; | ||||
| } | ||||
| return function general_fmt_num(v, opts) { | ||||
|   var V = Math.floor(Math.log(Math.abs(v))*Math.LOG10E), o; | ||||
|   if(V >= -4 && V <= -1) o = v.toPrecision(10+V); | ||||
|   else if(Math.abs(V) <= 9) o = gfn2(v); | ||||
|   else if(V === 10) o = v.toFixed(10).substr(0,12); | ||||
|   else o = gfn3(v); | ||||
|   return gfn5(gfn4(o)); | ||||
| };})(); | ||||
| SSF._general_num = general_fmt_num; | ||||
| function general_fmt(v, opts) { | ||||
|   switch(typeof v) { | ||||
|     case 'string': return v; | ||||
|     case 'boolean': return v ? "TRUE" : "FALSE"; | ||||
|     case 'number': return (v|0) === v ? general_fmt_int(v, opts) : general_fmt_num(v, opts); | ||||
|   } | ||||
|   if(typeof v === 'string') return v; | ||||
|   throw new Error("unsupported value in General format: " + v); | ||||
| }; | ||||
| } | ||||
| SSF._general = general_fmt; | ||||
| function fix_hijri(date, o) { return 0; } | ||||
| var parse_date_code = function parse_date_code(v,opts,b2) { | ||||
|   var date = Math.floor(v), time = Math.floor(86400 * (v - date)+1e-6), dow=0; | ||||
| function parse_date_code(v,opts,b2) { | ||||
|   if(v > 2958465 || v < 0) return null; | ||||
|   var date = (v|0), time = Math.floor(86400 * (v - date)), dow=0; | ||||
|   var dout=[]; | ||||
|   var out={D:date, T:time, u:86400*(v-date)-time,y:0,m:0,d:0,H:0,M:0,S:0,q:0}; | ||||
|   fixopts(opts = (opts||{})); | ||||
|   if(Math.abs(out.u) < 1e-6) out.u = 0; | ||||
|   fixopts(opts != null ? opts : (opts=[])); | ||||
|   if(opts.date1904) date += 1462; | ||||
|   if(date > 2958465) return null; | ||||
|   if(out.u > 0.999) { | ||||
|     out.u = 0; | ||||
|     if(++time == 86400) { time = 0; ++date; } | ||||
| @ -146,223 +173,374 @@ var parse_date_code = function parse_date_code(v,opts,b2) { | ||||
|   out.H = time; | ||||
|   out.q = dow; | ||||
|   return out; | ||||
| }; | ||||
| } | ||||
| SSF.parse_date_code = parse_date_code; | ||||
| /*jshint -W086 */ | ||||
| var write_date = function(type, fmt, val, ss0) { | ||||
|   var o, ss, tt, y = val.y, sss0; | ||||
| function write_date(type, fmt, val, ss0) { | ||||
|   var o="", ss=0, tt=0, y = val.y, out, outl = 0; | ||||
|   switch(type) { | ||||
|     case 'b': y = val.y + 543; | ||||
|     case 98: /* 'b' buddhist year */ | ||||
|       y = val.y + 543; | ||||
|       /* falls through */ | ||||
|     case 'y': switch(fmt.length) { /* year */ | ||||
|       case 1: case 2: return pad(y % 100,2); | ||||
|       default: return pad(y % 10000,4); | ||||
|     } | ||||
|     case 'm': switch(fmt) { | ||||
|       case 'm': return val.m; | ||||
|       case 'mm': return pad(val.m,2); | ||||
|       case 'mmm': return months[val.m-1][1]; | ||||
|       case 'mmmmm': return months[val.m-1][0]; | ||||
|     case 121: /* 'y' year */ | ||||
|     switch(fmt.length) { | ||||
|       case 1: case 2: out = y % 100; outl = 2; break; | ||||
|       default: out = y % 10000; outl = 4; break; | ||||
|     } break; | ||||
|     case 109: /* 'm' month */ | ||||
|     switch(fmt.length) { | ||||
|       case 1: case 2: out = val.m; outl = fmt.length; break; | ||||
|       case 3: return months[val.m-1][1]; | ||||
|       case 5: return months[val.m-1][0]; | ||||
|       default: return months[val.m-1][2]; | ||||
|     } | ||||
|     case 'd': switch(fmt) { /* day */ | ||||
|       case 'd': return val.d; | ||||
|       case 'dd': return pad(val.d,2); | ||||
|       case 'ddd': return days[val.q][0]; | ||||
|     } break; | ||||
|     case 100: /* 'd' day */ | ||||
|     switch(fmt.length) { | ||||
|       case 1: case 2: out = val.d; outl = fmt.length; break; | ||||
|       case 3: return days[val.q][0]; | ||||
|       default: return days[val.q][1]; | ||||
|     } | ||||
|     case 'h': switch(fmt) { /* 12-hour */ | ||||
|       case 'h': return 1+(val.H+11)%12; | ||||
|       case 'hh': return pad(1+(val.H+11)%12, 2); | ||||
|     } break; | ||||
|     case 104: /* 'h' 12-hour */ | ||||
|     switch(fmt.length) { | ||||
|       case 1: case 2: out = 1+(val.H+11)%12; outl = fmt.length; break; | ||||
|       default: throw 'bad hour format: ' + fmt; | ||||
|     } | ||||
|     case 'H': switch(fmt) { /* 24-hour */ | ||||
|       case 'h': return val.H; | ||||
|       case 'hh': return pad(val.H, 2); | ||||
|     } break; | ||||
|     case 72: /* 'H' 24-hour */ | ||||
|     switch(fmt.length) { | ||||
|       case 1: case 2: out = val.H; outl = fmt.length; break; | ||||
|       default: throw 'bad hour format: ' + fmt; | ||||
|     } | ||||
|     case 'M': switch(fmt) { /* minutes */ | ||||
|       case 'm': return val.M; | ||||
|       case 'mm': return pad(val.M, 2); | ||||
|     } break; | ||||
|     case 77: /* 'M' minutes */ | ||||
|     switch(fmt.length) { | ||||
|       case 1: case 2: out = val.M; outl = fmt.length; break; | ||||
|       default: throw 'bad minute format: ' + fmt; | ||||
|     } break; | ||||
|     case 115: /* 's' seconds */ | ||||
|     if(val.u === 0) switch(fmt) { | ||||
|       case 's': case 'ss': return pad0(val.S, fmt.length); | ||||
|       case '.0': case '.00': case '.000': | ||||
|     } | ||||
|     case 's': switch(fmt) { /* seconds */ | ||||
|     switch(fmt) { | ||||
|       case 's': case 'ss': case '.0': case '.00': case '.000': | ||||
|         sss0 = ss0 || 0; | ||||
|         tt = Math.pow(10,sss0); | ||||
|         if(ss0 >= 2) tt = ss0 === 3 ? 1000 : 100; | ||||
|         else tt = ss0 === 1 ? 10 : 1; | ||||
|         ss = Math.round((tt)*(val.S + val.u)); | ||||
|         if(fmt === 's') return ss >= 60*tt ? 0 : ss/tt; | ||||
|         else if(fmt === 'ss') { if(ss>=60*tt) ss=0; return pad(ss,(2+sss0)).substr(0,2); } | ||||
|         if(ss >= 60*tt) ss = 0; | ||||
|         o = pad(ss,2 + sss0); | ||||
|         if(fmt === 's') return ss === 0 ? "0" : ""+ss/tt; | ||||
|         o = pad0(ss,2 + ss0); | ||||
|         if(fmt === 'ss') return o.substr(0,2); | ||||
|         return "." + o.substr(2,fmt.length-1); | ||||
|       default: throw 'bad second format: ' + fmt; | ||||
|     } | ||||
|     case 'Z': switch(fmt) { | ||||
|       case '[h]': case '[hh]': o = val.D*24+val.H; break; | ||||
|       case '[m]': case '[mm]': o = (val.D*24+val.H)*60+val.M; break; | ||||
|       case '[s]': case '[ss]': o = ((val.D*24+val.H)*60+val.M)*60+Math.round(val.S+val.u); break; | ||||
|     case 90: /* 'Z' absolute time */ | ||||
|     switch(fmt) { | ||||
|       case '[h]': case '[hh]': out = val.D*24+val.H; break; | ||||
|       case '[m]': case '[mm]': out = (val.D*24+val.H)*60+val.M; break; | ||||
|       case '[s]': case '[ss]': out = ((val.D*24+val.H)*60+val.M)*60+Math.round(val.S+val.u); break; | ||||
|       default: throw 'bad abstime format: ' + fmt; | ||||
|     } return fmt.length === 3 ? o : pad(o, 2); | ||||
|     case 'e': { return val.y; } break; | ||||
|     } outl = fmt.length === 3 ? 1 : 2; break; | ||||
|     case 101: /* 'e' era */ | ||||
|       out = y; outl = 1; | ||||
|   } | ||||
| }; | ||||
|   if(outl > 0) return pad0(out, outl); else return ""; | ||||
| } | ||||
| /*jshint +W086 */ | ||||
| var commaify = function(s) { return _strrev(_strrev(s).replace(/.../g,"$&,")).replace(/^,/,""); }; | ||||
| var write_num = function(type, fmt, val) { | ||||
|   if(type === '(' && !fmt.match(/\).*[0#]/)) { | ||||
|     var ffmt = fmt.replace(/\( */,"").replace(/ \)/,"").replace(/\)/,""); | ||||
|     if(val >= 0) return write_num('n', ffmt, val); | ||||
|     return '(' + write_num('n', ffmt, -val) + ')'; | ||||
|   } | ||||
|   var mul = 0, o; | ||||
|   fmt = fmt.replace(/%/g,function() { 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$/)) { | ||||
|       var period = fmt.indexOf("."); if(period === -1) period=fmt.indexOf('E'); | ||||
|       var ee = (Number(val.toExponential(0).substr(2+(val<0?1:0))))%period; | ||||
|       if(ee < 0) ee += period; | ||||
|       o = (val/Math.pow(10,ee)).toPrecision(idx+1+(period+ee)%period); | ||||
|       if(!o.match(/[Ee]/)) { | ||||
|         var fakee = (Number(val.toExponential(0).substr(2+(val<0?1:0)))); | ||||
|         if(o.indexOf(".") === -1) o = o[0] + "." + o.substr(1) + "E+" + (fakee - o.length+ee); | ||||
|         else o += "E+" + (fakee - ee); | ||||
|         while(o.substr(0,2) === "0.") { | ||||
|           o = o[0] + o.substr(2,period) + "." + o.substr(2+period); | ||||
|           o = o.replace(/^0+([1-9])/,"$1").replace(/^0+\./,"0."); | ||||
|         } | ||||
|         o = o.replace(/\+-/,"-"); | ||||
| function commaify(s) { | ||||
|   if(s.length <= 3) return s; | ||||
|   var j = (s.length % 3), o = s.substr(0,j); | ||||
|   for(; j!=s.length; j+=3) o+=(o.length > 0 ? "," : "") + s.substr(j,3); | ||||
|   return o; | ||||
| } | ||||
| var write_num = (function make_write_num(){ | ||||
| var pct1 = /%/g; | ||||
| function write_num_pct(type, fmt, val){ | ||||
|   var sfmt = fmt.replace(pct1,""), mul = fmt.length - sfmt.length; | ||||
|   return write_num(type, sfmt, val * Math.pow(10,2*mul)) + fill("%",mul); | ||||
| } | ||||
| function write_num_cm(type, fmt, val){ | ||||
|   var idx = fmt.length - 1; | ||||
|   while(fmt.charCodeAt(idx-1) === 44) --idx; | ||||
|   return write_num(type, fmt.substr(0,idx), val / Math.pow(10,3*(fmt.length-idx))); | ||||
| } | ||||
| function write_num_exp(fmt, val){ | ||||
|   var o; | ||||
|   var idx = fmt.indexOf("E") - fmt.indexOf(".") - 1; | ||||
|   if(fmt.match(/^#+0.0E\+0$/)) { | ||||
|     var period = fmt.indexOf("."); if(period === -1) period=fmt.indexOf('E'); | ||||
|     var ee = Math.floor(Math.log(Math.abs(val))*Math.LOG10E)%period; | ||||
|     if(ee < 0) ee += period; | ||||
|     o = (val/Math.pow(10,ee)).toPrecision(idx+1+(period+ee)%period); | ||||
|     if(o.indexOf("e") === -1) { | ||||
|       var fakee = Math.floor(Math.log(Math.abs(val))*Math.LOG10E); | ||||
|       if(o.indexOf(".") === -1) o = o[0] + "." + o.substr(1) + "E+" + (fakee - o.length+ee); | ||||
|       else o += "E+" + (fakee - ee); | ||||
|       while(o.substr(0,2) === "0.") { | ||||
|         o = o[0] + o.substr(2,period) + "." + o.substr(2+period); | ||||
|         o = o.replace(/^0+([1-9])/,"$1").replace(/^0+\./,"0."); | ||||
|       } | ||||
|       o = o.replace(/^([+-]?)([0-9]*)\.([0-9]*)[Ee]/,function($$,$1,$2,$3) { return $1 + $2 + $3.substr(0,(period+ee)%period) + "." + $3.substr(ee) + "E"; }); | ||||
|     } else o = val.toExponential(idx); | ||||
|     if(fmt.match(/E\+00$/) && o.match(/e[+-][0-9]$/)) o = o.substr(0,o.length-1) + "0" + o[o.length-1]; | ||||
|     if(fmt.match(/E\-/) && o.match(/e\+/)) o = o.replace(/e\+/,"e"); | ||||
|     return o.replace("e","E"); | ||||
|       o = o.replace(/\+-/,"-"); | ||||
|     } | ||||
|     o = o.replace(/^([+-]?)(\d*)\.(\d*)[Ee]/,function($$,$1,$2,$3) { return $1 + $2 + $3.substr(0,(period+ee)%period) + "." + $3.substr(ee) + "E"; }); | ||||
|   } else o = val.toExponential(idx); | ||||
|   if(fmt.match(/E\+00$/) && o.match(/e[+-]\d$/)) o = o.substr(0,o.length-1) + "0" + o[o.length-1]; | ||||
|   if(fmt.match(/E\-/) && o.match(/e\+/)) o = o.replace(/e\+/,"e"); | ||||
|   return o.replace("e","E"); | ||||
| } | ||||
| var frac1 = /# (\?+)( ?)\/( ?)(\d+)/; | ||||
| function write_num_f1(r, aval, sign) { | ||||
|   var den = parseInt(r[4]), rr = Math.round(aval * den), base = Math.floor(rr/den); | ||||
|   var myn = (rr - base*den), myd = den; | ||||
|   return sign + (base === 0 ? "" : ""+base) + " " + (myn === 0 ? fill(" ", r[1].length + 1 + r[4].length) : pad_(myn,r[1].length) + r[2] + "/" + r[3] + pad0(myd,r[4].length)); | ||||
| } | ||||
| function write_num_f2(r, aval, sign) { | ||||
|   return sign + (aval === 0 ? "" : ""+aval) + fill(" ", r[1].length + 2 + r[4].length); | ||||
| } | ||||
| var dec1 = /^#*0*\.(0+)/; | ||||
| var closeparen = /\).*[0#]/; | ||||
| var phone = /\(###\) ###\\?-####/; | ||||
| function hashq(str) { | ||||
|   var o = "", cc; | ||||
|   for(var i = 0; i != str.length; ++i) switch((cc=str.charCodeAt(i))) { | ||||
|     case 35: break; | ||||
|     case 63: o+= " "; break; | ||||
|     case 48: o+= "0"; break; | ||||
|     default: o+= String.fromCharCode(cc); | ||||
|   } | ||||
|   if(fmt[0] === "$") return "$"+write_num(type,fmt.substr(fmt[1]==' '?2:1),val); | ||||
|   var r, rr, ff, aval = val < 0 ? -val : val, sign = val < 0 ? "-" : ""; | ||||
|   if((r = fmt.match(/# (\?+)([ ]?)\/([ ]?)(\d+)/))) { | ||||
|     var den = Number(r[4]), rnd = Math.round(aval * den), base = Math.floor(rnd/den); | ||||
|     var myn = (rnd - base*den), myd = den; | ||||
|     return sign + String(base||"") + " " + (myn === 0 ? fill(" ", r[1].length + 1 + r[4].length) : pad(myn,r[1].length," ") + r[2] + "/" + r[3] + pad(myd,r[4].length)); | ||||
|   return o; | ||||
| } | ||||
| function rnd(val, d) { var dd = Math.pow(10,d); return ""+(Math.round(val * dd)/dd); } | ||||
| function dec(val, d) { return Math.round((val-Math.floor(val))*Math.pow(10,d)); } | ||||
| function flr(val) { if(val < 2147483647 && val > -2147483648) return ""+(val >= 0 ? (val|0) : (val-1|0)); return ""+Math.floor(val); } | ||||
| function write_num_flt(type, fmt, val) { | ||||
|   if(type.charCodeAt(0) === 40 && !fmt.match(closeparen)) { | ||||
|     var ffmt = fmt.replace(/\( */,"").replace(/ \)/,"").replace(/\)/,""); | ||||
|     if(val >= 0) return write_num_flt('n', ffmt, val); | ||||
|     return '(' + write_num_flt('n', ffmt, -val) + ')'; | ||||
|   } | ||||
|   if(fmt.match(/^#+0+$/)) fmt = fmt.replace(/#/g,""); | ||||
|   if(fmt.match(/^00+$/)) return (val<0?"-":"")+pad(Math.round(aval),fmt.length); | ||||
|   if(fmt.charCodeAt(fmt.length - 1) === 44) return write_num_cm(type, fmt, val); | ||||
|   if(fmt.indexOf('%') !== -1) return write_num_pct(type, fmt, val); | ||||
|   if(fmt.indexOf('E') !== -1) return write_num_exp(fmt, val); | ||||
|   if(fmt.charCodeAt(0) === 36) return "$"+write_num_flt(type,fmt.substr(fmt[1]==' '?2:1),val); | ||||
|   var o, oo; | ||||
|   var r, ri, ff, aval = Math.abs(val), sign = val < 0 ? "-" : ""; | ||||
|   if(fmt.match(/^00+$/)) return sign + pad0r(aval,fmt.length); | ||||
|   if(fmt.match(/^[#?]+$/)) { | ||||
|     o = String(Math.round(val)).replace(/^0$/,""); | ||||
|     return o.length > fmt.length ? o : fmt.substr(0,fmt.length-o.length).replace(/#/g,"").replace(/[?]/g," ") + o; | ||||
|     o = pad0r(val,0); if(o === "0") o = ""; | ||||
|     return o.length > fmt.length ? o : hashq(fmt.substr(0,fmt.length-o.length)) + o; | ||||
|   } | ||||
|   if((r = fmt.match(/^#*0*\.(0+)/))) { | ||||
|     o = Math.round(val * Math.pow(10,r[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\./,"."); | ||||
|   if((r = fmt.match(frac1)) !== null) return write_num_f1(r, aval, sign); | ||||
|   if(fmt.match(/^#+0+$/) !== null) return sign + pad0r(aval,fmt.length - fmt.indexOf("0")); | ||||
|   if((r = fmt.match(dec1)) !== null) { | ||||
|     o = rnd(val, r[1].length).replace(/^([^\.]+)$/,"$1."+r[1]).replace(/\.$/,"."+r[1]).replace(/\.(\d*)$/,function($$, $1) { return "." + $1 + fill("0", r[1].length-$1.length); }); | ||||
|     return fmt.indexOf("0.") !== -1 ? o : o.replace(/^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.":"."); | ||||
|   if((r = fmt.match(/^(0*)\.(#*)$/)) !== null) { | ||||
|     return sign + rnd(aval, r[2].length).replace(/\.(\d*[1-9])0*$/,".$1").replace(/^(-?\d*)$/,"$1.").replace(/^0\./,r[1].length?"0.":"."); | ||||
|   } | ||||
|   if((r = fmt.match(/^#,##0([.]?)$/))) return sign + commaify(String(Math.round(aval))); | ||||
|   if((r = fmt.match(/^#,##0\.([#0]*0)$/))) { | ||||
|     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(\.?)$/)) !== null) return sign + commaify(pad0r(aval,0)); | ||||
|   if((r = fmt.match(/^#,##0\.([#0]*0)$/)) !== null) { | ||||
|     return val < 0 ? "-" + write_num_flt(type, fmt, -val) : commaify(""+(Math.floor(val))) + "." + pad0(dec(val, r[1].length),r[1].length); | ||||
|   } | ||||
|   if((r = fmt.match(/^#,#*,#0/))) return write_num(type,fmt.replace(/^#,#*,/,""),val); | ||||
|   if((r = fmt.match(/^([0#]+)(\\?-([0#]+))+$/))) { | ||||
|     ff = _strrev(write_num(type, fmt.replace(/[\\-]/g,""), val)); | ||||
|     rr = 0; | ||||
|     return _strrev(_strrev(fmt.replace(/\\/g,"")).replace(/[0#]/g,function(x){return rr<ff.length?ff[rr++]:x==='0'?'0':'';})); | ||||
|   if((r = fmt.match(/^#,#*,#0/)) !== null) return write_num_flt(type,fmt.replace(/^#,#*,/,""),val); | ||||
|   if((r = fmt.match(/^([0#]+)(\\?-([0#]+))+$/)) !== null) { | ||||
|     o = _strrev(write_num_flt(type, fmt.replace(/[\\-]/g,""), val)); | ||||
|     ri = 0; | ||||
|     return _strrev(_strrev(fmt.replace(/\\/g,"")).replace(/[0#]/g,function(x){return ri<o.length?o[ri++]:x==='0'?'0':"";})); | ||||
|   } | ||||
|   if(fmt.match(/\(###\) ###\\?-####/)) { | ||||
|     ff = write_num(type, "##########", val); | ||||
|     return "(" + ff.substr(0,3) + ") " + ff.substr(3, 3) + "-" + ff.substr(6); | ||||
|   if(fmt.match(phone) !== null) { | ||||
|     o = write_num_flt(type, "##########", val); | ||||
|     return "(" + o.substr(0,3) + ") " + o.substr(3, 3) + "-" + o.substr(6); | ||||
|   } | ||||
|   var oa = ""; | ||||
|   if((r = fmt.match(/^([#0?]+)([ ]?)\/([ ]?)([#0?]+)/))) { | ||||
|     o=""; | ||||
|     rr = Math.min(r[4].length,7); | ||||
|     ff = frac(aval, Math.pow(10,rr)-1, false); | ||||
|     o += sign; | ||||
|   if((r = fmt.match(/^([#0?]+)( ?)\/( ?)([#0?]+)/)) !== null) { | ||||
|     ri = Math.min(r[4].length,7); | ||||
|     ff = frac(aval, Math.pow(10,ri)-1, false); | ||||
|     o = "" + sign; | ||||
|     oa = write_num("n", r[1], ff[1]); | ||||
|     if(oa[oa.length-1] == " ") oa = oa.substr(0,oa.length-1) + "0"; | ||||
|     o += oa; | ||||
|     o += r[2]; | ||||
|     o += "/"; | ||||
|     o += r[3]; | ||||
|     oa = rpad(ff[2],rr," "); | ||||
|     if(oa.length < r[4].length) oa = r[4].substr(r[4].length-oa.length).replace(/[?]/g," ").replace(/#/g,"") + oa; | ||||
|     o += oa + r[2] + "/" + r[3]; | ||||
|     oa = rpad_(ff[2],ri); | ||||
|     if(oa.length < r[4].length) oa = hashq(r[4].substr(r[4].length-oa.length)) + oa; | ||||
|     o += oa; | ||||
|     return o; | ||||
|   } | ||||
|   if((r = fmt.match(/^# ([#0?]+)([ ]?)\/([ ]?)([#0?]+)/))) { | ||||
|     rr = Math.min(Math.max(r[1].length, r[4].length),7); | ||||
|     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(/^# ([#0?]+)( ?)\/( ?)([#0?]+)/)) !== null) { | ||||
|     ri = Math.min(Math.max(r[1].length, r[4].length),7); | ||||
|     ff = frac(aval, Math.pow(10,ri)-1, true); | ||||
|     return sign + (ff[0]||(ff[1] ? "" : "0")) + " " + (ff[1] ? pad_(ff[1],ri) + r[2] + "/" + r[3] + rpad_(ff[2],ri): fill(" ", 2*ri+1 + r[2].length + r[3].length)); | ||||
|   } | ||||
|   if((r = fmt.match(/^[#0?]+$/))) { | ||||
|     o = "" + Math.round(val); | ||||
|   if((r = fmt.match(/^[#0?]+$/)) !== null) { | ||||
|     o = pad0r(val, 0); | ||||
|     if(fmt.length <= o.length) return o; | ||||
|     return fmt.substr(0,fmt.length-o.length).replace(/#/g,"").replace(/\?/g," ") + o; | ||||
|     return hashq(fmt.substr(0,fmt.length-o.length)) + o; | ||||
|   } | ||||
|   if((r = fmt.match(/^([#0]+)\.([#0]+)$/))) { | ||||
|   if((r = fmt.match(/^([#0]+)\.([#0]+)$/)) !== null) { | ||||
|     o = "" + val.toFixed(Math.min(r[2].length,10)).replace(/([^0])0+$/,"$1"); | ||||
|     rr = o.indexOf("."); | ||||
|     var lres = fmt.indexOf(".") - rr, rres = fmt.length - o.length - lres; | ||||
|     return fmt.substr(0,lres).replace(/#/g,"") + o + fmt.substr(fmt.length-rres).replace(/#/g,""); | ||||
|     ri = o.indexOf("."); | ||||
|     var lres = fmt.indexOf(".") - ri, rres = fmt.length - o.length - lres; | ||||
|     return hashq(fmt.substr(0,lres) + o + fmt.substr(fmt.length-rres)); | ||||
|   } | ||||
|   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); | ||||
|   if((r = fmt.match(/^00,000\.([#0]*0)$/)) !== null) { | ||||
|     ri = dec(val, r[1].length); | ||||
|     return val < 0 ? "-" + write_num_flt(type, fmt, -val) : commaify(flr(val)).replace(/^\d,\d{3}$/,"0$&").replace(/^\d*$/,function($$) { return "00," + ($$.length < 3 ? pad0(0,3-$$.length) : "") + $$; }) + "." + pad0(ri,r[1].length); | ||||
|   } | ||||
|   switch(fmt) { | ||||
|     case "#,###": var x = commaify(String(Math.round(aval))); return x !== "0" ? sign + x : ""; | ||||
|     case "#,###": var x = commaify(pad0r(aval,0)); return x !== "0" ? sign + x : ""; | ||||
|     default: | ||||
|   } | ||||
|   throw new Error("unsupported format |" + fmt + "|"); | ||||
| }; | ||||
| } | ||||
| function write_num_cm2(type, fmt, val){ | ||||
|   var idx = fmt.length - 1; | ||||
|   while(fmt.charCodeAt(idx-1) === 44) --idx; | ||||
|   return write_num(type, fmt.substr(0,idx), val / Math.pow(10,3*(fmt.length-idx))); | ||||
| } | ||||
| function write_num_pct2(type, fmt, val){ | ||||
|   var sfmt = fmt.replace(pct1,""), mul = fmt.length - sfmt.length; | ||||
|   return write_num(type, sfmt, val * Math.pow(10,2*mul)) + fill("%",mul); | ||||
| } | ||||
| function write_num_exp2(fmt, val){ | ||||
|   var o; | ||||
|   var idx = fmt.indexOf("E") - fmt.indexOf(".") - 1; | ||||
|   if(fmt.match(/^#+0.0E\+0$/)) { | ||||
|     var period = fmt.indexOf("."); if(period === -1) period=fmt.indexOf('E'); | ||||
|     var ee = Math.floor(Math.log(Math.abs(val))*Math.LOG10E)%period; | ||||
|     if(ee < 0) ee += period; | ||||
|     o = (val/Math.pow(10,ee)).toPrecision(idx+1+(period+ee)%period); | ||||
|     if(!o.match(/[Ee]/)) { | ||||
|       var fakee = Math.floor(Math.log(Math.abs(val))*Math.LOG10E); | ||||
|       if(o.indexOf(".") === -1) o = o[0] + "." + o.substr(1) + "E+" + (fakee - o.length+ee); | ||||
|       else o += "E+" + (fakee - ee); | ||||
|       o = o.replace(/\+-/,"-"); | ||||
|     } | ||||
|     o = o.replace(/^([+-]?)(\d*)\.(\d*)[Ee]/,function($$,$1,$2,$3) { return $1 + $2 + $3.substr(0,(period+ee)%period) + "." + $3.substr(ee) + "E"; }); | ||||
|   } else o = val.toExponential(idx); | ||||
|   if(fmt.match(/E\+00$/) && o.match(/e[+-]\d$/)) o = o.substr(0,o.length-1) + "0" + o[o.length-1]; | ||||
|   if(fmt.match(/E\-/) && o.match(/e\+/)) o = o.replace(/e\+/,"e"); | ||||
|   return o.replace("e","E"); | ||||
| } | ||||
| function write_num_int(type, fmt, val) { | ||||
|   if(type.charCodeAt(0) === 40 && !fmt.match(closeparen)) { | ||||
|     var ffmt = fmt.replace(/\( */,"").replace(/ \)/,"").replace(/\)/,""); | ||||
|     if(val >= 0) return write_num_int('n', ffmt, val); | ||||
|     return '(' + write_num_int('n', ffmt, -val) + ')'; | ||||
|   } | ||||
|   if(fmt.charCodeAt(fmt.length - 1) === 44) return write_num_cm2(type, fmt, val); | ||||
|   if(fmt.indexOf('%') !== -1) return write_num_pct2(type, fmt, val); | ||||
|   if(fmt.indexOf('E') !== -1) return write_num_exp2(fmt, val); | ||||
|   if(fmt.charCodeAt(0) === 36) return "$"+write_num_int(type,fmt.substr(fmt[1]==' '?2:1),val); | ||||
|   var o; | ||||
|   var r, ri, ff, aval = Math.abs(val), sign = val < 0 ? "-" : ""; | ||||
|   if(fmt.match(/^00+$/)) return sign + pad0(aval,fmt.length); | ||||
|   if(fmt.match(/^[#?]+$/)) { | ||||
|     o = (""+val); if(val === 0) o = ""; | ||||
|     return o.length > fmt.length ? o : hashq(fmt.substr(0,fmt.length-o.length)) + o; | ||||
|   } | ||||
|   if((r = fmt.match(frac1)) !== null) return write_num_f2(r, aval, sign); | ||||
|   if(fmt.match(/^#+0+$/) !== null) return sign + pad0(aval,fmt.length - fmt.indexOf("0")); | ||||
|   if((r = fmt.match(dec1)) !== null) { | ||||
|     o = (""+val).replace(/^([^\.]+)$/,"$1."+r[1]).replace(/\.$/,"."+r[1]).replace(/\.(\d*)$/,function($$, $1) { return "." + $1 + fill("0", r[1].length-$1.length); }); | ||||
|     return fmt.indexOf("0.") !== -1 ? o : o.replace(/^0\./,"."); | ||||
|   } | ||||
|   fmt = fmt.replace(/^#+([0.])/, "$1"); | ||||
|   if((r = fmt.match(/^(0*)\.(#*)$/)) !== null) { | ||||
|     return sign + (""+aval).replace(/\.(\d*[1-9])0*$/,".$1").replace(/^(-?\d*)$/,"$1.").replace(/^0\./,r[1].length?"0.":"."); | ||||
|   } | ||||
|   if((r = fmt.match(/^#,##0(\.?)$/)) !== null) return sign + commaify((""+aval)); | ||||
|   if((r = fmt.match(/^#,##0\.([#0]*0)$/)) !== null) { | ||||
|     return val < 0 ? "-" + write_num_int(type, fmt, -val) : commaify((""+val)) + "." + fill('0',r[1].length); | ||||
|   } | ||||
|   if((r = fmt.match(/^#,#*,#0/)) !== null) return write_num_int(type,fmt.replace(/^#,#*,/,""),val); | ||||
|   if((r = fmt.match(/^([0#]+)(\\?-([0#]+))+$/)) !== null) { | ||||
|     o = _strrev(write_num_int(type, fmt.replace(/[\\-]/g,""), val)); | ||||
|     ri = 0; | ||||
|     return _strrev(_strrev(fmt.replace(/\\/g,"")).replace(/[0#]/g,function(x){return ri<o.length?o[ri++]:x==='0'?'0':"";})); | ||||
|   } | ||||
|   if(fmt.match(phone) !== null) { | ||||
|     o = write_num_int(type, "##########", val); | ||||
|     return "(" + o.substr(0,3) + ") " + o.substr(3, 3) + "-" + o.substr(6); | ||||
|   } | ||||
|   var oa = ""; | ||||
|   if((r = fmt.match(/^([#0?]+)( ?)\/( ?)([#0?]+)/)) !== null) { | ||||
|     ri = Math.min(r[4].length,7); | ||||
|     ff = frac(aval, Math.pow(10,ri)-1, false); | ||||
|     o = "" + sign; | ||||
|     oa = write_num("n", r[1], ff[1]); | ||||
|     if(oa[oa.length-1] == " ") oa = oa.substr(0,oa.length-1) + "0"; | ||||
|     o += oa + r[2] + "/" + r[3]; | ||||
|     oa = rpad_(ff[2],ri); | ||||
|     if(oa.length < r[4].length) oa = hashq(r[4].substr(r[4].length-oa.length)) + oa; | ||||
|     o += oa; | ||||
|     return o; | ||||
|   } | ||||
|   if((r = fmt.match(/^# ([#0?]+)( ?)\/( ?)([#0?]+)/)) !== null) { | ||||
|     ri = Math.min(Math.max(r[1].length, r[4].length),7); | ||||
|     ff = frac(aval, Math.pow(10,ri)-1, true); | ||||
|     return sign + (ff[0]||(ff[1] ? "" : "0")) + " " + (ff[1] ? pad_(ff[1],ri) + r[2] + "/" + r[3] + rpad_(ff[2],ri): fill(" ", 2*ri+1 + r[2].length + r[3].length)); | ||||
|   } | ||||
|   if((r = fmt.match(/^[#0?]+$/)) !== null) { | ||||
|     o = "" + val; | ||||
|     if(fmt.length <= o.length) return o; | ||||
|     return hashq(fmt.substr(0,fmt.length-o.length)) + o; | ||||
|   } | ||||
|   if((r = fmt.match(/^([#0]+)\.([#0]+)$/)) !== null) { | ||||
|     o = "" + val.toFixed(Math.min(r[2].length,10)).replace(/([^0])0+$/,"$1"); | ||||
|     ri = o.indexOf("."); | ||||
|     var lres = fmt.indexOf(".") - ri, rres = fmt.length - o.length - lres; | ||||
|     return hashq(fmt.substr(0,lres) + o + fmt.substr(fmt.length-rres)); | ||||
|   } | ||||
|   if((r = fmt.match(/^00,000\.([#0]*0)$/)) !== null) { | ||||
|     return val < 0 ? "-" + write_num_int(type, fmt, -val) : commaify(""+val).replace(/^\d,\d{3}$/,"0$&").replace(/^\d*$/,function($$) { return "00," + ($$.length < 3 ? pad0(0,3-$$.length) : "") + $$; }) + "." + pad0(0,r[1].length); | ||||
|   } | ||||
|   switch(fmt) { | ||||
|     case "#,###": var x = commaify(""+aval); return x !== "0" ? sign + x : ""; | ||||
|     default: | ||||
|   } | ||||
|   throw new Error("unsupported format |" + fmt + "|"); | ||||
| } | ||||
| return function write_num(type, fmt, val) { | ||||
|   return (val|0) === val ? write_num_int(type, fmt, val) : write_num_flt(type, fmt, val); | ||||
| };})(); | ||||
| function split_fmt(fmt) { | ||||
|   var out = []; | ||||
|   var in_str = -1; | ||||
|   for(var i = 0, j = 0; i < fmt.length; ++i) { | ||||
|     if(in_str != -1) { if(fmt[i] == '"') in_str = -1; continue; } | ||||
|     if(fmt[i] == "_" || fmt[i] == "*" || fmt[i] == "\\") { ++i; continue; } | ||||
|     if(fmt[i] == '"') { in_str = i; continue; } | ||||
|     if(fmt[i] != ";") continue; | ||||
|     out.push(fmt.slice(j,i)); | ||||
|     j = i+1; | ||||
|   var in_str = false, cc; | ||||
|   for(var i = 0, j = 0; i < fmt.length; ++i) switch((cc=fmt.charCodeAt(i))) { | ||||
|     case 34: /* '"' */ | ||||
|       in_str = !in_str; break; | ||||
|     case 95: case 42: case 92: /* '_' '*' '\\' */ | ||||
|       ++i; break; | ||||
|     case 59: /* ';' */ | ||||
|       out[out.length] = fmt.substr(j,i-j); | ||||
|       j = i+1; | ||||
|   } | ||||
|   out.push(fmt.slice(j)); | ||||
|   if(in_str !=-1) throw new Error("Format |" + fmt + "| unterminated string at " + in_str); | ||||
|   out[out.length] = fmt.substr(j); | ||||
|   if(in_str === true) throw new Error("Format |" + fmt + "| unterminated string "); | ||||
|   return out; | ||||
| } | ||||
| SSF._split = split_fmt; | ||||
| var abstime = /\[[HhMmSs]*\]/; | ||||
| function eval_fmt(fmt, v, opts, flen) { | ||||
|   var out = [], o = "", i = 0, c = "", lst='t', q, dt, j; | ||||
|   fixopts(opts = (opts || {})); | ||||
|   var out = [], o = "", i = 0, c = "", lst='t', q, dt, j, cc; | ||||
|   var hr='H'; | ||||
|   /* Tokenize */ | ||||
|   while(i < fmt.length) { | ||||
|     switch((c = fmt[i])) { | ||||
|       case 'G': /* General */ | ||||
|         if(fmt.substr(i, 7).toLowerCase() !== "general") | ||||
|           throw new Error('unrecognized character ' + fmt[i] + ' in ' +fmt); | ||||
|         out.push({t:'G',v:'General'}); i+=7; break; | ||||
|         if(!isgeneral(fmt, i)) throw new Error('unrecognized character ' + c + ' in ' +fmt); | ||||
|         out[out.length] = {t:'G', v:'General'}; i+=7; break; | ||||
|       case '"': /* Literal text */ | ||||
|         for(o="";fmt[++i] !== '"' && i < fmt.length;) o += fmt[i]; | ||||
|         out.push({t:'t', v:o}); ++i; break; | ||||
|       case '\\': var w = fmt[++i], t = "()".indexOf(w) === -1 ? 't' : w; | ||||
|         out.push({t:t, v:w}); ++i; break; | ||||
|       case '_': out.push({t:'t', v:" "}); i+=2; break; | ||||
|         for(o="";(cc=fmt.charCodeAt(++i)) !== 34 && i < fmt.length;) o += String.fromCharCode(cc); | ||||
|         out[out.length] = {t:'t', v:o}; ++i; break; | ||||
|       case '\\': var w = fmt[++i], t = (w === "(" || w === ")") ? w : 't'; | ||||
|         out[out.length] = {t:t, v:w}; ++i; break; | ||||
|       case '_': out[out.length] = {t:'t', v:" "}; i+=2; break; | ||||
|       case '@': /* Text Placeholder */ | ||||
|         out.push({t:'T', v:v}); ++i; break; | ||||
|         out[out.length] = {t:'T', v:v}; ++i; break; | ||||
|       case 'B': case 'b': | ||||
|         if(fmt[i+1] === "1" || fmt[i+1] === "2") { | ||||
|           if(!dt) dt = parse_date_code(v, opts, fmt[i+1] === "2"); | ||||
|           q={t:'X', v:fmt.substr(i,2)}; out.push(q); lst = c; i+=2; break; | ||||
|           if(dt == null) dt = parse_date_code(v, opts, fmt[i+1] === "2"); | ||||
|           out[out.length] = {t:'X', v:fmt.substr(i,2)}; lst = c; i+=2; break; | ||||
|         } | ||||
|         /* falls through */ | ||||
|       case 'M': case 'D': case 'Y': case 'H': case 'S': case 'E': | ||||
| @ -370,54 +548,50 @@ function eval_fmt(fmt, v, opts, flen) { | ||||
|         /* falls through */ | ||||
|       case 'm': case 'd': case 'y': case 'h': case 's': case 'e': case 'g': | ||||
|         if(v < 0) return ""; | ||||
|         if(!dt) dt = parse_date_code(v, opts); | ||||
|         if(!dt) return ""; | ||||
|         o = fmt[i]; while((fmt[++i]||"").toLowerCase() === c) o+=c; | ||||
|         if(dt==null) { dt=parse_date_code(v, opts); if(dt==null) return ""; } | ||||
|         o = c; while(++i<fmt.length && fmt[i].toLowerCase() === c) o+=c; | ||||
|         if(c === 'm' && lst.toLowerCase() === 'h') c = 'M'; /* m = minute */ | ||||
|         if(c === 'h') c = hr; | ||||
|         o = o.toLowerCase(); | ||||
|         q={t:c, v:o}; out.push(q); lst = c; break; | ||||
|         out[out.length] = {t:c, v:o}; lst = c; break; | ||||
|       case 'A': | ||||
|         if(!dt) dt = parse_date_code(v, opts); | ||||
|         if(!dt) return ""; | ||||
|         q={t:c,v:"A"}; | ||||
|         q={t:c, v:"A"}; | ||||
|         if(dt==null) dt=parse_date_code(v, opts); | ||||
|         if(fmt.substr(i, 3) === "A/P") {q.v = dt.H >= 12 ? "P" : "A"; q.t = 'T'; hr='h';i+=3;} | ||||
|         else if(fmt.substr(i,5) === "AM/PM") { q.v = dt.H >= 12 ? "PM" : "AM"; q.t = 'T'; i+=5; hr='h'; } | ||||
|         else { q.t = "t"; i++; } | ||||
|         out.push(q); lst = c; break; | ||||
|         else { q.t = "t"; ++i; } | ||||
|         if(dt==null && q.t === 'T') return ""; | ||||
|         out[out.length] = q; lst = c; break; | ||||
|       case '[': | ||||
|         o = c; | ||||
|         while(fmt[i++] !== ']' && i < fmt.length) o += fmt[i]; | ||||
|         if(o.substr(-1) !== ']') throw 'unterminated "[" block: |' + o + '|'; | ||||
|         if(o.match(/\[[HhMmSs]*\]/)) { | ||||
|           if(!dt) dt = parse_date_code(v, opts); | ||||
|           if(!dt) return ""; | ||||
|           out.push({t:'Z', v:o.toLowerCase()}); | ||||
|         if(o.match(abstime)) { | ||||
|           if(dt==null) { dt=parse_date_code(v, opts); if(dt==null) return ""; } | ||||
|           out[out.length] = {t:'Z', v:o.toLowerCase()}; | ||||
|         } else { o=""; } | ||||
|         break; | ||||
|       /* Numbers */ | ||||
|       case '.': | ||||
|         if(dt) { | ||||
|         if(dt != null) { | ||||
|           o = c; while((c=fmt[++i]) === "0") o += c; | ||||
|           out.push({t:'s', v:o}); break; | ||||
|           out[out.length] = {t:'s', v:o}; break; | ||||
|         } | ||||
|         /* falls through */ | ||||
|       case '0': case '#': | ||||
|         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; | ||||
|         out[out.length] = {t:'n', v:o}; break; | ||||
|       case '?': | ||||
|         o = fmt[i]; while(fmt[++i] === c) o+=c; | ||||
|         q={t:c, v:o}; out.push(q); lst = c; break; | ||||
|         o = c; while(fmt[++i] === c) o+=c; | ||||
|         q={t:c, v:o}; out[out.length] = q; lst = c; break; | ||||
|       case '*': ++i; if(fmt[i] == ' ' || fmt[i] == '*') ++i; break; // **
 | ||||
|       case '(': case ')': out.push({t:(flen===1?'t':c),v:c}); ++i; break; | ||||
|       case '(': case ')': out[out.length] = {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]; | ||||
|         out.push({t:'D', v:o}); break; | ||||
|       case ' ': out.push({t:c,v:c}); ++i; break; | ||||
|         o = c; while("0123456789".indexOf(fmt[++i]) > -1) o+=fmt[i]; | ||||
|         out[out.length] = {t:'D', v:o}; break; | ||||
|       case ' ': out[out.length] = {t:c, v:c}; ++i; break; | ||||
|       default: | ||||
|         if(",$-+/():!^&'~{}<>=€acfijklopqrtuvwxz".indexOf(c) === -1) | ||||
|           throw 'unrecognized character ' + fmt[i] + ' in ' + fmt; | ||||
|         out.push({t:'t', v:c}); ++i; break; | ||||
|         if(",$-+/():!^&'~{}<>=€acfijklopqrtuvwxz".indexOf(c) === -1) throw new Error('unrecognized character ' + c + ' in ' + fmt); | ||||
|         out[out.length] = {t:'t', v:c}; ++i; break; | ||||
|     } | ||||
|   } | ||||
|   var bt = 0, ss0 = 0, ssm; | ||||
| @ -455,130 +629,138 @@ function eval_fmt(fmt, v, opts, flen) { | ||||
|   for(i=0; i < out.length; ++i) { | ||||
|     switch(out[i].t) { | ||||
|       case 't': case 'T': case ' ': case 'D': break; | ||||
|       case 'X': delete out[i]; break; | ||||
|       case 'X': out[i] = undefined; break; | ||||
|       case 'd': case 'm': case 'y': case 'h': case 'H': case 'M': case 's': case 'e': case 'b': case 'Z': | ||||
|         out[i].v = write_date(out[i].t, out[i].v, dt, ss0); | ||||
|         out[i].v = write_date(out[i].t.charCodeAt(0), out[i].v, dt, ss0); | ||||
|         out[i].t = 't'; break; | ||||
|       case 'n': case '(': case '?': | ||||
|         jj = i+1; | ||||
|         while(out[jj] && ("?D".indexOf(out[jj].t) > -1 || (" t".indexOf(out[jj].t) > -1 && "?t".indexOf((out[jj+1]||{}).t)>-1 && (out[jj+1].t == '?' || out[jj+1].v == '/')) || out[i].t == '(' && (")n ".indexOf(out[jj].t) > -1) || out[jj].t == 't' && (out[jj].v == '/' || '$€'.indexOf(out[jj].v) > -1 || (out[jj].v == ' ' && (out[jj+1]||{}).t == '?')))) { | ||||
|         while(out[jj] != null && ( | ||||
|           (c=out[jj].t) === "?" || c === "D" || | ||||
|           (c === " " || c === "t") && out[jj+1] != null && (out[jj+1].t === '?' || out[jj+1].t === "t" && out[jj+1].v === '/') || | ||||
|           out[i].t === '(' && (c === ' ' || c === 'n' || c === ')') || | ||||
|           c === 't' && (out[jj].v === '/' || '$€'.indexOf(out[jj].v) > -1 || out[jj].v === ' ' && out[jj+1] != null && out[jj+1].t == '?') | ||||
|         )) { | ||||
|           out[i].v += out[jj].v; | ||||
|           delete out[jj]; ++jj; | ||||
|           out[jj] = undefined; ++jj; | ||||
|         } | ||||
|         nstr += out[i].v; | ||||
|         i = jj-1; break; | ||||
|       case 'G': out[i].t = 't'; out[i].v = general_fmt(v,opts); break; | ||||
|     } | ||||
|   } | ||||
|   if(nstr) { | ||||
|     var ostr = write_num(nstr[0]=='(' ? '(' : 'n', nstr, (v<0&&nstr[0] == "-" ? -v : v)); | ||||
|   var vv = "", myv, ostr; | ||||
|   if(nstr.length > 0) { | ||||
|     myv = (v<0&&nstr.charCodeAt(0) === 45 ? -v : v); /* '-' */ | ||||
|     ostr = write_num(nstr.charCodeAt(0) === 40 ? '(' : 'n', nstr, myv); /* '(' */ | ||||
|     jj=ostr.length-1; | ||||
|     var decpt = out.length; | ||||
|     for(i=0; i < out.length; ++i) if(out[i] && out[i].v.indexOf(".") > -1) { decpt = i; break; } | ||||
|     var lasti=out.length, vv; | ||||
|     if(decpt === out.length && !ostr.match(/E/)) { | ||||
|     for(i=0; i < out.length; ++i) if(out[i] != null && out[i].v.indexOf(".") > -1) { decpt = i; break; } | ||||
|     var lasti=out.length; | ||||
|     if(decpt === out.length && ostr.indexOf("E") === -1) { | ||||
|       for(i=out.length-1; i>= 0;--i) { | ||||
|         if(!out[i] || 'n?('.indexOf(out[i].t) === -1) continue; | ||||
|         vv = out[i].v.split(""); | ||||
|         for(j=vv.length-1; j>=0; --j) { | ||||
|           if(jj>=0) vv[j] = ostr[jj--]; | ||||
|           else vv[j] = ""; | ||||
|         } | ||||
|         out[i].v = vv.join(""); | ||||
|         if(out[i] == null || 'n?('.indexOf(out[i].t) === -1) continue; | ||||
|         if(jj>=out[i].v.length-1) { jj -= out[i].v.length; out[i].v = ostr.substr(jj+1, out[i].v.length); } | ||||
|         else if(jj < 0) out[i].v = ""; | ||||
|         else { out[i].v = ostr.substr(0, jj+1); jj = -1; } | ||||
|         out[i].t = 't'; | ||||
|         lasti = i; | ||||
|       } | ||||
|       if(jj>=0 && lasti<out.length) out[lasti].v = ostr.substr(0,jj+1) + out[lasti].v; | ||||
|     } | ||||
|     else if(decpt !== out.length && !ostr.match(/E/)) { | ||||
|     else if(decpt !== out.length && ostr.indexOf("E") === -1) { | ||||
|       jj = ostr.indexOf(".")-1; | ||||
|       for(i=decpt; i>= 0; --i) { | ||||
|         if(!out[i] || 'n?('.indexOf(out[i].t) === -1) continue; | ||||
|         vv = out[i].v.split(""); | ||||
|         for(j=out[i].v.indexOf(".")>-1&&i==decpt?out[i].v.indexOf(".")-1:vv.length-1; j>=0; --j) { | ||||
|           if(jj>=0 && "0#".indexOf(vv[j])>-1) vv[j] = ostr[jj--]; | ||||
|           else vv[j] = ""; | ||||
|         if(out[i] == null || 'n?('.indexOf(out[i].t) === -1) continue; | ||||
|         j=out[i].v.indexOf(".")>-1&&i===decpt?out[i].v.indexOf(".")-1:out[i].v.length-1; | ||||
|         vv = out[i].v.substr(j+1); | ||||
|         for(; j>=0; --j) { | ||||
|           if(jj>=0 && (out[i].v[j] === "0" || out[i].v[j] === "#")) vv = ostr[jj--] + vv; | ||||
|         } | ||||
|         out[i].v = vv.join(""); | ||||
|         out[i].v = vv; | ||||
|         out[i].t = 't'; | ||||
|         lasti = i; | ||||
|       } | ||||
|       if(jj>=0 && lasti<out.length) out[lasti].v = ostr.substr(0,jj+1) + out[lasti].v; | ||||
|       jj = ostr.indexOf(".")+1; | ||||
|       for(i=decpt; i<out.length; ++i) { | ||||
|         if(!out[i] || 'n?('.indexOf(out[i].t) === -1 && i != decpt ) continue; | ||||
|         vv = out[i].v.split(""); | ||||
|         for(j=out[i].v.indexOf(".")>-1&&i==decpt?out[i].v.indexOf(".")+1:0; j<vv.length; ++j) { | ||||
|           if(jj<ostr.length) vv[j] = ostr[jj++]; | ||||
|           else vv[j] = ""; | ||||
|         if(out[i] == null || 'n?('.indexOf(out[i].t) === -1 && i !== decpt ) continue; | ||||
|         j=out[i].v.indexOf(".")>-1&&i===decpt?out[i].v.indexOf(".")+1:0; | ||||
|         vv = out[i].v.substr(0,j); | ||||
|         for(; j<out[i].v.length; ++j) { | ||||
|           if(jj<ostr.length) vv += ostr[jj++]; | ||||
|         } | ||||
|         out[i].v = vv.join(""); | ||||
|         out[i].v = vv; | ||||
|         out[i].t = 't'; | ||||
|         lasti = i; | ||||
|       } | ||||
|     } | ||||
|   } | ||||
|   for(i=0; i<out.length; ++i) if(out[i] && 'n(?'.indexOf(out[i].t)>-1) { | ||||
|     out[i].v = write_num(out[i].t, out[i].v, (flen >1 && v < 0 && i>0 && out[i-1].v == "-" ? -v:v)); | ||||
|   for(i=0; i<out.length; ++i) if(out[i] != null && 'n(?'.indexOf(out[i].t)>-1) { | ||||
|     myv = (flen >1 && v < 0 && i>0 && out[i-1].v === "-" ? -v:v); | ||||
|     out[i].v = write_num(out[i].t, out[i].v, myv); | ||||
|     out[i].t = 't'; | ||||
|   } | ||||
|   var retval = ""; | ||||
|   for(i=0; i != out.length; ++i) if(out[i]) retval += out[i].v; | ||||
|   for(i=0; i !== out.length; ++i) if(out[i] != null) retval += out[i].v; | ||||
|   return retval; | ||||
| } | ||||
| SSF._eval = eval_fmt; | ||||
| function choose_fmt(fmt, v, o) { | ||||
|   if(typeof fmt === 'number') fmt = ((o&&o.table) ? o.table : table_fmt)[fmt]; | ||||
|   if(typeof fmt === "string") fmt = split_fmt(fmt); | ||||
|   var l = fmt.length; | ||||
|   if(l<4 && fmt[l-1].indexOf("@")>-1) --l; | ||||
|   switch(fmt.length) { | ||||
|     case 1: fmt = fmt[0].indexOf("@")>-1 ? ["General", "General", "General", fmt[0]] : [fmt[0], fmt[0], fmt[0], "@"]; break; | ||||
|     case 2: fmt = fmt[1].indexOf("@")>-1 ? [fmt[0], fmt[0], fmt[0], fmt[1]] : [fmt[0], fmt[1], fmt[0], "@"]; break; | ||||
|     case 3: fmt = fmt[2].indexOf("@")>-1 ? [fmt[0], fmt[1], fmt[0], fmt[2]] : [fmt[0], fmt[1], fmt[2], "@"]; break; | ||||
|     case 4: break; | ||||
|     default: throw "cannot find right format for |" + fmt + "|"; | ||||
| var cfregex = /\[[=<>]/; | ||||
| var cfregex2 = /\[([=<>]*)(-?\d+\.?\d*)\]/; | ||||
| function chkcond(v, rr) { | ||||
|   if(rr == null) return false; | ||||
|   var thresh = parseFloat(rr[2]); | ||||
|   switch(rr[1]) { | ||||
|     case "=":  if(v == thresh) return true; break; | ||||
|     case ">":  if(v >  thresh) return true; break; | ||||
|     case "<":  if(v <  thresh) return true; break; | ||||
|     case "<>": if(v != thresh) return true; break; | ||||
|     case ">=": if(v >= thresh) return true; break; | ||||
|     case "<=": if(v <= thresh) return true; break; | ||||
|   } | ||||
|   return false; | ||||
| } | ||||
| function choose_fmt(f, v) { | ||||
|   var fmt = split_fmt(f); | ||||
|   var l = fmt.length, lat = fmt[l-1].indexOf("@"); | ||||
|   if(l<4 && lat>-1) --l; | ||||
|   if(fmt.length > 4) throw "cannot find right format for |" + fmt + "|"; | ||||
|   if(typeof v !== "number") return [4, fmt.length === 4 || lat>-1?fmt[fmt.length-1]:"@"]; | ||||
|   switch(fmt.length) { | ||||
|     case 1: fmt = lat>-1 ? ["General", "General", "General", fmt[0]] : [fmt[0], fmt[0], fmt[0], "@"]; break; | ||||
|     case 2: fmt = lat>-1 ? [fmt[0], fmt[0], fmt[0], fmt[1]] : [fmt[0], fmt[1], fmt[0], "@"]; break; | ||||
|     case 3: fmt = lat>-1 ? [fmt[0], fmt[1], fmt[0], fmt[2]] : [fmt[0], fmt[1], fmt[2], "@"]; break; | ||||
|     case 4: break; | ||||
|   } | ||||
|   if(typeof v !== "number") return [fmt.length, fmt[3]]; | ||||
|   var ff = v > 0 ? fmt[0] : v < 0 ? fmt[1] : fmt[2]; | ||||
|   if(fmt[0].match(/\[[=<>]/) || fmt[1].match(/\[[=<>]/)) { | ||||
|     var chk = function(v, rr, out) { | ||||
|       if(!rr) return null; | ||||
|       var found = false; | ||||
|       var thresh = Number(rr[2]); | ||||
|       switch(rr[1]) { | ||||
|         case "=":  if(v == thresh) found = true; break; | ||||
|         case ">":  if(v >  thresh) found = true; break; | ||||
|         case "<":  if(v <  thresh) found = true; break; | ||||
|         case "<>": if(v != thresh) found = true; break; | ||||
|         case ">=": if(v >= thresh) found = true; break; | ||||
|         case "<=": if(v <= thresh) found = true; break; | ||||
|       } | ||||
|       return found ? out : null; | ||||
|     }; | ||||
|     var m1 = fmt[0].match(/\[([=<>]*)([-]?\d+)\]/); | ||||
|     var m2 = fmt[1].match(/\[([=<>]*)([-]?\d+)\]/); | ||||
|     return chk(v, m1, [l, fmt[0]]) || chk(v, m2, [l, fmt[1]]) || [l, fmt[m1&&m2?2:1]]; | ||||
|   if(fmt[0].indexOf("[") === -1 && fmt[1].indexOf("[") === -1) return [l, ff]; | ||||
|   if(fmt[0].match(cfregex) != null || fmt[1].match(cfregex) != null) { | ||||
|     var m1 = fmt[0].match(cfregex2); | ||||
|     var m2 = fmt[1].match(cfregex2); | ||||
|     return chkcond(v, m1) ? [l, fmt[0]] : chkcond(v, m2) ? [l, fmt[1]] : [l, fmt[m1 != null && m2 != null ? 2 : 1]]; | ||||
|   } | ||||
|   return [l, ff]; | ||||
| } | ||||
| var format = function format(fmt,v,o) { | ||||
|   fixopts(o = (o||{})); | ||||
|   if(typeof fmt === "string" && fmt.toLowerCase() === "general") return general_fmt(v, o); | ||||
|   if(typeof fmt === 'number') fmt = (o.table || table_fmt)[fmt]; | ||||
|   var f = choose_fmt(fmt, v, o); | ||||
|   if(f[1].toLowerCase() === "general") return general_fmt(v,o); | ||||
|   if(v === true) v = "TRUE"; if(v === false) v = "FALSE"; | ||||
|   if(v === "" || typeof v === "undefined") return ""; | ||||
| function format(fmt,v,o) { | ||||
|   fixopts(o != null ? o : (o=[])); | ||||
|   var sfmt = ""; | ||||
|   switch(typeof fmt) { | ||||
|     case "string": sfmt = fmt; break; | ||||
|     case "number": sfmt = (o.table != null ? o.table : table_fmt)[fmt]; break; | ||||
|   } | ||||
|   if(isgeneral(sfmt,0)) return general_fmt(v, o); | ||||
|   var f = choose_fmt(sfmt, v); | ||||
|   if(isgeneral(f[1])) return general_fmt(v, o); | ||||
|   if(v === true) v = "TRUE"; else if(v === false) v = "FALSE"; | ||||
|   else if(v === "" || v == null) return ""; | ||||
|   return eval_fmt(f[1], v, o, f[0]); | ||||
| }; | ||||
| 
 | ||||
| SSF._choose = choose_fmt; | ||||
| } | ||||
| SSF._table = table_fmt; | ||||
| SSF.load = function(fmt, idx) { table_fmt[idx] = fmt; }; | ||||
| SSF.load = function load_entry(fmt, idx) { table_fmt[idx] = fmt; }; | ||||
| SSF.format = format; | ||||
| SSF.get_table = function() { return table_fmt; }; | ||||
| SSF.load_table = function(tbl) { for(var i=0; i!=0x0188; ++i) if(tbl[i]) SSF.load(tbl[i], i); }; | ||||
| SSF.get_table = function get_table() { return table_fmt; }; | ||||
| SSF.load_table = function load_table(tbl) { for(var i=0; i!=0x0188; ++i) if(tbl[i] !== undefined) SSF.load(tbl[i], i); }; | ||||
| }; | ||||
| make_ssf(SSF); | ||||
| if(typeof module !== 'undefined' && typeof DO_NOT_EXPORT_SSF === 'undefined') module.exports = SSF; | ||||
|  | ||||
							
								
								
									
										12
									
								
								test/date.js
									
									
									
									
									
								
							
							
								
								
								
								
								
									
									
								
							
						
						
									
										12
									
								
								test/date.js
									
									
									
									
									
								
							| @ -1,23 +1,23 @@ | ||||
| /* vim: set ts=2: */ | ||||
| /*jshint -W041 */ | ||||
| /*jshint loopfunc:true */ | ||||
| var SSF = require('../'); | ||||
| var fs = require('fs'), assert = require('assert'); | ||||
| var dates = fs.readFileSync('./test/dates.tsv','utf8').split("\n"); | ||||
| var date2 = fs.readFileSync('./test/cal.tsv',  'utf8').split("\n"); | ||||
| var times = fs.readFileSync('./test/times.tsv','utf8').split("\n"); | ||||
| 
 | ||||
| function doit(data) { | ||||
|   var step = Math.ceil(data.length/100), i = 1; | ||||
|   var headers = data[0].split("\t"); | ||||
|   for(j=0;j<=100;++j) it(j, function() { | ||||
|     for(var k = 0; k <= step; ++k,++i) { | ||||
|       if(!data[i]) return; | ||||
|       if(data[i] == null || data[i].length < 3) return; | ||||
|       var d = data[i].replace(/#{255}/g,"").split("\t"); | ||||
|       for(var w = 1; w < headers.length; ++w) { | ||||
|         var expected = d[w], actual = SSF.format(headers[w], Number(d[0]), {}); | ||||
|         if(actual != expected) throw [actual, expected, w, headers[w],d[0],d].join("|"); | ||||
|         actual = SSF.format(headers[w].toUpperCase(), Number(d[0]), {}); | ||||
|         if(actual != expected) throw [actual, expected, w, headers[w],d[0],d].join("|"); | ||||
|         var expected = d[w], actual = SSF.format(headers[w], parseFloat(d[0]), {}); | ||||
|         if(actual != expected) throw [actual, expected, w, headers[w],d[0],d,i].join("|"); | ||||
|         actual = SSF.format(headers[w].toUpperCase(), parseFloat(d[0]), {}); | ||||
|         if(actual != expected) throw [actual, expected, w, headers[w].toUpperCase(),d[0],d,i].join("|"); | ||||
|       } | ||||
|     } | ||||
|   }); | ||||
|  | ||||
| @ -7,7 +7,7 @@ function doit(d, headers) { | ||||
|   it(d[0], function() { | ||||
|     for(var w = 1; w < headers.length; ++w) { | ||||
|       var expected = d[w].replace("|", ""), actual; | ||||
|       try { actual = SSF.format(headers[w], Number(d[0]), {}); } catch(e) { } | ||||
|       try { actual = SSF.format(headers[w], parseFloat(d[0]), {}); } catch(e) { } | ||||
|       if(actual != expected && d[w][0] !== "|") throw [actual, expected, w, headers[w],d[0],d].join("|"); | ||||
|     } | ||||
|   }); | ||||
|  | ||||
| @ -3,7 +3,7 @@ var fs = require('fs'), assert = require('assert'); | ||||
| var data = JSON.parse(fs.readFileSync('./test/implied.json','utf8')); | ||||
| var skip = []; | ||||
| function doit(d) { | ||||
|   d[1].forEach(function(r){if(!r[2])assert.equal(SSF.format(r[0],d[0]),r[1]);}); | ||||
|   d[1].forEach(function(r){if(r.length === 2)assert.equal(SSF.format(r[0],d[0]),r[1]);}); | ||||
| } | ||||
| describe('implied formats', function() { | ||||
|   data.forEach(function(d) { | ||||
|  | ||||
| @ -6,7 +6,7 @@ var data = JSON.parse(fs.readFileSync('./test/oddities.json','utf8')); | ||||
| describe('oddities', function() { | ||||
|   data.forEach(function(d) { | ||||
|     it(d[0], function(){ | ||||
|       for(j=1;j<d.length;++j) { | ||||
|       for(var j=1;j<d.length;++j) { | ||||
|         if(d[j].length == 2) { | ||||
|           var expected = d[j][1], actual = SSF.format(d[0], d[j][0], {}); | ||||
|           assert.equal(actual, expected); | ||||
|  | ||||
| @ -29,11 +29,11 @@ | ||||
|   ["#,##0.0000000", [1000000, "1,000,000.0000000"]], | ||||
|   ["#,##0.00000000", [1000000, "1,000,000.00000000"]], | ||||
|   ["#,##0.000000000", [1000000, "1,000,000.000000000"]], | ||||
|   ["#,###", [1, "1"], [-1, "-1"], [0,""], ["TODO", "TODO"]], | ||||
|   ["#,###", [1, "1"], [-1, "-1"], [0,""], [12345.6789, "12,346"], ["TODO", "TODO"]], | ||||
|   ["#.##", [1, "1."], [-1, "-1."], [0,"."], ["sheetjs", "sheetjs"]], | ||||
|   ["0;0", [1.1, "1"], [-1.1, "-1"], [0,"0"], ["sheetjs", "sheetjs"]], | ||||
|   ["0.0", [1, "1.0"], [-1, "-1.0"], [0,"0.0"], ["sheetjs", "sheetjs"]], | ||||
|   ["0.00", [1, "1.00"], [-1, "-1.00"], [0,"0.00"], ["sheetjs", "sheetjs"]], | ||||
|   ["0.00", [1.0001, "1.00"], [-1, "-1.00"], [0,"0.00"], ["sheetjs", "sheetjs"]], | ||||
|   ["0.000", [1, "1.000"], [-1, "-1.000"], [0,"0.000"], ["sheetjs", "sheetjs"]], | ||||
|   ["0.0000", [1, "1.0000"], [-1, "-1.0000"], [0,"0.0000"], ["sheetjs", "sheetjs"]], | ||||
|   ["hh:mm AM/PM", [0.7, "04:48 PM"]], | ||||
| @ -76,11 +76,23 @@ | ||||
|     [123456822333333000000,    " 123,456,822,333,333,000.00"], | ||||
|     [1234568223333330000000, " 1,234,568,223,333,330,000.00"] | ||||
|   ], | ||||
|   ["000#0#0#0##00##00##0#########", | ||||
|     [12345, "0000000000012345"] | ||||
|   ["00,000.00,", [12345, "00,012.35"] ], | ||||
|   ["00,000.00", [12345, "12,345.00"] ], | ||||
|   ["000#0#0#0##00##00##0#########", [12345, "0000000000012345"] ], | ||||
|   ["##0.0E+0", | ||||
|     [1,           "1.0E+0"], | ||||
|     [12,         "12.0E+0"], | ||||
|     [123,       "123.0E+0"], | ||||
|     [1234,        "1.2E+3"], | ||||
|     [12345,      "12.3E+3"], | ||||
|     [123456,    "123.5E+3"], | ||||
|     [1234567,     "1.2E+6"], | ||||
|     [12345678,   "12.3E+6"], | ||||
|     [123456789, "123.5E+6"] | ||||
|   ], | ||||
|   ["0#######0.##0##0######00######0", | ||||
|     [12.3456789, "012.3456789000"] | ||||
|     [12.3456789, "012.3456789000"], | ||||
|     [123456789, "123456789.00000"] | ||||
|   ], | ||||
|   ["###\\###\\##0.00", | ||||
|     [0.00101,              "##0.00"], | ||||
| @ -114,7 +126,7 @@ | ||||
|     [123456789.01,   "1234\\567\\890.01"] | ||||
|   ], | ||||
|   ["###\\\\###\\\\##\\0", [12345.6789, "\\123\\460"]], | ||||
|   ["00000-0000", [941051630, "94105-1630"]], | ||||
|   ["00000-0000", [941051630, "94105-1630"], [12345.6789, "00001-2346"]], | ||||
|   ["000-00-0000", [123456789, "123-45-6789"]], | ||||
|   ["00000\\-0000", [941051630, "94105-1630"]], | ||||
|   ["000\\-00\\-0000", [123456789, "123-45-6789"]], | ||||
| @ -122,15 +134,18 @@ | ||||
|   ["# ??/??", [12.3456789, "12 28/81"]], | ||||
|   ["#??/??", [12.3456789, "1000/81"]], | ||||
|   ["#0#00??/??", [12.3456789, "01000/81"]], | ||||
|   ["[<=9999999]###-####;(###) ###-####", [8675309, "867-5309"],[2813308004, "(281) 330-8004"]], | ||||
|   ["[<=9999999]###-####;(###) ###-####", [8675309, "867-5309"],[2813308004, "(281) 330-8004"], [2018675309, "(201) 867-5309"]], | ||||
|   ["[<=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][<-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.1,"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.1,"26","#"],[-50,"50","#"], ["foo","foo"],["bar","bar"]], | ||||
|   ["[Red]General ;[Blue]General\\ ;[Green]Generalp;[Yellow]General'", [50, "50 "],[0,"0p"],[-25,"-25 "],["foo","foo'"]], | ||||
|   ["[Red][=50]General;[Blue]000", [50, "50"], [51, "051"], [49, "049"]], | ||||
|   ["[Red][<>50]General;[Blue]000", [50, "050"], [51, "51"], [49, "49"]], | ||||
|   ["b", [1,"43"], [1000,"45"], [10000,"70"]], | ||||
|   ["B2yyyymmdd", [0,"13170829"], [1000,"13200624","#"], [10000,"13451117","#"]], | ||||
|   ["☃", [0], [1], [-1]], | ||||
|   ["#0#######", [12345, "012345"], [12345.4321, "012345"], [12345.6789, "012346"]], | ||||
|   ["##,##", [12345, "12,345", ""], [12345.4321, "12,345", ""], [12345.6789, "12,346", ""]], | ||||
|   [0, [12345,"12345"], [4294967296.5, 4294967297]], | ||||
|   ["\"foo\";\"bar\";\"baz\";\"qux\";\"foobar\"", [1], [0], [-1], ["sheetjs"]] | ||||
| ] | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user