@maz-ui/utils 4.0.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.
Files changed (108) hide show
  1. package/LICENSE +21 -0
  2. package/dist/formatters/camelCase.js +6 -0
  3. package/dist/formatters/capitalize.js +6 -0
  4. package/dist/formatters/formatCurrency.js +34 -0
  5. package/dist/formatters/formatDate.js +21 -0
  6. package/dist/formatters/formatNumber.js +23 -0
  7. package/dist/formatters/formatPhoneNumber.js +10 -0
  8. package/dist/formatters/index.js +14 -0
  9. package/dist/formatters/pascalCase.js +8 -0
  10. package/dist/index.js +54 -0
  11. package/dist/types/formatters/camelCase.d.ts +2 -0
  12. package/dist/types/formatters/camelCase.d.ts.map +1 -0
  13. package/dist/types/formatters/capitalize.d.ts +2 -0
  14. package/dist/types/formatters/capitalize.d.ts.map +1 -0
  15. package/dist/types/formatters/formatCurrency.d.ts +5 -0
  16. package/dist/types/formatters/formatCurrency.d.ts.map +1 -0
  17. package/dist/types/formatters/formatDate.d.ts +2 -0
  18. package/dist/types/formatters/formatDate.d.ts.map +1 -0
  19. package/dist/types/formatters/formatNumber.d.ts +2 -0
  20. package/dist/types/formatters/formatNumber.d.ts.map +1 -0
  21. package/dist/types/formatters/formatPhoneNumber.d.ts +2 -0
  22. package/dist/types/formatters/formatPhoneNumber.d.ts.map +1 -0
  23. package/dist/types/formatters/index.d.ts +7 -0
  24. package/dist/types/formatters/index.d.ts.map +1 -0
  25. package/dist/types/formatters/pascalCase.d.ts +2 -0
  26. package/dist/types/formatters/pascalCase.d.ts.map +1 -0
  27. package/dist/types/index.d.ts +4 -0
  28. package/dist/types/index.d.ts.map +1 -0
  29. package/dist/types/ts-helpers/DeepKeyOf.d.ts +4 -0
  30. package/dist/types/ts-helpers/DeepKeyOf.d.ts.map +1 -0
  31. package/dist/types/ts-helpers/DeepPartial.d.ts +4 -0
  32. package/dist/types/ts-helpers/DeepPartial.d.ts.map +1 -0
  33. package/dist/types/ts-helpers/FlattenObjectKeys.d.ts +4 -0
  34. package/dist/types/ts-helpers/FlattenObjectKeys.d.ts.map +1 -0
  35. package/dist/types/ts-helpers/InferMaybeRef.d.ts +3 -0
  36. package/dist/types/ts-helpers/InferMaybeRef.d.ts.map +1 -0
  37. package/dist/types/ts-helpers/index.d.ts +5 -0
  38. package/dist/types/ts-helpers/index.d.ts.map +1 -0
  39. package/dist/types/utils/TextareaAutogrow.d.ts +10 -0
  40. package/dist/types/utils/TextareaAutogrow.d.ts.map +1 -0
  41. package/dist/types/utils/checkAvailability.d.ts +8 -0
  42. package/dist/types/utils/checkAvailability.d.ts.map +1 -0
  43. package/dist/types/utils/countryCodeToUnicodeFlag.d.ts +2 -0
  44. package/dist/types/utils/countryCodeToUnicodeFlag.d.ts.map +1 -0
  45. package/dist/types/utils/debounce.d.ts +2 -0
  46. package/dist/types/utils/debounce.d.ts.map +1 -0
  47. package/dist/types/utils/debounceCallback.d.ts +2 -0
  48. package/dist/types/utils/debounceCallback.d.ts.map +1 -0
  49. package/dist/types/utils/debounceId.d.ts +7 -0
  50. package/dist/types/utils/debounceId.d.ts.map +1 -0
  51. package/dist/types/utils/fetchLocaleIp.d.ts +40 -0
  52. package/dist/types/utils/fetchLocaleIp.d.ts.map +1 -0
  53. package/dist/types/utils/getBrowserLocale.d.ts +2 -0
  54. package/dist/types/utils/getBrowserLocale.d.ts.map +1 -0
  55. package/dist/types/utils/getCountryFlagUrl.d.ts +9 -0
  56. package/dist/types/utils/getCountryFlagUrl.d.ts.map +1 -0
  57. package/dist/types/utils/idleTimeout.d.ts +57 -0
  58. package/dist/types/utils/idleTimeout.d.ts.map +1 -0
  59. package/dist/types/utils/index.d.ts +21 -0
  60. package/dist/types/utils/index.d.ts.map +1 -0
  61. package/dist/types/utils/isClient.d.ts +2 -0
  62. package/dist/types/utils/isClient.d.ts.map +1 -0
  63. package/dist/types/utils/isEqual.d.ts +2 -0
  64. package/dist/types/utils/isEqual.d.ts.map +1 -0
  65. package/dist/types/utils/isServer.d.ts +2 -0
  66. package/dist/types/utils/isServer.d.ts.map +1 -0
  67. package/dist/types/utils/isStandaloneMode.d.ts +2 -0
  68. package/dist/types/utils/isStandaloneMode.d.ts.map +1 -0
  69. package/dist/types/utils/normalizeString.d.ts +48 -0
  70. package/dist/types/utils/normalizeString.d.ts.map +1 -0
  71. package/dist/types/utils/scriptLoader.d.ts +20 -0
  72. package/dist/types/utils/scriptLoader.d.ts.map +1 -0
  73. package/dist/types/utils/sleep.d.ts +2 -0
  74. package/dist/types/utils/sleep.d.ts.map +1 -0
  75. package/dist/types/utils/swipeHandler.d.ts +103 -0
  76. package/dist/types/utils/swipeHandler.d.ts.map +1 -0
  77. package/dist/types/utils/throttle.d.ts +2 -0
  78. package/dist/types/utils/throttle.d.ts.map +1 -0
  79. package/dist/types/utils/throttleId.d.ts +7 -0
  80. package/dist/types/utils/throttleId.d.ts.map +1 -0
  81. package/dist/types/utils/truthyFilter.d.ts +3 -0
  82. package/dist/types/utils/truthyFilter.d.ts.map +1 -0
  83. package/dist/types/utils/userVisibility.d.ts +40 -0
  84. package/dist/types/utils/userVisibility.d.ts.map +1 -0
  85. package/dist/utils/TextareaAutogrow.js +25 -0
  86. package/dist/utils/checkAvailability.js +16 -0
  87. package/dist/utils/countryCodeToUnicodeFlag.js +10 -0
  88. package/dist/utils/debounce.js +11 -0
  89. package/dist/utils/debounceCallback.js +7 -0
  90. package/dist/utils/debounceId.js +21 -0
  91. package/dist/utils/fetchLocaleIp.js +12 -0
  92. package/dist/utils/getBrowserLocale.js +11 -0
  93. package/dist/utils/getCountryFlagUrl.js +9 -0
  94. package/dist/utils/idleTimeout.js +111 -0
  95. package/dist/utils/index.js +42 -0
  96. package/dist/utils/isClient.js +6 -0
  97. package/dist/utils/isEqual.js +26 -0
  98. package/dist/utils/isServer.js +6 -0
  99. package/dist/utils/isStandaloneMode.js +10 -0
  100. package/dist/utils/normalizeString.js +69 -0
  101. package/dist/utils/scriptLoader.js +43 -0
  102. package/dist/utils/sleep.js +6 -0
  103. package/dist/utils/swipeHandler.js +80 -0
  104. package/dist/utils/throttle.js +11 -0
  105. package/dist/utils/throttleId.js +19 -0
  106. package/dist/utils/truthyFilter.js +6 -0
  107. package/dist/utils/userVisibility.js +50 -0
  108. package/package.json +88 -0
@@ -0,0 +1,69 @@
1
+ const l = {
2
+ removeAccents: !0,
3
+ caseSensitive: !1,
4
+ replaceSpaces: !0,
5
+ removeSpecialCharacters: !1,
6
+ trim: !0,
7
+ normalizeSpaces: !0,
8
+ removeNumbers: !1,
9
+ customNormalizationForms: ["NFC", "NFKD"]
10
+ };
11
+ function s(o, i) {
12
+ const r = { ...l, ...i }, t = {
13
+ À: "A",
14
+ Á: "A",
15
+ Â: "A",
16
+ Ã: "A",
17
+ Ä: "A",
18
+ Å: "A",
19
+ à: "a",
20
+ á: "a",
21
+ â: "a",
22
+ ã: "a",
23
+ ä: "a",
24
+ å: "a",
25
+ È: "E",
26
+ É: "E",
27
+ Ê: "E",
28
+ Ë: "E",
29
+ è: "e",
30
+ é: "e",
31
+ ê: "e",
32
+ ë: "e",
33
+ Î: "I",
34
+ Ï: "I",
35
+ í: "I",
36
+ î: "i",
37
+ ï: "i",
38
+ Ô: "O",
39
+ Õ: "O",
40
+ Ö: "O",
41
+ Ø: "O",
42
+ ô: "o",
43
+ õ: "o",
44
+ ö: "o",
45
+ ø: "o",
46
+ Ù: "U",
47
+ Ú: "U",
48
+ Û: "U",
49
+ Ü: "U",
50
+ ù: "u",
51
+ ú: "u",
52
+ û: "u",
53
+ ü: "u",
54
+ Ç: "C",
55
+ ç: "c",
56
+ ÿ: "y",
57
+ Ñ: "N",
58
+ ñ: "n",
59
+ ó: "o"
60
+ };
61
+ let e = o.toString();
62
+ if (r.trim && (e = e.trim()), r.normalizeSpaces && (e = e.replaceAll(/\s+/g, " ")), r.replaceSpaces && (e = e.replaceAll(" ", "-")), r.removeNumbers && (e = e.replaceAll(/\d/g, "")), r.removeAccents && (e = e.replaceAll(/[ÀÁÂÃÄÅÇÈÉÊËÎÏÑÔÕÖØÙÚÛÜàáâãäåçèéêëíîïñóôõöøùúûüÿ]/g, (a) => t[a] || a), e = e.replaceAll(/[\u0300-\u036F]/g, "")), r.caseSensitive === !1 && (e = e.toLowerCase()), r.removeSpecialCharacters && (e = e.replaceAll(/[^\dA-Z-]/gi, "")), r.trim && (e = e.trim()), r.customNormalizationForms)
63
+ for (const a of r.customNormalizationForms)
64
+ e = e.normalize(a);
65
+ return e;
66
+ }
67
+ export {
68
+ s as normalizeString
69
+ };
@@ -0,0 +1,43 @@
1
+ class s {
2
+ src;
3
+ script;
4
+ once;
5
+ async;
6
+ defer;
7
+ identifier;
8
+ constructor({ src: e, identifier: t, once: n = !0, async: r = !0, defer: i = !0 }) {
9
+ if (typeof window > "u")
10
+ throw new TypeError("[ScriptLoader]: Is supported only on browser side");
11
+ if (!e)
12
+ throw new Error('[ScriptLoader]: You should provide the attribut "src"');
13
+ if (!t)
14
+ throw new Error('[ScriptLoader]: You should provide the attribut "identifier"');
15
+ this.src = e, this.identifier = t, this.once = n, this.async = r, this.defer = i;
16
+ }
17
+ removeTag(e) {
18
+ typeof e == "string" ? document.head.querySelector(`[data-identifier="${e}"]`)?.remove() : e.remove();
19
+ }
20
+ load() {
21
+ const e = window, t = document.head.querySelectorAll(`[data-identifier="${this.identifier}"]`);
22
+ if (this.once && e[this.identifier] && t.length > 0)
23
+ return this.script = e[this.identifier], Promise.resolve(this.script);
24
+ if (!this.once && t.length > 0)
25
+ for (const n of t)
26
+ this.removeTag(n);
27
+ return this.injectScript();
28
+ }
29
+ injectScript() {
30
+ const e = window;
31
+ return new Promise((t, n) => {
32
+ try {
33
+ const r = document.createElement("script");
34
+ r.src = this.src, r.async = this.async, r.defer = this.defer, r.dataset.identifier = this.identifier, r.addEventListener("error", (i) => n(new Error(`[ScriptLoader](injectScript) ${i.message}`))), r.addEventListener("load", (i) => (this.script = i, e[this.identifier] = i, t(i))), document.head.append(r);
35
+ } catch (r) {
36
+ throw new Error(`[ScriptLoader](init) ${r}`);
37
+ }
38
+ });
39
+ }
40
+ }
41
+ export {
42
+ s as ScriptLoader
43
+ };
@@ -0,0 +1,6 @@
1
+ function n(e) {
2
+ return new Promise((t) => setTimeout(t, e));
3
+ }
4
+ export {
5
+ n as sleep
6
+ };
@@ -0,0 +1,80 @@
1
+ const s = {
2
+ preventDefaultOnTouchMove: !1,
3
+ preventDefaultOnMouseWheel: !1,
4
+ threshold: 50,
5
+ immediate: !1,
6
+ triggerOnEnd: !1
7
+ };
8
+ class n {
9
+ constructor(t) {
10
+ this.inputOption = t, this.options = { ...s, ...t }, this.onToucheStartCallback = this.toucheStartHandler.bind(this), this.onToucheMoveCallback = this.handleTouchMove.bind(this), this.onToucheEndCallback = this.handleTouchEnd.bind(this), this.onMouseWheelCallback = this.handleMouseWheel.bind(this), this.start = this.startListening.bind(this), this.stop = this.stopListening.bind(this), this.options.element && this.setElement(this.options.element), this.options.immediate && this.start();
11
+ }
12
+ element;
13
+ xStart;
14
+ yStart;
15
+ xEnd;
16
+ yEnd;
17
+ xDiff;
18
+ yDiff;
19
+ onToucheStartCallback;
20
+ onToucheMoveCallback;
21
+ onToucheEndCallback;
22
+ onMouseWheelCallback;
23
+ start;
24
+ stop;
25
+ options;
26
+ startListening() {
27
+ this.setElement(this.options.element), this.element?.addEventListener("touchstart", this.onToucheStartCallback, { passive: !0 }), this.element?.addEventListener("touchmove", this.onToucheMoveCallback, { passive: !0 }), this.options.triggerOnEnd && this.element?.addEventListener("touchend", this.onToucheEndCallback, { passive: !0 }), this.options.preventDefaultOnMouseWheel && this.element?.addEventListener("mousewheel", this.onMouseWheelCallback, { passive: !1 });
28
+ }
29
+ stopListening() {
30
+ this.element?.removeEventListener("touchstart", this.onToucheStartCallback), this.element?.removeEventListener("touchmove", this.onToucheMoveCallback), this.element?.removeEventListener("touchend", this.onToucheEndCallback), this.options.preventDefaultOnMouseWheel && this.element?.removeEventListener("mousewheel", this.onMouseWheelCallback);
31
+ }
32
+ setElement(t) {
33
+ if (!t) {
34
+ console.error(
35
+ "[maz-ui][SwipeHandler](setElement) Element should be provided. Its can be a string selector or an HTMLElement"
36
+ );
37
+ return;
38
+ }
39
+ if (typeof t == "string") {
40
+ const e = document.querySelector(t);
41
+ if (!(e instanceof HTMLElement)) {
42
+ console.error("[maz-ui][SwipeHandler](setElement) String selector for element is not found");
43
+ return;
44
+ }
45
+ this.element = e;
46
+ } else
47
+ this.element = t;
48
+ }
49
+ handleMouseWheel(t) {
50
+ t.preventDefault();
51
+ }
52
+ toucheStartHandler(t) {
53
+ this.xStart = t.touches[0].clientX, this.yStart = t.touches[0].clientY, this.emitValuesChanged();
54
+ }
55
+ emitValuesChanged() {
56
+ this.options.onValuesChanged?.({
57
+ xStart: this.xStart,
58
+ yStart: this.yStart,
59
+ xEnd: this.xEnd,
60
+ yEnd: this.yEnd,
61
+ xDiff: this.xDiff,
62
+ yDiff: this.yDiff
63
+ });
64
+ }
65
+ handleTouchMove(t) {
66
+ this.options.preventDefaultOnTouchMove && t.cancelable && t.preventDefault(), this.xEnd = t.touches[0].clientX, this.yEnd = t.touches[0].clientY, !(!this.xStart || !this.yStart) && (this.xDiff = this.xStart - this.xEnd, this.yDiff = this.yStart - this.yEnd, this.emitValuesChanged(), this.options.triggerOnEnd || this.runCallbacks(t));
67
+ }
68
+ handleTouchEnd(t) {
69
+ this.runCallbacks(t), this.emitValuesChanged();
70
+ }
71
+ runCallbacks(t) {
72
+ typeof this.xDiff != "number" || typeof this.yDiff != "number" || Math.abs(this.xDiff) < this.options.threshold && Math.abs(this.yDiff) < this.options.threshold || (Math.abs(this.xDiff) > Math.abs(this.yDiff) ? this.xDiff > 0 ? this.options.onLeft?.(t) : this.options.onRight?.(t) : this.yDiff > 0 ? this.options.onUp?.(t) : this.options.onDown?.(t), this.resetValues());
73
+ }
74
+ resetValues() {
75
+ this.xStart = void 0, this.yStart = void 0, this.xEnd = void 0, this.yEnd = void 0, this.xDiff = void 0, this.yDiff = void 0, this.emitValuesChanged();
76
+ }
77
+ }
78
+ export {
79
+ n as Swipe
80
+ };
@@ -0,0 +1,11 @@
1
+ function i(e, a) {
2
+ let l = !1, n, t;
3
+ return function(...o) {
4
+ l ? (clearTimeout(n), n = setTimeout(() => {
5
+ Date.now() - t >= a && (e.apply(this, o), t = Date.now());
6
+ }, Math.max(a - (Date.now() - t), 0))) : (e.apply(this, o), t = Date.now(), l = !0);
7
+ };
8
+ }
9
+ export {
10
+ i as throttle
11
+ };
@@ -0,0 +1,19 @@
1
+ function c(s, o, t) {
2
+ const l = {};
3
+ return async (...m) => {
4
+ const a = Date.now();
5
+ return l[s] || (l[s] = { promise: null, lastCall: 0, lastArgs: [] }), a - l[s].lastCall >= t ? (l[s].lastCall = a, o(...m)) : (l[s].lastArgs = m, l[s].promise || (l[s].promise = new Promise((u) => {
6
+ setTimeout(
7
+ async () => {
8
+ l[s].lastCall = Date.now();
9
+ const p = await o(...l[s].lastArgs);
10
+ l[s].promise = null, u(p);
11
+ },
12
+ t - (a - l[s].lastCall)
13
+ );
14
+ })), l[s].promise);
15
+ };
16
+ }
17
+ export {
18
+ c as throttleId
19
+ };
@@ -0,0 +1,6 @@
1
+ function r(t) {
2
+ return !!t;
3
+ }
4
+ export {
5
+ r as truthyFilter
6
+ };
@@ -0,0 +1,50 @@
1
+ import { isClient as i } from "./isClient.js";
2
+ class o {
3
+ constructor(t, e) {
4
+ this.callback = t, this.options = {
5
+ ...this.defaultOptions,
6
+ ...e
7
+ }, this.eventHandlerFunction = this.eventHandler.bind(this), i() && this.start();
8
+ }
9
+ eventHandlerFunction;
10
+ event = "visibilitychange";
11
+ timeoutHandler;
12
+ options;
13
+ defaultOptions = {
14
+ timeout: 5e3,
15
+ once: !1,
16
+ immediate: !0
17
+ };
18
+ isVisible = !1;
19
+ start() {
20
+ if (!i()) {
21
+ console.warn("[UserVisibility](start) you should run this method on client side");
22
+ return;
23
+ }
24
+ this.options.immediate && this.emitCallback(), this.addEventListener();
25
+ }
26
+ emitCallback() {
27
+ this.isVisible = document.visibilityState === "visible", this.callback({ isVisible: this.isVisible }), this.options.once && this.destroy();
28
+ }
29
+ eventHandler() {
30
+ document.visibilityState === "visible" && !this.isVisible ? (this.clearTimeout(), this.emitCallback()) : document.visibilityState === "hidden" && this.initTimeout();
31
+ }
32
+ clearTimeout() {
33
+ this.timeoutHandler && (clearTimeout(this.timeoutHandler), this.timeoutHandler = void 0);
34
+ }
35
+ initTimeout() {
36
+ this.clearTimeout(), this.timeoutHandler = setTimeout(this.emitCallback.bind(this), this.options.timeout);
37
+ }
38
+ addEventListener() {
39
+ document.addEventListener(this.event, this.eventHandlerFunction);
40
+ }
41
+ removeEventListener() {
42
+ document.removeEventListener(this.event, this.eventHandlerFunction);
43
+ }
44
+ destroy() {
45
+ this.removeEventListener(), this.timeoutHandler && clearTimeout(this.timeoutHandler);
46
+ }
47
+ }
48
+ export {
49
+ o as UserVisibility
50
+ };
package/package.json ADDED
@@ -0,0 +1,88 @@
1
+ {
2
+ "name": "@maz-ui/utils",
3
+ "type": "module",
4
+ "version": "4.0.0-beta.0",
5
+ "description": "Utils of maz-ui for JavaScript/TypeScript users",
6
+ "author": "Louis Mazel <me@loicmazuel.com>",
7
+ "license": "MIT",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/LouisMazel/maz-ui.git"
11
+ },
12
+ "bugs": "https://github.com/LouisMazel/maz-ui/issues",
13
+ "keywords": [
14
+ "utils",
15
+ "javascript",
16
+ "typescript",
17
+ "maz-ui"
18
+ ],
19
+ "publishConfig": {
20
+ "access": "public"
21
+ },
22
+ "exports": {
23
+ ".": {
24
+ "types": "./dist/types/index.d.ts",
25
+ "import": "./dist/index.js",
26
+ "module": "./dist/index.js",
27
+ "default": "./dist/index.js"
28
+ },
29
+ "./utils": {
30
+ "types": "./dist/types/utils/index.d.ts",
31
+ "import": "./dist/utils/index.js",
32
+ "module": "./dist/utils/index.js",
33
+ "default": "./dist/utils/index.js"
34
+ },
35
+ "./formatters": {
36
+ "types": "./dist/types/formatters/index.d.ts",
37
+ "import": "./dist/formatters/index.js",
38
+ "module": "./dist/formatters/index.js",
39
+ "default": "./dist/formatters/index.js"
40
+ },
41
+ "./utils/*": {
42
+ "types": "./dist/types/utils/*.d.ts",
43
+ "import": "./dist/utils/*.js",
44
+ "module": "./dist/utils/*.js",
45
+ "default": "./dist/utils/*.js"
46
+ },
47
+ "./formatters/*": {
48
+ "types": "./dist/types/formatters/*.d.ts",
49
+ "import": "./dist/formatters/*.js",
50
+ "module": "./dist/formatters/*.js",
51
+ "default": "./dist/formatters/*.js"
52
+ },
53
+ "./*": "./*"
54
+ },
55
+ "main": "./dist/index.js",
56
+ "types": "./dist/index.d.ts",
57
+ "files": [
58
+ "LICENSE",
59
+ "README.md",
60
+ "dist"
61
+ ],
62
+ "engines": {
63
+ "node": ">=18.0.0"
64
+ },
65
+ "scripts": {
66
+ "build": "vite build",
67
+ "dev": "vite build --watch",
68
+ "typecheck": "tsc --noEmit --skipLibCheck",
69
+ "lint": "eslint .",
70
+ "lint:fix": "eslint . --fix",
71
+ "pre-commit": "lint-staged"
72
+ },
73
+ "devDependencies": {
74
+ "eslint": "^9.29.0",
75
+ "eslint-plugin-format": "^1.0.1",
76
+ "glob": "catalog:",
77
+ "libphonenumber-js": "catalog:",
78
+ "typescript": "^5.8.3",
79
+ "vite": "^6.3.5",
80
+ "vue": "catalog:"
81
+ },
82
+ "lint-staged": {
83
+ "*.{js,jsx,ts,tsx,mjs,mts,cjs,md}": [
84
+ "eslint --fix"
85
+ ]
86
+ },
87
+ "gitHead": "9ef7beb4feeee0b0cb44e7df8af7115d5d5ef334"
88
+ }