2018-02-28 10:41:49 +00:00
|
|
|
function keys(o/*:any*/)/*:Array<any>*/ {
|
|
|
|
var ks = Object.keys(o), o2 = [];
|
2020-03-15 07:42:05 +00:00
|
|
|
for(var i = 0; i < ks.length; ++i) if(Object.prototype.hasOwnProperty.call(o, ks[i])) o2.push(ks[i]);
|
2018-02-28 10:41:49 +00:00
|
|
|
return o2;
|
|
|
|
}
|
2014-05-16 00:33:34 +00:00
|
|
|
|
2017-02-10 19:23:01 +00:00
|
|
|
function evert_key(obj/*:any*/, key/*:string*/)/*:EvertType*/ {
|
|
|
|
var o = ([]/*:any*/), K = keys(obj);
|
2018-02-14 20:06:35 +00:00
|
|
|
for(var i = 0; i !== K.length; ++i) if(o[obj[K[i]][key]] == null) o[obj[K[i]][key]] = K[i];
|
2014-06-29 18:29:45 +00:00
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2017-02-10 19:23:01 +00:00
|
|
|
function evert(obj/*:any*/)/*:EvertType*/ {
|
|
|
|
var o = ([]/*:any*/), K = keys(obj);
|
2014-06-29 18:29:45 +00:00
|
|
|
for(var i = 0; i !== K.length; ++i) o[obj[K[i]]] = K[i];
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2017-02-10 19:23:01 +00:00
|
|
|
function evert_num(obj/*:any*/)/*:EvertNumType*/ {
|
|
|
|
var o = ([]/*:any*/), K = keys(obj);
|
2014-06-29 18:29:45 +00:00
|
|
|
for(var i = 0; i !== K.length; ++i) o[obj[K[i]]] = parseInt(K[i],10);
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2017-02-10 19:23:01 +00:00
|
|
|
function evert_arr(obj/*:any*/)/*:EvertArrType*/ {
|
|
|
|
var o/*:EvertArrType*/ = ([]/*:any*/), K = keys(obj);
|
2014-06-29 18:29:45 +00:00
|
|
|
for(var i = 0; i !== K.length; ++i) {
|
|
|
|
if(o[obj[K[i]]] == null) o[obj[K[i]]] = [];
|
|
|
|
o[obj[K[i]]].push(K[i]);
|
|
|
|
}
|
2014-05-16 00:33:34 +00:00
|
|
|
return o;
|
|
|
|
}
|
2014-05-22 12:16:51 +00:00
|
|
|
|
2017-06-01 21:22:11 +00:00
|
|
|
var basedate = new Date(1899, 11, 30, 0, 0, 0); // 2209161600000
|
2017-03-09 05:24:32 +00:00
|
|
|
function datenum(v/*:Date*/, date1904/*:?boolean*/)/*:number*/ {
|
|
|
|
var epoch = v.getTime();
|
2017-08-05 06:32:57 +00:00
|
|
|
if(date1904) epoch -= 1462*24*60*60*1000;
|
2020-03-20 06:44:37 +00:00
|
|
|
var dnthresh = basedate.getTime() + (v.getTimezoneOffset() - basedate.getTimezoneOffset()) * 60000;
|
2017-06-01 21:22:11 +00:00
|
|
|
return (epoch - dnthresh) / (24 * 60 * 60 * 1000);
|
2014-06-29 18:29:45 +00:00
|
|
|
}
|
2020-03-20 06:44:37 +00:00
|
|
|
var refdate = new Date();
|
|
|
|
var dnthresh = basedate.getTime() + (refdate.getTimezoneOffset() - basedate.getTimezoneOffset()) * 60000;
|
|
|
|
var refoffset = refdate.getTimezoneOffset();
|
2017-03-25 01:36:40 +00:00
|
|
|
function numdate(v/*:number*/)/*:Date*/ {
|
2017-06-01 21:22:11 +00:00
|
|
|
var out = new Date();
|
|
|
|
out.setTime(v * 24 * 60 * 60 * 1000 + dnthresh);
|
2020-03-20 06:44:37 +00:00
|
|
|
if (out.getTimezoneOffset() !== refoffset) {
|
|
|
|
out.setTime(out.getTime() + (out.getTimezoneOffset() - refoffset) * 60000);
|
|
|
|
}
|
2017-06-01 21:22:11 +00:00
|
|
|
return out;
|
2017-03-25 01:36:40 +00:00
|
|
|
}
|
2014-06-29 18:29:45 +00:00
|
|
|
|
2017-03-10 01:09:18 +00:00
|
|
|
/* ISO 8601 Duration */
|
|
|
|
function parse_isodur(s) {
|
|
|
|
var sec = 0, mt = 0, time = false;
|
|
|
|
var m = s.match(/P([0-9\.]+Y)?([0-9\.]+M)?([0-9\.]+D)?T([0-9\.]+H)?([0-9\.]+M)?([0-9\.]+S)?/);
|
|
|
|
if(!m) throw new Error("|" + s + "| is not an ISO8601 Duration");
|
|
|
|
for(var i = 1; i != m.length; ++i) {
|
|
|
|
if(!m[i]) continue;
|
|
|
|
mt = 1;
|
|
|
|
if(i > 3) time = true;
|
2018-01-11 08:01:25 +00:00
|
|
|
switch(m[i].slice(m[i].length-1)) {
|
2017-03-10 01:09:18 +00:00
|
|
|
case 'Y':
|
2018-01-11 08:01:25 +00:00
|
|
|
throw new Error("Unsupported ISO Duration Field: " + m[i].slice(m[i].length-1));
|
2017-03-10 01:09:18 +00:00
|
|
|
case 'D': mt *= 24;
|
|
|
|
/* falls through */
|
|
|
|
case 'H': mt *= 60;
|
|
|
|
/* falls through */
|
|
|
|
case 'M':
|
|
|
|
if(!time) throw new Error("Unsupported ISO Duration Field: M");
|
|
|
|
else mt *= 60;
|
|
|
|
/* falls through */
|
|
|
|
case 'S': break;
|
|
|
|
}
|
|
|
|
sec += mt * parseInt(m[i], 10);
|
|
|
|
}
|
|
|
|
return sec;
|
|
|
|
}
|
|
|
|
|
2017-03-23 01:18:40 +00:00
|
|
|
var good_pd_date = new Date('2017-02-19T19:06:09.000Z');
|
2017-05-17 04:23:36 +00:00
|
|
|
if(isNaN(good_pd_date.getFullYear())) good_pd_date = new Date('2/19/17');
|
2017-03-23 01:18:40 +00:00
|
|
|
var good_pd = good_pd_date.getFullYear() == 2017;
|
2017-08-10 23:46:34 +00:00
|
|
|
/* parses a date as a local date */
|
2017-06-01 21:22:11 +00:00
|
|
|
function parseDate(str/*:string|Date*/, fixdate/*:?number*/)/*:Date*/ {
|
2017-05-17 04:23:36 +00:00
|
|
|
var d = new Date(str);
|
2017-06-01 21:22:11 +00:00
|
|
|
if(good_pd) {
|
|
|
|
/*:: if(fixdate == null) fixdate = 0; */
|
|
|
|
if(fixdate > 0) d.setTime(d.getTime() + d.getTimezoneOffset() * 60 * 1000);
|
|
|
|
else if(fixdate < 0) d.setTime(d.getTime() - d.getTimezoneOffset() * 60 * 1000);
|
|
|
|
return d;
|
|
|
|
}
|
2017-03-23 01:18:40 +00:00
|
|
|
if(str instanceof Date) return str;
|
2017-05-17 04:23:36 +00:00
|
|
|
if(good_pd_date.getFullYear() == 1917 && !isNaN(d.getFullYear())) {
|
|
|
|
var s = d.getFullYear();
|
|
|
|
if(str.indexOf("" + s) > -1) return d;
|
|
|
|
d.setFullYear(d.getFullYear() + 100); return d;
|
|
|
|
}
|
2017-03-23 01:18:40 +00:00
|
|
|
var n = str.match(/\d+/g)||["2017","2","19","0","0","0"];
|
2017-08-10 23:46:34 +00:00
|
|
|
var out = new Date(+n[0], +n[1] - 1, +n[2], (+n[3]||0), (+n[4]||0), (+n[5]||0));
|
|
|
|
if(str.indexOf("Z") > -1) out = new Date(out.getTime() - out.getTimezoneOffset() * 60 * 1000);
|
|
|
|
return out;
|
2017-03-23 01:18:40 +00:00
|
|
|
}
|
|
|
|
|
2022-02-10 12:40:50 +00:00
|
|
|
function cc2str(arr/*:Array<number>*/, debomit)/*:string*/ {
|
|
|
|
if(has_buf && Buffer.isBuffer(arr)) {
|
|
|
|
if(debomit) {
|
|
|
|
if(arr[0] == 0xFF && arr[1] == 0xFE) return arr.slice(2).toString("utf16le");
|
|
|
|
if(arr[1] == 0xFE && arr[2] == 0xFF) return utf16beread(arr.slice(2).toString("binary"));
|
|
|
|
}
|
|
|
|
return arr.toString("binary");
|
|
|
|
}
|
|
|
|
/* TODO: investigate performance degradation of TextEncoder in Edge 13 */
|
|
|
|
var o = [];
|
|
|
|
for(var i = 0; i != arr.length; ++i) o.push(String.fromCharCode(arr[i]));
|
|
|
|
return o.join("");
|
2014-05-22 12:16:51 +00:00
|
|
|
}
|
2014-07-28 13:22:32 +00:00
|
|
|
|
2017-02-19 20:36:32 +00:00
|
|
|
function dup(o/*:any*/)/*:any*/ {
|
2017-03-20 09:02:25 +00:00
|
|
|
if(typeof JSON != 'undefined' && !Array.isArray(o)) return JSON.parse(JSON.stringify(o));
|
2017-03-19 23:46:41 +00:00
|
|
|
if(typeof o != 'object' || o == null) return o;
|
2018-01-11 08:01:25 +00:00
|
|
|
if(o instanceof Date) return new Date(o.getTime());
|
2017-02-19 20:36:32 +00:00
|
|
|
var out = {};
|
2020-03-15 07:42:05 +00:00
|
|
|
for(var k in o) if(Object.prototype.hasOwnProperty.call(o, k)) out[k] = dup(o[k]);
|
2017-02-19 20:36:32 +00:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
function fill(c/*:string*/,l/*:number*/)/*:string*/ { var o = ""; while(o.length < l) o+=c; return o; }
|
2017-05-11 07:29:59 +00:00
|
|
|
|
|
|
|
/* TODO: stress test */
|
2017-08-09 22:38:23 +00:00
|
|
|
function fuzzynum(s/*:string*/)/*:number*/ {
|
|
|
|
var v/*:number*/ = Number(s);
|
2021-09-13 02:06:46 +00:00
|
|
|
if(isFinite(v)) return v;
|
|
|
|
if(!isNaN(v)) return NaN;
|
2020-09-11 08:38:33 +00:00
|
|
|
if(!/\d/.test(s)) return v;
|
2017-08-10 23:46:34 +00:00
|
|
|
var wt = 1;
|
|
|
|
var ss = s.replace(/([\d]),([\d])/g,"$1$2").replace(/[$]/g,"").replace(/[%]/g, function() { wt *= 100; return "";});
|
|
|
|
if(!isNaN(v = Number(ss))) return v / wt;
|
|
|
|
ss = ss.replace(/[(](.*)[)]/,function($$, $1) { wt = -wt; return $1;});
|
|
|
|
if(!isNaN(v = Number(ss))) return v / wt;
|
2017-08-09 22:38:23 +00:00
|
|
|
return v;
|
|
|
|
}
|
2017-05-11 07:29:59 +00:00
|
|
|
function fuzzydate(s/*:string*/)/*:Date*/ {
|
|
|
|
var o = new Date(s), n = new Date(NaN);
|
|
|
|
var y = o.getYear(), m = o.getMonth(), d = o.getDate();
|
|
|
|
if(isNaN(d)) return n;
|
|
|
|
if(y < 0 || y > 8099) return n;
|
|
|
|
if((m > 0 || d > 1) && y != 101) return o;
|
|
|
|
if(s.toLowerCase().match(/jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec/)) return o;
|
2017-08-17 15:59:17 +00:00
|
|
|
if(s.match(/[^-0-9:,\/\\]/)) return n;
|
|
|
|
return o;
|
2017-05-11 07:29:59 +00:00
|
|
|
}
|
|
|
|
|
2017-08-10 23:46:34 +00:00
|
|
|
var safe_split_regex = "abacaba".split(/(:?b)/i).length == 5;
|
|
|
|
function split_regex(str/*:string*/, re, def/*:string*/)/*:Array<string>*/ {
|
|
|
|
if(safe_split_regex || typeof re == "string") return str.split(re);
|
|
|
|
var p = str.split(re), o = [p[0]];
|
|
|
|
for(var i = 1; i < p.length; ++i) { o.push(def); o.push(p[i]); }
|
|
|
|
return o;
|
|
|
|
}
|