ssf.flow.js 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  1. /* ssf.js (C) 2013-present SheetJS -- http://sheetjs.com */
  2. /* vim: set ts=2: */
  3. /*jshint -W041 */
  4. /*:: declare var DO_NOT_EXPORT_SSF: any; */
  5. var SSF/*:SSFModule*/ = ({}/*:any*/);
  6. var make_ssf = function make_ssf(SSF/*:SSFModule*/){
  7. SSF.version = '0.11.2';
  8. function _strrev(x/*:string*/)/*:string*/ { var o = "", i = x.length-1; while(i>=0) o += x.charAt(i--); return o; }
  9. function fill(c/*:string*/,l/*:number*/)/*:string*/ { var o = ""; while(o.length < l) o+=c; return o; }
  10. function pad0(v/*:any*/,d/*:number*/)/*:string*/{var t=""+v; return t.length>=d?t:fill('0',d-t.length)+t;}
  11. function pad_(v/*:any*/,d/*:number*/)/*:string*/{var t=""+v;return t.length>=d?t:fill(' ',d-t.length)+t;}
  12. function rpad_(v/*:any*/,d/*:number*/)/*:string*/{var t=""+v; return t.length>=d?t:t+fill(' ',d-t.length);}
  13. function pad0r1(v/*:any*/,d/*:number*/)/*:string*/{var t=""+Math.round(v); return t.length>=d?t:fill('0',d-t.length)+t;}
  14. function pad0r2(v/*:any*/,d/*:number*/)/*:string*/{var t=""+v; return t.length>=d?t:fill('0',d-t.length)+t;}
  15. var p2_32 = Math.pow(2,32);
  16. function pad0r(v/*:any*/,d/*:number*/)/*:string*/{if(v>p2_32||v<-p2_32) return pad0r1(v,d); var i = Math.round(v); return pad0r2(i,d); }
  17. function isgeneral(s/*:string*/, i/*:?number*/)/*:boolean*/ { i = i || 0; 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; }
  18. /*::
  19. type SSF_write_num = {(type:string, fmt:string, val:number):string};
  20. */
  21. var days/*:Array<Array<string> >*/ = [
  22. ['Sun', 'Sunday'],
  23. ['Mon', 'Monday'],
  24. ['Tue', 'Tuesday'],
  25. ['Wed', 'Wednesday'],
  26. ['Thu', 'Thursday'],
  27. ['Fri', 'Friday'],
  28. ['Sat', 'Saturday']
  29. ];
  30. var months/*:Array<Array<string> >*/ = [
  31. ['J', 'Jan', 'January'],
  32. ['F', 'Feb', 'February'],
  33. ['M', 'Mar', 'March'],
  34. ['A', 'Apr', 'April'],
  35. ['M', 'May', 'May'],
  36. ['J', 'Jun', 'June'],
  37. ['J', 'Jul', 'July'],
  38. ['A', 'Aug', 'August'],
  39. ['S', 'Sep', 'September'],
  40. ['O', 'Oct', 'October'],
  41. ['N', 'Nov', 'November'],
  42. ['D', 'Dec', 'December']
  43. ];
  44. function init_table(t/*:any*/) {
  45. t[0]= 'General';
  46. t[1]= '0';
  47. t[2]= '0.00';
  48. t[3]= '#,##0';
  49. t[4]= '#,##0.00';
  50. t[9]= '0%';
  51. t[10]= '0.00%';
  52. t[11]= '0.00E+00';
  53. t[12]= '# ?/?';
  54. t[13]= '# ??/??';
  55. t[14]= 'm/d/yy';
  56. t[15]= 'd-mmm-yy';
  57. t[16]= 'd-mmm';
  58. t[17]= 'mmm-yy';
  59. t[18]= 'h:mm AM/PM';
  60. t[19]= 'h:mm:ss AM/PM';
  61. t[20]= 'h:mm';
  62. t[21]= 'h:mm:ss';
  63. t[22]= 'm/d/yy h:mm';
  64. t[37]= '#,##0 ;(#,##0)';
  65. t[38]= '#,##0 ;[Red](#,##0)';
  66. t[39]= '#,##0.00;(#,##0.00)';
  67. t[40]= '#,##0.00;[Red](#,##0.00)';
  68. t[45]= 'mm:ss';
  69. t[46]= '[h]:mm:ss';
  70. t[47]= 'mmss.0';
  71. t[48]= '##0.0E+0';
  72. t[49]= '@';
  73. t[56]= '"上午/下午 "hh"時"mm"分"ss"秒 "';
  74. }
  75. var table_fmt = {};
  76. init_table(table_fmt);
  77. /* Defaults determined by systematically testing in Excel 2019 */
  78. /* These formats appear to default to other formats in the table */
  79. var default_map/*:Array<number>*/ = [];
  80. var defi = 0;
  81. // 5 -> 37 ... 8 -> 40
  82. for(defi = 5; defi <= 8; ++defi) default_map[defi] = 32 + defi;
  83. // 23 -> 0 ... 26 -> 0
  84. for(defi = 23; defi <= 26; ++defi) default_map[defi] = 0;
  85. // 27 -> 14 ... 31 -> 14
  86. for(defi = 27; defi <= 31; ++defi) default_map[defi] = 14;
  87. // 50 -> 14 ... 58 -> 14
  88. for(defi = 50; defi <= 58; ++defi) default_map[defi] = 14;
  89. // 59 -> 1 ... 62 -> 4
  90. for(defi = 59; defi <= 62; ++defi) default_map[defi] = defi - 58;
  91. // 67 -> 9 ... 68 -> 10
  92. for(defi = 67; defi <= 68; ++defi) default_map[defi] = defi - 58;
  93. // 72 -> 14 ... 75 -> 17
  94. for(defi = 72; defi <= 75; ++defi) default_map[defi] = defi - 58;
  95. // 69 -> 12 ... 71 -> 14
  96. for(defi = 67; defi <= 68; ++defi) default_map[defi] = defi - 57;
  97. // 76 -> 20 ... 78 -> 22
  98. for(defi = 76; defi <= 78; ++defi) default_map[defi] = defi - 56;
  99. // 79 -> 45 ... 81 -> 47
  100. for(defi = 79; defi <= 81; ++defi) default_map[defi] = defi - 34;
  101. // 82 -> 0 ... 65536 -> 0 (omitted)
  102. /* These formats technically refer to Accounting formats with no equivalent */
  103. var default_str/*:Array<string>*/ = [];
  104. // 5 -- Currency, 0 decimal, black negative
  105. default_str[5] = default_str[63] = '"$"#,##0_);\\("$"#,##0\\)';
  106. // 6 -- Currency, 0 decimal, red negative
  107. default_str[6] = default_str[64] = '"$"#,##0_);[Red]\\("$"#,##0\\)';
  108. // 7 -- Currency, 2 decimal, black negative
  109. default_str[7] = default_str[65] = '"$"#,##0.00_);\\("$"#,##0.00\\)';
  110. // 8 -- Currency, 2 decimal, red negative
  111. default_str[8] = default_str[66] = '"$"#,##0.00_);[Red]\\("$"#,##0.00\\)';
  112. // 41 -- Accounting, 0 decimal, No Symbol
  113. default_str[41] = '_(* #,##0_);_(* \\(#,##0\\);_(* "-"_);_(@_)';
  114. // 42 -- Accounting, 0 decimal, $ Symbol
  115. default_str[42] = '_("$"* #,##0_);_("$"* \\(#,##0\\);_("$"* "-"_);_(@_)';
  116. // 43 -- Accounting, 2 decimal, No Symbol
  117. default_str[43] = '_(* #,##0.00_);_(* \\(#,##0.00\\);_(* "-"??_);_(@_)';
  118. // 44 -- Accounting, 2 decimal, $ Symbol
  119. default_str[44] = '_("$"* #,##0.00_);_("$"* \\(#,##0.00\\);_("$"* "-"??_);_(@_)';
  120. function frac(x/*:number*/, D/*:number*/, mixed/*:?boolean*/)/*:Array<number>*/ {
  121. var sgn = x < 0 ? -1 : 1;
  122. var B = x * sgn;
  123. var P_2 = 0, P_1 = 1, P = 0;
  124. var Q_2 = 1, Q_1 = 0, Q = 0;
  125. var A = Math.floor(B);
  126. while(Q_1 < D) {
  127. A = Math.floor(B);
  128. P = A * P_1 + P_2;
  129. Q = A * Q_1 + Q_2;
  130. if((B - A) < 0.00000005) break;
  131. B = 1 / (B - A);
  132. P_2 = P_1; P_1 = P;
  133. Q_2 = Q_1; Q_1 = Q;
  134. }
  135. if(Q > D) { if(Q_1 > D) { Q = Q_2; P = P_2; } else { Q = Q_1; P = P_1; } }
  136. if(!mixed) return [0, sgn * P, Q];
  137. var q = Math.floor(sgn * P/Q);
  138. return [q, sgn*P - q*Q, Q];
  139. }
  140. function parse_date_code(v/*:number*/,opts/*:?any*/,b2/*:?boolean*/) {
  141. if(v > 2958465 || v < 0) return null;
  142. var date = (v|0), time = Math.floor(86400 * (v - date)), dow=0;
  143. var dout=[];
  144. 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};
  145. if(Math.abs(out.u) < 1e-6) out.u = 0;
  146. if(opts && opts.date1904) date += 1462;
  147. if(out.u > 0.9999) {
  148. out.u = 0;
  149. if(++time == 86400) { out.T = time = 0; ++date; ++out.D; }
  150. }
  151. if(date === 60) {dout = b2 ? [1317,10,29] : [1900,2,29]; dow=3;}
  152. else if(date === 0) {dout = b2 ? [1317,8,29] : [1900,1,0]; dow=6;}
  153. else {
  154. if(date > 60) --date;
  155. /* 1 = Jan 1 1900 in Gregorian */
  156. var d = new Date(1900, 0, 1);
  157. d.setDate(d.getDate() + date - 1);
  158. dout = [d.getFullYear(), d.getMonth()+1,d.getDate()];
  159. dow = d.getDay();
  160. if(date < 60) dow = (dow + 6) % 7;
  161. if(b2) dow = fix_hijri(d, dout);
  162. }
  163. out.y = dout[0]; out.m = dout[1]; out.d = dout[2];
  164. out.S = time % 60; time = Math.floor(time / 60);
  165. out.M = time % 60; time = Math.floor(time / 60);
  166. out.H = time;
  167. out.q = dow;
  168. return out;
  169. }
  170. SSF.parse_date_code = parse_date_code;
  171. var basedate = new Date(1899, 11, 31, 0, 0, 0);
  172. var dnthresh = basedate.getTime();
  173. var base1904 = new Date(1900, 2, 1, 0, 0, 0);
  174. function datenum_local(v/*:Date*/, date1904/*:?boolean*/)/*:number*/ {
  175. var epoch = v.getTime();
  176. if(date1904) epoch -= 1461*24*60*60*1000;
  177. else if(v >= base1904) epoch += 24*60*60*1000;
  178. return (epoch - (dnthresh + (v.getTimezoneOffset() - basedate.getTimezoneOffset()) * 60000)) / (24 * 60 * 60 * 1000);
  179. }
  180. /* The longest 32-bit integer text is "-4294967296", exactly 11 chars */
  181. function general_fmt_int(v/*:number*/)/*:string*/ { return v.toString(10); }
  182. SSF._general_int = general_fmt_int;
  183. /* ECMA-376 18.8.30 numFmt*/
  184. /* Note: `toPrecision` uses standard form when prec > E and E >= -6 */
  185. var general_fmt_num = (function make_general_fmt_num() {
  186. var trailing_zeroes_and_decimal = /(?:\.0*|(\.\d*[1-9])0+)$/;
  187. function strip_decimal(o/*:string*/)/*:string*/ {
  188. return (o.indexOf(".") == -1) ? o : o.replace(trailing_zeroes_and_decimal, "$1");
  189. }
  190. /* General Exponential always shows 2 digits exp and trims the mantissa */
  191. var mantissa_zeroes_and_decimal = /(?:\.0*|(\.\d*[1-9])0+)[Ee]/;
  192. var exp_with_single_digit = /(E[+-])(\d)$/;
  193. function normalize_exp(o/*:string*/)/*:string*/ {
  194. if(o.indexOf("E") == -1) return o;
  195. return o.replace(mantissa_zeroes_and_decimal,"$1E").replace(exp_with_single_digit,"$10$2");
  196. }
  197. /* exponent >= -9 and <= 9 */
  198. function small_exp(v/*:number*/)/*:string*/ {
  199. var w = (v<0?12:11);
  200. var o = strip_decimal(v.toFixed(12)); if(o.length <= w) return o;
  201. o = v.toPrecision(10); if(o.length <= w) return o;
  202. return v.toExponential(5);
  203. }
  204. /* exponent >= 11 or <= -10 likely exponential */
  205. function large_exp(v/*:number*/)/*:string*/ {
  206. var o = strip_decimal(v.toFixed(11));
  207. return (o.length > (v<0?12:11) || o === "0" || o === "-0") ? v.toPrecision(6) : o;
  208. }
  209. function general_fmt_num_base(v/*:number*/)/*:string*/ {
  210. var V = Math.floor(Math.log(Math.abs(v))*Math.LOG10E), o;
  211. if(V >= -4 && V <= -1) o = v.toPrecision(10+V);
  212. else if(Math.abs(V) <= 9) o = small_exp(v);
  213. else if(V === 10) o = v.toFixed(10).substr(0,12);
  214. else o = large_exp(v);
  215. return strip_decimal(normalize_exp(o.toUpperCase()));
  216. }
  217. return general_fmt_num_base;
  218. })();
  219. SSF._general_num = general_fmt_num;
  220. /*
  221. "General" rules:
  222. - text is passed through ("@")
  223. - booleans are rendered as TRUE/FALSE
  224. - "up to 11 characters" displayed for numbers
  225. - Default date format (code 14) used for Dates
  226. TODO: technically the display depends on the width of the cell
  227. */
  228. function general_fmt(v/*:any*/, opts/*:any*/) {
  229. switch(typeof v) {
  230. case 'string': return v;
  231. case 'boolean': return v ? "TRUE" : "FALSE";
  232. case 'number': return (v|0) === v ? v.toString(10) : general_fmt_num(v);
  233. case 'undefined': return "";
  234. case 'object':
  235. if(v == null) return "";
  236. if(v instanceof Date) return format(14, datenum_local(v, opts && opts.date1904), opts);
  237. }
  238. throw new Error("unsupported value in General format: " + v);
  239. }
  240. SSF._general = general_fmt;
  241. function fix_hijri(date/*:Date*/, o/*:[number, number, number]*/) {
  242. /* TODO: properly adjust y/m/d and */
  243. o[0] -= 581;
  244. var dow = date.getDay();
  245. if(date < 60) dow = (dow + 6) % 7;
  246. return dow;
  247. }
  248. var THAI_DIGITS = "\u0E50\u0E51\u0E52\u0E53\u0E54\u0E55\u0E56\u0E57\u0E58\u0E59".split("");
  249. /*jshint -W086 */
  250. function write_date(type/*:number*/, fmt/*:string*/, val, ss0/*:?number*/)/*:string*/ {
  251. var o="", ss=0, tt=0, y = val.y, out, outl = 0;
  252. switch(type) {
  253. case 98: /* 'b' buddhist year */
  254. y = val.y + 543;
  255. /* falls through */
  256. case 121: /* 'y' year */
  257. switch(fmt.length) {
  258. case 1: case 2: out = y % 100; outl = 2; break;
  259. default: out = y % 10000; outl = 4; break;
  260. } break;
  261. case 109: /* 'm' month */
  262. switch(fmt.length) {
  263. case 1: case 2: out = val.m; outl = fmt.length; break;
  264. case 3: return months[val.m-1][1];
  265. case 5: return months[val.m-1][0];
  266. default: return months[val.m-1][2];
  267. } break;
  268. case 100: /* 'd' day */
  269. switch(fmt.length) {
  270. case 1: case 2: out = val.d; outl = fmt.length; break;
  271. case 3: return days[val.q][0];
  272. default: return days[val.q][1];
  273. } break;
  274. case 104: /* 'h' 12-hour */
  275. switch(fmt.length) {
  276. case 1: case 2: out = 1+(val.H+11)%12; outl = fmt.length; break;
  277. default: throw 'bad hour format: ' + fmt;
  278. } break;
  279. case 72: /* 'H' 24-hour */
  280. switch(fmt.length) {
  281. case 1: case 2: out = val.H; outl = fmt.length; break;
  282. default: throw 'bad hour format: ' + fmt;
  283. } break;
  284. case 77: /* 'M' minutes */
  285. switch(fmt.length) {
  286. case 1: case 2: out = val.M; outl = fmt.length; break;
  287. default: throw 'bad minute format: ' + fmt;
  288. } break;
  289. case 115: /* 's' seconds */
  290. if(fmt != 's' && fmt != 'ss' && fmt != '.0' && fmt != '.00' && fmt != '.000') throw 'bad second format: ' + fmt;
  291. if(val.u === 0 && (fmt == "s" || fmt == "ss")) return pad0(val.S, fmt.length);
  292. /*::if(!ss0) ss0 = 0; */
  293. if(ss0 >= 2) tt = ss0 === 3 ? 1000 : 100;
  294. else tt = ss0 === 1 ? 10 : 1;
  295. ss = Math.round((tt)*(val.S + val.u));
  296. if(ss >= 60*tt) ss = 0;
  297. if(fmt === 's') return ss === 0 ? "0" : ""+ss/tt;
  298. o = pad0(ss,2 + ss0);
  299. if(fmt === 'ss') return o.substr(0,2);
  300. return "." + o.substr(2,fmt.length-1);
  301. case 90: /* 'Z' absolute time */
  302. switch(fmt) {
  303. case '[h]': case '[hh]': out = val.D*24+val.H; break;
  304. case '[m]': case '[mm]': out = (val.D*24+val.H)*60+val.M; break;
  305. case '[s]': case '[ss]': out = ((val.D*24+val.H)*60+val.M)*60+Math.round(val.S+val.u); break;
  306. default: throw 'bad abstime format: ' + fmt;
  307. } outl = fmt.length === 3 ? 1 : 2; break;
  308. case 101: /* 'e' era */
  309. out = y; outl = 1; break;
  310. }
  311. var outstr = outl > 0 ? pad0(out, outl) : "";
  312. return outstr;
  313. }
  314. /*jshint +W086 */
  315. function commaify(s/*:string*/)/*:string*/ {
  316. var w = 3;
  317. if(s.length <= w) return s;
  318. var j = (s.length % w), o = s.substr(0,j);
  319. for(; j!=s.length; j+=w) o+=(o.length > 0 ? "," : "") + s.substr(j,w);
  320. return o;
  321. }
  322. var write_num/*:SSF_write_num*/ = (function make_write_num(){
  323. var pct1 = /%/g;
  324. function write_num_pct(type/*:string*/, fmt/*:string*/, val/*:number*/)/*:string*/{
  325. var sfmt = fmt.replace(pct1,""), mul = fmt.length - sfmt.length;
  326. return write_num(type, sfmt, val * Math.pow(10,2*mul)) + fill("%",mul);
  327. }
  328. function write_num_cm(type/*:string*/, fmt/*:string*/, val/*:number*/)/*:string*/{
  329. var idx = fmt.length - 1;
  330. while(fmt.charCodeAt(idx-1) === 44) --idx;
  331. return write_num(type, fmt.substr(0,idx), val / Math.pow(10,3*(fmt.length-idx)));
  332. }
  333. function write_num_exp(fmt/*:string*/, val/*:number*/)/*:string*/{
  334. var o/*:string*/;
  335. var idx = fmt.indexOf("E") - fmt.indexOf(".") - 1;
  336. if(fmt.match(/^#+0.0E\+0$/)) {
  337. if(val == 0) return "0.0E+0";
  338. else if(val < 0) return "-" + write_num_exp(fmt, -val);
  339. var period = fmt.indexOf("."); if(period === -1) period=fmt.indexOf('E');
  340. var ee = Math.floor(Math.log(val)*Math.LOG10E)%period;
  341. if(ee < 0) ee += period;
  342. o = (val/Math.pow(10,ee)).toPrecision(idx+1+(period+ee)%period);
  343. if(o.indexOf("e") === -1) {
  344. var fakee = Math.floor(Math.log(val)*Math.LOG10E);
  345. if(o.indexOf(".") === -1) o = o.charAt(0) + "." + o.substr(1) + "E+" + (fakee - o.length+ee);
  346. else o += "E+" + (fakee - ee);
  347. while(o.substr(0,2) === "0.") {
  348. o = o.charAt(0) + o.substr(2,period) + "." + o.substr(2+period);
  349. o = o.replace(/^0+([1-9])/,"$1").replace(/^0+\./,"0.");
  350. }
  351. o = o.replace(/\+-/,"-");
  352. }
  353. o = o.replace(/^([+-]?)(\d*)\.(\d*)[Ee]/,function($$,$1,$2,$3) { return $1 + $2 + $3.substr(0,(period+ee)%period) + "." + $3.substr(ee) + "E"; });
  354. } else o = val.toExponential(idx);
  355. if(fmt.match(/E\+00$/) && o.match(/e[+-]\d$/)) o = o.substr(0,o.length-1) + "0" + o.charAt(o.length-1);
  356. if(fmt.match(/E\-/) && o.match(/e\+/)) o = o.replace(/e\+/,"e");
  357. return o.replace("e","E");
  358. }
  359. var frac1 = /# (\?+)( ?)\/( ?)(\d+)/;
  360. function write_num_f1(r/*:Array<string>*/, aval/*:number*/, sign/*:string*/)/*:string*/ {
  361. var den = parseInt(r[4],10), rr = Math.round(aval * den), base = Math.floor(rr/den);
  362. var myn = (rr - base*den), myd = den;
  363. 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));
  364. }
  365. function write_num_f2(r/*:Array<string>*/, aval/*:number*/, sign/*:string*/)/*:string*/ {
  366. return sign + (aval === 0 ? "" : ""+aval) + fill(" ", r[1].length + 2 + r[4].length);
  367. }
  368. var dec1 = /^#*0*\.([0#]+)/;
  369. var closeparen = /\).*[0#]/;
  370. var phone = /\(###\) ###\\?-####/;
  371. function hashq(str/*:string*/)/*:string*/ {
  372. var o = "", cc;
  373. for(var i = 0; i != str.length; ++i) switch((cc=str.charCodeAt(i))) {
  374. case 35: break;
  375. case 63: o+= " "; break;
  376. case 48: o+= "0"; break;
  377. default: o+= String.fromCharCode(cc);
  378. }
  379. return o;
  380. }
  381. function rnd(val/*:number*/, d/*:number*/)/*:string*/ { var dd = Math.pow(10,d); return ""+(Math.round(val * dd)/dd); }
  382. function dec(val/*:number*/, d/*:number*/)/*:number*/ {
  383. var _frac = val - Math.floor(val), dd = Math.pow(10,d);
  384. if (d < ('' + Math.round(_frac * dd)).length) return 0;
  385. return Math.round(_frac * dd);
  386. }
  387. function carry(val/*:number*/, d/*:number*/)/*:number*/ {
  388. if (d < ('' + Math.round((val-Math.floor(val))*Math.pow(10,d))).length) {
  389. return 1;
  390. }
  391. return 0;
  392. }
  393. function flr(val/*:number*/)/*:string*/ {
  394. if(val < 2147483647 && val > -2147483648) return ""+(val >= 0 ? (val|0) : (val-1|0));
  395. return ""+Math.floor(val);
  396. }
  397. function write_num_flt(type/*:string*/, fmt/*:string*/, val/*:number*/)/*:string*/ {
  398. if(type.charCodeAt(0) === 40 && !fmt.match(closeparen)) {
  399. var ffmt = fmt.replace(/\( */,"").replace(/ \)/,"").replace(/\)/,"");
  400. if(val >= 0) return write_num_flt('n', ffmt, val);
  401. return '(' + write_num_flt('n', ffmt, -val) + ')';
  402. }
  403. if(fmt.charCodeAt(fmt.length - 1) === 44) return write_num_cm(type, fmt, val);
  404. if(fmt.indexOf('%') !== -1) return write_num_pct(type, fmt, val);
  405. if(fmt.indexOf('E') !== -1) return write_num_exp(fmt, val);
  406. if(fmt.charCodeAt(0) === 36) return "$"+write_num_flt(type,fmt.substr(fmt.charAt(1)==' '?2:1),val);
  407. var o;
  408. var r/*:?Array<string>*/, ri, ff, aval = Math.abs(val), sign = val < 0 ? "-" : "";
  409. if(fmt.match(/^00+$/)) return sign + pad0r(aval,fmt.length);
  410. if(fmt.match(/^[#?]+$/)) {
  411. o = pad0r(val,0); if(o === "0") o = "";
  412. return o.length > fmt.length ? o : hashq(fmt.substr(0,fmt.length-o.length)) + o;
  413. }
  414. if((r = fmt.match(frac1))) return write_num_f1(r, aval, sign);
  415. if(fmt.match(/^#+0+$/)) return sign + pad0r(aval,fmt.length - fmt.indexOf("0"));
  416. if((r = fmt.match(dec1))) {
  417. o = rnd(val, r[1].length).replace(/^([^\.]+)$/,"$1."+hashq(r[1])).replace(/\.$/,"."+hashq(r[1])).replace(/\.(\d*)$/,function($$, $1) { return "." + $1 + fill("0", hashq(/*::(*/r/*::||[""])*/[1]).length-$1.length); });
  418. return fmt.indexOf("0.") !== -1 ? o : o.replace(/^0\./,".");
  419. }
  420. fmt = fmt.replace(/^#+([0.])/, "$1");
  421. if((r = fmt.match(/^(0*)\.(#*)$/))) {
  422. return sign + rnd(aval, r[2].length).replace(/\.(\d*[1-9])0*$/,".$1").replace(/^(-?\d*)$/,"$1.").replace(/^0\./,r[1].length?"0.":".");
  423. }
  424. if((r = fmt.match(/^#{1,3},##0(\.?)$/))) return sign + commaify(pad0r(aval,0));
  425. if((r = fmt.match(/^#,##0\.([#0]*0)$/))) {
  426. return val < 0 ? "-" + write_num_flt(type, fmt, -val) : commaify(""+(Math.floor(val) + carry(val, r[1].length))) + "." + pad0(dec(val, r[1].length),r[1].length);
  427. }
  428. if((r = fmt.match(/^#,#*,#0/))) return write_num_flt(type,fmt.replace(/^#,#*,/,""),val);
  429. if((r = fmt.match(/^([0#]+)(\\?-([0#]+))+$/))) {
  430. o = _strrev(write_num_flt(type, fmt.replace(/[\\-]/g,""), val));
  431. ri = 0;
  432. return _strrev(_strrev(fmt.replace(/\\/g,"")).replace(/[0#]/g,function(x){return ri<o.length?o.charAt(ri++):x==='0'?'0':"";}));
  433. }
  434. if(fmt.match(phone)) {
  435. o = write_num_flt(type, "##########", val);
  436. return "(" + o.substr(0,3) + ") " + o.substr(3, 3) + "-" + o.substr(6);
  437. }
  438. var oa = "";
  439. if((r = fmt.match(/^([#0?]+)( ?)\/( ?)([#0?]+)/))) {
  440. ri = Math.min(/*::String(*/r[4]/*::)*/.length,7);
  441. ff = frac(aval, Math.pow(10,ri)-1, false);
  442. o = "" + sign;
  443. oa = write_num("n", /*::String(*/r[1]/*::)*/, ff[1]);
  444. if(oa.charAt(oa.length-1) == " ") oa = oa.substr(0,oa.length-1) + "0";
  445. o += oa + /*::String(*/r[2]/*::)*/ + "/" + /*::String(*/r[3]/*::)*/;
  446. oa = rpad_(ff[2],ri);
  447. if(oa.length < r[4].length) oa = hashq(r[4].substr(r[4].length-oa.length)) + oa;
  448. o += oa;
  449. return o;
  450. }
  451. if((r = fmt.match(/^# ([#0?]+)( ?)\/( ?)([#0?]+)/))) {
  452. ri = Math.min(Math.max(r[1].length, r[4].length),7);
  453. ff = frac(aval, Math.pow(10,ri)-1, true);
  454. 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));
  455. }
  456. if((r = fmt.match(/^[#0?]+$/))) {
  457. o = pad0r(val, 0);
  458. if(fmt.length <= o.length) return o;
  459. return hashq(fmt.substr(0,fmt.length-o.length)) + o;
  460. }
  461. if((r = fmt.match(/^([#0?]+)\.([#0]+)$/))) {
  462. o = "" + val.toFixed(Math.min(r[2].length,10)).replace(/([^0])0+$/,"$1");
  463. ri = o.indexOf(".");
  464. var lres = fmt.indexOf(".") - ri, rres = fmt.length - o.length - lres;
  465. return hashq(fmt.substr(0,lres) + o + fmt.substr(fmt.length-rres));
  466. }
  467. if((r = fmt.match(/^00,000\.([#0]*0)$/))) {
  468. ri = dec(val, r[1].length);
  469. 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);
  470. }
  471. switch(fmt) {
  472. case "###,##0.00": return write_num_flt(type, "#,##0.00", val);
  473. case "###,###":
  474. case "##,###":
  475. case "#,###": var x = commaify(pad0r(aval,0)); return x !== "0" ? sign + x : "";
  476. case "###,###.00": return write_num_flt(type, "###,##0.00",val).replace(/^0\./,".");
  477. case "#,###.00": return write_num_flt(type, "#,##0.00",val).replace(/^0\./,".");
  478. default:
  479. }
  480. throw new Error("unsupported format |" + fmt + "|");
  481. }
  482. function write_num_cm2(type/*:string*/, fmt/*:string*/, val/*:number*/)/*:string*/{
  483. var idx = fmt.length - 1;
  484. while(fmt.charCodeAt(idx-1) === 44) --idx;
  485. return write_num(type, fmt.substr(0,idx), val / Math.pow(10,3*(fmt.length-idx)));
  486. }
  487. function write_num_pct2(type/*:string*/, fmt/*:string*/, val/*:number*/)/*:string*/{
  488. var sfmt = fmt.replace(pct1,""), mul = fmt.length - sfmt.length;
  489. return write_num(type, sfmt, val * Math.pow(10,2*mul)) + fill("%",mul);
  490. }
  491. function write_num_exp2(fmt/*:string*/, val/*:number*/)/*:string*/{
  492. var o/*:string*/;
  493. var idx = fmt.indexOf("E") - fmt.indexOf(".") - 1;
  494. if(fmt.match(/^#+0.0E\+0$/)) {
  495. if(val == 0) return "0.0E+0";
  496. else if(val < 0) return "-" + write_num_exp2(fmt, -val);
  497. var period = fmt.indexOf("."); if(period === -1) period=fmt.indexOf('E');
  498. var ee = Math.floor(Math.log(val)*Math.LOG10E)%period;
  499. if(ee < 0) ee += period;
  500. o = (val/Math.pow(10,ee)).toPrecision(idx+1+(period+ee)%period);
  501. if(!o.match(/[Ee]/)) {
  502. var fakee = Math.floor(Math.log(val)*Math.LOG10E);
  503. if(o.indexOf(".") === -1) o = o.charAt(0) + "." + o.substr(1) + "E+" + (fakee - o.length+ee);
  504. else o += "E+" + (fakee - ee);
  505. o = o.replace(/\+-/,"-");
  506. }
  507. o = o.replace(/^([+-]?)(\d*)\.(\d*)[Ee]/,function($$,$1,$2,$3) { return $1 + $2 + $3.substr(0,(period+ee)%period) + "." + $3.substr(ee) + "E"; });
  508. } else o = val.toExponential(idx);
  509. if(fmt.match(/E\+00$/) && o.match(/e[+-]\d$/)) o = o.substr(0,o.length-1) + "0" + o.charAt(o.length-1);
  510. if(fmt.match(/E\-/) && o.match(/e\+/)) o = o.replace(/e\+/,"e");
  511. return o.replace("e","E");
  512. }
  513. function write_num_int(type/*:string*/, fmt/*:string*/, val/*:number*/)/*:string*/ {
  514. if(type.charCodeAt(0) === 40 && !fmt.match(closeparen)) {
  515. var ffmt = fmt.replace(/\( */,"").replace(/ \)/,"").replace(/\)/,"");
  516. if(val >= 0) return write_num_int('n', ffmt, val);
  517. return '(' + write_num_int('n', ffmt, -val) + ')';
  518. }
  519. if(fmt.charCodeAt(fmt.length - 1) === 44) return write_num_cm2(type, fmt, val);
  520. if(fmt.indexOf('%') !== -1) return write_num_pct2(type, fmt, val);
  521. if(fmt.indexOf('E') !== -1) return write_num_exp2(fmt, val);
  522. if(fmt.charCodeAt(0) === 36) return "$"+write_num_int(type,fmt.substr(fmt.charAt(1)==' '?2:1),val);
  523. var o;
  524. var r/*:?Array<string>*/, ri, ff, aval = Math.abs(val), sign = val < 0 ? "-" : "";
  525. if(fmt.match(/^00+$/)) return sign + pad0(aval,fmt.length);
  526. if(fmt.match(/^[#?]+$/)) {
  527. o = (""+val); if(val === 0) o = "";
  528. return o.length > fmt.length ? o : hashq(fmt.substr(0,fmt.length-o.length)) + o;
  529. }
  530. if((r = fmt.match(frac1))) return write_num_f2(r, aval, sign);
  531. if(fmt.match(/^#+0+$/)) return sign + pad0(aval,fmt.length - fmt.indexOf("0"));
  532. if((r = fmt.match(dec1))) {
  533. /*:: if(!Array.isArray(r)) throw new Error("unreachable"); */
  534. o = (""+val).replace(/^([^\.]+)$/,"$1."+hashq(r[1])).replace(/\.$/,"."+hashq(r[1]));
  535. o = o.replace(/\.(\d*)$/,function($$, $1) {
  536. /*:: if(!Array.isArray(r)) throw new Error("unreachable"); */
  537. return "." + $1 + fill("0", hashq(r[1]).length-$1.length); });
  538. return fmt.indexOf("0.") !== -1 ? o : o.replace(/^0\./,".");
  539. }
  540. fmt = fmt.replace(/^#+([0.])/, "$1");
  541. if((r = fmt.match(/^(0*)\.(#*)$/))) {
  542. return sign + (""+aval).replace(/\.(\d*[1-9])0*$/,".$1").replace(/^(-?\d*)$/,"$1.").replace(/^0\./,r[1].length?"0.":".");
  543. }
  544. if((r = fmt.match(/^#{1,3},##0(\.?)$/))) return sign + commaify((""+aval));
  545. if((r = fmt.match(/^#,##0\.([#0]*0)$/))) {
  546. return val < 0 ? "-" + write_num_int(type, fmt, -val) : commaify((""+val)) + "." + fill('0',r[1].length);
  547. }
  548. if((r = fmt.match(/^#,#*,#0/))) return write_num_int(type,fmt.replace(/^#,#*,/,""),val);
  549. if((r = fmt.match(/^([0#]+)(\\?-([0#]+))+$/))) {
  550. o = _strrev(write_num_int(type, fmt.replace(/[\\-]/g,""), val));
  551. ri = 0;
  552. return _strrev(_strrev(fmt.replace(/\\/g,"")).replace(/[0#]/g,function(x){return ri<o.length?o.charAt(ri++):x==='0'?'0':"";}));
  553. }
  554. if(fmt.match(phone)) {
  555. o = write_num_int(type, "##########", val);
  556. return "(" + o.substr(0,3) + ") " + o.substr(3, 3) + "-" + o.substr(6);
  557. }
  558. var oa = "";
  559. if((r = fmt.match(/^([#0?]+)( ?)\/( ?)([#0?]+)/))) {
  560. ri = Math.min(/*::String(*/r[4]/*::)*/.length,7);
  561. ff = frac(aval, Math.pow(10,ri)-1, false);
  562. o = "" + sign;
  563. oa = write_num("n", /*::String(*/r[1]/*::)*/, ff[1]);
  564. if(oa.charAt(oa.length-1) == " ") oa = oa.substr(0,oa.length-1) + "0";
  565. o += oa + /*::String(*/r[2]/*::)*/ + "/" + /*::String(*/r[3]/*::)*/;
  566. oa = rpad_(ff[2],ri);
  567. if(oa.length < r[4].length) oa = hashq(r[4].substr(r[4].length-oa.length)) + oa;
  568. o += oa;
  569. return o;
  570. }
  571. if((r = fmt.match(/^# ([#0?]+)( ?)\/( ?)([#0?]+)/))) {
  572. ri = Math.min(Math.max(r[1].length, r[4].length),7);
  573. ff = frac(aval, Math.pow(10,ri)-1, true);
  574. 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));
  575. }
  576. if((r = fmt.match(/^[#0?]+$/))) {
  577. o = "" + val;
  578. if(fmt.length <= o.length) return o;
  579. return hashq(fmt.substr(0,fmt.length-o.length)) + o;
  580. }
  581. if((r = fmt.match(/^([#0]+)\.([#0]+)$/))) {
  582. o = "" + val.toFixed(Math.min(r[2].length,10)).replace(/([^0])0+$/,"$1");
  583. ri = o.indexOf(".");
  584. var lres = fmt.indexOf(".") - ri, rres = fmt.length - o.length - lres;
  585. return hashq(fmt.substr(0,lres) + o + fmt.substr(fmt.length-rres));
  586. }
  587. if((r = fmt.match(/^00,000\.([#0]*0)$/))) {
  588. 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);
  589. }
  590. switch(fmt) {
  591. case "###,###":
  592. case "##,###":
  593. case "#,###": var x = commaify(""+aval); return x !== "0" ? sign + x : "";
  594. default:
  595. if(fmt.match(/\.[0#?]*$/)) return write_num_int(type, fmt.slice(0,fmt.lastIndexOf(".")), val) + hashq(fmt.slice(fmt.lastIndexOf(".")));
  596. }
  597. throw new Error("unsupported format |" + fmt + "|");
  598. }
  599. return function write_num(type/*:string*/, fmt/*:string*/, val/*:number*/)/*:string*/ {
  600. return (val|0) === val ? write_num_int(type, fmt, val) : write_num_flt(type, fmt, val);
  601. };})();
  602. function split_fmt(fmt/*:string*/)/*:Array<string>*/ {
  603. var out/*:Array<string>*/ = [];
  604. var in_str = false/*, cc*/;
  605. for(var i = 0, j = 0; i < fmt.length; ++i) switch((/*cc=*/fmt.charCodeAt(i))) {
  606. case 34: /* '"' */
  607. in_str = !in_str; break;
  608. case 95: case 42: case 92: /* '_' '*' '\\' */
  609. ++i; break;
  610. case 59: /* ';' */
  611. out[out.length] = fmt.substr(j,i-j);
  612. j = i+1;
  613. }
  614. out[out.length] = fmt.substr(j);
  615. if(in_str === true) throw new Error("Format |" + fmt + "| unterminated string ");
  616. return out;
  617. }
  618. SSF._split = split_fmt;
  619. var abstime = /\[[HhMmSs\u0E0A\u0E19\u0E17]*\]/;
  620. function fmt_is_date(fmt/*:string*/)/*:boolean*/ {
  621. var i = 0, /*cc = 0,*/ c = "", o = "";
  622. while(i < fmt.length) {
  623. switch((c = fmt.charAt(i))) {
  624. case 'G': if(isgeneral(fmt, i)) i+= 6; i++; break;
  625. case '"': for(;(/*cc=*/fmt.charCodeAt(++i)) !== 34 && i < fmt.length;){/*empty*/} ++i; break;
  626. case '\\': i+=2; break;
  627. case '_': i+=2; break;
  628. case '@': ++i; break;
  629. case 'B': case 'b':
  630. if(fmt.charAt(i+1) === "1" || fmt.charAt(i+1) === "2") return true;
  631. /* falls through */
  632. case 'M': case 'D': case 'Y': case 'H': case 'S': case 'E':
  633. /* falls through */
  634. case 'm': case 'd': case 'y': case 'h': case 's': case 'e': case 'g': return true;
  635. case 'A': case 'a': case '上':
  636. if(fmt.substr(i, 3).toUpperCase() === "A/P") return true;
  637. if(fmt.substr(i, 5).toUpperCase() === "AM/PM") return true;
  638. if(fmt.substr(i, 5).toUpperCase() === "上午/下午") return true;
  639. ++i; break;
  640. case '[':
  641. o = c;
  642. while(fmt.charAt(i++) !== ']' && i < fmt.length) o += fmt.charAt(i);
  643. if(o.match(abstime)) return true;
  644. break;
  645. case '.':
  646. /* falls through */
  647. case '0': case '#':
  648. while(i < fmt.length && ("0#?.,E+-%".indexOf(c=fmt.charAt(++i)) > -1 || (c=='\\' && fmt.charAt(i+1) == "-" && "0#".indexOf(fmt.charAt(i+2))>-1))){/* empty */}
  649. break;
  650. case '?': while(fmt.charAt(++i) === c){/* empty */} break;
  651. case '*': ++i; if(fmt.charAt(i) == ' ' || fmt.charAt(i) == '*') ++i; break;
  652. case '(': case ')': ++i; break;
  653. case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
  654. while(i < fmt.length && "0123456789".indexOf(fmt.charAt(++i)) > -1){/* empty */} break;
  655. case ' ': ++i; break;
  656. default: ++i; break;
  657. }
  658. }
  659. return false;
  660. }
  661. SSF.is_date = fmt_is_date;
  662. function eval_fmt(fmt/*:string*/, v/*:any*/, opts/*:any*/, flen/*:number*/) {
  663. var out = [], o = "", i = 0, c = "", lst='t', dt, j, cc;
  664. var hr='H';
  665. /* Tokenize */
  666. while(i < fmt.length) {
  667. switch((c = fmt.charAt(i))) {
  668. case 'G': /* General */
  669. if(!isgeneral(fmt, i)) throw new Error('unrecognized character ' + c + ' in ' +fmt);
  670. out[out.length] = {t:'G', v:'General'}; i+=7; break;
  671. case '"': /* Literal text */
  672. for(o="";(cc=fmt.charCodeAt(++i)) !== 34 && i < fmt.length;) o += String.fromCharCode(cc);
  673. out[out.length] = {t:'t', v:o}; ++i; break;
  674. case '\\': var w = fmt.charAt(++i), t = (w === "(" || w === ")") ? w : 't';
  675. out[out.length] = {t:t, v:w}; ++i; break;
  676. case '_': out[out.length] = {t:'t', v:" "}; i+=2; break;
  677. case '@': /* Text Placeholder */
  678. out[out.length] = {t:'T', v:v}; ++i; break;
  679. case 'B': case 'b':
  680. if(fmt.charAt(i+1) === "1" || fmt.charAt(i+1) === "2") {
  681. if(dt==null) { dt=parse_date_code(v, opts, fmt.charAt(i+1) === "2"); if(dt==null) return ""; }
  682. out[out.length] = {t:'X', v:fmt.substr(i,2)}; lst = c; i+=2; break;
  683. }
  684. /* falls through */
  685. case 'M': case 'D': case 'Y': case 'H': case 'S': case 'E':
  686. c = c.toLowerCase();
  687. /* falls through */
  688. case 'm': case 'd': case 'y': case 'h': case 's': case 'e': case 'g':
  689. if(v < 0) return "";
  690. if(dt==null) { dt=parse_date_code(v, opts); if(dt==null) return ""; }
  691. o = c; while(++i < fmt.length && fmt.charAt(i).toLowerCase() === c) o+=c;
  692. if(c === 'm' && lst.toLowerCase() === 'h') c = 'M';
  693. if(c === 'h') c = hr;
  694. out[out.length] = {t:c, v:o}; lst = c; break;
  695. case 'A': case 'a': case '上':
  696. var q={t:c, v:c};
  697. if(dt==null) dt=parse_date_code(v, opts);
  698. if(fmt.substr(i, 3).toUpperCase() === "A/P") { if(dt!=null) q.v = dt.H >= 12 ? "P" : "A"; q.t = 'T'; hr='h';i+=3;}
  699. else if(fmt.substr(i,5).toUpperCase() === "AM/PM") { if(dt!=null) q.v = dt.H >= 12 ? "PM" : "AM"; q.t = 'T'; i+=5; hr='h'; }
  700. else if(fmt.substr(i,5).toUpperCase() === "上午/下午") { if(dt!=null) q.v = dt.H >= 12 ? "下午" : "上午"; q.t = 'T'; i+=5; hr='h'; }
  701. else { q.t = "t"; ++i; }
  702. if(dt==null && q.t === 'T') return "";
  703. out[out.length] = q; lst = c; break;
  704. case '[':
  705. o = c;
  706. while(fmt.charAt(i++) !== ']' && i < fmt.length) o += fmt.charAt(i);
  707. if(o.slice(-1) !== ']') throw 'unterminated "[" block: |' + o + '|';
  708. if(o.match(abstime)) {
  709. if(dt==null) { dt=parse_date_code(v, opts); if(dt==null) return ""; }
  710. out[out.length] = {t:'Z', v:o.toLowerCase()};
  711. lst = o.charAt(1);
  712. } else if(o.indexOf("$") > -1) {
  713. o = (o.match(/\$([^-\[\]]*)/)||[])[1]||"$";
  714. if(!fmt_is_date(fmt)) out[out.length] = {t:'t',v:o};
  715. }
  716. break;
  717. /* Numbers */
  718. case '.':
  719. if(dt != null) {
  720. o = c; while(++i < fmt.length && (c=fmt.charAt(i)) === "0") o += c;
  721. out[out.length] = {t:'s', v:o}; break;
  722. }
  723. /* falls through */
  724. case '0': case '#':
  725. o = c; while(++i < fmt.length && "0#?.,E+-%".indexOf(c=fmt.charAt(i)) > -1) o += c;
  726. out[out.length] = {t:'n', v:o}; break;
  727. case '?':
  728. o = c; while(fmt.charAt(++i) === c) o+=c;
  729. out[out.length] = {t:c, v:o}; lst = c; break;
  730. case '*': ++i; if(fmt.charAt(i) == ' ' || fmt.charAt(i) == '*') ++i; break; // **
  731. case '(': case ')': out[out.length] = {t:(flen===1?'t':c), v:c}; ++i; break;
  732. case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
  733. o = c; while(i < fmt.length && "0123456789".indexOf(fmt.charAt(++i)) > -1) o+=fmt.charAt(i);
  734. out[out.length] = {t:'D', v:o}; break;
  735. case ' ': out[out.length] = {t:c, v:c}; ++i; break;
  736. case '$': out[out.length] = {t:'t', v:'$'}; ++i; break;
  737. default:
  738. if(",$-+/():!^&'~{}<>=€acfijklopqrtuvwxzP".indexOf(c) === -1) throw new Error('unrecognized character ' + c + ' in ' + fmt);
  739. out[out.length] = {t:'t', v:c}; ++i; break;
  740. }
  741. }
  742. /* Scan for date/time parts */
  743. var bt = 0, ss0 = 0, ssm;
  744. for(i=out.length-1, lst='t'; i >= 0; --i) {
  745. switch(out[i].t) {
  746. case 'h': case 'H': out[i].t = hr; lst='h'; if(bt < 1) bt = 1; break;
  747. case 's':
  748. if((ssm=out[i].v.match(/\.0+$/))) ss0=Math.max(ss0,ssm[0].length-1);
  749. if(bt < 3) bt = 3;
  750. /* falls through */
  751. case 'd': case 'y': case 'M': case 'e': lst=out[i].t; break;
  752. case 'm': if(lst === 's') { out[i].t = 'M'; if(bt < 2) bt = 2; } break;
  753. case 'X': /*if(out[i].v === "B2");*/
  754. break;
  755. case 'Z':
  756. if(bt < 1 && out[i].v.match(/[Hh]/)) bt = 1;
  757. if(bt < 2 && out[i].v.match(/[Mm]/)) bt = 2;
  758. if(bt < 3 && out[i].v.match(/[Ss]/)) bt = 3;
  759. }
  760. }
  761. /* time rounding depends on presence of minute / second / usec fields */
  762. switch(bt) {
  763. case 0: break;
  764. case 1:
  765. /*::if(!dt) break;*/
  766. if(dt.u >= 0.5) { dt.u = 0; ++dt.S; }
  767. if(dt.S >= 60) { dt.S = 0; ++dt.M; }
  768. if(dt.M >= 60) { dt.M = 0; ++dt.H; }
  769. break;
  770. case 2:
  771. /*::if(!dt) break;*/
  772. if(dt.u >= 0.5) { dt.u = 0; ++dt.S; }
  773. if(dt.S >= 60) { dt.S = 0; ++dt.M; }
  774. break;
  775. }
  776. /* replace fields */
  777. var nstr = "", jj;
  778. for(i=0; i < out.length; ++i) {
  779. switch(out[i].t) {
  780. case 't': case 'T': case ' ': case 'D': break;
  781. case 'X': out[i].v = ""; out[i].t = ";"; break;
  782. case 'd': case 'm': case 'y': case 'h': case 'H': case 'M': case 's': case 'e': case 'b': case 'Z':
  783. /*::if(!dt) throw "unreachable"; */
  784. out[i].v = write_date(out[i].t.charCodeAt(0), out[i].v, dt, ss0);
  785. out[i].t = 't'; break;
  786. case 'n': case '?':
  787. jj = i+1;
  788. while(out[jj] != null && (
  789. (c=out[jj].t) === "?" || c === "D" ||
  790. ((c === " " || c === "t") && out[jj+1] != null && (out[jj+1].t === '?' || out[jj+1].t === "t" && out[jj+1].v === '/')) ||
  791. (out[i].t === '(' && (c === ' ' || c === 'n' || c === ')')) ||
  792. (c === 't' && (out[jj].v === '/' || out[jj].v === ' ' && out[jj+1] != null && out[jj+1].t == '?'))
  793. )) {
  794. out[i].v += out[jj].v;
  795. out[jj] = {v:"", t:";"}; ++jj;
  796. }
  797. nstr += out[i].v;
  798. i = jj-1; break;
  799. case 'G': out[i].t = 't'; out[i].v = general_fmt(v,opts); break;
  800. }
  801. }
  802. var vv = "", myv, ostr;
  803. if(nstr.length > 0) {
  804. if(nstr.charCodeAt(0) == 40) /* '(' */ {
  805. myv = (v<0&&nstr.charCodeAt(0) === 45 ? -v : v);
  806. ostr = write_num('n', nstr, myv);
  807. } else {
  808. myv = (v<0 && flen > 1 ? -v : v);
  809. ostr = write_num('n', nstr, myv);
  810. if(myv < 0 && out[0] && out[0].t == 't') {
  811. ostr = ostr.substr(1);
  812. out[0].v = "-" + out[0].v;
  813. }
  814. }
  815. jj=ostr.length-1;
  816. var decpt = out.length;
  817. for(i=0; i < out.length; ++i) if(out[i] != null && out[i].t != 't' && out[i].v.indexOf(".") > -1) { decpt = i; break; }
  818. var lasti=out.length;
  819. if(decpt === out.length && ostr.indexOf("E") === -1) {
  820. for(i=out.length-1; i>= 0;--i) {
  821. if(out[i] == null || 'n?'.indexOf(out[i].t) === -1) continue;
  822. if(jj>=out[i].v.length-1) { jj -= out[i].v.length; out[i].v = ostr.substr(jj+1, out[i].v.length); }
  823. else if(jj < 0) out[i].v = "";
  824. else { out[i].v = ostr.substr(0, jj+1); jj = -1; }
  825. out[i].t = 't';
  826. lasti = i;
  827. }
  828. if(jj>=0 && lasti<out.length) out[lasti].v = ostr.substr(0,jj+1) + out[lasti].v;
  829. }
  830. else if(decpt !== out.length && ostr.indexOf("E") === -1) {
  831. jj = ostr.indexOf(".")-1;
  832. for(i=decpt; i>= 0; --i) {
  833. if(out[i] == null || 'n?'.indexOf(out[i].t) === -1) continue;
  834. j=out[i].v.indexOf(".")>-1&&i===decpt?out[i].v.indexOf(".")-1:out[i].v.length-1;
  835. vv = out[i].v.substr(j+1);
  836. for(; j>=0; --j) {
  837. if(jj>=0 && (out[i].v.charAt(j) === "0" || out[i].v.charAt(j) === "#")) vv = ostr.charAt(jj--) + vv;
  838. }
  839. out[i].v = vv;
  840. out[i].t = 't';
  841. lasti = i;
  842. }
  843. if(jj>=0 && lasti<out.length) out[lasti].v = ostr.substr(0,jj+1) + out[lasti].v;
  844. jj = ostr.indexOf(".")+1;
  845. for(i=decpt; i<out.length; ++i) {
  846. if(out[i] == null || ('n?('.indexOf(out[i].t) === -1 && i !== decpt)) continue;
  847. j=out[i].v.indexOf(".")>-1&&i===decpt?out[i].v.indexOf(".")+1:0;
  848. vv = out[i].v.substr(0,j);
  849. for(; j<out[i].v.length; ++j) {
  850. if(jj<ostr.length) vv += ostr.charAt(jj++);
  851. }
  852. out[i].v = vv;
  853. out[i].t = 't';
  854. lasti = i;
  855. }
  856. }
  857. }
  858. for(i=0; i<out.length; ++i) if(out[i] != null && 'n?'.indexOf(out[i].t)>-1) {
  859. myv = (flen >1 && v < 0 && i>0 && out[i-1].v === "-" ? -v:v);
  860. out[i].v = write_num(out[i].t, out[i].v, myv);
  861. out[i].t = 't';
  862. }
  863. var retval = "";
  864. for(i=0; i !== out.length; ++i) if(out[i] != null) retval += out[i].v;
  865. return retval;
  866. }
  867. SSF._eval = eval_fmt;
  868. var cfregex = /\[[=<>]/;
  869. var cfregex2 = /\[(=|>[=]?|<[>=]?)(-?\d+(?:\.\d*)?)\]/;
  870. function chkcond(v, rr) {
  871. if(rr == null) return false;
  872. var thresh = parseFloat(rr[2]);
  873. switch(rr[1]) {
  874. case "=": if(v == thresh) return true; break;
  875. case ">": if(v > thresh) return true; break;
  876. case "<": if(v < thresh) return true; break;
  877. case "<>": if(v != thresh) return true; break;
  878. case ">=": if(v >= thresh) return true; break;
  879. case "<=": if(v <= thresh) return true; break;
  880. }
  881. return false;
  882. }
  883. function choose_fmt(f/*:string*/, v/*:any*/) {
  884. var fmt = split_fmt(f);
  885. var l = fmt.length, lat = fmt[l-1].indexOf("@");
  886. if(l<4 && lat>-1) --l;
  887. if(fmt.length > 4) throw new Error("cannot find right format for |" + fmt.join("|") + "|");
  888. if(typeof v !== "number") return [4, fmt.length === 4 || lat>-1?fmt[fmt.length-1]:"@"];
  889. switch(fmt.length) {
  890. case 1: fmt = lat>-1 ? ["General", "General", "General", fmt[0]] : [fmt[0], fmt[0], fmt[0], "@"]; break;
  891. case 2: fmt = lat>-1 ? [fmt[0], fmt[0], fmt[0], fmt[1]] : [fmt[0], fmt[1], fmt[0], "@"]; break;
  892. case 3: fmt = lat>-1 ? [fmt[0], fmt[1], fmt[0], fmt[2]] : [fmt[0], fmt[1], fmt[2], "@"]; break;
  893. case 4: break;
  894. }
  895. var ff = v > 0 ? fmt[0] : v < 0 ? fmt[1] : fmt[2];
  896. if(fmt[0].indexOf("[") === -1 && fmt[1].indexOf("[") === -1) return [l, ff];
  897. if(fmt[0].match(cfregex) != null || fmt[1].match(cfregex) != null) {
  898. var m1 = fmt[0].match(cfregex2);
  899. var m2 = fmt[1].match(cfregex2);
  900. return chkcond(v, m1) ? [l, fmt[0]] : chkcond(v, m2) ? [l, fmt[1]] : [l, fmt[m1 != null && m2 != null ? 2 : 1]];
  901. }
  902. return [l, ff];
  903. }
  904. function format(fmt/*:string|number*/,v/*:any*/,o/*:?any*/) {
  905. if(o == null) o = {};
  906. var sfmt = "";
  907. switch(typeof fmt) {
  908. case "string":
  909. if(fmt == "m/d/yy" && o.dateNF) sfmt = o.dateNF;
  910. else sfmt = fmt;
  911. break;
  912. case "number":
  913. if(fmt == 14 && o.dateNF) sfmt = o.dateNF;
  914. else sfmt = (o.table != null ? (o.table/*:any*/) : table_fmt)[fmt];
  915. if(sfmt == null) sfmt = (o.table && o.table[default_map[fmt]]) || table_fmt[default_map[fmt]];
  916. if(sfmt == null) sfmt = default_str[fmt] || "General";
  917. break;
  918. }
  919. if(isgeneral(sfmt,0)) return general_fmt(v, o);
  920. if(v instanceof Date) v = datenum_local(v, o.date1904);
  921. var f = choose_fmt(sfmt, v);
  922. if(isgeneral(f[1])) return general_fmt(v, o);
  923. if(v === true) v = "TRUE"; else if(v === false) v = "FALSE";
  924. else if(v === "" || v == null) return "";
  925. return eval_fmt(f[1], v, o, f[0]);
  926. }
  927. function load_entry(fmt/*:string*/, idx/*:?number*/)/*:number*/ {
  928. if(typeof idx != 'number') {
  929. idx = +idx || -1;
  930. /*::if(typeof idx != 'number') return 0x188; */
  931. for(var i = 0; i < 0x0188; ++i) {
  932. /*::if(typeof idx != 'number') return 0x188; */
  933. if(table_fmt[i] == undefined) { if(idx < 0) idx = i; continue; }
  934. if(table_fmt[i] == fmt) { idx = i; break; }
  935. }
  936. /*::if(typeof idx != 'number') return 0x188; */
  937. if(idx < 0) idx = 0x187;
  938. }
  939. /*::if(typeof idx != 'number') return 0x188; */
  940. table_fmt[idx] = fmt;
  941. return idx;
  942. }
  943. SSF.load = load_entry;
  944. SSF._table = table_fmt;
  945. SSF.get_table = function get_table()/*:SSFTable*/ { return table_fmt; };
  946. SSF.load_table = function load_table(tbl/*:SSFTable*/)/*:void*/ {
  947. for(var i=0; i!=0x0188; ++i)
  948. if(tbl[i] !== undefined) load_entry(tbl[i], i);
  949. };
  950. SSF.init_table = init_table;
  951. SSF.format = format;
  952. };
  953. make_ssf(SSF);
  954. /*global module */
  955. if(typeof module !== 'undefined' && typeof DO_NOT_EXPORT_SSF === 'undefined') module.exports = SSF;