2020-08-07 20:44:33 +08:00
|
|
|
|
2020-10-15 16:45:18 +08:00
|
|
|
(doc=>{
|
2020-09-26 15:34:23 +08:00
|
|
|
Array.prototype.unique = function() { return this.filter((v, i, a) => a.indexOf(v) === i); };
|
2020-10-03 05:54:15 +08:00
|
|
|
Array.prototype.validUnique = function(fn) {
|
|
|
|
return this.filter((v, i, a) => (fn ? fn(v) : v) && a.indexOf(v) === i);
|
|
|
|
};
|
2020-08-07 20:44:33 +08:00
|
|
|
|
2022-12-12 20:03:41 +08:00
|
|
|
// full = Monday, December 12, 2022 at 12:16:21 PM Central European Standard Time
|
|
|
|
// long = December 12, 2022 at 12:16:21 PM GMT+1
|
|
|
|
// medium = Dec 12, 2022, 12:16:21 PM
|
|
|
|
// short = 12/12/22, 12:16 PM
|
2020-10-15 06:26:40 +08:00
|
|
|
let formats = {
|
2023-01-17 22:48:48 +08:00
|
|
|
// LT : {timeStyle: 'short'}, // Issue in Safari
|
|
|
|
LT : {hour: 'numeric', minute: 'numeric'},
|
2022-12-23 19:22:57 +08:00
|
|
|
LLL : {dateStyle: 'long', timeStyle: 'short'}
|
2022-12-24 02:03:35 +08:00
|
|
|
};
|
2020-08-07 20:44:33 +08:00
|
|
|
|
2020-10-15 16:45:18 +08:00
|
|
|
// Format momentjs/PHP date formats to Intl.DateTimeFormat
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat
|
2022-12-12 20:03:41 +08:00
|
|
|
Date.prototype.format = function (options, UTC, hourCycle) {
|
2020-10-17 20:08:27 +08:00
|
|
|
if (typeof options == 'string') {
|
|
|
|
if (formats[options]) {
|
|
|
|
options = formats[options];
|
|
|
|
} else {
|
2022-12-23 23:28:02 +08:00
|
|
|
console.log('Date.format('+options+')');
|
2020-10-17 20:08:27 +08:00
|
|
|
options = {};
|
2020-10-15 06:26:40 +08:00
|
|
|
}
|
2020-08-07 20:44:33 +08:00
|
|
|
}
|
2022-12-12 20:03:41 +08:00
|
|
|
if (hourCycle) {
|
|
|
|
options.hourCycle = hourCycle;
|
|
|
|
}
|
2023-04-04 17:28:15 +08:00
|
|
|
let el = doc.documentElement;
|
|
|
|
return this.toLocaleString(el.dataset.dateLang || el.lang, options);
|
2020-08-07 20:44:33 +08:00
|
|
|
};
|
|
|
|
|
2020-09-04 23:07:35 +08:00
|
|
|
Element.prototype.closestWithin = function(selector, parent) {
|
|
|
|
const el = this.closest(selector);
|
|
|
|
return (el && el !== parent && parent.contains(el)) ? el : null;
|
|
|
|
};
|
|
|
|
|
|
|
|
Element.fromHTML = string => {
|
2020-10-15 16:45:18 +08:00
|
|
|
const template = doc.createElement('template');
|
2020-09-04 23:07:35 +08:00
|
|
|
template.innerHTML = string.trim();
|
|
|
|
return template.content.firstChild;
|
|
|
|
};
|
|
|
|
|
2024-02-27 00:36:55 +08:00
|
|
|
/**
|
|
|
|
* https://github.com/tc39/proposal-regex-escaping
|
|
|
|
*/
|
|
|
|
if (!RegExp.escape){
|
|
|
|
RegExp.escape = s => String(s).replace(/[\\^$*+?.()|[\]{}]/g, '\\$&');
|
|
|
|
}
|
|
|
|
|
2020-09-04 23:07:35 +08:00
|
|
|
/**
|
|
|
|
* Every time the function is executed,
|
|
|
|
* it will delay the execution with the given amount of milliseconds.
|
|
|
|
*/
|
|
|
|
if (!Function.prototype.debounce) {
|
|
|
|
Function.prototype.debounce = function(ms) {
|
|
|
|
let func = this, timer;
|
|
|
|
return function(...args) {
|
|
|
|
timer && clearTimeout(timer);
|
|
|
|
timer = setTimeout(()=>{
|
2023-02-21 19:59:56 +08:00
|
|
|
func.apply(this, args);
|
2020-09-04 23:07:35 +08:00
|
|
|
timer = 0;
|
|
|
|
}, ms);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* No matter how many times the event is executed,
|
|
|
|
* the function will be executed only once, after the given amount of milliseconds.
|
|
|
|
*/
|
|
|
|
if (!Function.prototype.throttle) {
|
|
|
|
Function.prototype.throttle = function(ms) {
|
|
|
|
let func = this, timer;
|
|
|
|
return function(...args) {
|
2023-02-21 19:59:56 +08:00
|
|
|
timer = timer || setTimeout(()=>{
|
|
|
|
func.apply(this, args);
|
2020-09-04 23:07:35 +08:00
|
|
|
timer = 0;
|
|
|
|
}, ms);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-10-15 16:45:18 +08:00
|
|
|
})(document);
|