forked from sheetjs/sheetjs
SheetJS
f335d310ac
- dateNF option for default date format override - general format renders undefined/null as empty string - ignore text elements when searching for decimal point - bubble negative sign to the front when format starts with text - fixes for eslint + closure - updated frac to 1.0.6 Issues: - fixes #10 h/t @adamgundy @SegFaultx64 @RichardCzechowski - fixes #15 h/t @wilg - fixes #25 h/t @dougschiller - fixes #26 h/t @rjmcguire
43 lines
1.7 KiB
JavaScript
43 lines
1.7 KiB
JavaScript
function choose_fmt(f/*:string*/, 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 new Error("cannot find right format for |" + fmt.join("|") + "|");
|
|
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;
|
|
}
|
|
var ff = v > 0 ? fmt[0] : v < 0 ? fmt[1] : fmt[2];
|
|
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];
|
|
}
|
|
function format(fmt/*:string|number*/,v/*:any*/,o/*:?any*/) {
|
|
if(o == null) o = {};
|
|
//fixopts(o != null ? o : (o=[]));
|
|
var sfmt = "";
|
|
switch(typeof fmt) {
|
|
case "string":
|
|
if(fmt == "m/d/yy" && o.dateNF) sfmt = o.dateNF;
|
|
else sfmt = fmt;
|
|
break;
|
|
case "number":
|
|
if(fmt == 14 && o.dateNF) sfmt = o.dateNF;
|
|
else sfmt = (o.table != null ? (o.table/*:any*/) : 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]);
|
|
}
|