@domql/utils 3.2.3 → 3.2.10

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 (53) hide show
  1. package/array.js +11 -5
  2. package/cache.js +3 -0
  3. package/component.js +3 -4
  4. package/dist/cjs/array.js +11 -5
  5. package/dist/cjs/component.js +4 -6
  6. package/dist/cjs/element.js +5 -5
  7. package/dist/cjs/extends.js +43 -27
  8. package/dist/cjs/function.js +3 -3
  9. package/dist/cjs/index.js +1 -0
  10. package/dist/cjs/key.js +2 -2
  11. package/dist/cjs/keys.js +30 -16
  12. package/dist/cjs/methods.js +64 -28
  13. package/dist/cjs/object.js +141 -125
  14. package/dist/cjs/props.js +41 -32
  15. package/dist/cjs/scope.js +1 -2
  16. package/dist/cjs/state.js +9 -8
  17. package/dist/cjs/string.js +15 -20
  18. package/dist/cjs/tags.js +69 -4
  19. package/dist/cjs/triggerEvent.js +90 -0
  20. package/dist/cjs/types.js +4 -12
  21. package/dist/esm/array.js +11 -5
  22. package/dist/esm/component.js +4 -6
  23. package/dist/esm/element.js +8 -26
  24. package/dist/esm/extends.js +47 -49
  25. package/dist/esm/function.js +3 -3
  26. package/dist/esm/index.js +1 -0
  27. package/dist/esm/key.js +2 -2
  28. package/dist/esm/keys.js +30 -16
  29. package/dist/esm/methods.js +63 -42
  30. package/dist/esm/object.js +145 -149
  31. package/dist/esm/props.js +41 -48
  32. package/dist/esm/scope.js +1 -2
  33. package/dist/esm/state.js +17 -34
  34. package/dist/esm/string.js +15 -20
  35. package/dist/esm/tags.js +69 -4
  36. package/dist/esm/triggerEvent.js +70 -0
  37. package/dist/esm/types.js +4 -12
  38. package/dist/iife/index.js +2779 -0
  39. package/element.js +2 -2
  40. package/extends.js +28 -17
  41. package/function.js +4 -6
  42. package/index.js +1 -0
  43. package/keys.js +26 -16
  44. package/methods.js +63 -18
  45. package/object.js +142 -200
  46. package/package.json +33 -12
  47. package/props.js +42 -25
  48. package/state.js +7 -7
  49. package/string.js +20 -38
  50. package/tags.js +43 -4
  51. package/triggerEvent.js +76 -0
  52. package/types.js +4 -23
  53. package/dist/cjs/package.json +0 -4
package/dist/cjs/state.js CHANGED
@@ -37,7 +37,7 @@ var import_object = require("./object.js");
37
37
  var import_types = require("./types.js");
38
38
  const checkForStateTypes = (element) => {
39
39
  const { state: orig, props, __ref: ref } = element;
40
- const state = (props == null ? void 0 : props.state) || orig;
40
+ const state = props?.state || orig;
41
41
  if ((0, import_types.isFunction)(state)) {
42
42
  ref.__state = state;
43
43
  return (0, import_object.exec)(state, element);
@@ -107,7 +107,7 @@ const findInheritedState = (element, parent, options = {}) => {
107
107
  const createInheritedState = (element, parent) => {
108
108
  const ref = element.__ref;
109
109
  const inheritedState = findInheritedState(element, parent);
110
- if ((0, import_types.isUndefined)(inheritedState)) return element.state;
110
+ if (inheritedState === void 0) return element.state;
111
111
  if ((0, import_types.is)(inheritedState)("object", "array")) {
112
112
  return (0, import_object.deepClone)(inheritedState);
113
113
  } else if ((0, import_types.is)(inheritedState)("string", "number", "boolean")) {
@@ -118,7 +118,7 @@ const createInheritedState = (element, parent) => {
118
118
  };
119
119
  const checkIfInherits = (element) => {
120
120
  const { __ref: ref } = element;
121
- const stateKey = ref == null ? void 0 : ref.__state;
121
+ const stateKey = ref?.__state;
122
122
  if (stateKey && (0, import_types.is)(stateKey)("number", "string", "boolean")) return true;
123
123
  return false;
124
124
  };
@@ -135,15 +135,16 @@ const createNestedObjectByKeyPath = (path, value) => {
135
135
  const keys = path.split("/");
136
136
  const obj = {};
137
137
  let ref = obj;
138
- keys.forEach((key, index) => {
139
- ref[key] = index === keys.length - 1 ? value || {} : {};
140
- ref = ref[key];
141
- });
138
+ const lastIdx = keys.length - 1;
139
+ for (let i = 0; i <= lastIdx; i++) {
140
+ ref[keys[i]] = i === lastIdx ? value || {} : {};
141
+ ref = ref[keys[i]];
142
+ }
142
143
  return obj;
143
144
  };
144
145
  const applyDependentState = (element, state) => {
145
146
  const { __element } = state;
146
- const origState = (0, import_object.exec)(__element == null ? void 0 : __element.state, element);
147
+ const origState = (0, import_object.exec)(__element?.state, element);
147
148
  if (!origState) return;
148
149
  const dependentState = (0, import_object.deepClone)(origState, import_keys.STATE_METHODS);
149
150
  const newDepends = { [element.key]: dependentState };
@@ -48,15 +48,15 @@ const brackRegex = {
48
48
  };
49
49
  const getNestedValue = (obj, path) => {
50
50
  return path.split(".").reduce((acc, part) => {
51
- return acc && acc[part] !== void 0 ? acc[part] : void 0;
51
+ return acc?.[part];
52
52
  }, obj);
53
53
  };
54
- function replaceLiteralsWithObjectFields(str, state = {}, options = {}) {
54
+ function replaceLiteralsWithObjectFields(str, state, options = {}) {
55
55
  const { bracketsLength = 2 } = options;
56
56
  const bracketPattern = bracketsLength === 3 ? "{{{" : "{{";
57
57
  if (!str.includes(bracketPattern)) return str;
58
58
  const reg = brackRegex[bracketsLength];
59
- const obj = state || {};
59
+ const obj = state || this.state || {};
60
60
  return str.replace(reg, (_, parentPath, variable) => {
61
61
  if (parentPath) {
62
62
  const parentLevels = (parentPath.match(/\.\.\//g) || []).length;
@@ -67,13 +67,13 @@ function replaceLiteralsWithObjectFields(str, state = {}, options = {}) {
67
67
  }
68
68
  const key = variable.trim();
69
69
  if (key === "parent") {
70
- return parentState.value !== void 0 ? String(parentState.value) : "";
70
+ return String(parentState.value ?? "");
71
71
  }
72
72
  const value = getNestedValue(parentState, key);
73
- return value !== void 0 ? String(value) : "";
73
+ return String(value ?? "");
74
74
  } else {
75
75
  const value = getNestedValue(obj, variable.trim());
76
- return value !== void 0 ? String(value) : "";
76
+ return String(value ?? "");
77
77
  }
78
78
  });
79
79
  }
@@ -126,27 +126,22 @@ const findKeyPosition = (str, key) => {
126
126
  endLineNumber
127
127
  };
128
128
  };
129
+ const RE_OCTAL = /\\([0-7]{1,3})/g;
129
130
  const replaceOctalEscapeSequences = (str) => {
130
- const octalRegex = /\\([0-7]{1,3})/g;
131
- return str.replace(octalRegex, (match, p1) => {
132
- const octalValue = parseInt(p1, 8);
133
- const char = String.fromCharCode(octalValue);
134
- return char;
135
- });
131
+ return str.replace(RE_OCTAL, (_, p1) => String.fromCharCode(parseInt(p1, 8)));
136
132
  };
137
133
  const encodeNewlines = (str) => {
138
- return str.split("\n").join("/////n").split("`").join("/////tilde").split("$").join("/////dlrsgn");
134
+ return str.replace(/\n/g, "/////n").replace(/`/g, "/////tilde").replace(/\$/g, "/////dlrsgn");
139
135
  };
140
136
  const decodeNewlines = (encodedStr) => {
141
- return encodedStr.split("/////n").join("\n").split("/////tilde").join("`").split("/////dlrsgn").join("$");
137
+ return encodedStr.replace(/\/\/\/\/\/n/g, "\n").replace(/\/\/\/\/\/tilde/g, "`").replace(/\/\/\/\/\/dlrsgn/g, "$");
142
138
  };
139
+ const RE_NON_ALNUM = /[^a-zA-Z0-9\s]/g;
143
140
  const customEncodeURIComponent = (str) => {
144
- return str.split("").map((char) => {
145
- if (/[^a-zA-Z0-9\s]/.test(char)) {
146
- return "%" + char.charCodeAt(0).toString(16).toUpperCase();
147
- }
148
- return char;
149
- }).join("");
141
+ return str.replace(
142
+ RE_NON_ALNUM,
143
+ (char) => "%" + char.charCodeAt(0).toString(16).toUpperCase()
144
+ );
150
145
  };
151
146
  const customDecodeURIComponent = (encodedStr) => {
152
147
  return encodedStr.replace(
package/dist/cjs/tags.js CHANGED
@@ -19,13 +19,14 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
19
19
  var tags_exports = {};
20
20
  __export(tags_exports, {
21
21
  HTML_TAGS: () => HTML_TAGS,
22
+ SVG_TAGS: () => SVG_TAGS,
22
23
  isValidHtmlTag: () => isValidHtmlTag
23
24
  });
24
25
  module.exports = __toCommonJS(tags_exports);
25
26
  const HTML_TAGS = {
26
27
  root: ["body", "html"],
27
28
  head: ["title", "base", "meta", "style", "noscript", "script"],
28
- body: [
29
+ body: /* @__PURE__ */ new Set([
29
30
  "string",
30
31
  "style",
31
32
  "fragment",
@@ -149,7 +150,71 @@ const HTML_TAGS = {
149
150
  "wbr",
150
151
  // SVG
151
152
  "svg",
152
- "path"
153
- ]
153
+ "path",
154
+ "circle",
155
+ "ellipse",
156
+ "line",
157
+ "polygon",
158
+ "polyline",
159
+ "rect",
160
+ "g",
161
+ "defs",
162
+ "symbol",
163
+ "use",
164
+ "text",
165
+ "tspan",
166
+ "image",
167
+ "clipPath",
168
+ "mask",
169
+ "pattern",
170
+ "marker",
171
+ "linearGradient",
172
+ "radialGradient",
173
+ "stop",
174
+ "filter",
175
+ "feGaussianBlur",
176
+ "feOffset",
177
+ "feMerge",
178
+ "feMergeNode",
179
+ "feBlend",
180
+ "feColorMatrix",
181
+ "feFlood",
182
+ "feComposite",
183
+ "foreignObject"
184
+ ])
154
185
  };
155
- const isValidHtmlTag = (arg) => HTML_TAGS.body.includes(arg);
186
+ const SVG_TAGS = /* @__PURE__ */ new Set([
187
+ "svg",
188
+ "path",
189
+ "circle",
190
+ "ellipse",
191
+ "line",
192
+ "polygon",
193
+ "polyline",
194
+ "rect",
195
+ "g",
196
+ "defs",
197
+ "symbol",
198
+ "use",
199
+ "text",
200
+ "tspan",
201
+ "image",
202
+ "clipPath",
203
+ "mask",
204
+ "pattern",
205
+ "marker",
206
+ "linearGradient",
207
+ "radialGradient",
208
+ "stop",
209
+ "filter",
210
+ "feGaussianBlur",
211
+ "feOffset",
212
+ "feMerge",
213
+ "feMergeNode",
214
+ "feBlend",
215
+ "feColorMatrix",
216
+ "feFlood",
217
+ "feComposite",
218
+ "foreignObject"
219
+ ]);
220
+ const isValidHtmlTag = (arg) => HTML_TAGS.body.has(arg);
@@ -0,0 +1,90 @@
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 triggerEvent_exports = {};
20
+ __export(triggerEvent_exports, {
21
+ applyEvent: () => applyEvent,
22
+ applyEventUpdate: () => applyEventUpdate,
23
+ triggerEventOn: () => triggerEventOn,
24
+ triggerEventOnUpdate: () => triggerEventOnUpdate
25
+ });
26
+ module.exports = __toCommonJS(triggerEvent_exports);
27
+ var import_types = require("./types.js");
28
+ const getOnOrPropsEvent = (param, element) => {
29
+ const onEvent = element.on?.[param];
30
+ if (onEvent) return onEvent;
31
+ const props = element.props;
32
+ if (!props) return;
33
+ const propKey = "on" + param.charAt(0).toUpperCase() + param.slice(1);
34
+ return props[propKey];
35
+ };
36
+ const applyEvent = (param, element, state, context, options) => {
37
+ if (!(0, import_types.isFunction)(param)) return;
38
+ const result = param.call(
39
+ element,
40
+ element,
41
+ state || element.state,
42
+ context || element.context,
43
+ options
44
+ );
45
+ if (result && typeof result.then === "function") {
46
+ result.catch(() => {
47
+ });
48
+ }
49
+ return result;
50
+ };
51
+ const triggerEventOn = (param, element, options) => {
52
+ if (!element) {
53
+ throw new Error("Element is required");
54
+ }
55
+ const appliedFunction = getOnOrPropsEvent(param, element);
56
+ if (appliedFunction) {
57
+ const { state, context } = element;
58
+ return applyEvent(appliedFunction, element, state, context, options);
59
+ }
60
+ };
61
+ const applyEventUpdate = (param, updatedObj, element, state, context, options) => {
62
+ if (!(0, import_types.isFunction)(param)) return;
63
+ const result = param.call(
64
+ element,
65
+ updatedObj,
66
+ element,
67
+ state || element.state,
68
+ context || element.context,
69
+ options
70
+ );
71
+ if (result && typeof result.then === "function") {
72
+ result.catch(() => {
73
+ });
74
+ }
75
+ return result;
76
+ };
77
+ const triggerEventOnUpdate = (param, updatedObj, element, options) => {
78
+ const appliedFunction = getOnOrPropsEvent(param, element);
79
+ if (appliedFunction) {
80
+ const { state, context } = element;
81
+ return applyEventUpdate(
82
+ appliedFunction,
83
+ updatedObj,
84
+ element,
85
+ state,
86
+ context,
87
+ options
88
+ );
89
+ }
90
+ };
package/dist/cjs/types.js CHANGED
@@ -50,12 +50,8 @@ const isObjectLike = (arg) => {
50
50
  if (arg === null) return false;
51
51
  return typeof arg === "object";
52
52
  };
53
- const isDefined = (arg) => {
54
- return isObject(arg) || isObjectLike(arg) || isString(arg) || isNumber(arg) || isFunction(arg) || isArray(arg) || isObjectLike(arg) || isBoolean(arg) || isDate(arg) || isNull(arg);
55
- };
56
- const isUndefined = (arg) => {
57
- return arg === void 0;
58
- };
53
+ const isDefined = (arg) => arg !== void 0;
54
+ const isUndefined = (arg) => arg === void 0;
59
55
  const TYPES = {
60
56
  boolean: isBoolean,
61
57
  array: isArray,
@@ -71,12 +67,8 @@ const TYPES = {
71
67
  defined: isDefined
72
68
  };
73
69
  const is = (arg) => {
74
- return (...args) => {
75
- return args.map((val) => TYPES[val](arg)).filter((v) => v).length > 0;
76
- };
70
+ return (...args) => args.some((val) => TYPES[val](arg));
77
71
  };
78
72
  const isNot = (arg) => {
79
- return (...args) => {
80
- return args.map((val) => TYPES[val](arg)).filter((v) => v).length === 0;
81
- };
73
+ return (...args) => !args.some((val) => TYPES[val](arg));
82
74
  };
package/dist/esm/array.js CHANGED
@@ -4,9 +4,11 @@ const arrayContainsOtherArray = (arr1, arr2) => {
4
4
  return arr2.every((val) => arr1.includes(val));
5
5
  };
6
6
  const getFrequencyInArray = (arr, value) => {
7
- return arr.reduce((count, currentValue) => {
8
- return currentValue === value ? count + 1 : count;
9
- }, 0);
7
+ let count = 0;
8
+ for (let i = 0; i < arr.length; i++) {
9
+ if (arr[i] === value) count++;
10
+ }
11
+ return count;
10
12
  };
11
13
  const removeFromArray = (arr, index) => {
12
14
  if (isString(index)) index = parseInt(index);
@@ -100,8 +102,12 @@ const filterArraysFast = (sourceArr, excludeArr) => {
100
102
  return sourceArr.filter((item) => !excludeSet.has(item));
101
103
  };
102
104
  const checkIfStringIsInArray = (string, arr) => {
103
- if (!string) return;
104
- return arr.filter((v) => string.includes(v)).length;
105
+ if (!string) return 0;
106
+ let count = 0;
107
+ for (let i = 0; i < arr.length; i++) {
108
+ if (string.includes(arr[i])) count++;
109
+ }
110
+ return count;
105
111
  };
106
112
  const removeDuplicatesInArray = (arr) => {
107
113
  if (!isArray(arr)) return arr;
@@ -1,10 +1,9 @@
1
1
  import { createExtendsFromKeys } from "./extends.js";
2
2
  import { isString } from "./types.js";
3
3
  const matchesComponentNaming = (key) => {
4
- const isFirstKeyString = isString(key);
5
- if (!isFirstKeyString) return;
6
- const firstCharKey = key.slice(0, 1);
7
- return /^[A-Z]*$/.test(firstCharKey);
4
+ if (!isString(key) || !key.length) return false;
5
+ const code = key.charCodeAt(0);
6
+ return code >= 65 && code <= 90;
8
7
  };
9
8
  function getCapitalCaseKeys(obj) {
10
9
  return Object.keys(obj).filter((key) => /^[A-Z]/.test(key));
@@ -13,11 +12,10 @@ function getSpreadChildren(obj) {
13
12
  return Object.keys(obj).filter((key) => /^\d+$/.test(key));
14
13
  }
15
14
  function isContextComponent(element, parent, passedKey) {
16
- var _a, _b;
17
15
  const { context } = parent || {};
18
16
  const [extendsKey] = createExtendsFromKeys(passedKey);
19
17
  const key = passedKey || extendsKey;
20
- 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]);
18
+ return context?.components?.[key] || context?.pages?.[key];
21
19
  }
22
20
  export {
23
21
  getCapitalCaseKeys,
@@ -1,22 +1,3 @@
1
- var __defProp = Object.defineProperty;
2
- var __defProps = Object.defineProperties;
3
- var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
- var __spreadValues = (a, b) => {
9
- for (var prop in b || (b = {}))
10
- if (__hasOwnProp.call(b, prop))
11
- __defNormalProp(a, prop, b[prop]);
12
- if (__getOwnPropSymbols)
13
- for (var prop of __getOwnPropSymbols(b)) {
14
- if (__propIsEnum.call(b, prop))
15
- __defNormalProp(a, prop, b[prop]);
16
- }
17
- return a;
18
- };
19
- var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
1
  import { createExtends } from "./extends.js";
21
2
  import { createKey } from "./key.js";
22
3
  import { isNode } from "./node.js";
@@ -27,7 +8,7 @@ const ENV = process.env.NODE_ENV;
27
8
  const returnValueAsText = (element, parent, key) => {
28
9
  const childExtendsTag = parent.childExtends && parent.childExtends.tag;
29
10
  const childPropsTag = parent.props.childProps && parent.props.childProps.tag;
30
- const isKeyValidHTMLTag = HTML_TAGS.body.indexOf(key) > -1 && key;
11
+ const isKeyValidHTMLTag = HTML_TAGS.body.has(key) && key;
31
12
  return {
32
13
  text: element,
33
14
  tag: childExtendsTag || childPropsTag || isKeyValidHTMLTag || "string"
@@ -39,7 +20,7 @@ const createBasedOnType = (element, parent, key) => {
39
20
  console.warn(
40
21
  key,
41
22
  "element is undefined in",
42
- parent && parent.__ref && parent.__ref.path
23
+ parent?.__ref?.path
43
24
  );
44
25
  }
45
26
  return {};
@@ -73,8 +54,8 @@ const createRoot = (element, parent) => {
73
54
  const { __ref: ref } = element;
74
55
  const { __ref: parentRef } = parent;
75
56
  const hasRoot = parent && parent.key === ":root";
76
- if (!(ref == null ? void 0 : ref.root)) {
77
- ref.root = hasRoot ? element : parentRef == null ? void 0 : parentRef.root;
57
+ if (!ref?.root) {
58
+ ref.root = hasRoot ? element : parentRef?.root;
78
59
  }
79
60
  };
80
61
  const createPath = (element, parent, key) => {
@@ -104,7 +85,7 @@ const addCaching = (element, parent, key) => {
104
85
  return ref;
105
86
  };
106
87
  const createElement = (passedProps, parentEl, passedKey, opts, root) => {
107
- const hashed = passedProps == null ? void 0 : passedProps.__hash;
88
+ const hashed = passedProps?.__hash;
108
89
  const element = hashed ? { extends: [passedProps] } : createBasedOnType(passedProps, parentEl, passedKey);
109
90
  if (!element) return;
110
91
  const parent = createParent(element, parentEl, passedKey, opts, root);
@@ -113,13 +94,14 @@ const createElement = (passedProps, parentEl, passedKey, opts, root) => {
113
94
  const props = createProps(element, parent, key);
114
95
  const context = addContext(element, parent, key, opts, root);
115
96
  const on = element.on || {};
116
- return __spreadProps(__spreadValues({}, element), {
97
+ return {
98
+ ...element,
117
99
  key,
118
100
  props,
119
101
  parent,
120
102
  context,
121
103
  on
122
- });
104
+ };
123
105
  };
124
106
  export {
125
107
  addCaching,