@design-edito/tools 0.1.21 → 0.1.23

Sign up to get free protection for your applications and to get access to all the features.
Files changed (223) hide show
  1. package/agnostic/arrays/find-duplicates/index.d.ts +1 -0
  2. package/agnostic/arrays/find-duplicates/index.js +6 -0
  3. package/agnostic/arrays/index.d.ts +8 -7
  4. package/agnostic/arrays/make/index.d.ts +3 -0
  5. package/agnostic/arrays/make/index.js +6 -0
  6. package/agnostic/arrays/random-pick/index.d.ts +2 -0
  7. package/agnostic/arrays/random-pick/index.js +8 -0
  8. package/agnostic/css/index.js +13 -13
  9. package/agnostic/html/hyper-json/cast/index.d.ts +18 -0
  10. package/agnostic/html/hyper-json/cast/index.js +11 -0
  11. package/agnostic/html/hyper-json/crossenv/index.d.ts +13 -0
  12. package/agnostic/html/hyper-json/crossenv/index.js +6 -0
  13. package/agnostic/html/hyper-json/index.d.ts +14 -43
  14. package/agnostic/html/hyper-json/index.js +32 -2
  15. package/agnostic/html/hyper-json/serialize/index.d.ts +5 -0
  16. package/agnostic/html/hyper-json/serialize/index.js +7 -0
  17. package/agnostic/html/hyper-json/transformers/append/index.d.ts +2 -0
  18. package/agnostic/html/hyper-json/transformers/append/index.js +12 -0
  19. package/agnostic/html/hyper-json/transformers/classList/index.d.ts +2 -0
  20. package/agnostic/html/hyper-json/transformers/classList/index.js +12 -0
  21. package/agnostic/html/hyper-json/transformers/index.d.ts +4 -0
  22. package/agnostic/html/hyper-json/transformers/index.js +39 -0
  23. package/agnostic/html/hyper-json/transformers/join/index.d.ts +2 -0
  24. package/agnostic/html/hyper-json/transformers/join/index.js +12 -0
  25. package/agnostic/html/hyper-json/transformers/length/index.d.ts +2 -0
  26. package/agnostic/html/hyper-json/transformers/length/index.js +8 -0
  27. package/agnostic/html/hyper-json/transformers/print/index.d.ts +2 -0
  28. package/agnostic/html/hyper-json/transformers/print/index.js +7 -0
  29. package/agnostic/html/hyper-json/transformers/push/index.d.ts +2 -0
  30. package/agnostic/html/hyper-json/transformers/push/index.js +7 -0
  31. package/agnostic/html/hyper-json/transformers/querySelector/index.d.ts +2 -0
  32. package/agnostic/html/hyper-json/transformers/querySelector/index.js +12 -0
  33. package/agnostic/html/hyper-json/transformers/ref/index.d.ts +2 -0
  34. package/agnostic/html/hyper-json/transformers/ref/index.js +12 -0
  35. package/agnostic/html/hyper-json/transformers/replace/index.d.ts +2 -0
  36. package/agnostic/html/hyper-json/transformers/replace/index.js +13 -0
  37. package/agnostic/html/hyper-json/transformers/split/index.d.ts +2 -0
  38. package/agnostic/html/hyper-json/transformers/split/index.js +12 -0
  39. package/agnostic/html/hyper-json/transformers/toArray/index.d.ts +2 -0
  40. package/agnostic/html/hyper-json/transformers/toArray/index.js +12 -0
  41. package/agnostic/html/hyper-json/transformers/toBoolean/index.d.ts +2 -0
  42. package/agnostic/html/hyper-json/transformers/toBoolean/index.js +12 -0
  43. package/agnostic/html/hyper-json/transformers/toElement/index.d.ts +2 -0
  44. package/agnostic/html/hyper-json/transformers/toElement/index.js +12 -0
  45. package/agnostic/html/hyper-json/transformers/toNodeList/index.d.ts +2 -0
  46. package/agnostic/html/hyper-json/transformers/toNodeList/index.js +12 -0
  47. package/agnostic/html/hyper-json/transformers/toNull/index.d.ts +2 -0
  48. package/agnostic/html/hyper-json/transformers/toNull/index.js +7 -0
  49. package/agnostic/html/hyper-json/transformers/toNumber/index.d.ts +2 -0
  50. package/agnostic/html/hyper-json/transformers/toNumber/index.js +12 -0
  51. package/agnostic/html/hyper-json/transformers/toRecord/index.d.ts +2 -0
  52. package/agnostic/html/hyper-json/transformers/toRecord/index.js +12 -0
  53. package/agnostic/html/hyper-json/transformers/toRef/index.d.ts +2 -0
  54. package/agnostic/html/hyper-json/transformers/toRef/index.js +12 -0
  55. package/agnostic/html/hyper-json/transformers/toString/index.d.ts +2 -0
  56. package/agnostic/html/hyper-json/transformers/toString/index.js +12 -0
  57. package/agnostic/html/hyper-json/transformers/toText/index.d.ts +2 -0
  58. package/agnostic/html/hyper-json/transformers/toText/index.js +12 -0
  59. package/agnostic/html/hyper-json/transformers/toTransformer/index.d.ts +2 -0
  60. package/agnostic/html/hyper-json/transformers/toTransformer/index.js +12 -0
  61. package/agnostic/html/hyper-json/transformers/transformSelected/index.d.ts +2 -0
  62. package/agnostic/html/hyper-json/transformers/transformSelected/index.js +39 -0
  63. package/agnostic/html/hyper-json/transformers/trim/index.d.ts +2 -0
  64. package/agnostic/html/hyper-json/transformers/trim/index.js +12 -0
  65. package/agnostic/html/hyper-json/tree/index.d.ts +58 -0
  66. package/agnostic/html/hyper-json/tree/index.js +39 -0
  67. package/agnostic/html/hyper-json/types/index.d.ts +86 -0
  68. package/agnostic/html/hyper-json/types/index.js +6 -0
  69. package/agnostic/html/hyper-json/utils/index.d.ts +10 -0
  70. package/agnostic/html/hyper-json/utils/index.js +6 -0
  71. package/agnostic/html/index.js +48 -16
  72. package/agnostic/html/replace-in-element/index.d.ts +1 -0
  73. package/agnostic/html/replace-in-element/index.js +6 -0
  74. package/agnostic/html/sanitize/index.js +5 -3
  75. package/agnostic/html/selector-to-element/index.js +5 -3
  76. package/agnostic/html/string-to-nodes/index.js +6 -4
  77. package/agnostic/index.d.ts +0 -2
  78. package/agnostic/misc/{web-crawler → crawler}/index.d.ts +1 -1
  79. package/agnostic/misc/crawler/index.js +7 -0
  80. package/agnostic/misc/crossenv/get-document/index.d.ts +2 -0
  81. package/agnostic/misc/crossenv/get-document/index.js +7 -0
  82. package/agnostic/misc/crossenv/get-window/browser.d.ts +2 -0
  83. package/agnostic/misc/crossenv/get-window/index.d.ts +2 -0
  84. package/agnostic/misc/crossenv/get-window/index.js +6 -0
  85. package/agnostic/misc/crossenv/index.d.ts +4 -1
  86. package/agnostic/misc/crossenv/index.js +3 -1
  87. package/agnostic/misc/crossenv/types.d.ts +3 -0
  88. package/agnostic/misc/index.d.ts +3 -1
  89. package/agnostic/misc/index.js +24 -14
  90. package/agnostic/misc/lorem-ipsum/index.js +5 -2
  91. package/agnostic/numbers/index.js +6 -6
  92. package/agnostic/objects/index.js +9 -9
  93. package/agnostic/optim/index.js +3 -3
  94. package/agnostic/random/index.js +6 -0
  95. package/agnostic/strings/index.js +3 -3
  96. package/agnostic/time/duration/index.d.ts +26 -0
  97. package/agnostic/time/duration/index.js +6 -0
  98. package/agnostic/time/index.d.ts +8 -7
  99. package/agnostic/time/index.js +17 -28
  100. package/agnostic/time/timeout/index.d.ts +1 -0
  101. package/agnostic/time/timeout/index.js +6 -0
  102. package/agnostic/time/transitions/index.js +7 -0
  103. package/agnostic/time/wait/index.js +6 -0
  104. package/chunks/chunk-2DMWYLFG.js +16 -0
  105. package/chunks/chunk-2KT7AKRW.js +17 -0
  106. package/chunks/{chunk-UCV2AZZV.js → chunk-2PI5AZIL.js} +14 -25
  107. package/chunks/chunk-3KQRMKQJ.js +27 -0
  108. package/chunks/chunk-3LVR6KZH.js +22 -0
  109. package/chunks/chunk-3PHPA25U.js +18 -0
  110. package/chunks/chunk-3PM7GZ6B.js +33 -0
  111. package/chunks/chunk-3SQJEEBK.js +642 -0
  112. package/chunks/chunk-3UURICGY.js +211 -0
  113. package/chunks/chunk-3X6RXJBR.js +37 -0
  114. package/chunks/{chunk-DQNJQBC6.js → chunk-4CIS3R7T.js} +1 -1
  115. package/chunks/chunk-4F7ZVE7T.js +332 -0
  116. package/chunks/chunk-4GUDL44X.js +13 -0
  117. package/chunks/chunk-4RDW62CU.js +36 -0
  118. package/chunks/chunk-5IR2PWO6.js +211 -0
  119. package/chunks/chunk-6CM2SWSX.js +35 -0
  120. package/chunks/chunk-6EYBQHJS.js +33 -0
  121. package/chunks/chunk-6NYUQXJT.js +109 -0
  122. package/chunks/chunk-6NZCBAT4.js +19 -0
  123. package/chunks/chunk-6V23XMYE.js +982 -0
  124. package/chunks/chunk-73V5RDSG.js +13 -0
  125. package/chunks/chunk-7IGZP4TV.js +30 -0
  126. package/chunks/chunk-7QMAYGNV.js +25 -0
  127. package/chunks/chunk-7YW5HL6N.js +168 -0
  128. package/chunks/chunk-AFFLZUW5.js +33 -0
  129. package/chunks/chunk-AMS3FB24.js +85 -0
  130. package/chunks/chunk-AXMPRLPI.js +35 -0
  131. package/chunks/chunk-B6CSCX7J.js +278 -0
  132. package/chunks/chunk-BDHNULU7.js +211 -0
  133. package/chunks/chunk-BNB7LEMQ.js +35 -0
  134. package/chunks/chunk-CECF6UZV.js +29 -0
  135. package/chunks/chunk-DBAJ66BR.js +18 -0
  136. package/chunks/chunk-DGQLPQS2.js +33 -0
  137. package/chunks/chunk-DLLTITVO.js +33 -0
  138. package/chunks/chunk-DMDOCC72.js +18 -0
  139. package/chunks/{chunk-IFT6HLDW.js → chunk-DRH7Q4FL.js} +3 -6
  140. package/chunks/chunk-DRNK3RLE.js +64 -0
  141. package/chunks/chunk-EAY65ZP6.js +22 -0
  142. package/chunks/{chunk-4GDNHUCZ.js → chunk-ENMBK6H7.js} +2 -2
  143. package/chunks/chunk-F5YMP5WU.js +18 -0
  144. package/chunks/chunk-F7EVMWVG.js +27 -0
  145. package/chunks/chunk-F7XWUG2V.js +18 -0
  146. package/chunks/chunk-FCU6IHKD.js +15 -0
  147. package/chunks/chunk-FFOATPL4.js +211 -0
  148. package/chunks/chunk-FWHUASVA.js +22 -0
  149. package/chunks/chunk-GBMHGF7P.js +20 -0
  150. package/chunks/{chunk-GJOU3UAL.js → chunk-GDYVCEWA.js} +8 -6
  151. package/chunks/chunk-GNBIAWSY.js +278 -0
  152. package/chunks/chunk-GQSPY2CV.js +19 -0
  153. package/chunks/chunk-HDALT7XM.js +620 -0
  154. package/chunks/chunk-HENMQK5Y.js +647 -0
  155. package/chunks/chunk-HFEOHBYG.js +647 -0
  156. package/chunks/chunk-HGECIEKD.js +35 -0
  157. package/chunks/chunk-HUKHWFVL.js +19 -0
  158. package/chunks/chunk-IGD53YL5.js +870 -0
  159. package/chunks/chunk-ILEM73XB.js +981 -0
  160. package/chunks/{chunk-DMGR57KA.js → chunk-IPB3U2PD.js} +20 -18
  161. package/chunks/chunk-JY3QZ2YO.js +211 -0
  162. package/chunks/chunk-K3OQEJI3.js +33 -0
  163. package/chunks/chunk-KXBKJ3GS.js +642 -0
  164. package/chunks/chunk-L7SL3MND.js +35 -0
  165. package/chunks/chunk-LCFDDWJR.js +278 -0
  166. package/chunks/chunk-LGECI2WL.js +306 -0
  167. package/chunks/chunk-LGUEB5L5.js +211 -0
  168. package/chunks/chunk-LI6QDZCA.js +211 -0
  169. package/chunks/{chunk-ZMHKLIP2.js → chunk-LYMZZ4BB.js} +1 -1
  170. package/chunks/chunk-MDEDNPU5.js +38 -0
  171. package/chunks/chunk-MQHKEGP4.js +33 -0
  172. package/chunks/chunk-NF5ACCXZ.js +211 -0
  173. package/chunks/chunk-NK557MKV.js +35 -0
  174. package/chunks/chunk-NWMEWFTK.js +33 -0
  175. package/chunks/chunk-O5OTBN22.js +35 -0
  176. package/chunks/chunk-O7MDUYEF.js +211 -0
  177. package/chunks/{chunk-LI5KVXAJ.js → chunk-OO5SJJDW.js} +2 -5
  178. package/chunks/chunk-OWRQ3QBI.js +211 -0
  179. package/chunks/chunk-Q52UJOBC.js +18 -0
  180. package/chunks/chunk-QFEF4EET.js +211 -0
  181. package/chunks/chunk-QHSVCGLD.js +280 -0
  182. package/chunks/chunk-QO64L4KB.js +97 -0
  183. package/chunks/{chunk-WLL3FNVL.js → chunk-R6KCN6CK.js} +1 -1
  184. package/chunks/chunk-RHYLRLKT.js +211 -0
  185. package/chunks/chunk-RJ5E22H5.js +15 -0
  186. package/chunks/chunk-RLAZR3NL.js +8 -0
  187. package/chunks/chunk-RWUPHV54.js +25 -0
  188. package/chunks/chunk-S2757FI3.js +36 -0
  189. package/chunks/chunk-S42P73SC.js +30 -0
  190. package/chunks/chunk-S5XDYLJH.js +18 -0
  191. package/chunks/chunk-SCM6NACT.js +15 -0
  192. package/chunks/chunk-SH3Y2I5P.js +18 -0
  193. package/chunks/chunk-SMLKMOAX.js +18 -0
  194. package/chunks/chunk-TATRVKBS.js +211 -0
  195. package/chunks/chunk-UODQZRY2.js +620 -0
  196. package/chunks/chunk-UOULDS6J.js +33 -0
  197. package/chunks/chunk-UUDXUSI5.js +211 -0
  198. package/chunks/{chunk-GL23YDEH.js → chunk-UWAMSJ2X.js} +3 -6
  199. package/chunks/chunk-UZUXLD2Q.js +35 -0
  200. package/chunks/chunk-VA6HXRX4.js +211 -0
  201. package/chunks/chunk-VRUJR6WR.js +39 -0
  202. package/chunks/chunk-W6YBEEZM.js +211 -0
  203. package/chunks/chunk-WECTUHS5.js +127 -0
  204. package/chunks/chunk-YE5HI3RK.js +211 -0
  205. package/chunks/chunk-YEBOL67V.js +32 -0
  206. package/chunks/chunk-YS6WMSWC.js +15 -0
  207. package/{agnostic/misc/web-crawler/index.js → chunks/chunk-YUP434IY.js} +8 -7
  208. package/chunks/chunk-YYE2MVZO.js +211 -0
  209. package/chunks/chunk-Z7ZDDBV5.js +18 -0
  210. package/chunks/chunk-ZB73RE7P.js +35 -0
  211. package/chunks/chunk-ZONKYNKA.js +28 -0
  212. package/package.json +4 -1
  213. package/agnostic/async/index.d.ts +0 -8
  214. package/agnostic/async/index.js +0 -20
  215. package/agnostic/async/timeout-call/index.d.ts +0 -1
  216. package/agnostic/async/timeout-call/index.js +0 -6
  217. package/agnostic/async/transitions/index.js +0 -7
  218. package/agnostic/async/wait/index.js +0 -6
  219. package/agnostic/misc/random/index.js +0 -6
  220. package/chunks/chunk-4VC5PT5T.js +0 -13
  221. /package/agnostic/{misc/random → random}/index.d.ts +0 -0
  222. /package/agnostic/{async → time}/transitions/index.d.ts +0 -0
  223. /package/agnostic/{async → time}/wait/index.d.ts +0 -0
@@ -0,0 +1,642 @@
1
+ import {
2
+ toNodeList
3
+ } from "./chunk-DBAJ66BR.js";
4
+ import {
5
+ toRecord
6
+ } from "./chunk-F5YMP5WU.js";
7
+ import {
8
+ toRef
9
+ } from "./chunk-4RDW62CU.js";
10
+ import {
11
+ toString
12
+ } from "./chunk-DMDOCC72.js";
13
+ import {
14
+ toText
15
+ } from "./chunk-3PHPA25U.js";
16
+ import {
17
+ trim
18
+ } from "./chunk-7IGZP4TV.js";
19
+ import {
20
+ toTransformer
21
+ } from "./chunk-SMLKMOAX.js";
22
+ import {
23
+ ref
24
+ } from "./chunk-3X6RXJBR.js";
25
+ import {
26
+ replace
27
+ } from "./chunk-MDEDNPU5.js";
28
+ import {
29
+ toArray
30
+ } from "./chunk-Q52UJOBC.js";
31
+ import {
32
+ split
33
+ } from "./chunk-6CM2SWSX.js";
34
+ import {
35
+ toBoolean
36
+ } from "./chunk-SH3Y2I5P.js";
37
+ import {
38
+ toNull
39
+ } from "./chunk-RJ5E22H5.js";
40
+ import {
41
+ toElement
42
+ } from "./chunk-S5XDYLJH.js";
43
+ import {
44
+ toNumber
45
+ } from "./chunk-F7XWUG2V.js";
46
+ import {
47
+ append
48
+ } from "./chunk-K3OQEJI3.js";
49
+ import {
50
+ length
51
+ } from "./chunk-S42P73SC.js";
52
+ import {
53
+ classList
54
+ } from "./chunk-VRUJR6WR.js";
55
+ import {
56
+ join
57
+ } from "./chunk-RWUPHV54.js";
58
+ import {
59
+ print
60
+ } from "./chunk-2DMWYLFG.js";
61
+ import {
62
+ push
63
+ } from "./chunk-FWHUASVA.js";
64
+ import {
65
+ querySelector
66
+ } from "./chunk-BNB7LEMQ.js";
67
+ import {
68
+ Cast
69
+ } from "./chunk-QO64L4KB.js";
70
+ import {
71
+ Serialize
72
+ } from "./chunk-DRNK3RLE.js";
73
+ import {
74
+ Crossenv
75
+ } from "./chunk-3LVR6KZH.js";
76
+ import {
77
+ Utils
78
+ } from "./chunk-YEBOL67V.js";
79
+ import {
80
+ Types
81
+ } from "./chunk-7QMAYGNV.js";
82
+ import {
83
+ replaceInElement
84
+ } from "./chunk-GBMHGF7P.js";
85
+ import {
86
+ isInEnum
87
+ } from "./chunk-E6MSDKON.js";
88
+ import {
89
+ isRecord
90
+ } from "./chunk-YDIBNEGA.js";
91
+
92
+ // src/agnostic/html/hyper-json/transformers/transformSelected/index.ts
93
+ var transformSelected = (callerTagName, ...args) => {
94
+ return Utils.toNamedTransformer(callerTagName, (currentValue, callerTree) => {
95
+ const { NodeList, Element, Text } = Crossenv.getWindow();
96
+ if (!(currentValue instanceof Element) && !(currentValue instanceof NodeList)) return Utils.makeTransformerError({
97
+ message: "Current value must be Element or NodeList",
98
+ input: currentValue
99
+ });
100
+ const [selector, ...transformerDescriptors] = args;
101
+ if (typeof selector !== "string" && !(selector instanceof Text)) return Utils.makeTransformerError({
102
+ message: "Selector argument must be string or text",
103
+ input: selector !== void 0 ? selector : "<undefined>"
104
+ });
105
+ if (transformerDescriptors.length === 0) return Utils.makeTransformerError("No transformer descriptor provided");
106
+ let transformers = [];
107
+ for (const descriptor of transformerDescriptors) {
108
+ const errorResponse = Utils.makeTransformerError({
109
+ message: "Transformer descriptors must be of shape { name: string, arguments: Value[] }",
110
+ input: descriptor
111
+ });
112
+ if (!isRecord(descriptor)) return errorResponse;
113
+ if (!("name" in descriptor) || typeof descriptor.name !== "string") return errorResponse;
114
+ if (!("arguments" in descriptor) || !Array.isArray(descriptor.arguments)) return errorResponse;
115
+ const { name, arguments: args2 } = descriptor;
116
+ const generator = callerTree.getGenerator(name);
117
+ if (generator === void 0) return Utils.makeTransformerError(`No transformer found under the name ${name}`);
118
+ transformers.push(generator(name, ...args2));
119
+ }
120
+ const elementInput = Cast.toElement(currentValue);
121
+ const strSelector = Cast.toString(selector);
122
+ const selected = Array.from(elementInput.querySelectorAll(strSelector));
123
+ const transformationErrors = [];
124
+ const transformed = selected.map((input) => {
125
+ const output = transformers.reduce((reduced, transformer, transformerPos) => {
126
+ const result = transformer(reduced, callerTree);
127
+ if (result.action === "REPLACE") return result.value;
128
+ else if (result.action === "MERGE") {
129
+ const merged = Tree.mergeValues(
130
+ reduced,
131
+ result.value,
132
+ transformerPos,
133
+ callerTree
134
+ );
135
+ return merged;
136
+ }
137
+ if (result.action === "ERROR") transformationErrors.push({
138
+ message: result.value,
139
+ transformerName: transformer.transformerName,
140
+ transformerPos,
141
+ transforming: reduced
142
+ });
143
+ return reduced;
144
+ }, input);
145
+ return { input, output };
146
+ });
147
+ if (transformationErrors.length !== 0) return Utils.makeTransformerError({
148
+ message: "Some nested transformers returned an error",
149
+ errors: transformationErrors
150
+ });
151
+ const nonElementTransformResult = transformed.find((item) => !(item.output instanceof Element));
152
+ if (nonElementTransformResult !== void 0) return Utils.makeTransformerError({
153
+ message: "Some transformations resulted in a non Element output",
154
+ inputElement: nonElementTransformResult.input,
155
+ outputValue: nonElementTransformResult.output
156
+ });
157
+ const transformMap = new Map(transformed.map((e) => [e.input, e.output]));
158
+ const elementOutput = replaceInElement(elementInput, transformMap);
159
+ return {
160
+ action: "REPLACE",
161
+ value: elementOutput
162
+ };
163
+ });
164
+ };
165
+
166
+ // src/agnostic/html/hyper-json/transformers/index.ts
167
+ var Transformers;
168
+ ((Transformers2) => {
169
+ Transformers2.defaultGeneratorsMap = /* @__PURE__ */ new Map([
170
+ ["append", append],
171
+ ["classList", classList],
172
+ ["join", join],
173
+ ["length", length],
174
+ ["print", print],
175
+ ["push", push],
176
+ ["querySelector", querySelector],
177
+ ["ref", ref],
178
+ ["replace", replace],
179
+ ["split", split],
180
+ ["toArray", toArray],
181
+ ["toBoolean", toBoolean],
182
+ ["toElement", toElement],
183
+ ["toNodeList", toNodeList],
184
+ ["toNull", toNull],
185
+ ["toNumber", toNumber],
186
+ ["toRecord", toRecord],
187
+ ["toRef", toRef],
188
+ ["toString", toString],
189
+ ["toText", toText],
190
+ ["toTransformer", toTransformer],
191
+ ["transformSelected", transformSelected],
192
+ ["trim", trim]
193
+ ]);
194
+ })(Transformers || (Transformers = {}));
195
+
196
+ // src/agnostic/html/hyper-json/tree/index.ts
197
+ var getWindow = Crossenv.getWindow;
198
+ var isElement = (node) => node.nodeType === getWindow().Node.ELEMENT_NODE;
199
+ var isText = (node) => node.nodeType === getWindow().Node.TEXT_NODE;
200
+ var Tree;
201
+ ((_Tree) => {
202
+ _Tree.defaultKeyAttribute = "_key";
203
+ _Tree.defaultActionAttribute = "_action";
204
+ _Tree.defaultRootKey = "_ROOT_";
205
+ function mergeValues(currentValue, incomingValue, mergeKey, initiatorTree) {
206
+ const { Element, Text, NodeList, document } = getWindow();
207
+ if (typeof incomingValue === "function") {
208
+ const evaluated = incomingValue(currentValue, initiatorTree);
209
+ if (evaluated.action === null) return currentValue;
210
+ if (evaluated.action === "REPLACE") return evaluated.value;
211
+ if (evaluated.action === "ERROR") {
212
+ const errorMessage = `Tranformer error:
213
+ from: ${incomingValue.transformerName}
214
+ at: ${initiatorTree.pathString.slice(1)}/${mergeKey}
215
+ message:`;
216
+ console.warn(errorMessage, evaluated.value);
217
+ return currentValue;
218
+ }
219
+ return mergeValues(
220
+ currentValue,
221
+ evaluated.value,
222
+ mergeKey,
223
+ initiatorTree
224
+ );
225
+ }
226
+ if (Array.isArray(currentValue)) {
227
+ if (typeof mergeKey === "string") return currentValue;
228
+ return [...currentValue, incomingValue];
229
+ }
230
+ if (currentValue === null || typeof currentValue === "boolean" || typeof currentValue === "number" || typeof currentValue === "string" || typeof currentValue === "function" || currentValue instanceof Text || currentValue instanceof Element) {
231
+ if (incomingValue === null || typeof incomingValue === "boolean" || typeof incomingValue === "number" || typeof incomingValue === "string" || Array.isArray(incomingValue) || isRecord(incomingValue)) {
232
+ return incomingValue;
233
+ }
234
+ const frag = document.createDocumentFragment();
235
+ if (currentValue instanceof Element || currentValue instanceof Text) frag.append(currentValue.cloneNode(true));
236
+ else frag.append(`${currentValue}`);
237
+ if (incomingValue instanceof Element || incomingValue instanceof Text) {
238
+ frag.append(incomingValue.cloneNode(true));
239
+ return frag.childNodes;
240
+ }
241
+ frag.append(...Array.from(incomingValue).map((e) => e.cloneNode(true)));
242
+ return frag.childNodes;
243
+ }
244
+ if (currentValue instanceof NodeList) {
245
+ if (incomingValue instanceof Element || incomingValue instanceof Text) {
246
+ const frag = document.createDocumentFragment();
247
+ frag.append(...Array.from(currentValue).map((e) => e.cloneNode(true)), incomingValue);
248
+ return frag.childNodes;
249
+ }
250
+ if (incomingValue instanceof NodeList) {
251
+ const frag = document.createDocumentFragment();
252
+ frag.append(
253
+ ...Array.from(currentValue).map((e) => e.cloneNode(true)),
254
+ ...Array.from(incomingValue).map((e) => e.cloneNode(true))
255
+ );
256
+ return frag.childNodes;
257
+ }
258
+ if (incomingValue === null || typeof incomingValue === "string" || typeof incomingValue === "number" || typeof incomingValue === "boolean") {
259
+ const frag = document.createDocumentFragment();
260
+ frag.append(...Array.from(currentValue).map((e) => e.cloneNode(true)), `${incomingValue}`);
261
+ return frag.childNodes;
262
+ }
263
+ if (Array.isArray(incomingValue)) return [
264
+ ...Array.from(currentValue),
265
+ ...incomingValue
266
+ ];
267
+ return { ...incomingValue };
268
+ }
269
+ if (typeof mergeKey === "number") return currentValue;
270
+ return {
271
+ ...currentValue,
272
+ [mergeKey]: incomingValue
273
+ };
274
+ }
275
+ _Tree.mergeValues = mergeValues;
276
+ function mergeRootNodes(...args) {
277
+ const [nodes, options] = args;
278
+ const actionAttribute = options?.actionAttribute ?? _Tree.defaultActionAttribute;
279
+ const keyAttribute = options?.keyAttribute ?? _Tree.defaultKeyAttribute;
280
+ const rootKey = options?.rootKey ?? _Tree.defaultRootKey;
281
+ const { Element } = getWindow();
282
+ const elements = nodes.filter((e) => e instanceof Element);
283
+ elements.forEach((element) => {
284
+ element.setAttribute(keyAttribute, rootKey);
285
+ const elementAction = element.getAttribute(actionAttribute) ?? Types.ReductionAction.APPEND;
286
+ element.setAttribute(actionAttribute, elementAction);
287
+ });
288
+ const merged = mergeNodes(elements, options);
289
+ return merged;
290
+ }
291
+ _Tree.mergeRootNodes = mergeRootNodes;
292
+ function mergeNodes(nodes, options = {}) {
293
+ const [first, ...rest] = nodes;
294
+ const actionAttribute = options?.actionAttribute ?? _Tree.defaultActionAttribute;
295
+ const keyAttribute = options?.keyAttribute ?? _Tree.defaultKeyAttribute;
296
+ if (first === void 0) throw new Error("Expecting at least one node");
297
+ const { Text, Element, document } = getWindow();
298
+ function isTextOrElement(node) {
299
+ return node instanceof Text || node instanceof Element;
300
+ }
301
+ let CURRENT = first;
302
+ rest.forEach((node) => {
303
+ if (node instanceof Text) {
304
+ CURRENT.remove();
305
+ CURRENT = node;
306
+ return;
307
+ }
308
+ const actionRaw = node.getAttribute(actionAttribute);
309
+ const action = isInEnum(Types.ReductionAction, actionRaw) ? actionRaw : Types.ReductionAction.REPLACE;
310
+ if (action === Types.ReductionAction.REPLACE) {
311
+ CURRENT.remove();
312
+ CURRENT = node;
313
+ return;
314
+ }
315
+ if (CURRENT instanceof Text) {
316
+ if (node instanceof Text) {
317
+ const appended = action === Types.ReductionAction.APPEND ? document.createTextNode(`${CURRENT.textContent}${node.textContent}`) : document.createTextNode(`${node.textContent}${CURRENT.textContent}`);
318
+ CURRENT.remove();
319
+ node.remove();
320
+ CURRENT = appended;
321
+ return;
322
+ }
323
+ CURRENT.remove();
324
+ CURRENT = node;
325
+ return;
326
+ }
327
+ if (node instanceof Text) {
328
+ CURRENT.remove();
329
+ CURRENT = node;
330
+ return;
331
+ }
332
+ const currentAttributes = Array.from(CURRENT.attributes);
333
+ const nodeAttributes = Array.from(node.attributes);
334
+ const nodeChildren = Array.from(node.childNodes).filter(isTextOrElement);
335
+ const outputAttributes = action === Types.ReductionAction.APPEND ? [...currentAttributes, ...nodeAttributes] : [...nodeAttributes, ...currentAttributes];
336
+ if (action === Types.ReductionAction.APPEND) CURRENT.append(...nodeChildren);
337
+ else CURRENT.prepend(...nodeChildren);
338
+ outputAttributes.forEach((attr) => CURRENT.setAttribute(attr.name, attr.value));
339
+ node.remove();
340
+ return;
341
+ });
342
+ const wrapperChildren = Array.from(CURRENT.childNodes);
343
+ const subpaths = /* @__PURE__ */ new Map();
344
+ let positionnedChildrenCount = 0;
345
+ wrapperChildren.forEach((child) => {
346
+ if (child instanceof Text) {
347
+ const childKey = positionnedChildrenCount;
348
+ const found = subpaths.get(childKey) ?? [];
349
+ found.push(child);
350
+ subpaths.set(childKey, found);
351
+ positionnedChildrenCount += 1;
352
+ } else {
353
+ const rawChildKey = child.getAttribute(keyAttribute);
354
+ const childKey = rawChildKey ?? positionnedChildrenCount;
355
+ const found = subpaths.get(childKey) ?? [];
356
+ found.push(child);
357
+ subpaths.set(childKey, found);
358
+ if (rawChildKey === null) {
359
+ positionnedChildrenCount += 1;
360
+ }
361
+ }
362
+ });
363
+ subpaths.forEach((nodes2, subpath) => {
364
+ if (nodes2.length < 2) return;
365
+ return mergeNodes(nodes2, {
366
+ actionAttribute,
367
+ keyAttribute
368
+ });
369
+ });
370
+ return CURRENT;
371
+ }
372
+ _Tree.mergeNodes = mergeNodes;
373
+ function fillOptions(options) {
374
+ const defaultOptions = {
375
+ generatorsMap: Transformers.defaultGeneratorsMap,
376
+ keyAttribute: _Tree.defaultKeyAttribute,
377
+ actionAttribute: _Tree.defaultActionAttribute
378
+ };
379
+ return {
380
+ ...defaultOptions,
381
+ ...options
382
+ };
383
+ }
384
+ class Tree2 {
385
+ node;
386
+ name;
387
+ parent;
388
+ root;
389
+ isRoot;
390
+ path;
391
+ pathString;
392
+ tagName;
393
+ attributes;
394
+ subtrees = /* @__PURE__ */ new Map();
395
+ children;
396
+ type;
397
+ generators;
398
+ keyAttribute;
399
+ actionAttribute;
400
+ constructor(node, parentOrOptions, pathFromParent, options) {
401
+ this.resolve = this.resolve.bind(this);
402
+ this.getGenerator = this.getGenerator.bind(this);
403
+ this.initValue = this.initValue.bind(this);
404
+ this.getInnerValue = this.getInnerValue.bind(this);
405
+ this.wrapInnerValue = this.wrapInnerValue.bind(this);
406
+ this.setCache = this.setCache.bind(this);
407
+ this.getPerfCounters = this.getPerfCounters.bind(this);
408
+ this.printPerfCounters = this.printPerfCounters.bind(this);
409
+ this.pushToEvalCallStack = this.pushToEvalCallStack.bind(this);
410
+ this.flushEvalCallStack = this.flushEvalCallStack.bind(this);
411
+ this.evaluate = this.evaluate.bind(this);
412
+ const { Element, Text } = getWindow();
413
+ const _node = node;
414
+ const _parent = parentOrOptions instanceof Tree2 ? parentOrOptions : void 0;
415
+ this.node = _node;
416
+ this.parent = _parent ?? null;
417
+ this.root = this.parent === null ? this : this.parent.root;
418
+ this.isRoot = this.root === this;
419
+ const _options = options ?? parentOrOptions instanceof Tree2 ? fillOptions({}) : fillOptions(parentOrOptions ?? {});
420
+ this.keyAttribute = _options.keyAttribute;
421
+ this.actionAttribute = _options.actionAttribute;
422
+ this.generators = this.isRoot ? _options.generatorsMap : this.root.generators;
423
+ this.name = this.node instanceof Element ? this.node.getAttribute(this.keyAttribute) : null;
424
+ const _pathFromParent = pathFromParent !== void 0 ? pathFromParent : void 0;
425
+ if (this.parent === null) this.path = [];
426
+ else if (_pathFromParent === void 0) {
427
+ this.path = [...this.parent.path, 0];
428
+ } else {
429
+ this.path = [...this.parent.path, _pathFromParent];
430
+ }
431
+ this.pathString = `/${this.path.join("/")}`;
432
+ this.tagName = node instanceof Element ? node.tagName.toLowerCase() : null;
433
+ this.attributes = isElement(node) ? Array.from(node.attributes) : null;
434
+ const { childNodes } = node;
435
+ let positionnedChildrenCount = 0;
436
+ const mutableSubtrees = /* @__PURE__ */ new Map();
437
+ Array.from(childNodes).filter((node2, _, nodes) => {
438
+ if (isElement(node2)) return true;
439
+ if (isText(node2)) {
440
+ const hasContent = (node2.textContent ?? "").trim() !== "";
441
+ if (hasContent) return true;
442
+ if (nodes.some((n) => n instanceof Element)) return false;
443
+ return true;
444
+ }
445
+ return false;
446
+ }).forEach((childNode) => {
447
+ if (childNode instanceof Text) {
448
+ mutableSubtrees.set(
449
+ positionnedChildrenCount,
450
+ new Tree2(childNode, this, positionnedChildrenCount)
451
+ );
452
+ positionnedChildrenCount += 1;
453
+ } else {
454
+ const propertyName = childNode.getAttribute(this.keyAttribute);
455
+ if (propertyName === null) {
456
+ mutableSubtrees.set(
457
+ positionnedChildrenCount,
458
+ new Tree2(childNode, this, positionnedChildrenCount)
459
+ );
460
+ positionnedChildrenCount += 1;
461
+ } else {
462
+ mutableSubtrees.set(
463
+ propertyName,
464
+ new Tree2(childNode, this, propertyName)
465
+ );
466
+ }
467
+ }
468
+ });
469
+ this.subtrees = mutableSubtrees;
470
+ this.children = Array.from(this.subtrees.values());
471
+ if (this.tagName === null) {
472
+ this.type = "text";
473
+ } else if (this.tagName === Types.TyperTagName.NULL) {
474
+ this.type = "null";
475
+ } else if (this.tagName === Types.TyperTagName.BOOLEAN) {
476
+ this.type = "boolean";
477
+ } else if (this.tagName === Types.TyperTagName.NUMBER) {
478
+ this.type = "number";
479
+ } else if (this.tagName === Types.TyperTagName.STRING) {
480
+ this.type = "string";
481
+ } else if (this.tagName === Types.TyperTagName.TEXT) {
482
+ this.type = "text";
483
+ } else if (this.tagName === Types.TyperTagName.NODELIST) {
484
+ this.type = "nodelist";
485
+ } else if (this.tagName === Types.TyperTagName.ARRAY) {
486
+ this.type = "array";
487
+ } else if (this.tagName === Types.TyperTagName.RECORD) {
488
+ this.type = "record";
489
+ } else if (this.generators.get(this.tagName) !== void 0) {
490
+ this.type = "transformer";
491
+ } else {
492
+ this.type = "element";
493
+ }
494
+ }
495
+ resolve = function(path) {
496
+ let currentTree = this.root;
497
+ for (const chunk of path) {
498
+ const { subtrees } = currentTree;
499
+ const foundSubtree = subtrees.get(chunk);
500
+ if (foundSubtree === void 0) return void 0;
501
+ currentTree = foundSubtree;
502
+ }
503
+ return currentTree;
504
+ };
505
+ getGenerator(name) {
506
+ return this.generators.get(name);
507
+ }
508
+ initValue() {
509
+ const { type } = this;
510
+ let currentValue;
511
+ if (type === "null") {
512
+ currentValue = null;
513
+ } else if (type === "boolean") {
514
+ currentValue = false;
515
+ } else if (type === "number") {
516
+ currentValue = 0;
517
+ } else if (type === "string") {
518
+ currentValue = "";
519
+ } else if (type === "text") {
520
+ currentValue = this.node.textContent;
521
+ } else if (type === "element") {
522
+ currentValue = getWindow().document.createDocumentFragment().childNodes;
523
+ } else if (type === "nodelist") {
524
+ currentValue = getWindow().document.createDocumentFragment().childNodes;
525
+ } else if (type === "array") {
526
+ currentValue = [];
527
+ } else if (type === "record") {
528
+ currentValue = {};
529
+ } else if (type === "transformer") {
530
+ currentValue = [];
531
+ } else {
532
+ currentValue = null;
533
+ }
534
+ return currentValue;
535
+ }
536
+ getInnerValue(initialValue) {
537
+ const { subtrees } = this;
538
+ const innerValue = Array.from(subtrees.entries()).reduce((currentValue, [subpath, subtree]) => mergeValues(
539
+ currentValue,
540
+ subtree.evaluate(),
541
+ subpath,
542
+ this
543
+ ), initialValue);
544
+ return innerValue;
545
+ }
546
+ wrapInnerValue(innerValue) {
547
+ const { type } = this;
548
+ if (type === "transformer") {
549
+ const transformerName = this.tagName;
550
+ if (transformerName === null) return innerValue;
551
+ const generator = this.getGenerator(transformerName);
552
+ if (generator === void 0) return innerValue;
553
+ const transformer = Array.isArray(innerValue) ? generator(transformerName, ...innerValue) : generator(transformerName, innerValue);
554
+ return transformer;
555
+ }
556
+ if (type === "null") return Cast.toNull();
557
+ if (type === "boolean") return Cast.toBoolean(innerValue);
558
+ if (type === "number") return Cast.toNumber(innerValue);
559
+ if (type === "string") return Cast.toString(innerValue);
560
+ if (type === "array") return Cast.toArray(innerValue);
561
+ if (type === "record") return Cast.toRecord(innerValue);
562
+ if (type === "text") return Cast.toText(innerValue);
563
+ if (type === "element") return Cast.toElement(innerValue);
564
+ if (type === "nodelist") return Cast.toNodeList(innerValue);
565
+ return Cast.toNull();
566
+ }
567
+ cache = void 0;
568
+ setCache(value) {
569
+ this.cache = Serialize.serialize(value);
570
+ }
571
+ perfCounters = {
572
+ computed: 0,
573
+ computeTime: 0,
574
+ computeTimeAvg: 0,
575
+ cached: 0,
576
+ cacheTime: 0,
577
+ cacheTimeAvg: 0,
578
+ totalTime: 0
579
+ };
580
+ getPerfCounters() {
581
+ const { subtrees } = this;
582
+ const subCounters = [];
583
+ subCounters.push([this.pathString, this.perfCounters]);
584
+ subtrees.forEach((subtree) => subCounters.push(...subtree.getPerfCounters()));
585
+ return subCounters;
586
+ }
587
+ printPerfCounters() {
588
+ const perfCounters = this.getPerfCounters().sort((a, b) => b[1].totalTime - a[1].totalTime).map((e) => ({
589
+ path: e[0],
590
+ totalMs: e[1].totalTime,
591
+ computeMs: e[1].computeTime,
592
+ cacheMs: e[1].cacheTime,
593
+ ops: `${e[1].computed}/${e[1].cached}`
594
+ }));
595
+ console.table(perfCounters);
596
+ }
597
+ callstack = [];
598
+ pushToEvalCallStack(path) {
599
+ this.callstack.push(path);
600
+ this.parent?.pushToEvalCallStack(path);
601
+ }
602
+ flushEvalCallStack() {
603
+ this.callstack.length = 0;
604
+ }
605
+ evaluate() {
606
+ const start = Date.now();
607
+ const circularPatternDetected = this.callstack.some((p) => p.startsWith(this.pathString));
608
+ if (circularPatternDetected) throw new Error(`Circular reference pattern detected @ ${this.pathString}`);
609
+ this.pushToEvalCallStack(this.pathString);
610
+ const { perfCounters, cache } = this;
611
+ if (cache !== void 0) {
612
+ const deserialized = Serialize.deserialize(cache);
613
+ const end2 = Date.now();
614
+ const time2 = end2 - start;
615
+ perfCounters.cached++;
616
+ perfCounters.cacheTime += time2;
617
+ perfCounters.cacheTimeAvg = perfCounters.cacheTime / perfCounters.cached;
618
+ perfCounters.totalTime = perfCounters.computeTime + perfCounters.cacheTime;
619
+ return deserialized;
620
+ }
621
+ const init = this.initValue();
622
+ const inner = this.getInnerValue(init);
623
+ const wrapped = this.wrapInnerValue(inner);
624
+ this.setCache(wrapped);
625
+ const end = Date.now();
626
+ const time = end - start;
627
+ perfCounters.computed++;
628
+ perfCounters.computeTime += time;
629
+ perfCounters.computeTimeAvg = perfCounters.computeTime / perfCounters.computed;
630
+ perfCounters.totalTime = perfCounters.computeTime + perfCounters.cacheTime;
631
+ this.flushEvalCallStack();
632
+ return wrapped;
633
+ }
634
+ }
635
+ _Tree.Tree = Tree2;
636
+ })(Tree || (Tree = {}));
637
+
638
+ export {
639
+ Tree,
640
+ transformSelected,
641
+ Transformers
642
+ };