1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
4 |
|
5 | const last = (v = []) => v[v.length - 1];
|
6 |
|
7 | const isWhitespace = (node) => {
|
8 | return node.nodeType === node.TEXT_NODE && node.nodeValue.trim() === ""
|
9 | };
|
10 |
|
11 | const walk = (node, callback, deep = true) => {
|
12 | if (!node) return
|
13 | if (!isWhitespace(node)) {
|
14 | let v = callback(node);
|
15 | if (v === false) return
|
16 | if (v?.nodeName) return walk(v, callback, deep)
|
17 | }
|
18 | if (deep) walk(node.firstChild, callback, deep);
|
19 | walk(node.nextSibling, callback, deep);
|
20 | };
|
21 |
|
22 | const transformBrackets = (str = "") => {
|
23 | let parts = str.split(/(\[[^\]]+\])/).filter((v) => v);
|
24 | return parts.reduce((a, part) => {
|
25 | let v = part.charAt(0) === "[" ? "." + part.replace(/\./g, ":") : part;
|
26 | return a + v
|
27 | }, "")
|
28 | };
|
29 |
|
30 | const getTarget = (path, target) => {
|
31 | let parts = transformBrackets(path)
|
32 | .split(".")
|
33 | .map((k) => {
|
34 | if (k.charAt(0) === "[") {
|
35 | let p = k.slice(1, -1).replace(/:/g, ".");
|
36 | return getValueAtPath(p, target)
|
37 | } else {
|
38 | return k
|
39 | }
|
40 | });
|
41 |
|
42 | let t =
|
43 | parts.slice(0, -1).reduce((o, k) => {
|
44 | return o && o[k]
|
45 | }, target) || target;
|
46 | return [t, last(parts)]
|
47 | };
|
48 |
|
49 | const getValueAtPath = (path, target) => {
|
50 | let [a, b] = getTarget(path, target);
|
51 | let v = a?.[b];
|
52 | if (typeof v === "function") return v.bind(a)
|
53 | return v
|
54 | };
|
55 |
|
56 | const fragmentFromTemplate = (v) => {
|
57 | if (typeof v === "string") {
|
58 | let tpl = document.createElement("template");
|
59 | tpl.innerHTML = v.trim();
|
60 | return tpl.content
|
61 | }
|
62 | if (v.nodeName === "TEMPLATE") return v.cloneNode(true).content
|
63 | if (v.nodeName === "defs") return v.firstElementChild.cloneNode(true)
|
64 | };
|
65 |
|
66 | const debounce = (fn) => {
|
67 | let wait = false;
|
68 | let invoke = false;
|
69 | return () => {
|
70 | if (wait) {
|
71 | invoke = true;
|
72 | } else {
|
73 | wait = true;
|
74 | fn();
|
75 | requestAnimationFrame(() => {
|
76 | if (invoke) fn();
|
77 | wait = false;
|
78 | });
|
79 | }
|
80 | }
|
81 | };
|
82 |
|
83 | const isPrimitive = (v) => v === null || typeof v !== "object";
|
84 |
|
85 | const typeOf = (v) =>
|
86 | Object.prototype.toString.call(v).match(/\s(.+[^\]])/)[1];
|
87 |
|
88 | const pascalToKebab = (string) =>
|
89 | string.replace(/[\w]([A-Z])/g, function (m) {
|
90 | return m[0] + "-" + m[1].toLowerCase()
|
91 | });
|
92 |
|
93 | const kebabToPascal = (string) =>
|
94 | string.replace(/[\w]-([\w])/g, function (m) {
|
95 | return m[0] + m[2].toUpperCase()
|
96 | });
|
97 |
|
98 | const applyAttribute = (node, name, value) => {
|
99 | name = pascalToKebab(name);
|
100 |
|
101 | if (typeof value === "boolean") {
|
102 | if (name.startsWith("aria-")) {
|
103 | value = "" + value;
|
104 | } else if (value) {
|
105 | value = "";
|
106 | }
|
107 | }
|
108 |
|
109 | if (typeof value === "string" || typeof value === "number") {
|
110 | node.setAttribute(name, value);
|
111 | } else {
|
112 | node.removeAttribute(name);
|
113 | }
|
114 | };
|
115 |
|
116 | const attributeToProp = (k, v) => {
|
117 | let name = kebabToPascal(k);
|
118 | if (v === "") v = true;
|
119 | if (k.startsWith("aria-")) {
|
120 | if (v === "true") v = true;
|
121 | if (v === "false") v = false;
|
122 | }
|
123 | return {
|
124 | name,
|
125 | value: v,
|
126 | }
|
127 | };
|
128 |
|
129 | exports.applyAttribute = applyAttribute;
|
130 | exports.attributeToProp = attributeToProp;
|
131 | exports.debounce = debounce;
|
132 | exports.fragmentFromTemplate = fragmentFromTemplate;
|
133 | exports.getValueAtPath = getValueAtPath;
|
134 | exports.isPrimitive = isPrimitive;
|
135 | exports.isWhitespace = isWhitespace;
|
136 | exports.kebabToPascal = kebabToPascal;
|
137 | exports.last = last;
|
138 | exports.pascalToKebab = pascalToKebab;
|
139 | exports.typeOf = typeOf;
|
140 | exports.walk = walk;
|