@design-edito/tools 0.1.31 → 0.1.33

Sign up to get free protection for your applications and to get access to all the features.
Files changed (151) hide show
  1. package/agnostic/css/index.js +3 -3
  2. package/agnostic/errors/index.js +2 -2
  3. package/agnostic/html/index.js +11 -11
  4. package/agnostic/html/sanitize/index.js +4 -4
  5. package/agnostic/html/selector-to-element/index.js +4 -4
  6. package/agnostic/html/string-to-nodes/index.js +5 -5
  7. package/agnostic/misc/index.js +7 -7
  8. package/agnostic/misc/lorem-ipsum/index.js +2 -2
  9. package/agnostic/numbers/index.js +3 -3
  10. package/agnostic/optim/index.js +3 -3
  11. package/agnostic/strings/index.js +3 -3
  12. package/agnostic/time/index.js +3 -3
  13. package/chunks/{chunk-EVOTJ7SU.js → chunk-COLNKMBN.js} +1 -1
  14. package/chunks/{chunk-NWFX6TEY.js → chunk-EX7FS6IR.js} +2 -2
  15. package/chunks/{chunk-CB52KVOZ.js → chunk-UDY37VIJ.js} +1 -1
  16. package/chunks/{chunk-WABWQCFE.js → chunk-WXXW6YLW.js} +1 -1
  17. package/package.json +1 -2
  18. package/agnostic/html/hyper-json/crossenv/index.d.ts +0 -13
  19. package/agnostic/html/hyper-json/crossenv/index.js +0 -6
  20. package/agnostic/misc/crossenv/get-document/index.d.ts +0 -2
  21. package/agnostic/misc/crossenv/get-document/index.js +0 -7
  22. package/agnostic/misc/crossenv/get-window/browser.d.ts +0 -2
  23. package/agnostic/misc/crossenv/get-window/index.d.ts +0 -2
  24. package/agnostic/misc/crossenv/get-window/index.js +0 -6
  25. package/agnostic/misc/crossenv/types.d.ts +0 -3
  26. package/chunks/chunk-2DMWYLFG.js +0 -16
  27. package/chunks/chunk-2PI5AZIL.js +0 -211
  28. package/chunks/chunk-2UYTYTX4.js +0 -31
  29. package/chunks/chunk-3KQRMKQJ.js +0 -27
  30. package/chunks/chunk-3LVR6KZH.js +0 -22
  31. package/chunks/chunk-3PHPA25U.js +0 -18
  32. package/chunks/chunk-3PM7GZ6B.js +0 -33
  33. package/chunks/chunk-3SQJEEBK.js +0 -642
  34. package/chunks/chunk-3UURICGY.js +0 -211
  35. package/chunks/chunk-3X6RXJBR.js +0 -37
  36. package/chunks/chunk-4F7ZVE7T.js +0 -332
  37. package/chunks/chunk-4GUDL44X.js +0 -13
  38. package/chunks/chunk-4QQBKBYH.js +0 -59
  39. package/chunks/chunk-4RDW62CU.js +0 -36
  40. package/chunks/chunk-5IR2PWO6.js +0 -211
  41. package/chunks/chunk-6CM2SWSX.js +0 -35
  42. package/chunks/chunk-6EYBQHJS.js +0 -33
  43. package/chunks/chunk-6NZCBAT4.js +0 -19
  44. package/chunks/chunk-6R54C426.js +0 -35
  45. package/chunks/chunk-6V23XMYE.js +0 -982
  46. package/chunks/chunk-73V5RDSG.js +0 -13
  47. package/chunks/chunk-7IGZP4TV.js +0 -30
  48. package/chunks/chunk-7QMAYGNV.js +0 -25
  49. package/chunks/chunk-7YW5HL6N.js +0 -168
  50. package/chunks/chunk-AFFLZUW5.js +0 -33
  51. package/chunks/chunk-AMS3FB24.js +0 -85
  52. package/chunks/chunk-APWPG7BU.js +0 -647
  53. package/chunks/chunk-AXMPRLPI.js +0 -35
  54. package/chunks/chunk-B2O7PFSF.js +0 -1108
  55. package/chunks/chunk-B6CSCX7J.js +0 -278
  56. package/chunks/chunk-B74HAAS6.js +0 -35
  57. package/chunks/chunk-BDHNULU7.js +0 -211
  58. package/chunks/chunk-BEJSITDG.js +0 -31
  59. package/chunks/chunk-BNB7LEMQ.js +0 -35
  60. package/chunks/chunk-CECF6UZV.js +0 -29
  61. package/chunks/chunk-DBAJ66BR.js +0 -18
  62. package/chunks/chunk-DGQLPQS2.js +0 -33
  63. package/chunks/chunk-DLLTITVO.js +0 -33
  64. package/chunks/chunk-DMDOCC72.js +0 -18
  65. package/chunks/chunk-DRH7Q4FL.js +0 -127
  66. package/chunks/chunk-DRNK3RLE.js +0 -64
  67. package/chunks/chunk-EAY65ZP6.js +0 -22
  68. package/chunks/chunk-EW46EXFB.js +0 -8
  69. package/chunks/chunk-F5YMP5WU.js +0 -18
  70. package/chunks/chunk-F7EVMWVG.js +0 -27
  71. package/chunks/chunk-F7XWUG2V.js +0 -18
  72. package/chunks/chunk-FFOATPL4.js +0 -211
  73. package/chunks/chunk-FSCSFZOD.js +0 -647
  74. package/chunks/chunk-FWHUASVA.js +0 -22
  75. package/chunks/chunk-G2NZXHW6.js +0 -21
  76. package/chunks/chunk-GDYVCEWA.js +0 -38
  77. package/chunks/chunk-GNBIAWSY.js +0 -278
  78. package/chunks/chunk-GQSPY2CV.js +0 -19
  79. package/chunks/chunk-HDALT7XM.js +0 -620
  80. package/chunks/chunk-HEJV6JIO.js +0 -133
  81. package/chunks/chunk-HENMQK5Y.js +0 -647
  82. package/chunks/chunk-HFEOHBYG.js +0 -647
  83. package/chunks/chunk-HGECIEKD.js +0 -35
  84. package/chunks/chunk-HUKHWFVL.js +0 -19
  85. package/chunks/chunk-IGD53YL5.js +0 -870
  86. package/chunks/chunk-ILEM73XB.js +0 -981
  87. package/chunks/chunk-IPB3U2PD.js +0 -278
  88. package/chunks/chunk-JAST3E5G.js +0 -647
  89. package/chunks/chunk-JY3QZ2YO.js +0 -211
  90. package/chunks/chunk-K3OQEJI3.js +0 -33
  91. package/chunks/chunk-KN4RZPDY.js +0 -211
  92. package/chunks/chunk-KXBKJ3GS.js +0 -642
  93. package/chunks/chunk-L7SL3MND.js +0 -35
  94. package/chunks/chunk-LCFDDWJR.js +0 -278
  95. package/chunks/chunk-LFUY5AWK.js +0 -1108
  96. package/chunks/chunk-LGECI2WL.js +0 -306
  97. package/chunks/chunk-LGUEB5L5.js +0 -211
  98. package/chunks/chunk-LI6QDZCA.js +0 -211
  99. package/chunks/chunk-LYMZZ4BB.js +0 -22
  100. package/chunks/chunk-MDEDNPU5.js +0 -38
  101. package/chunks/chunk-MQHKEGP4.js +0 -33
  102. package/chunks/chunk-NF5ACCXZ.js +0 -211
  103. package/chunks/chunk-NFUY7SLX.js +0 -211
  104. package/chunks/chunk-NK557MKV.js +0 -35
  105. package/chunks/chunk-NWMEWFTK.js +0 -33
  106. package/chunks/chunk-O5OTBN22.js +0 -35
  107. package/chunks/chunk-O7MDUYEF.js +0 -211
  108. package/chunks/chunk-OO5SJJDW.js +0 -35
  109. package/chunks/chunk-OVG5RGI3.js +0 -29
  110. package/chunks/chunk-OWRQ3QBI.js +0 -211
  111. package/chunks/chunk-PO3V3I57.js +0 -23
  112. package/chunks/chunk-PXB7U2DH.js +0 -31
  113. package/chunks/chunk-Q52UJOBC.js +0 -18
  114. package/chunks/chunk-QFEF4EET.js +0 -211
  115. package/chunks/chunk-QHSVCGLD.js +0 -280
  116. package/chunks/chunk-QO64L4KB.js +0 -97
  117. package/chunks/chunk-R6KCN6CK.js +0 -40
  118. package/chunks/chunk-RHYLRLKT.js +0 -211
  119. package/chunks/chunk-RJ5E22H5.js +0 -15
  120. package/chunks/chunk-RWUPHV54.js +0 -25
  121. package/chunks/chunk-S2757FI3.js +0 -36
  122. package/chunks/chunk-S42P73SC.js +0 -30
  123. package/chunks/chunk-S5XDYLJH.js +0 -18
  124. package/chunks/chunk-S7MJLTCR.js +0 -211
  125. package/chunks/chunk-SCM6NACT.js +0 -15
  126. package/chunks/chunk-SFV2NYY4.js +0 -211
  127. package/chunks/chunk-SH3Y2I5P.js +0 -18
  128. package/chunks/chunk-SMLKMOAX.js +0 -18
  129. package/chunks/chunk-SQZGZ3VT.js +0 -15
  130. package/chunks/chunk-T5CGXYST.js +0 -647
  131. package/chunks/chunk-TATRVKBS.js +0 -211
  132. package/chunks/chunk-TRB6DPKJ.js +0 -35
  133. package/chunks/chunk-UFKH5WD2.js +0 -21
  134. package/chunks/chunk-UODQZRY2.js +0 -620
  135. package/chunks/chunk-UOULDS6J.js +0 -33
  136. package/chunks/chunk-UPHVZROF.js +0 -35
  137. package/chunks/chunk-UUDXUSI5.js +0 -211
  138. package/chunks/chunk-UWAMSJ2X.js +0 -28
  139. package/chunks/chunk-UZUXLD2Q.js +0 -35
  140. package/chunks/chunk-VA6HXRX4.js +0 -211
  141. package/chunks/chunk-VRUJR6WR.js +0 -39
  142. package/chunks/chunk-W6YBEEZM.js +0 -211
  143. package/chunks/chunk-WECTUHS5.js +0 -127
  144. package/chunks/chunk-YE5HI3RK.js +0 -211
  145. package/chunks/chunk-YEBOL67V.js +0 -32
  146. package/chunks/chunk-YRGOY5YU.js +0 -1108
  147. package/chunks/chunk-YYE2MVZO.js +0 -211
  148. package/chunks/chunk-ZB73RE7P.js +0 -35
  149. package/chunks/chunk-ZONKYNKA.js +0 -28
  150. package/chunks/{chunk-GPWFYPER.js → chunk-5HWYTIYR.js} +3 -3
  151. package/chunks/{chunk-MUGIN5OW.js → chunk-MZSIKE3H.js} +3 -3
@@ -1,870 +0,0 @@
1
- import {
2
- Serialize
3
- } from "./chunk-DRNK3RLE.js";
4
- import {
5
- Crossenv
6
- } from "./chunk-3LVR6KZH.js";
7
- import {
8
- Utils
9
- } from "./chunk-F7EVMWVG.js";
10
- import {
11
- Types
12
- } from "./chunk-6NZCBAT4.js";
13
- import {
14
- replaceInElement
15
- } from "./chunk-GBMHGF7P.js";
16
- import {
17
- isRecord
18
- } from "./chunk-YDIBNEGA.js";
19
- import {
20
- replaceAll
21
- } from "./chunk-MF56TTC5.js";
22
- import {
23
- isFalsy
24
- } from "./chunk-QXAJXTXV.js";
25
-
26
- // src/agnostic/html/hyper-json/cast/index.ts
27
- var getWindow = Crossenv.getWindow;
28
- var Cast;
29
- ((Cast2) => {
30
- Cast2.toNull = () => null;
31
- Cast2.toBoolean = (input) => !isFalsy(input);
32
- Cast2.toNumber = (input) => {
33
- if (typeof input === "number") return input;
34
- if (typeof input === "string") return parseFloat(`${input}`);
35
- if (input instanceof getWindow().Text) return parseFloat(`${input.textContent}`);
36
- return 0;
37
- };
38
- Cast2.toString = (input) => {
39
- if (typeof input === "string") return input;
40
- if (typeof input === "number" || typeof input === "boolean" || input === null) return `${input}`;
41
- if (input instanceof getWindow().Element) return input.outerHTML;
42
- if (input instanceof getWindow().Text) return input.textContent ?? "";
43
- if (input instanceof getWindow().NodeList) return Array.from(input).map((e) => {
44
- if (e instanceof getWindow().Element) return e.outerHTML;
45
- return e.textContent;
46
- }).join("");
47
- return input.toString();
48
- };
49
- Cast2.toText = (input) => {
50
- if (input instanceof getWindow().Text) return input.cloneNode(true);
51
- return getWindow().document.createTextNode((0, Cast2.toString)(input));
52
- };
53
- Cast2.toElement = (input) => {
54
- if (input instanceof getWindow().Element) return input.cloneNode(true);
55
- const elt = getWindow().document.createElement("div");
56
- if (input instanceof getWindow().Text) {
57
- elt.append(input.cloneNode(true));
58
- return elt;
59
- }
60
- if (input instanceof getWindow().NodeList) {
61
- elt.append(...Array.from(input).map((e) => e.cloneNode(true)));
62
- return elt;
63
- }
64
- if (Array.isArray(input)) return elt;
65
- if (isRecord(input)) return elt;
66
- elt.append(`${input}`);
67
- return elt;
68
- };
69
- Cast2.toNodeList = (input) => {
70
- const elt = getWindow().document.createElement("div");
71
- if (input instanceof getWindow().NodeList) {
72
- elt.append(...Array.from(input).map((i) => i.cloneNode(true)));
73
- return elt.childNodes;
74
- }
75
- if (input instanceof getWindow().Element || input instanceof getWindow().Text) {
76
- elt.append(input.cloneNode(true));
77
- return elt.childNodes;
78
- }
79
- if (Array.isArray(input)) return elt.childNodes;
80
- if (isRecord(input)) return elt.childNodes;
81
- elt.innerHTML = `${input}`;
82
- return elt.childNodes;
83
- };
84
- Cast2.toArray = (input) => {
85
- if (Array.isArray(input)) return [...input];
86
- if (input instanceof getWindow().NodeList) return Array.from(input);
87
- return [input];
88
- };
89
- Cast2.toRecord = (input) => {
90
- if (isRecord(input)) return { ...input };
91
- return {};
92
- };
93
- Cast2.toTransformer = (input) => {
94
- return Transformers.toNamed("utils/Cast.toTransformer", () => ({
95
- action: "REPLACE",
96
- value: input
97
- }));
98
- };
99
- })(Cast || (Cast = {}));
100
-
101
- // src/agnostic/html/hyper-json/transformers/classList/index.ts
102
- var classList = (callerTagName, ...args) => {
103
- return Transformers.toNamed(callerTagName, (currentValue) => {
104
- const { Element } = Crossenv.getWindow();
105
- if (!(currentValue instanceof Element)) return Utils.makeTransformerError("Current value must be an Element");
106
- const [actionArg, classNameArg] = args;
107
- if (actionArg === void 0 || classNameArg === void 0) return Utils.makeTransformerError(`Expecting 2 arguments: (action: 'add' | 'remove' | 'toggle', classList: string)`);
108
- const action = Cast.toString(actionArg);
109
- const className = Cast.toString(actionArg).trim();
110
- if (action === "add" /* ADD */) {
111
- const outputValue = currentValue.cloneNode(true);
112
- outputValue.classList.add(className);
113
- return { action: "REPLACE", value: outputValue };
114
- } else if (action === "remove" /* REMOVE */) {
115
- const outputValue = currentValue.cloneNode(true);
116
- outputValue.classList.remove(className);
117
- return { action: "REPLACE", value: outputValue };
118
- } else if (action === "toggle" /* TOGGLE */) {
119
- const outputValue = currentValue.cloneNode(true);
120
- outputValue.classList.toggle(className);
121
- return { action: "REPLACE", value: outputValue };
122
- }
123
- return Utils.makeTransformerError(`Unknown action: ${action}`);
124
- });
125
- };
126
-
127
- // src/agnostic/html/hyper-json/transformers/join/index.ts
128
- var join = (callerTagName, ...args) => {
129
- return Transformers.toNamed(callerTagName, (currentValue) => {
130
- const joiner = Cast.toString(args.at(0) ?? ",");
131
- if (!Array.isArray(currentValue)) return Utils.makeTransformerError({
132
- message: `Current value must be an array`,
133
- input: currentValue
134
- });
135
- return {
136
- action: "REPLACE",
137
- value: currentValue.join(joiner)
138
- };
139
- });
140
- };
141
-
142
- // src/agnostic/html/hyper-json/transformers/length/index.ts
143
- var length = (callerTagName) => {
144
- return Transformers.toNamed(callerTagName, (currentValue) => {
145
- const { Element, NodeList, Text } = Crossenv.getWindow();
146
- if (typeof currentValue === "string" || Array.isArray(currentValue) || currentValue instanceof NodeList) return {
147
- action: "REPLACE",
148
- value: currentValue.length
149
- };
150
- if (currentValue instanceof Element) return {
151
- action: "REPLACE",
152
- value: Array.from(currentValue.childNodes).length
153
- };
154
- if (currentValue instanceof Text) return {
155
- action: "REPLACE",
156
- value: currentValue.data.length
157
- };
158
- return Utils.makeTransformerError(`Current value must be of type string, Array, NodeList, Element or Text`);
159
- });
160
- };
161
-
162
- // src/agnostic/html/hyper-json/transformers/print/index.ts
163
- var print = (callerTagName, ...args) => {
164
- return Transformers.toNamed(callerTagName, (currentValue) => {
165
- if (args.length === 0) console.log(currentValue);
166
- else console.log(...args);
167
- return { action: null };
168
- });
169
- };
170
-
171
- // src/agnostic/html/hyper-json/transformers/push/index.ts
172
- var push = (callerTagName, ...args) => {
173
- return Transformers.toNamed(callerTagName, (currentValue) => {
174
- if (!Array.isArray(currentValue)) return Utils.makeTransformerError({
175
- message: "Current value must be an array",
176
- input: currentValue
177
- });
178
- const outputValue = [...currentValue, ...args];
179
- return {
180
- action: "REPLACE",
181
- value: outputValue
182
- };
183
- });
184
- };
185
-
186
- // src/agnostic/html/hyper-json/transformers/querySelector/index.ts
187
- var querySelector = (callerTagName, ...args) => {
188
- return Transformers.toNamed(callerTagName, (currentValue) => {
189
- const { document, Element, NodeList } = Crossenv.getWindow();
190
- const [selectorRaw] = args;
191
- if (selectorRaw === void 0) return Utils.makeTransformerError("Selector argument expected.");
192
- const selector = Cast.toString(selectorRaw);
193
- let toQuery;
194
- if (currentValue instanceof Element) {
195
- toQuery = currentValue.cloneNode(true);
196
- } else if (currentValue instanceof NodeList) {
197
- toQuery = document.createElement("div");
198
- toQuery.append(...Array.from(currentValue).map((e) => e.cloneNode(true)));
199
- } else return Utils.makeTransformerError("Current value must be an Element or a NodeList");
200
- const found = toQuery.querySelectorAll(selector);
201
- return {
202
- action: "REPLACE",
203
- value: found
204
- };
205
- });
206
- };
207
-
208
- // src/agnostic/html/hyper-json/transformers/ref/index.ts
209
- var ref = (callerTagName, ...args) => {
210
- return Transformers.toNamed(callerTagName, (_, callerTree) => {
211
- const [refPathStrRaw] = args;
212
- const { Text } = Crossenv.getWindow();
213
- if (typeof refPathStrRaw !== "string" && !(refPathStrRaw instanceof Text)) return Utils.makeTransformerError({
214
- message: "PathString must be of type string or Text",
215
- input: refPathStrRaw !== void 0 ? refPathStrRaw : "<undefined>"
216
- });
217
- const refPathStr = Cast.toString(refPathStrRaw);
218
- const refPath = Utils.pathStringToPath(Cast.toString(refPathStrRaw));
219
- const foundTree = callerTree.resolve(refPath);
220
- if (foundTree === void 0) return Utils.makeTransformerError({
221
- message: "Referenced value has not been found",
222
- input: refPathStr
223
- });
224
- const evaluated = foundTree.evaluate();
225
- return {
226
- action: "MERGE",
227
- value: evaluated
228
- };
229
- });
230
- };
231
-
232
- // src/agnostic/html/hyper-json/transformers/replace/index.ts
233
- var replace = (callerTagName, ...args) => {
234
- return Transformers.toNamed(callerTagName, (currentValue) => {
235
- const { Text, NodeList, Element } = Crossenv.getWindow();
236
- if (typeof currentValue !== "string" && !(currentValue instanceof Text) && !(currentValue instanceof Element) && !(currentValue instanceof NodeList)) return Utils.makeTransformerError("Current value must be string, Text, Element or NodeList");
237
- if (args.some((arg) => typeof arg !== "string" && !(arg instanceof Text) && !(arg instanceof Element) && !(arg instanceof NodeList))) return Utils.makeTransformerError("Arguments must be of type string, Text, Element or NodeList");
238
- const [first, second] = args;
239
- if (first === void 0 || second === void 0) return Utils.makeTransformerError(`Expecting at least 2 arguments. Found only ${args.length}`);
240
- const replacer = args.at(-1);
241
- const strReplacer = Cast.toString(replacer);
242
- const toReplace = args.slice(0, -1);
243
- const strToReplace = toReplace.map(Cast.toString);
244
- const strInput = Cast.toString(currentValue);
245
- const strOutput = strToReplace.reduce((str, torpl) => replaceAll(str, torpl, strReplacer), strInput);
246
- if (typeof currentValue === "string") return { action: "REPLACE", value: strOutput };
247
- if (currentValue instanceof Text) return { action: "REPLACE", value: Cast.toText(strOutput) };
248
- if (currentValue instanceof Element) return { action: "REPLACE", value: Cast.toElement(strOutput) };
249
- if (currentValue instanceof NodeList) return { action: "REPLACE", value: Cast.toNodeList(strOutput) };
250
- return { action: null };
251
- });
252
- };
253
-
254
- // src/agnostic/html/hyper-json/transformers/split/index.ts
255
- var split = (callerTagName, ...args) => {
256
- return Transformers.toNamed(callerTagName, (currentValue) => {
257
- const { Text } = Crossenv.getWindow();
258
- const [splitter] = args;
259
- if (typeof currentValue !== "string" && !(currentValue instanceof Text)) return Utils.makeTransformerError({
260
- message: "Current value must be string or Text",
261
- input: currentValue
262
- });
263
- if (typeof splitter !== "string") return Utils.makeTransformerError({
264
- message: "Splitter argument must be string",
265
- input: splitter !== void 0 ? splitter : "<undefined>"
266
- });
267
- const strCurrentValue = Cast.toString(currentValue);
268
- const splitted = strCurrentValue.split(splitter).map((e) => typeof currentValue === "string" ? Cast.toString(e) : Cast.toText(e));
269
- return {
270
- action: "REPLACE",
271
- value: splitted
272
- };
273
- });
274
- };
275
-
276
- // src/agnostic/html/hyper-json/transformers/toArray/index.ts
277
- var toArray = (callerTagName) => {
278
- return Transformers.toNamed(callerTagName, (currentValue) => ({
279
- action: "REPLACE",
280
- value: Cast.toArray(currentValue)
281
- }));
282
- };
283
-
284
- // src/agnostic/html/hyper-json/transformers/toBoolean/index.ts
285
- var toBoolean = (callerTagName) => {
286
- return Transformers.toNamed(callerTagName, (currentValue) => ({
287
- action: "REPLACE",
288
- value: Cast.toBoolean(currentValue)
289
- }));
290
- };
291
-
292
- // src/agnostic/html/hyper-json/transformers/toElement/index.ts
293
- var toElement = (callerTagName) => {
294
- return Transformers.toNamed(callerTagName, (currentValue) => ({
295
- action: "REPLACE",
296
- value: Cast.toElement(currentValue)
297
- }));
298
- };
299
-
300
- // src/agnostic/html/hyper-json/transformers/toNodeList/index.ts
301
- var toNodeList = (callerTagName) => {
302
- return Transformers.toNamed(callerTagName, (currentValue) => ({
303
- action: "REPLACE",
304
- value: Cast.toNodeList(currentValue)
305
- }));
306
- };
307
-
308
- // src/agnostic/html/hyper-json/transformers/toNull/index.ts
309
- var toNull = (callerTagName) => {
310
- return Transformers.toNamed(callerTagName, (currentValue) => ({
311
- action: "REPLACE",
312
- value: null
313
- }));
314
- };
315
-
316
- // src/agnostic/html/hyper-json/transformers/toNumber/index.ts
317
- var toNumber = (callerTagName) => {
318
- return Transformers.toNamed(callerTagName, (currentValue) => ({
319
- action: "REPLACE",
320
- value: Cast.toNumber(currentValue)
321
- }));
322
- };
323
-
324
- // src/agnostic/html/hyper-json/transformers/toRecord/index.ts
325
- var toRecord = (callerTagName) => {
326
- return Transformers.toNamed(callerTagName, (currentValue) => ({
327
- action: "REPLACE",
328
- value: Cast.toRecord(currentValue)
329
- }));
330
- };
331
-
332
- // src/agnostic/html/hyper-json/transformers/toRef/index.ts
333
- var toRef = (callerTagName) => {
334
- return Transformers.toNamed(callerTagName, (currentValue, callerTree) => {
335
- const { Text } = Crossenv.getWindow();
336
- if (typeof currentValue !== "string" && !(currentValue instanceof Text)) return Utils.makeTransformerError({
337
- message: "Current value must be string or Text",
338
- input: currentValue
339
- });
340
- const refPathStr = Cast.toString(currentValue);
341
- const refPath = Utils.pathStringToPath(refPathStr);
342
- const foundTree = callerTree.resolve(refPath);
343
- if (foundTree === void 0) return Utils.makeTransformerError({
344
- message: "Referenced value has not been found",
345
- input: refPathStr
346
- });
347
- const evaluated = foundTree.evaluate();
348
- return {
349
- action: "REPLACE",
350
- value: evaluated
351
- };
352
- });
353
- };
354
-
355
- // src/agnostic/html/hyper-json/transformers/toString/index.ts
356
- var toString = (callerTagName) => {
357
- return Transformers.toNamed(callerTagName, (currentValue) => ({
358
- action: "REPLACE",
359
- value: Cast.toString(currentValue)
360
- }));
361
- };
362
-
363
- // src/agnostic/html/hyper-json/transformers/toText/index.ts
364
- var toText = (callerTagName) => {
365
- return Transformers.toNamed(callerTagName, (currentValue) => ({
366
- action: "REPLACE",
367
- value: Cast.toText(currentValue)
368
- }));
369
- };
370
-
371
- // src/agnostic/html/hyper-json/transformers/toTransformer/index.ts
372
- var toTransformer = (callerTagName) => {
373
- return Transformers.toNamed(callerTagName, (currentValue) => ({
374
- action: "REPLACE",
375
- value: Cast.toTransformer(currentValue)
376
- }));
377
- };
378
-
379
- // src/agnostic/html/hyper-json/tree/index.ts
380
- var getWindow2 = Crossenv.getWindow;
381
- var isElement = (node) => node.nodeType === getWindow2().Node.ELEMENT_NODE;
382
- var isText = (node) => node.nodeType === getWindow2().Node.TEXT_NODE;
383
- var Tree = class _Tree {
384
- node;
385
- name;
386
- parent;
387
- root;
388
- isRoot;
389
- path;
390
- pathString;
391
- tagName;
392
- attributes;
393
- subtrees = /* @__PURE__ */ new Map();
394
- children;
395
- type;
396
- generators;
397
- constructor(node, generatorsOrParent, pathFromParent) {
398
- this.resolve = this.resolve.bind(this);
399
- this.getGenerator = this.getGenerator.bind(this);
400
- this.initValue = this.initValue.bind(this);
401
- this.getInnerValue = this.getInnerValue.bind(this);
402
- this.wrapInnerValue = this.wrapInnerValue.bind(this);
403
- this.setCache = this.setCache.bind(this);
404
- this.getPerfCounters = this.getPerfCounters.bind(this);
405
- this.printPerfCounters = this.printPerfCounters.bind(this);
406
- this.pushToEvalCallStack = this.pushToEvalCallStack.bind(this);
407
- this.flushEvalCallStack = this.flushEvalCallStack.bind(this);
408
- this.evaluate = this.evaluate.bind(this);
409
- this.node = node;
410
- this.name = this.node instanceof getWindow2().Element ? this.node.getAttribute("_name") : null;
411
- this.parent = generatorsOrParent instanceof _Tree ? generatorsOrParent : null;
412
- this.root = this.parent === null ? this : this.parent.root;
413
- this.isRoot = this.root === this;
414
- const generators = generatorsOrParent instanceof _Tree ? /* @__PURE__ */ new Map() : Transformers.defaultGeneratorsMap;
415
- this.generators = this.isRoot ? generators : this.root.generators;
416
- if (this.parent === null) this.path = [];
417
- else if (pathFromParent === void 0) {
418
- this.path = [...this.parent.path, 0];
419
- } else {
420
- this.path = [...this.parent.path, pathFromParent];
421
- }
422
- this.pathString = `/${this.path.join("/")}`;
423
- this.tagName = node instanceof getWindow2().Element ? node.tagName.toLowerCase() : null;
424
- this.attributes = isElement(node) ? Array.from(node.attributes) : null;
425
- const { childNodes } = node;
426
- let positionnedChildrenCount = 0;
427
- const mutableSubtrees = /* @__PURE__ */ new Map();
428
- Array.from(childNodes).filter((node2, _, nodes) => {
429
- if (isElement(node2)) return true;
430
- if (isText(node2)) {
431
- const hasContent = (node2.textContent ?? "").trim() !== "";
432
- if (hasContent) return true;
433
- if (nodes.some((n) => n instanceof getWindow2().Element)) return false;
434
- return true;
435
- }
436
- return false;
437
- }).forEach((childNode) => {
438
- if (childNode instanceof getWindow2().Text) {
439
- mutableSubtrees.set(
440
- positionnedChildrenCount,
441
- new _Tree(childNode, this, positionnedChildrenCount)
442
- );
443
- positionnedChildrenCount += 1;
444
- } else {
445
- const propertyName = childNode.getAttribute("_name");
446
- if (propertyName === null) {
447
- mutableSubtrees.set(
448
- positionnedChildrenCount,
449
- new _Tree(childNode, this, positionnedChildrenCount)
450
- );
451
- positionnedChildrenCount += 1;
452
- } else {
453
- mutableSubtrees.set(
454
- propertyName,
455
- new _Tree(childNode, this, propertyName)
456
- );
457
- }
458
- }
459
- });
460
- this.subtrees = mutableSubtrees;
461
- this.children = Array.from(this.subtrees.values());
462
- if (this.tagName === null) {
463
- this.type = "text";
464
- } else if (this.tagName === Types.TyperTagName.NULL) {
465
- this.type = "null";
466
- } else if (this.tagName === Types.TyperTagName.BOOLEAN) {
467
- this.type = "boolean";
468
- } else if (this.tagName === Types.TyperTagName.NUMBER) {
469
- this.type = "number";
470
- } else if (this.tagName === Types.TyperTagName.STRING) {
471
- this.type = "string";
472
- } else if (this.tagName === Types.TyperTagName.TEXT) {
473
- this.type = "text";
474
- } else if (this.tagName === Types.TyperTagName.NODELIST) {
475
- this.type = "nodelist";
476
- } else if (this.tagName === Types.TyperTagName.ARRAY) {
477
- this.type = "array";
478
- } else if (this.tagName === Types.TyperTagName.RECORD) {
479
- this.type = "record";
480
- } else if (this.generators.get(this.tagName) !== void 0) {
481
- this.type = "transformer";
482
- } else {
483
- this.type = "element";
484
- }
485
- }
486
- resolve = function(path) {
487
- let currentTree = this.root;
488
- for (const chunk of path) {
489
- const { subtrees } = currentTree;
490
- const foundSubtree = subtrees.get(chunk);
491
- if (foundSubtree === void 0) return void 0;
492
- currentTree = foundSubtree;
493
- }
494
- return currentTree;
495
- };
496
- getGenerator(name) {
497
- return this.generators.get(name);
498
- }
499
- initValue() {
500
- const { type } = this;
501
- let currentValue;
502
- if (type === "null") {
503
- currentValue = null;
504
- } else if (type === "boolean") {
505
- currentValue = false;
506
- } else if (type === "number") {
507
- currentValue = 0;
508
- } else if (type === "string") {
509
- currentValue = "";
510
- } else if (type === "text") {
511
- currentValue = this.node.textContent;
512
- } else if (type === "element") {
513
- currentValue = getWindow2().document.createDocumentFragment().childNodes;
514
- } else if (type === "nodelist") {
515
- currentValue = getWindow2().document.createDocumentFragment().childNodes;
516
- } else if (type === "array") {
517
- currentValue = [];
518
- } else if (type === "record") {
519
- currentValue = {};
520
- } else if (type === "transformer") {
521
- currentValue = [];
522
- } else {
523
- currentValue = null;
524
- }
525
- return currentValue;
526
- }
527
- static mergeValues(currentValue, incomingValue, mergeKey, initiatorTree) {
528
- const { Element, Text, NodeList, document } = getWindow2();
529
- if (typeof incomingValue === "function") {
530
- const evaluated = incomingValue(currentValue, initiatorTree);
531
- if (evaluated.action === null) return currentValue;
532
- if (evaluated.action === "REPLACE") return evaluated.value;
533
- if (evaluated.action === "ERROR") {
534
- const errorMessage = `Tranformer error:
535
- from: ${incomingValue.transformerName}
536
- at: ${initiatorTree.pathString.slice(1)}/${mergeKey}
537
- message:`;
538
- console.warn(errorMessage, evaluated.value);
539
- return currentValue;
540
- }
541
- return _Tree.mergeValues(
542
- currentValue,
543
- evaluated.value,
544
- mergeKey,
545
- initiatorTree
546
- );
547
- }
548
- if (Array.isArray(currentValue)) {
549
- if (typeof mergeKey === "string") return currentValue;
550
- return [...currentValue, incomingValue];
551
- }
552
- if (currentValue === null || typeof currentValue === "boolean" || typeof currentValue === "number" || typeof currentValue === "string" || typeof currentValue === "function" || currentValue instanceof Text || currentValue instanceof Element) {
553
- if (incomingValue === null || typeof incomingValue === "boolean" || typeof incomingValue === "number" || typeof incomingValue === "string" || Array.isArray(incomingValue) || isRecord(incomingValue)) {
554
- return incomingValue;
555
- }
556
- const frag = document.createDocumentFragment();
557
- if (currentValue instanceof Element || currentValue instanceof Text) frag.append(currentValue.cloneNode(true));
558
- else frag.append(`${currentValue}`);
559
- if (incomingValue instanceof Element || incomingValue instanceof Text) {
560
- frag.append(incomingValue.cloneNode(true));
561
- return frag.childNodes;
562
- }
563
- frag.append(...Array.from(incomingValue).map((e) => e.cloneNode(true)));
564
- return frag.childNodes;
565
- }
566
- if (currentValue instanceof NodeList) {
567
- if (incomingValue instanceof Element || incomingValue instanceof Text) {
568
- const frag = document.createDocumentFragment();
569
- frag.append(...Array.from(currentValue).map((e) => e.cloneNode(true)), incomingValue);
570
- return frag.childNodes;
571
- }
572
- if (incomingValue instanceof NodeList) {
573
- const frag = document.createDocumentFragment();
574
- frag.append(
575
- ...Array.from(currentValue).map((e) => e.cloneNode(true)),
576
- ...Array.from(incomingValue).map((e) => e.cloneNode(true))
577
- );
578
- return frag.childNodes;
579
- }
580
- if (incomingValue === null || typeof incomingValue === "string" || typeof incomingValue === "number" || typeof incomingValue === "boolean") {
581
- const frag = document.createDocumentFragment();
582
- frag.append(...Array.from(currentValue).map((e) => e.cloneNode(true)), `${incomingValue}`);
583
- return frag.childNodes;
584
- }
585
- if (Array.isArray(incomingValue)) return [
586
- ...Array.from(currentValue),
587
- ...incomingValue
588
- ];
589
- return { ...incomingValue };
590
- }
591
- if (typeof mergeKey === "number") return currentValue;
592
- return {
593
- ...currentValue,
594
- [mergeKey]: incomingValue
595
- };
596
- }
597
- getInnerValue(initialValue) {
598
- const { subtrees } = this;
599
- const innerValue = Array.from(subtrees.entries()).reduce((currentValue, [subpath, subtree]) => _Tree.mergeValues(
600
- currentValue,
601
- subtree.evaluate(),
602
- subpath,
603
- this
604
- ), initialValue);
605
- return innerValue;
606
- }
607
- wrapInnerValue(innerValue) {
608
- const { type } = this;
609
- if (type === "transformer") {
610
- const transformerName = this.tagName;
611
- if (transformerName === null) return innerValue;
612
- const generator = this.getGenerator(transformerName);
613
- if (generator === void 0) return innerValue;
614
- const transformer = Array.isArray(innerValue) ? generator(transformerName, ...innerValue) : generator(transformerName, innerValue);
615
- return transformer;
616
- }
617
- if (type === "null") return Cast.toNull();
618
- if (type === "boolean") return Cast.toBoolean(innerValue);
619
- if (type === "number") return Cast.toNumber(innerValue);
620
- if (type === "string") return Cast.toString(innerValue);
621
- if (type === "array") return Cast.toArray(innerValue);
622
- if (type === "record") return Cast.toRecord(innerValue);
623
- if (type === "text") return Cast.toText(innerValue);
624
- if (type === "element") return Cast.toElement(innerValue);
625
- if (type === "nodelist") return Cast.toNodeList(innerValue);
626
- return Cast.toNull();
627
- }
628
- cache = void 0;
629
- setCache(value) {
630
- this.cache = Serialize.serialize(value);
631
- }
632
- perfCounters = {
633
- computed: 0,
634
- computeTime: 0,
635
- computeTimeAvg: 0,
636
- cached: 0,
637
- cacheTime: 0,
638
- cacheTimeAvg: 0,
639
- totalTime: 0
640
- };
641
- getPerfCounters() {
642
- const { subtrees } = this;
643
- const subCounters = [];
644
- subCounters.push([this.pathString, this.perfCounters]);
645
- subtrees.forEach((subtree) => subCounters.push(...subtree.getPerfCounters()));
646
- return subCounters;
647
- }
648
- printPerfCounters() {
649
- const perfCounters = this.getPerfCounters().sort((a, b) => b[1].totalTime - a[1].totalTime).map((e) => ({
650
- path: e[0],
651
- totalMs: e[1].totalTime,
652
- computeMs: e[1].computeTime,
653
- cacheMs: e[1].cacheTime,
654
- ops: `${e[1].computed}/${e[1].cached}`
655
- }));
656
- console.table(perfCounters);
657
- }
658
- callstack = [];
659
- pushToEvalCallStack(path) {
660
- this.callstack.push(path);
661
- this.parent?.pushToEvalCallStack(path);
662
- }
663
- flushEvalCallStack() {
664
- this.callstack.length = 0;
665
- }
666
- evaluate() {
667
- const start = Date.now();
668
- const circularPatternDetected = this.callstack.some((p) => p.startsWith(this.pathString));
669
- if (circularPatternDetected) throw new Error(`Circular reference pattern detected @ ${this.pathString}`);
670
- this.pushToEvalCallStack(this.pathString);
671
- const { perfCounters, cache } = this;
672
- if (cache !== void 0) {
673
- const deserialized = Serialize.deserialize(cache);
674
- const end2 = Date.now();
675
- const time2 = end2 - start;
676
- perfCounters.cached++;
677
- perfCounters.cacheTime += time2;
678
- perfCounters.cacheTimeAvg = perfCounters.cacheTime / perfCounters.cached;
679
- perfCounters.totalTime = perfCounters.computeTime + perfCounters.cacheTime;
680
- return deserialized;
681
- }
682
- const init = this.initValue();
683
- const inner = this.getInnerValue(init);
684
- const wrapped = this.wrapInnerValue(inner);
685
- this.setCache(wrapped);
686
- const end = Date.now();
687
- const time = end - start;
688
- perfCounters.computed++;
689
- perfCounters.computeTime += time;
690
- perfCounters.computeTimeAvg = perfCounters.computeTime / perfCounters.computed;
691
- perfCounters.totalTime = perfCounters.computeTime + perfCounters.cacheTime;
692
- this.flushEvalCallStack();
693
- return wrapped;
694
- }
695
- };
696
-
697
- // src/agnostic/html/hyper-json/transformers/transformSelected/index.ts
698
- var transformSelected = (callerTagName, ...args) => {
699
- return Transformers.toNamed(callerTagName, (currentValue, callerTree) => {
700
- const { NodeList, Element, Text } = Crossenv.getWindow();
701
- if (!(currentValue instanceof Element) && !(currentValue instanceof NodeList)) return Utils.makeTransformerError({
702
- message: "Current value must be Element or NodeList",
703
- input: currentValue
704
- });
705
- const [selector, ...transformerDescriptors] = args;
706
- if (typeof selector !== "string" && !(selector instanceof Text)) return Utils.makeTransformerError({
707
- message: "Selector argument must be string or text",
708
- input: selector !== void 0 ? selector : "<undefined>"
709
- });
710
- if (transformerDescriptors.length === 0) return Utils.makeTransformerError("No transformer descriptor provided");
711
- let transformers = [];
712
- for (const descriptor of transformerDescriptors) {
713
- const errorResponse = Utils.makeTransformerError({
714
- message: "Transformer descriptors must be of shape { name: string, arguments: Value[] }",
715
- input: descriptor
716
- });
717
- if (!isRecord(descriptor)) return errorResponse;
718
- if (!("name" in descriptor) || typeof descriptor.name !== "string") return errorResponse;
719
- if (!("arguments" in descriptor) || !Array.isArray(descriptor.arguments)) return errorResponse;
720
- const { name, arguments: args2 } = descriptor;
721
- const generator = callerTree.getGenerator(name);
722
- if (generator === void 0) return Utils.makeTransformerError(`No transformer found under the name ${name}`);
723
- transformers.push(generator(name, ...args2));
724
- }
725
- const elementInput = Cast.toElement(currentValue);
726
- const strSelector = Cast.toString(selector);
727
- const selected = Array.from(elementInput.querySelectorAll(strSelector));
728
- const transformationErrors = [];
729
- const transformed = selected.map((input) => {
730
- const output = transformers.reduce((reduced, transformer, transformerPos) => {
731
- const result = transformer(reduced, callerTree);
732
- if (result.action === "REPLACE") return result.value;
733
- else if (result.action === "MERGE") {
734
- const merged = Tree.mergeValues(
735
- reduced,
736
- result.value,
737
- transformerPos,
738
- callerTree
739
- );
740
- return merged;
741
- }
742
- if (result.action === "ERROR") transformationErrors.push({
743
- message: result.value,
744
- transformerName: transformer.transformerName,
745
- transformerPos,
746
- transforming: reduced
747
- });
748
- return reduced;
749
- }, input);
750
- return { input, output };
751
- });
752
- if (transformationErrors.length !== 0) return Utils.makeTransformerError({
753
- message: "Some nested transformers returned an error",
754
- errors: transformationErrors
755
- });
756
- const nonElementTransformResult = transformed.find((item) => !(item.output instanceof Element));
757
- if (nonElementTransformResult !== void 0) return Utils.makeTransformerError({
758
- message: "Some transformations resulted in a non Element output",
759
- inputElement: nonElementTransformResult.input,
760
- outputValue: nonElementTransformResult.output
761
- });
762
- const transformMap = new Map(transformed.map((e) => [e.input, e.output]));
763
- const elementOutput = replaceInElement(elementInput, transformMap);
764
- return {
765
- action: "REPLACE",
766
- value: elementOutput
767
- };
768
- });
769
- };
770
-
771
- // src/agnostic/html/hyper-json/transformers/trim/index.ts
772
- var trim = (callerTagName) => {
773
- return Transformers.toNamed(callerTagName, (currentValue) => {
774
- const { Text } = Crossenv.getWindow();
775
- if (typeof currentValue !== "string" && !(currentValue instanceof Text)) return Utils.makeTransformerError({
776
- message: "Current value must be of type string or Text",
777
- input: currentValue
778
- });
779
- const inputStr = Cast.toString(currentValue);
780
- const trimmed = inputStr.trim();
781
- return {
782
- action: "REPLACE",
783
- value: currentValue instanceof Text ? Cast.toText(trimmed) : trimmed
784
- };
785
- });
786
- };
787
-
788
- // src/agnostic/html/hyper-json/transformers/index.ts
789
- var Transformers;
790
- ((Transformers2) => {
791
- Transformers2.defaultGeneratorsMap = /* @__PURE__ */ new Map([
792
- ["append", append],
793
- ["classList", classList],
794
- ["join", join],
795
- ["length", length],
796
- ["print", print],
797
- ["push", push],
798
- ["querySelector", querySelector],
799
- ["ref", ref],
800
- ["replace", replace],
801
- ["split", split],
802
- ["toArray", toArray],
803
- ["toBoolean", toBoolean],
804
- ["toElement", toElement],
805
- ["toNodeList", toNodeList],
806
- ["toNull", toNull],
807
- ["toNumber", toNumber],
808
- ["toRecord", toRecord],
809
- ["toRef", toRef],
810
- ["toString", toString],
811
- ["toText", toText],
812
- ["toTransformer", toTransformer],
813
- ["transformSelected", transformSelected],
814
- ["trim", trim]
815
- ]);
816
- Transformers2.toNamed = (name, anonymous) => {
817
- const named = anonymous;
818
- named.transformerName = name;
819
- return named;
820
- };
821
- })(Transformers || (Transformers = {}));
822
-
823
- // src/agnostic/html/hyper-json/transformers/append/index.ts
824
- var append = (callerTagName, ...args) => {
825
- return Transformers.toNamed(callerTagName, (currentValue) => {
826
- const { document, Element, Text, NodeList } = Crossenv.getWindow();
827
- const [...toAppend] = args;
828
- const frag = document.createDocumentFragment();
829
- frag.append(...Array.from(Cast.toNodeList(currentValue)).map((e) => e.cloneNode(true)));
830
- toAppend.forEach((item) => {
831
- if (item instanceof Element) frag.append(item.cloneNode(true));
832
- else if (item instanceof Text) frag.append(item.cloneNode(true));
833
- else if (item instanceof NodeList) frag.append(...Array.from(item).map((e) => e.cloneNode(true)));
834
- else frag.append(Cast.toString(item));
835
- });
836
- return {
837
- action: "REPLACE",
838
- value: frag.childNodes
839
- };
840
- });
841
- };
842
-
843
- export {
844
- append,
845
- classList,
846
- join,
847
- length,
848
- print,
849
- push,
850
- querySelector,
851
- ref,
852
- replace,
853
- split,
854
- toArray,
855
- toBoolean,
856
- toElement,
857
- toNodeList,
858
- toNull,
859
- toNumber,
860
- toRecord,
861
- toRef,
862
- toString,
863
- toText,
864
- toTransformer,
865
- Tree,
866
- transformSelected,
867
- trim,
868
- Transformers,
869
- Cast
870
- };