forked from sheetjs/sheetjs
36 lines
1.6 KiB
JavaScript
36 lines
1.6 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*/) {
|
|
fixopts(o != null ? o : (o=[]));
|
|
var sfmt = "";
|
|
switch(typeof fmt) {
|
|
case "string": sfmt = fmt; break;
|
|
case "number": 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]);
|
|
}
|