@domql/utils 2.5.200 → 3.0.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 (58) hide show
  1. package/array.js +26 -13
  2. package/cache.js +4 -0
  3. package/component.js +10 -227
  4. package/cookie.js +27 -24
  5. package/dist/cjs/array.js +27 -10
  6. package/dist/cjs/cache.js +26 -0
  7. package/dist/cjs/component.js +15 -206
  8. package/dist/cjs/cookie.js +14 -14
  9. package/dist/cjs/element.js +137 -0
  10. package/dist/cjs/events.js +37 -0
  11. package/dist/cjs/extends.js +351 -0
  12. package/dist/cjs/if.js +30 -0
  13. package/dist/cjs/index.js +10 -0
  14. package/dist/cjs/key.js +5 -0
  15. package/dist/cjs/keys.js +178 -0
  16. package/dist/cjs/methods.js +305 -0
  17. package/dist/cjs/object.js +78 -191
  18. package/dist/cjs/props.js +220 -0
  19. package/dist/cjs/scope.js +28 -0
  20. package/dist/cjs/state.js +175 -0
  21. package/dist/cjs/string.js +27 -15
  22. package/dist/cjs/update.js +42 -0
  23. package/dist/esm/array.js +27 -10
  24. package/dist/esm/cache.js +6 -0
  25. package/dist/esm/component.js +16 -225
  26. package/dist/esm/cookie.js +14 -14
  27. package/dist/esm/element.js +135 -0
  28. package/dist/esm/events.js +17 -0
  29. package/dist/esm/extends.js +349 -0
  30. package/dist/esm/if.js +10 -0
  31. package/dist/esm/index.js +10 -0
  32. package/dist/esm/key.js +5 -0
  33. package/dist/esm/keys.js +158 -0
  34. package/dist/esm/methods.js +285 -0
  35. package/dist/esm/object.js +79 -193
  36. package/dist/esm/props.js +216 -0
  37. package/dist/esm/scope.js +8 -0
  38. package/dist/esm/state.js +185 -0
  39. package/dist/esm/string.js +27 -15
  40. package/dist/esm/update.js +22 -0
  41. package/element.js +149 -0
  42. package/env.js +5 -2
  43. package/events.js +17 -0
  44. package/extends.js +425 -0
  45. package/if.js +14 -0
  46. package/index.js +10 -0
  47. package/key.js +6 -0
  48. package/keys.js +157 -0
  49. package/log.js +4 -1
  50. package/methods.js +315 -0
  51. package/node.js +21 -13
  52. package/object.js +122 -236
  53. package/package.json +3 -3
  54. package/props.js +249 -0
  55. package/scope.js +8 -0
  56. package/state.js +208 -0
  57. package/string.js +66 -30
  58. package/update.js +27 -0
@@ -18,221 +18,30 @@ var __copyProps = (to, from, except, desc) => {
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
  var component_exports = {};
20
20
  __export(component_exports, {
21
- addAdditionalExtend: () => addAdditionalExtend,
22
- addChildrenIfNotInOriginal: () => addChildrenIfNotInOriginal,
23
- applyComponentFromContext: () => applyComponentFromContext,
24
- applyKeyComponentAsExtend: () => applyKeyComponentAsExtend,
25
- checkIfKeyIsComponent: () => checkIfKeyIsComponent,
26
- checkIfKeyIsProperty: () => checkIfKeyIsProperty,
27
- checkIfSugar: () => checkIfSugar,
28
- extendizeByKey: () => extendizeByKey,
29
- extractComponentKeyFromKey: () => extractComponentKeyFromKey,
30
21
  getCapitalCaseKeys: () => getCapitalCaseKeys,
31
- getChildrenComponentsByKey: () => getChildrenComponentsByKey,
32
- getExtendsInElement: () => getExtendsInElement,
33
- hasVariantProp: () => hasVariantProp,
34
- isVariant: () => isVariant,
35
- setContentKey: () => setContentKey
22
+ getSpreadChildren: () => getSpreadChildren,
23
+ isContextComponent: () => isContextComponent,
24
+ matchesComponentNaming: () => matchesComponentNaming
36
25
  });
37
26
  module.exports = __toCommonJS(component_exports);
38
- var import_array = require("./array.js");
39
- var import_object = require("./object.js");
27
+ var import_extends = require("./extends.js");
40
28
  var import_types = require("./types.js");
41
- const ENV = "development";
42
- const checkIfKeyIsComponent = (key) => {
29
+ const matchesComponentNaming = (key) => {
43
30
  const isFirstKeyString = (0, import_types.isString)(key);
44
31
  if (!isFirstKeyString) return;
45
32
  const firstCharKey = key.slice(0, 1);
46
33
  return /^[A-Z]*$/.test(firstCharKey);
47
34
  };
48
- const checkIfKeyIsProperty = (key) => {
49
- const isFirstKeyString = (0, import_types.isString)(key);
50
- if (!isFirstKeyString) return;
51
- const firstCharKey = key.slice(0, 1);
52
- return /^[a-z]*$/.test(firstCharKey);
53
- };
54
- const addAdditionalExtend = (newExtend, element) => {
55
- if (!newExtend) return element;
56
- const { extend: elementExtend } = element;
57
- const originalArray = (0, import_types.isArray)(elementExtend) ? elementExtend : [elementExtend];
58
- const receivedArray = (0, import_types.isArray)(newExtend) ? newExtend : [newExtend];
59
- const extend = (0, import_array.joinArrays)(receivedArray, originalArray);
60
- return { ...element, extend };
61
- };
62
- const checkIfSugar = (element, parent, key) => {
63
- var _a;
64
- const {
65
- extend,
66
- props,
67
- childExtend,
68
- extends: extendProps,
69
- childExtends,
70
- childProps,
71
- children,
72
- on,
73
- $collection,
74
- $stateCollection,
75
- $propsCollection
76
- } = element;
77
- const hasComponentAttrs = extend || childExtend || props || on || $collection || $stateCollection || $propsCollection;
78
- if (hasComponentAttrs && (childProps || extendProps || children || childExtends)) {
79
- const logErr = (_a = parent || element) == null ? void 0 : _a.error;
80
- if (logErr) logErr.call(element, "Sugar component includes params for builtin components", { verbose: true });
81
- }
82
- return !hasComponentAttrs || childProps || extendProps || children || childExtends;
83
- };
84
- const extractComponentKeyFromKey = (key) => {
85
- return key.includes("+") ? key.split("+") : key.includes("_") ? [key.split("_")[0]] : key.includes(".") && !checkIfKeyIsComponent(key.split(".")[1]) ? [key.split(".")[0]] : [key];
86
- };
87
- const extendizeByKey = (element, parent, key) => {
88
- const { context } = parent;
89
- const { tag, extend, childExtends } = element;
90
- const isSugar = checkIfSugar(element, parent, key);
91
- const extendFromKey = extractComponentKeyFromKey(key);
92
- const isExtendKeyComponent = context && context.components[extendFromKey];
93
- if (element === isExtendKeyComponent) return element;
94
- else if (isSugar) {
95
- const newElem = addAdditionalExtend(element.extends, {
96
- extend: extendFromKey,
97
- tag,
98
- props: { ...element }
99
- });
100
- if (newElem.props.data) {
101
- newElem.data = newElem.props.data;
102
- delete newElem.props.data;
103
- }
104
- if (newElem.props.state) {
105
- newElem.state = newElem.props.state;
106
- delete newElem.props.state;
107
- }
108
- if (newElem.props.attr) {
109
- newElem.attr = newElem.props.attr;
110
- delete newElem.props.attr;
111
- }
112
- if (newElem.props.if) {
113
- newElem.if = newElem.props.if;
114
- delete newElem.props.if;
115
- }
116
- if (childExtends) newElem.childExtend = childExtends;
117
- return newElem;
118
- } else if (!extend || extend === true) {
119
- return {
120
- ...element,
121
- tag,
122
- extend: extendFromKey
123
- };
124
- } else if (extend) {
125
- return addAdditionalExtend(extendFromKey, element);
126
- } else if ((0, import_types.isFunction)(element)) {
127
- return {
128
- extend: extendFromKey,
129
- tag,
130
- props: { ...(0, import_object.exec)(element, parent) }
131
- };
132
- }
133
- };
134
35
  function getCapitalCaseKeys(obj) {
135
36
  return Object.keys(obj).filter((key) => /^[A-Z]/.test(key));
136
37
  }
137
- const addChildrenIfNotInOriginal = (element, parent, key) => {
138
- const childElems = getCapitalCaseKeys(element.props);
139
- if (!childElems.length) return element;
140
- for (const i in childElems) {
141
- const childKey = childElems[i];
142
- const childElem = element[childKey];
143
- const newChild = element.props[childKey];
144
- const assignChild = (val) => {
145
- element[childKey] = val;
146
- delete element.props[childKey];
147
- };
148
- if (newChild == null ? void 0 : newChild.ignoreExtend) continue;
149
- if (newChild === null) assignChild(null);
150
- else if (!childElem) assignChild((0, import_object.deepClone)(newChild));
151
- else {
152
- const isSugarChildElem = checkIfSugar(childElem, parent, key);
153
- if (isSugarChildElem) continue;
154
- assignChild({
155
- extend: element[childKey],
156
- props: newChild
157
- });
158
- }
159
- }
160
- };
161
- const applyKeyComponentAsExtend = (element, parent, key) => {
162
- return extendizeByKey(element, parent, key) || element;
163
- };
164
- const applyComponentFromContext = (element, parent, options) => {
165
- const { context } = element;
166
- if (!context || !context.components) return;
167
- const { components } = context;
168
- const { extend } = element;
169
- const execExtend = (0, import_object.exec)(extend, element);
170
- if ((0, import_types.isString)(execExtend)) {
171
- const componentExists = components[execExtend] || components["smbls." + execExtend];
172
- if (componentExists) element.extend = componentExists;
173
- else {
174
- if ((ENV === "test" || ENV === "development") && options.verbose) {
175
- console.warn(execExtend, "is not in library", components, element);
176
- console.warn("replacing with ", {});
177
- }
178
- element.extend = {};
179
- }
180
- }
181
- };
182
- const isVariant = (param) => {
183
- if (!(0, import_types.isString)(param)) return;
184
- const firstCharKey = param.slice(0, 1);
185
- return firstCharKey === ".";
186
- };
187
- const hasVariantProp = (element) => {
188
- const { props } = element;
189
- if ((0, import_types.isObject)(props) && (0, import_types.isString)(props.variant)) return true;
190
- };
191
- const getChildrenComponentsByKey = (key, el) => {
192
- if (key === el.key || el.__ref.__componentKey === key) {
193
- return el;
194
- }
195
- if (el.extend) {
196
- const foundString = (0, import_types.isString)(el.extend) && el.extend === key;
197
- const foundInArray = (0, import_types.isArray)(el.extend) && el.extend.filter((v) => v === key).length;
198
- if (foundString || foundInArray) return el;
199
- }
200
- if (el.parent && el.parent.childExtend) {
201
- const foundString = (0, import_types.isString)(el.parent.childExtend) && el.parent.childExtend === key;
202
- const foundInArray = (0, import_types.isArray)(el.parent.childExtend) && el.parent.childExtend.filter((v) => v === key).length;
203
- if (foundString || foundInArray) return el;
204
- }
205
- };
206
- const getExtendsInElement = (obj) => {
207
- let result = [];
208
- function traverse(o) {
209
- for (const key in o) {
210
- if (Object.hasOwnProperty.call(o, key)) {
211
- if (checkIfKeyIsComponent(key)) {
212
- result.push(key);
213
- }
214
- if (key === "extend" || key === "extends") {
215
- if (typeof o[key] === "string") {
216
- result.push(o[key]);
217
- } else if (Array.isArray(o[key])) {
218
- result = result.concat(o[key]);
219
- }
220
- }
221
- if (typeof o[key] === "object" && o[key] !== null) {
222
- traverse(o[key]);
223
- }
224
- }
225
- }
226
- }
227
- traverse(obj);
228
- return result;
229
- };
230
- const setContentKey = (el, opts = {}) => {
231
- const { __ref: ref } = el;
232
- const contentElementKey = opts.contentElementKey;
233
- if (contentElementKey !== "content" && contentElementKey !== ref.contentElementKey || !ref.contentElementKey) {
234
- ref.contentElementKey = contentElementKey || "content";
235
- } else ref.contentElementKey = "content";
236
- if (contentElementKey !== "content") opts.contentElementKey = "content";
237
- return ref.contentElementKey;
238
- };
38
+ function getSpreadChildren(obj) {
39
+ return Object.keys(obj).filter((key) => /^\d+$/.test(key));
40
+ }
41
+ function isContextComponent(element, parent, passedKey) {
42
+ var _a, _b;
43
+ const { context } = parent || {};
44
+ const [extendsKey] = (0, import_extends.createExtendsFromKeys)(passedKey);
45
+ const key = passedKey || extendsKey;
46
+ return ((_a = context == null ? void 0 : context.components) == null ? void 0 : _a[key]) || ((_b = context == null ? void 0 : context.pages) == null ? void 0 : _b[key]);
47
+ }
@@ -53,23 +53,23 @@ const removeCookie = (cname) => {
53
53
  import_globals.document.cookie = cname + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
54
54
  };
55
55
  function getLocalStorage(key) {
56
- let savedJSON;
57
- if (window.localStorage) {
58
- try {
59
- savedJSON = JSON.parse(window.localStorage.getItem(key));
60
- } catch (e) {
61
- }
56
+ if (!window.localStorage) {
57
+ return void 0;
62
58
  }
63
- if (typeof savedJSON !== "undefined") {
64
- return savedJSON;
59
+ const item = window.localStorage.getItem(key);
60
+ if (item === null) {
61
+ return void 0;
62
+ }
63
+ try {
64
+ return JSON.parse(item);
65
+ } catch (e) {
66
+ return void 0;
65
67
  }
66
68
  }
67
69
  function setLocalStorage(key, data) {
68
- if (data && window.localStorage) {
69
- if (typeof data === "object") {
70
- window.localStorage.setItem(key, JSON.stringify(data));
71
- } else {
72
- window.localStorage.setItem(key, data);
73
- }
70
+ if (!window.localStorage || data === void 0 || data === null) {
71
+ return;
74
72
  }
73
+ const value = typeof data === "object" ? JSON.stringify(data) : data;
74
+ window.localStorage.setItem(key, value);
75
75
  }
@@ -0,0 +1,137 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var element_exports = {};
20
+ __export(element_exports, {
21
+ addCaching: () => addCaching,
22
+ addContext: () => addContext,
23
+ addRef: () => addRef,
24
+ createBasedOnType: () => createBasedOnType,
25
+ createElement: () => createElement,
26
+ createParent: () => createParent,
27
+ createPath: () => createPath,
28
+ createRoot: () => createRoot,
29
+ returnValueAsText: () => returnValueAsText
30
+ });
31
+ module.exports = __toCommonJS(element_exports);
32
+ var import_extends = require("./extends.js");
33
+ var import_key = require("./key.js");
34
+ var import_node = require("./node.js");
35
+ var import_props = require("./props.js");
36
+ var import_tags = require("./tags.js");
37
+ var import_types = require("./types.js");
38
+ const ENV = "development";
39
+ const returnValueAsText = (element, parent, key) => {
40
+ const childExtendsTag = parent.childExtends && parent.childExtends.tag;
41
+ const childPropsTag = parent.props.childProps && parent.props.childProps.tag;
42
+ const isKeyValidHTMLTag = import_tags.HTML_TAGS.body.indexOf(key) > -1 && key;
43
+ return {
44
+ text: element,
45
+ tag: childExtendsTag || childPropsTag || isKeyValidHTMLTag || "string"
46
+ };
47
+ };
48
+ const createBasedOnType = (element, parent, key) => {
49
+ if (element === void 0) {
50
+ if (ENV === "test" || ENV === "development") {
51
+ console.warn(
52
+ key,
53
+ "element is undefined in",
54
+ parent && parent.__ref && parent.__ref.path
55
+ );
56
+ }
57
+ return {};
58
+ }
59
+ if (element === null) return;
60
+ if (element === true) return {};
61
+ if ((0, import_types.is)(element)("string", "number")) {
62
+ return returnValueAsText(element, parent, key);
63
+ }
64
+ if ((0, import_types.isFunction)(element)) {
65
+ return { props: element };
66
+ }
67
+ return element;
68
+ };
69
+ const addRef = (element, parent) => {
70
+ const ref = {};
71
+ ref.origin = element;
72
+ ref.parent = parent;
73
+ return ref;
74
+ };
75
+ const createParent = (element, parent, key, options, root) => {
76
+ if (!parent) return root;
77
+ if ((0, import_node.isNode)(parent)) {
78
+ const parentNodeWrapper = { key: ":root", node: parent };
79
+ root[`${key}_parent`] = parentNodeWrapper;
80
+ return parentNodeWrapper;
81
+ }
82
+ return parent;
83
+ };
84
+ const createRoot = (element, parent) => {
85
+ const { __ref: ref } = element;
86
+ const { __ref: parentRef } = parent;
87
+ const hasRoot = parent && parent.key === ":root";
88
+ if (!(ref == null ? void 0 : ref.root)) {
89
+ return hasRoot ? element : parentRef == null ? void 0 : parentRef.root;
90
+ }
91
+ };
92
+ const createPath = (element, parent, key) => {
93
+ let { __ref: parentRef } = parent;
94
+ if (!parentRef) parentRef = parent.ref = {};
95
+ if (!parentRef.path) parentRef.path = [];
96
+ return parentRef.path.concat(key);
97
+ };
98
+ const addContext = (element, parent, key, options, root) => {
99
+ const forcedOptionsContext = options.context && !root.context && !element.context;
100
+ if (forcedOptionsContext) root.context = options.context;
101
+ return options.context || parent.context || root.context || element.context;
102
+ };
103
+ const addCaching = (element, parent, key) => {
104
+ const ref = addRef(element, parent);
105
+ element.__ref = ref;
106
+ if (!ref.__defineCache) ref.__defineCache = {};
107
+ if (!ref.__exec) ref.__exec = {};
108
+ if (!ref.__execProps) ref.__execProps = {};
109
+ if (!ref.__class) ref.__class = {};
110
+ if (!ref.__classNames) ref.__classNames = {};
111
+ if (!ref.__attr) ref.__attr = {};
112
+ if (!ref.__changes) ref.__changes = [];
113
+ if (!ref.__children) ref.__children = [];
114
+ ref.__extends = (0, import_extends.createExtends)(element, parent, key);
115
+ ref.path = createPath(element, parent, key);
116
+ ref.root = createRoot(element, parent);
117
+ return ref;
118
+ };
119
+ const createElement = (passedProps, parentEl, passedKey, opts, root) => {
120
+ const hashed = passedProps == null ? void 0 : passedProps.__hash;
121
+ const element = hashed ? { extends: [passedProps] } : createBasedOnType(passedProps, parentEl, passedKey);
122
+ if (!element) return;
123
+ const parent = createParent(element, parentEl, passedKey, opts, root);
124
+ const key = (0, import_key.createKey)(element, parent, passedKey);
125
+ addCaching(element, parent, key);
126
+ const props = (0, import_props.createProps)(element, parent, key);
127
+ const context = addContext(element, parent, key, opts, root);
128
+ const on = element.on || {};
129
+ return {
130
+ ...element,
131
+ key,
132
+ props,
133
+ parent,
134
+ context,
135
+ on
136
+ };
137
+ };
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var events_exports = {};
20
+ __export(events_exports, {
21
+ addEventFromProps: () => addEventFromProps
22
+ });
23
+ module.exports = __toCommonJS(events_exports);
24
+ var import_string = require("./string.js");
25
+ var import_types = require("./types.js");
26
+ const addEventFromProps = (key, element) => {
27
+ const { props, on } = element;
28
+ const eventName = (0, import_string.lowercaseFirstLetter)(key.split("on")[1]);
29
+ const origEvent = on[eventName];
30
+ const funcFromProps = props[key];
31
+ if ((0, import_types.isFunction)(origEvent)) {
32
+ on[eventName] = (...args) => {
33
+ const originalEventRetunrs = origEvent(...args);
34
+ if (originalEventRetunrs !== false) funcFromProps(...args);
35
+ };
36
+ } else on[eventName] = funcFromProps;
37
+ };