@maz-ui/utils 5.0.0-canary.ef64a02.0 → 5.2.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/helpers/TextareaAutogrow.js +25 -1
- package/dist/helpers/camelCase.js +6 -1
- package/dist/helpers/capitalize.js +6 -1
- package/dist/helpers/checkAvailability.js +16 -1
- package/dist/helpers/cookie.d.ts +38 -0
- package/dist/helpers/cookie.js +23 -0
- package/dist/helpers/countryCodeToUnicodeFlag.js +10 -1
- package/dist/helpers/debounce.js +11 -1
- package/dist/helpers/debounceCallback.js +7 -1
- package/dist/helpers/debounceId.js +24 -1
- package/dist/helpers/fetchLocaleIp.js +12 -1
- package/dist/helpers/formatCurrency.js +30 -1
- package/dist/helpers/formatDate.js +19 -1
- package/dist/helpers/formatJson.js +6 -1
- package/dist/helpers/formatNumber.js +18 -1
- package/dist/helpers/formatPhoneNumber.js +9 -1
- package/dist/helpers/getBrowserLocale.js +11 -1
- package/dist/helpers/getCountryFlagUrl.js +8 -1
- package/dist/helpers/getErrorMessage.js +6 -1
- package/dist/helpers/idleTimeout.js +121 -1
- package/dist/helpers/index.d.ts +6 -0
- package/dist/helpers/index.js +38 -1
- package/dist/helpers/isClient.js +6 -1
- package/dist/helpers/isEqual.js +21 -1
- package/dist/helpers/isIos.d.ts +1 -0
- package/dist/helpers/isIos.js +9 -0
- package/dist/helpers/isServer.js +6 -1
- package/dist/helpers/isStandaloneMode.js +10 -1
- package/dist/helpers/kebabCase.js +6 -1
- package/dist/helpers/normalizeString.js +87 -1
- package/dist/helpers/pascalCase.js +13 -1
- package/dist/helpers/preventIosInputZoom.d.ts +12 -0
- package/dist/helpers/preventIosInputZoom.js +15 -0
- package/dist/helpers/scriptLoader.js +38 -1
- package/dist/helpers/sleep.js +6 -1
- package/dist/helpers/snakeCase.js +6 -1
- package/dist/helpers/swipeHandler.d.ts +21 -13
- package/dist/helpers/swipeHandler.js +83 -1
- package/dist/helpers/throttle.js +13 -1
- package/dist/helpers/throttleId.js +20 -1
- package/dist/helpers/truthyFilter.js +6 -1
- package/dist/helpers/upperFirst.js +6 -1
- package/dist/helpers/userVisibility.js +47 -1
- package/dist/index.js +39 -1
- package/package.json +24 -4
package/README.md
CHANGED
|
@@ -1 +1,25 @@
|
|
|
1
|
-
import{debounce}from"./debounce.js";
|
|
1
|
+
import { debounce } from "./debounce.js";
|
|
2
|
+
//#region src/helpers/TextareaAutogrow.ts
|
|
3
|
+
var TextareaAutogrow = class {
|
|
4
|
+
element;
|
|
5
|
+
constructor(element) {
|
|
6
|
+
this.element = element, this.onFocus = this.onFocus.bind(this), this.autogrow = this.autogrow.bind(this), this.onResize = debounce(this.onResize.bind(this), 200), this.connect();
|
|
7
|
+
}
|
|
8
|
+
connect() {
|
|
9
|
+
this.element.addEventListener("focus", this.onFocus), this.element.style.resize = "none", this.element.style.boxSizing = "border-box";
|
|
10
|
+
}
|
|
11
|
+
disconnect() {
|
|
12
|
+
window.removeEventListener("resize", this.onResize), this.element.removeEventListener("input", this.autogrow);
|
|
13
|
+
}
|
|
14
|
+
onFocus() {
|
|
15
|
+
this.autogrow(), this.element.addEventListener("input", this.autogrow), window.addEventListener("resize", this.onResize), this.element.removeEventListener("focus", this.onFocus);
|
|
16
|
+
}
|
|
17
|
+
onResize() {
|
|
18
|
+
this.autogrow();
|
|
19
|
+
}
|
|
20
|
+
autogrow() {
|
|
21
|
+
this.element.style.height = "auto", this.element.style.overflow = "hidden", this.element.style.height = `${this.element.scrollHeight}px`;
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
//#endregion
|
|
25
|
+
export { TextareaAutogrow };
|
|
@@ -1 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
//#region src/helpers/camelCase.ts
|
|
2
|
+
function camelCase(str) {
|
|
3
|
+
return str.replaceAll(/-(\w)/g, (_, c) => c ? c.toUpperCase() : "");
|
|
4
|
+
}
|
|
5
|
+
//#endregion
|
|
6
|
+
export { camelCase };
|
|
@@ -1 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
//#region src/helpers/capitalize.ts
|
|
2
|
+
function capitalize(value) {
|
|
3
|
+
return typeof value == "string" ? value.charAt(0).toUpperCase() + value.slice(1) : "";
|
|
4
|
+
}
|
|
5
|
+
//#endregion
|
|
6
|
+
export { capitalize };
|
|
@@ -1 +1,16 @@
|
|
|
1
|
-
|
|
1
|
+
//#region src/helpers/checkAvailability.ts
|
|
2
|
+
function checkAvailability(getRef, callback, options) {
|
|
3
|
+
let opts = {
|
|
4
|
+
maxAttempts: 20,
|
|
5
|
+
interval: 100,
|
|
6
|
+
...options
|
|
7
|
+
};
|
|
8
|
+
let attempts = 0;
|
|
9
|
+
function check() {
|
|
10
|
+
let ref = getRef();
|
|
11
|
+
ref == null || opts.expectedValue !== void 0 && ref !== opts.expectedValue ? attempts < opts.maxAttempts ? (attempts++, setTimeout(check, opts.interval)) : opts.onError?.(Error(opts.errorMessage || `[maz-ui](checkAvailability) Nothing found after ${opts.maxAttempts} attempts`)) : callback(ref);
|
|
12
|
+
}
|
|
13
|
+
check();
|
|
14
|
+
}
|
|
15
|
+
//#endregion
|
|
16
|
+
export { checkAvailability };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
export type CookieSameSite = 'Lax' | 'Strict' | 'None' | 'lax' | 'strict' | 'none';
|
|
2
|
+
export interface CookieOptions {
|
|
3
|
+
/** Path attribute. Defaults to `'/'`. Pass `null` to omit. */
|
|
4
|
+
path?: string | null;
|
|
5
|
+
/** Domain attribute. */
|
|
6
|
+
domain?: string;
|
|
7
|
+
/** Lifetime in seconds. Defaults to one year. Pass `null` to omit (session cookie). */
|
|
8
|
+
maxAge?: number | null;
|
|
9
|
+
/** Explicit expiration date. Takes precedence over `maxAge` when both are set. */
|
|
10
|
+
expires?: Date;
|
|
11
|
+
/** Restrict the cookie to HTTPS connections. */
|
|
12
|
+
secure?: boolean;
|
|
13
|
+
/** Same-site policy. Defaults to `'Lax'`. Pass `null` to omit. */
|
|
14
|
+
sameSite?: CookieSameSite | null;
|
|
15
|
+
/** Mark the cookie as partitioned (CHIPS). */
|
|
16
|
+
partitioned?: boolean;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Read a cookie by key.
|
|
20
|
+
*
|
|
21
|
+
* - On the client, the cookie is read from `document.cookie`.
|
|
22
|
+
* - On the server, pass the raw `Cookie` request header via `cookieHeader`
|
|
23
|
+
* (e.g. from `useSSRContext().event.node.req.headers.cookie` in Nuxt) to
|
|
24
|
+
* resolve the value during SSR. Without it, the function returns `null`.
|
|
25
|
+
*/
|
|
26
|
+
export declare function getCookie(key: string, cookieHeader?: string): string | null;
|
|
27
|
+
/**
|
|
28
|
+
* Write a cookie via `document.cookie` with full attribute support.
|
|
29
|
+
*
|
|
30
|
+
* No-op on the server. `HttpOnly` is intentionally omitted as it cannot be set
|
|
31
|
+
* from `document.cookie` (browsers reject it).
|
|
32
|
+
*/
|
|
33
|
+
export declare function setCookie(key: string, value: string, options?: CookieOptions): void;
|
|
34
|
+
/**
|
|
35
|
+
* Delete a cookie by writing `max-age=0`. `path` and `domain` must match the
|
|
36
|
+
* cookie's original attributes for the browser to actually drop it.
|
|
37
|
+
*/
|
|
38
|
+
export declare function deleteCookie(key: string, options?: Pick<CookieOptions, 'path' | 'domain'>): void;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { isServer } from "./isServer.js";
|
|
2
|
+
//#region src/helpers/cookie.ts
|
|
3
|
+
var ONE_YEAR_SECONDS = 31536e3;
|
|
4
|
+
function getCookie(key, cookieHeader) {
|
|
5
|
+
let source = isServer() ? cookieHeader : document.cookie;
|
|
6
|
+
if (!source) return null;
|
|
7
|
+
let cookie = source.split(";").find((c) => c.trim().startsWith(`${key}=`));
|
|
8
|
+
return cookie ? decodeURIComponent(cookie.split("=")[1]) : null;
|
|
9
|
+
}
|
|
10
|
+
function setCookie(key, value, options = {}) {
|
|
11
|
+
if (isServer()) return;
|
|
12
|
+
let { path = "/", domain, maxAge = ONE_YEAR_SECONDS, expires, secure, sameSite = "Lax", partitioned } = options;
|
|
13
|
+
let parts = [`${key}=${encodeURIComponent(value)}`];
|
|
14
|
+
path != null && parts.push(`path=${path}`), domain && parts.push(`domain=${domain}`), maxAge != null && parts.push(`max-age=${maxAge}`), expires && parts.push(`expires=${expires.toUTCString()}`), secure && parts.push("secure"), sameSite != null && parts.push(`SameSite=${sameSite}`), partitioned && parts.push("partitioned"), document.cookie = parts.join("; ");
|
|
15
|
+
}
|
|
16
|
+
function deleteCookie(key, options = {}) {
|
|
17
|
+
setCookie(key, "", {
|
|
18
|
+
...options,
|
|
19
|
+
maxAge: 0
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
//#endregion
|
|
23
|
+
export { deleteCookie, getCookie, setCookie };
|
|
@@ -1 +1,10 @@
|
|
|
1
|
-
import{truthyFilter}from"./truthyFilter.js";
|
|
1
|
+
import { truthyFilter } from "./truthyFilter.js";
|
|
2
|
+
//#region src/helpers/countryCodeToUnicodeFlag.ts
|
|
3
|
+
function countryCodeToUnicodeFlag(locale) {
|
|
4
|
+
return Array.from(locale, (letter) => {
|
|
5
|
+
let code = letter.codePointAt(0);
|
|
6
|
+
return code ? code % 32 + 127461 : void 0;
|
|
7
|
+
}).filter(truthyFilter).map((n) => String.fromCodePoint(n)).join("");
|
|
8
|
+
}
|
|
9
|
+
//#endregion
|
|
10
|
+
export { countryCodeToUnicodeFlag };
|
package/dist/helpers/debounce.js
CHANGED
|
@@ -1 +1,11 @@
|
|
|
1
|
-
|
|
1
|
+
//#region src/helpers/debounce.ts
|
|
2
|
+
function debounce(fn, delay) {
|
|
3
|
+
let timeout;
|
|
4
|
+
return function(...args) {
|
|
5
|
+
clearTimeout(timeout), timeout = setTimeout(() => {
|
|
6
|
+
fn.apply(this, args);
|
|
7
|
+
}, delay);
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
//#endregion
|
|
11
|
+
export { debounce };
|
|
@@ -1 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
//#region src/helpers/debounceCallback.ts
|
|
2
|
+
var timeout = null;
|
|
3
|
+
function debounceCallback(callback, delay) {
|
|
4
|
+
timeout && clearTimeout(timeout), timeout = setTimeout(callback, delay);
|
|
5
|
+
}
|
|
6
|
+
//#endregion
|
|
7
|
+
export { debounceCallback };
|
|
@@ -1 +1,24 @@
|
|
|
1
|
-
|
|
1
|
+
//#region src/helpers/debounceId.ts
|
|
2
|
+
function debounceId(identifier, func, delay) {
|
|
3
|
+
let debouncedFunctions = {};
|
|
4
|
+
return function(...args) {
|
|
5
|
+
debouncedFunctions[identifier] || (debouncedFunctions[identifier] = {
|
|
6
|
+
timer: null,
|
|
7
|
+
promise: null
|
|
8
|
+
});
|
|
9
|
+
let debounced = debouncedFunctions[identifier];
|
|
10
|
+
return debounced.timer && clearTimeout(debounced.timer), debounced.promise = new Promise((resolve, reject) => {
|
|
11
|
+
debounced.timer = setTimeout(async () => {
|
|
12
|
+
try {
|
|
13
|
+
resolve(await func(...args));
|
|
14
|
+
} catch (error) {
|
|
15
|
+
reject(error);
|
|
16
|
+
} finally {
|
|
17
|
+
delete debouncedFunctions[identifier];
|
|
18
|
+
}
|
|
19
|
+
}, delay);
|
|
20
|
+
}), debounced.promise;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
//#endregion
|
|
24
|
+
export { debounceId };
|
|
@@ -1 +1,12 @@
|
|
|
1
|
-
|
|
1
|
+
//#region src/helpers/fetchLocaleIp.ts
|
|
2
|
+
async function fetchLocaleIp() {
|
|
3
|
+
try {
|
|
4
|
+
let { country_code } = await (await fetch("https://ipwho.is")).json();
|
|
5
|
+
return country_code;
|
|
6
|
+
} catch (error) {
|
|
7
|
+
console.error(`[maz-ui](fetchLocaleIp) ${error}`);
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
//#endregion
|
|
12
|
+
export { fetchLocaleIp };
|
|
@@ -1 +1,30 @@
|
|
|
1
|
-
|
|
1
|
+
//#region src/helpers/formatCurrency.ts
|
|
2
|
+
var DEFAULT_OPTIONS = {
|
|
3
|
+
style: "currency",
|
|
4
|
+
minimumFractionDigits: 2,
|
|
5
|
+
round: !1
|
|
6
|
+
};
|
|
7
|
+
function getFormattedCurrency(number, locale, options) {
|
|
8
|
+
let numberToFormat = +number;
|
|
9
|
+
return options.round && (numberToFormat = Math.floor(numberToFormat), options.minimumFractionDigits = 0), new Intl.NumberFormat(locale, options).format(numberToFormat);
|
|
10
|
+
}
|
|
11
|
+
function validRequiredAttributes(number, locale, options) {
|
|
12
|
+
if (number === void 0) throw TypeError("[maz-ui](FilterCurrency) The `number` attribute is required.");
|
|
13
|
+
if (locale === void 0) throw TypeError("[maz-ui](FilterCurrency) The `locale` attribute is required.");
|
|
14
|
+
if (typeof locale != "string") throw TypeError("[maz-ui](FilterCurrency) The `locale` attribute must be a string.");
|
|
15
|
+
if (options.currency === void 0) throw TypeError("[maz-ui](FilterCurrency) The `options.currency` attribute is required.");
|
|
16
|
+
}
|
|
17
|
+
function formatCurrency(number, locale, options) {
|
|
18
|
+
let options_ = {
|
|
19
|
+
...DEFAULT_OPTIONS,
|
|
20
|
+
...options
|
|
21
|
+
};
|
|
22
|
+
validRequiredAttributes(number, locale, options_);
|
|
23
|
+
try {
|
|
24
|
+
return getFormattedCurrency(number, locale, options_);
|
|
25
|
+
} catch (error) {
|
|
26
|
+
throw Error(`[maz-ui](FilterCurrency) ${error}`, { cause: error });
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
//#endregion
|
|
30
|
+
export { formatCurrency };
|
|
@@ -1 +1,19 @@
|
|
|
1
|
-
|
|
1
|
+
//#region src/helpers/formatDate.ts
|
|
2
|
+
var DEFAULT_OPTIONS = {
|
|
3
|
+
month: "short",
|
|
4
|
+
day: "numeric",
|
|
5
|
+
year: "numeric"
|
|
6
|
+
};
|
|
7
|
+
function formatDate(date, locale, options) {
|
|
8
|
+
if (locale === void 0) throw TypeError("[maz-ui](FilterDate) The `locale` attribute is required.");
|
|
9
|
+
if (typeof locale != "string") throw TypeError("[maz-ui](FilterDate) The `locale` attribute must be a string.");
|
|
10
|
+
let opts = options ?? DEFAULT_OPTIONS;
|
|
11
|
+
try {
|
|
12
|
+
let usedDate = date instanceof Date ? date : new Date(date);
|
|
13
|
+
return new Intl.DateTimeFormat(locale, opts).format(usedDate);
|
|
14
|
+
} catch (error) {
|
|
15
|
+
throw Error(`[maz-ui](FilterDate) ${error}`, { cause: error });
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
//#endregion
|
|
19
|
+
export { formatDate };
|
|
@@ -1 +1,18 @@
|
|
|
1
|
-
|
|
1
|
+
//#region src/helpers/formatNumber.ts
|
|
2
|
+
var DEFAULT_OPTIONS = { minimumFractionDigits: 2 };
|
|
3
|
+
function formatNumber(number, locale, options) {
|
|
4
|
+
let filterOptions = {
|
|
5
|
+
...DEFAULT_OPTIONS,
|
|
6
|
+
...options
|
|
7
|
+
};
|
|
8
|
+
if (number === void 0) throw TypeError("[maz-ui](FilterNumber) The `number` attribute is required.");
|
|
9
|
+
if (locale === void 0) throw TypeError("[maz-ui](FilterNumber) The `locale` attribute is required.");
|
|
10
|
+
if (typeof locale != "string") throw TypeError("[maz-ui](FilterNumber) The `locale` attribute must be a string.");
|
|
11
|
+
try {
|
|
12
|
+
return new Intl.NumberFormat(locale, filterOptions).format(Number(number));
|
|
13
|
+
} catch (error) {
|
|
14
|
+
throw Error(`[maz-ui](FilterNumber) ${error}`, { cause: error });
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
//#endregion
|
|
18
|
+
export { formatNumber };
|
|
@@ -1 +1,9 @@
|
|
|
1
|
-
import{parsePhoneNumberFromString}from"libphonenumber-js";
|
|
1
|
+
import { parsePhoneNumberFromString } from "libphonenumber-js";
|
|
2
|
+
//#region src/helpers/formatPhoneNumber.ts
|
|
3
|
+
function formatPhoneNumber(phoneNumber) {
|
|
4
|
+
if (!phoneNumber) throw TypeError("[maz-ui](formatPhoneNumber) The `phoneNumber` argument is required.");
|
|
5
|
+
let parsedPhone = parsePhoneNumberFromString(phoneNumber);
|
|
6
|
+
return parsedPhone ? parsedPhone.formatInternational() : phoneNumber;
|
|
7
|
+
}
|
|
8
|
+
//#endregion
|
|
9
|
+
export { formatPhoneNumber };
|
|
@@ -1 +1,11 @@
|
|
|
1
|
-
import{isServer}from"./isServer.js";
|
|
1
|
+
import { isServer } from "./isServer.js";
|
|
2
|
+
//#region src/helpers/getBrowserLocale.ts
|
|
3
|
+
function getBrowserLocale() {
|
|
4
|
+
try {
|
|
5
|
+
return isServer() ? void 0 : globalThis.navigator.language;
|
|
6
|
+
} catch (error) {
|
|
7
|
+
throw Error(`[MazInputPhoneNumber] (browserLocale) ${error}`, { cause: error });
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
//#endregion
|
|
11
|
+
export { getBrowserLocale };
|
|
@@ -1 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
//#region src/helpers/getCountryFlagUrl.ts
|
|
2
|
+
var _supportedCodes = /* @__PURE__ */ "ad.ae.af.ag.ai.al.am.ao.aq.ar.as.at.au.aw.ax.az.ba.bb.bd.be.bf.bg.bh.bi.bj.bl.bm.bn.bo.bq.br.bs.bt.bv.bw.by.bz.ca.cc.cd.cf.cg.ch.ci.ck.cl.cm.cn.co.cr.cu.cv.cw.cx.cy.cz.de.dj.dk.dm.do.dz.ec.ee.eg.eh.er.es.et.eu.fi.fj.fk.fm.fo.fr.ga.gb.gb-eng.gb-nir.gb-sct.gb-wls.gd.ge.gf.gg.gh.gi.gl.gm.gn.gp.gq.gr.gs.gt.gu.gw.gy.hk.hm.hn.hr.ht.hu.id.ie.il.im.in.io.iq.ir.is.it.je.jm.jo.jp.ke.kg.kh.ki.km.kn.kp.kr.kw.ky.kz.la.lb.lc.li.lk.lr.ls.lt.lu.lv.ly.ma.mc.md.me.mf.mg.mh.mk.ml.mm.mn.mo.mp.mq.mr.ms.mt.mu.mv.mw.mx.my.mz.na.nc.ne.nf.ng.ni.nl.no.np.nr.nu.nz.om.pa.pe.pf.pg.ph.pk.pl.pm.pn.pr.ps.pt.pw.py.qa.re.ro.rs.ru.rw.sa.sb.sc.sd.se.sg.sh.si.sj.sk.sl.sm.sn.so.sr.ss.st.sv.sx.sy.sz.tc.td.tf.tg.th.tj.tk.tl.tm.tn.to.tr.tt.tv.tw.tz.ua.ug.um.un.us.us-ak.us-al.us-ar.us-az.us-ca.us-co.us-ct.us-de.us-fl.us-ga.us-hi.us-ia.us-id.us-il.us-in.us-ks.us-ky.us-la.us-ma.us-md.us-me.us-mi.us-mn.us-mo.us-ms.us-mt.us-nc.us-nd.us-ne.us-nh.us-nj.us-nm.us-nv.us-ny.us-oh.us-ok.us-or.us-pa.us-ri.us-sc.us-sd.us-tn.us-tx.us-ut.us-va.us-vt.us-wa.us-wi.us-wv.us-wy.uy.uz.va.vc.ve.vg.vi.vn.vu.wf.ws.xk.ye.yt.za.zm.zw".split(".");
|
|
3
|
+
function getCountryFlagUrl(countryIsoCode, size) {
|
|
4
|
+
let code = countryIsoCode.toLowerCase();
|
|
5
|
+
if (_supportedCodes.includes(code)) return size ? `https://flagcdn.com/${size}/${code}.png` : `https://flagcdn.com/${code}.svg`;
|
|
6
|
+
}
|
|
7
|
+
//#endregion
|
|
8
|
+
export { getCountryFlagUrl };
|
|
@@ -1 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
//#region src/helpers/getErrorMessage.ts
|
|
2
|
+
function getErrorMessage(error) {
|
|
3
|
+
return error instanceof Error ? error.message : typeof error == "string" ? error : error && typeof error == "object" && "message" in error ? String(error.message) : error ? String(error) : "An unexpected error occurred";
|
|
4
|
+
}
|
|
5
|
+
//#endregion
|
|
6
|
+
export { getErrorMessage };
|
|
@@ -1 +1,121 @@
|
|
|
1
|
-
import{isClient}from"./isClient.js";
|
|
1
|
+
import { isClient } from "./isClient.js";
|
|
2
|
+
//#region src/helpers/idleTimeout.ts
|
|
3
|
+
var IdleTimeout = class {
|
|
4
|
+
callback;
|
|
5
|
+
defaultOptions = {
|
|
6
|
+
element: void 0,
|
|
7
|
+
timeout: 3e5,
|
|
8
|
+
once: !1,
|
|
9
|
+
immediate: !0
|
|
10
|
+
};
|
|
11
|
+
options;
|
|
12
|
+
timeoutHandler;
|
|
13
|
+
isIdle = !1;
|
|
14
|
+
isDestroy = !1;
|
|
15
|
+
startTime = 0;
|
|
16
|
+
remainingTime = 0;
|
|
17
|
+
lastClientX = -1;
|
|
18
|
+
lastClientY = -1;
|
|
19
|
+
eventNames = [
|
|
20
|
+
"DOMMouseScroll",
|
|
21
|
+
"mousedown",
|
|
22
|
+
"mousemove",
|
|
23
|
+
"mousewheel",
|
|
24
|
+
"MSPointerDown",
|
|
25
|
+
"MSPointerMove",
|
|
26
|
+
"keydown",
|
|
27
|
+
"touchmove",
|
|
28
|
+
"touchstart",
|
|
29
|
+
"wheel",
|
|
30
|
+
"click"
|
|
31
|
+
];
|
|
32
|
+
constructor(callback, options) {
|
|
33
|
+
this.callback = callback, this.options = {
|
|
34
|
+
...this.defaultOptions,
|
|
35
|
+
...options
|
|
36
|
+
}, isClient() && this.start();
|
|
37
|
+
}
|
|
38
|
+
get element() {
|
|
39
|
+
return this.options.element ?? document.body;
|
|
40
|
+
}
|
|
41
|
+
start() {
|
|
42
|
+
if (!isClient()) console.warn("[IdleTimeout](start) you should run this method on client side");
|
|
43
|
+
else {
|
|
44
|
+
for (let eventName of this.eventNames) this.element.addEventListener(eventName, this.handleEvent);
|
|
45
|
+
this.resetTimeout(), this.options.immediate && this.callback({
|
|
46
|
+
isIdle: !1,
|
|
47
|
+
instance: this
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
pause() {
|
|
52
|
+
let remainingTime = this.startTime + this.options.timeout - Date.now();
|
|
53
|
+
remainingTime <= 0 || (this.remainingTime = remainingTime, this.timeoutHandler &&= (clearTimeout(this.timeoutHandler), void 0));
|
|
54
|
+
}
|
|
55
|
+
resume() {
|
|
56
|
+
this.remainingTime <= 0 || (this.resetTimeout(), this.callback({
|
|
57
|
+
isIdle: this.isIdle,
|
|
58
|
+
instance: this
|
|
59
|
+
}), this.remainingTime = 0);
|
|
60
|
+
}
|
|
61
|
+
reset() {
|
|
62
|
+
this.isDestroy = !1, this.isIdle = !1, this.remainingTime = 0, this.resetTimeout(), this.callback({
|
|
63
|
+
isIdle: this.isIdle,
|
|
64
|
+
instance: this
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
destroy() {
|
|
68
|
+
if (!isClient()) console.warn("[IdleTimeout](destroy) you should run this method on client side");
|
|
69
|
+
else {
|
|
70
|
+
this.isDestroy = !0;
|
|
71
|
+
for (let eventName of this.eventNames) this.element.removeEventListener(eventName, this.handleEvent);
|
|
72
|
+
this.timeoutHandler && clearTimeout(this.timeoutHandler);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
resetTimeout() {
|
|
76
|
+
this.isIdle = !1, this.timeoutHandler &&= (clearTimeout(this.timeoutHandler), void 0), this.timeoutHandler = setTimeout(this.handleTimeout.bind(this), this.remainingTime || this.options.timeout), this.startTime = Date.now();
|
|
77
|
+
}
|
|
78
|
+
handleEvent = (event) => {
|
|
79
|
+
try {
|
|
80
|
+
if (this.remainingTime > 0) return;
|
|
81
|
+
if (event.type === "mousemove") {
|
|
82
|
+
let { clientX, clientY } = event;
|
|
83
|
+
if (clientX === void 0 && clientY === void 0 || clientX === this.lastClientX && clientY === this.lastClientY) return;
|
|
84
|
+
this.lastClientX = clientX, this.lastClientY = clientY;
|
|
85
|
+
}
|
|
86
|
+
this.resetTimeout(), this.callback({
|
|
87
|
+
isIdle: this.isIdle,
|
|
88
|
+
eventType: event.type,
|
|
89
|
+
instance: this
|
|
90
|
+
});
|
|
91
|
+
} catch (error) {
|
|
92
|
+
throw Error(`[IdleTimeout](handleEvent) ${error}`, { cause: error });
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
handleTimeout() {
|
|
96
|
+
this.isIdle = !0, this.callback({
|
|
97
|
+
isIdle: this.isIdle,
|
|
98
|
+
instance: this
|
|
99
|
+
}), this.options.once && this.destroy();
|
|
100
|
+
}
|
|
101
|
+
get destroyed() {
|
|
102
|
+
return this.isDestroy;
|
|
103
|
+
}
|
|
104
|
+
get timeout() {
|
|
105
|
+
return this.options.timeout;
|
|
106
|
+
}
|
|
107
|
+
set timeout(value) {
|
|
108
|
+
this.options.timeout = value;
|
|
109
|
+
}
|
|
110
|
+
get idle() {
|
|
111
|
+
return this.isIdle;
|
|
112
|
+
}
|
|
113
|
+
set idle(value) {
|
|
114
|
+
value ? this.handleTimeout() : this.reset(), this.callback({
|
|
115
|
+
isIdle: this.isIdle,
|
|
116
|
+
instance: this
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
//#endregion
|
|
121
|
+
export { IdleTimeout };
|
package/dist/helpers/index.d.ts
CHANGED
|
@@ -1,24 +1,30 @@
|
|
|
1
1
|
export * from './camelCase';
|
|
2
2
|
export * from './capitalize';
|
|
3
3
|
export * from './checkAvailability';
|
|
4
|
+
export * from './cookie';
|
|
4
5
|
export * from './countryCodeToUnicodeFlag';
|
|
5
6
|
export * from './debounce';
|
|
6
7
|
export * from './debounceCallback';
|
|
7
8
|
export * from './debounceId';
|
|
9
|
+
export * from './fetchLocaleIp';
|
|
8
10
|
export * from './formatCurrency';
|
|
9
11
|
export * from './formatDate';
|
|
10
12
|
export * from './formatJson';
|
|
11
13
|
export * from './formatNumber';
|
|
14
|
+
export * from './formatPhoneNumber';
|
|
15
|
+
export * from './getBrowserLocale';
|
|
12
16
|
export * from './getCountryFlagUrl';
|
|
13
17
|
export * from './getErrorMessage';
|
|
14
18
|
export * from './idleTimeout';
|
|
15
19
|
export * from './isClient';
|
|
16
20
|
export * from './isEqual';
|
|
21
|
+
export * from './isIos';
|
|
17
22
|
export * from './isServer';
|
|
18
23
|
export * from './isStandaloneMode';
|
|
19
24
|
export * from './kebabCase';
|
|
20
25
|
export * from './normalizeString';
|
|
21
26
|
export * from './pascalCase';
|
|
27
|
+
export * from './preventIosInputZoom';
|
|
22
28
|
export * from './scriptLoader';
|
|
23
29
|
export * from './sleep';
|
|
24
30
|
export * from './snakeCase';
|
package/dist/helpers/index.js
CHANGED
|
@@ -1 +1,38 @@
|
|
|
1
|
-
import{camelCase}from"./camelCase.js";
|
|
1
|
+
import { camelCase } from "./camelCase.js";
|
|
2
|
+
import { capitalize } from "./capitalize.js";
|
|
3
|
+
import { checkAvailability } from "./checkAvailability.js";
|
|
4
|
+
import { isServer } from "./isServer.js";
|
|
5
|
+
import { deleteCookie, getCookie, setCookie } from "./cookie.js";
|
|
6
|
+
import { truthyFilter } from "./truthyFilter.js";
|
|
7
|
+
import { countryCodeToUnicodeFlag } from "./countryCodeToUnicodeFlag.js";
|
|
8
|
+
import { debounce } from "./debounce.js";
|
|
9
|
+
import { debounceCallback } from "./debounceCallback.js";
|
|
10
|
+
import { debounceId } from "./debounceId.js";
|
|
11
|
+
import { fetchLocaleIp } from "./fetchLocaleIp.js";
|
|
12
|
+
import { formatCurrency } from "./formatCurrency.js";
|
|
13
|
+
import { formatDate } from "./formatDate.js";
|
|
14
|
+
import { formatJson } from "./formatJson.js";
|
|
15
|
+
import { formatNumber } from "./formatNumber.js";
|
|
16
|
+
import { formatPhoneNumber } from "./formatPhoneNumber.js";
|
|
17
|
+
import { getBrowserLocale } from "./getBrowserLocale.js";
|
|
18
|
+
import { getCountryFlagUrl } from "./getCountryFlagUrl.js";
|
|
19
|
+
import { getErrorMessage } from "./getErrorMessage.js";
|
|
20
|
+
import { isClient } from "./isClient.js";
|
|
21
|
+
import { IdleTimeout } from "./idleTimeout.js";
|
|
22
|
+
import { isEqual } from "./isEqual.js";
|
|
23
|
+
import { isIos } from "./isIos.js";
|
|
24
|
+
import { isStandaloneMode } from "./isStandaloneMode.js";
|
|
25
|
+
import { kebabCase } from "./kebabCase.js";
|
|
26
|
+
import { pascalCase } from "./pascalCase.js";
|
|
27
|
+
import { snakeCase } from "./snakeCase.js";
|
|
28
|
+
import { normalizeString } from "./normalizeString.js";
|
|
29
|
+
import { addViewportMaximumScale, preventIosInputZoom } from "./preventIosInputZoom.js";
|
|
30
|
+
import { ScriptLoader } from "./scriptLoader.js";
|
|
31
|
+
import { sleep } from "./sleep.js";
|
|
32
|
+
import { Swipe } from "./swipeHandler.js";
|
|
33
|
+
import { TextareaAutogrow } from "./TextareaAutogrow.js";
|
|
34
|
+
import { throttle } from "./throttle.js";
|
|
35
|
+
import { throttleId } from "./throttleId.js";
|
|
36
|
+
import { upperFirst } from "./upperFirst.js";
|
|
37
|
+
import { UserVisibility } from "./userVisibility.js";
|
|
38
|
+
export { IdleTimeout, ScriptLoader, Swipe, TextareaAutogrow, UserVisibility, addViewportMaximumScale, camelCase, capitalize, checkAvailability, countryCodeToUnicodeFlag, debounce, debounceCallback, debounceId, deleteCookie, fetchLocaleIp, formatCurrency, formatDate, formatJson, formatNumber, formatPhoneNumber, getBrowserLocale, getCookie, getCountryFlagUrl, getErrorMessage, isClient, isEqual, isIos, isServer, isStandaloneMode, kebabCase, normalizeString, pascalCase, preventIosInputZoom, setCookie, sleep, snakeCase, throttle, throttleId, truthyFilter, upperFirst };
|
package/dist/helpers/isClient.js
CHANGED
package/dist/helpers/isEqual.js
CHANGED
|
@@ -1 +1,21 @@
|
|
|
1
|
-
|
|
1
|
+
//#region src/helpers/isEqual.ts
|
|
2
|
+
function isPrimitive(value) {
|
|
3
|
+
return value == null || typeof value == "string" || typeof value == "number" || typeof value == "boolean" || typeof value == "symbol" || typeof value == "bigint";
|
|
4
|
+
}
|
|
5
|
+
function isEqualArrays(a, b) {
|
|
6
|
+
if (a.length !== b.length) return !1;
|
|
7
|
+
for (let [i, element] of a.entries()) if (!isEqual(element, b[i])) return !1;
|
|
8
|
+
return !0;
|
|
9
|
+
}
|
|
10
|
+
function isEqualObjects(a, b) {
|
|
11
|
+
let keysA = Object.keys(a);
|
|
12
|
+
let keysB = Object.keys(b);
|
|
13
|
+
if (keysA.length !== keysB.length) return !1;
|
|
14
|
+
for (let key of keysA) if (!keysB.includes(key) || !isEqual(a[key], b[key])) return !1;
|
|
15
|
+
return !0;
|
|
16
|
+
}
|
|
17
|
+
function isEqual(a, b) {
|
|
18
|
+
return isPrimitive(a) || isPrimitive(b) ? a === b : a instanceof Date && b instanceof Date ? a.getTime() === b.getTime() : typeof a != typeof b || Array.isArray(a) !== Array.isArray(b) ? !1 : Array.isArray(a) && Array.isArray(b) ? isEqualArrays(a, b) : typeof a == "object" && typeof b == "object" && isEqualObjects(a, b);
|
|
19
|
+
}
|
|
20
|
+
//#endregion
|
|
21
|
+
export { isEqual };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function isIos(): boolean;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { isClient } from "./isClient.js";
|
|
2
|
+
//#region src/helpers/isIos.ts
|
|
3
|
+
function isIos() {
|
|
4
|
+
if (!isClient()) return !1;
|
|
5
|
+
let userAgent = navigator.userAgent;
|
|
6
|
+
return /iPhone|iPad|iPod/.test(userAgent) || /Macintosh/.test(userAgent) && navigator.maxTouchPoints > 1;
|
|
7
|
+
}
|
|
8
|
+
//#endregion
|
|
9
|
+
export { isIos };
|
package/dist/helpers/isServer.js
CHANGED
|
@@ -1 +1,10 @@
|
|
|
1
|
-
import{isClient}from"./isClient.js";
|
|
1
|
+
import { isClient } from "./isClient.js";
|
|
2
|
+
//#region src/helpers/isStandaloneMode.ts
|
|
3
|
+
function isStandaloneMode() {
|
|
4
|
+
if (!isClient()) return !1;
|
|
5
|
+
let nav = navigator;
|
|
6
|
+
let isStandalone = globalThis.matchMedia("(display-mode: standalone)").matches;
|
|
7
|
+
return nav.standalone || isStandalone;
|
|
8
|
+
}
|
|
9
|
+
//#endregion
|
|
10
|
+
export { isStandaloneMode };
|
|
@@ -1 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
//#region src/helpers/kebabCase.ts
|
|
2
|
+
function kebabCase(str) {
|
|
3
|
+
return str.replace(/([a-z])([A-Z])/g, "$1-$2").replace(/([A-Z])([A-Z][a-z])/g, "$1-$2").replace(/[\s_]+/g, "-").toLowerCase().replace(/-+/g, "-").replace(/(^-)|(-$)/g, "");
|
|
4
|
+
}
|
|
5
|
+
//#endregion
|
|
6
|
+
export { kebabCase };
|