UNPKG

3.4 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5const last = (v = []) => v[v.length - 1];
6
7const isWhitespace = (node) => {
8 return node.nodeType === node.TEXT_NODE && node.nodeValue.trim() === ""
9};
10
11const 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
22const 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
30const 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
49const 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
56const 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
66const 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
83const isPrimitive = (v) => v === null || typeof v !== "object";
84
85const typeOf = (v) =>
86 Object.prototype.toString.call(v).match(/\s(.+[^\]])/)[1];
87
88const pascalToKebab = (string) =>
89 string.replace(/[\w]([A-Z])/g, function (m) {
90 return m[0] + "-" + m[1].toLowerCase()
91 });
92
93const kebabToPascal = (string) =>
94 string.replace(/[\w]-([\w])/g, function (m) {
95 return m[0] + m[2].toUpperCase()
96 });
97
98const 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
116const 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
129exports.applyAttribute = applyAttribute;
130exports.attributeToProp = attributeToProp;
131exports.debounce = debounce;
132exports.fragmentFromTemplate = fragmentFromTemplate;
133exports.getValueAtPath = getValueAtPath;
134exports.isPrimitive = isPrimitive;
135exports.isWhitespace = isWhitespace;
136exports.kebabToPascal = kebabToPascal;
137exports.last = last;
138exports.pascalToKebab = pascalToKebab;
139exports.typeOf = typeOf;
140exports.walk = walk;