@domql/utils 2.5.200 → 3.0.1

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 +136 -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 +134 -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 +148 -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
@@ -0,0 +1,185 @@
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
+ import { addProtoToArray } from "./array.js";
21
+ import { STATE_METHODS } from "./keys.js";
22
+ import {
23
+ deepClone,
24
+ deepMerge,
25
+ execPromise,
26
+ overwriteDeep,
27
+ overwriteShallow
28
+ } from "./object.js";
29
+ import {
30
+ is,
31
+ isFunction,
32
+ isObject,
33
+ isObjectLike,
34
+ isString,
35
+ isUndefined
36
+ } from "./types.js";
37
+ const checkForStateTypes = async (element) => {
38
+ const { state: orig, props, __ref: ref } = element;
39
+ const state = (props == null ? void 0 : props.state) || orig;
40
+ if (isFunction(state)) {
41
+ ref.__state = state;
42
+ return await execPromise(state, element);
43
+ } else if (is(state)("string", "number")) {
44
+ ref.__state = state;
45
+ return { value: state };
46
+ } else if (state === true) {
47
+ ref.__state = element.key;
48
+ return {};
49
+ } else if (state) {
50
+ ref.__hasRootState = true;
51
+ return state;
52
+ } else {
53
+ return false;
54
+ }
55
+ };
56
+ const getRootStateInKey = (stateKey, parentState) => {
57
+ if (!stateKey.includes("~/")) return;
58
+ const arr = stateKey.split("~/");
59
+ if (arr.length > 1) return parentState.root;
60
+ };
61
+ const getParentStateInKey = (stateKey, parentState) => {
62
+ if (!stateKey.includes("../")) return;
63
+ const arr = stateKey.split("../");
64
+ const arrLength = arr.length - 1;
65
+ for (let i = 0; i < arrLength; i++) {
66
+ if (!parentState.parent) return null;
67
+ parentState = parentState.parent;
68
+ }
69
+ return parentState;
70
+ };
71
+ const getChildStateInKey = (stateKey, parentState, options = {}) => {
72
+ const arr = isString(stateKey) ? stateKey.split("/") : [stateKey];
73
+ const arrLength = arr.length - 1;
74
+ for (let i = 0; i < arrLength; i++) {
75
+ const childKey = arr[i];
76
+ const grandChildKey = arr[i + 1];
77
+ if (childKey === "__proto__" || grandChildKey === "__proto__") return;
78
+ let childInParent = parentState[childKey];
79
+ if (!childInParent) childInParent = parentState[childKey] = {};
80
+ if (!childInParent[grandChildKey]) childInParent[grandChildKey] = {};
81
+ stateKey = grandChildKey;
82
+ parentState = childInParent;
83
+ }
84
+ if (options.returnParent) return parentState;
85
+ return parentState[stateKey];
86
+ };
87
+ const findInheritedState = (element, parent, options = {}) => {
88
+ const ref = element.__ref;
89
+ let stateKey = ref.__state;
90
+ if (!checkIfInherits(element)) return;
91
+ const rootState = getRootStateInKey(stateKey, parent.state);
92
+ let parentState = parent.state;
93
+ if (rootState) {
94
+ parentState = rootState;
95
+ stateKey = stateKey.replaceAll("~/", "");
96
+ } else {
97
+ const findGrandParentState = getParentStateInKey(stateKey, parent.state);
98
+ if (findGrandParentState) {
99
+ parentState = findGrandParentState;
100
+ stateKey = stateKey.replaceAll("../", "");
101
+ }
102
+ }
103
+ if (!parentState) return;
104
+ return getChildStateInKey(stateKey, parentState, options);
105
+ };
106
+ const createInheritedState = (element, parent) => {
107
+ const ref = element.__ref;
108
+ const inheritedState = findInheritedState(element, parent);
109
+ if (isUndefined(inheritedState)) return element.state;
110
+ if (is(inheritedState)("object", "array")) {
111
+ return deepClone(inheritedState);
112
+ } else if (is(inheritedState)("string", "number", "boolean")) {
113
+ ref.__stateType = typeof inheritedState;
114
+ return { value: inheritedState };
115
+ }
116
+ console.warn(ref.__state, "is not present. Replacing with", {});
117
+ };
118
+ const checkIfInherits = (element) => {
119
+ const { __ref: ref } = element;
120
+ const stateKey = ref == null ? void 0 : ref.__state;
121
+ if (stateKey && is(stateKey)("number", "string", "boolean")) return true;
122
+ return false;
123
+ };
124
+ const isState = function(state) {
125
+ if (!isObjectLike(state)) return false;
126
+ return Boolean(
127
+ state.update && state.parse && state.clean && state.create && state.parent && state.destroy && state.rootUpdate && state.parentUpdate && state.keys && state.values && state.toggle && state.replace && state.quietUpdate && state.quietReplace && state.add && state.apply && state.applyReplace && state.setByPath && state.setPathCollection && state.removeByPath && state.removePathCollection && state.getByPath && state.applyFunction && state.__element && state.__children
128
+ );
129
+ };
130
+ const createNestedObjectByKeyPath = (path, value) => {
131
+ if (!path) {
132
+ return value || {};
133
+ }
134
+ const keys = path.split("/");
135
+ const obj = {};
136
+ let ref = obj;
137
+ keys.forEach((key, index) => {
138
+ ref[key] = index === keys.length - 1 ? value || {} : {};
139
+ ref = ref[key];
140
+ });
141
+ return obj;
142
+ };
143
+ const applyDependentState = async (element, state) => {
144
+ const { __element } = state;
145
+ const origState = await execPromise(__element == null ? void 0 : __element.state, element);
146
+ if (!origState) return;
147
+ const dependentState = deepClone(origState, STATE_METHODS);
148
+ const newDepends = { [element.key]: dependentState };
149
+ const __depends = isObject(origState.__depends) ? __spreadValues(__spreadValues({}, origState.__depends), newDepends) : newDepends;
150
+ if (Array.isArray(origState)) {
151
+ addProtoToArray(origState, __spreadProps(__spreadValues({}, Object.getPrototypeOf(origState)), {
152
+ __depends
153
+ }));
154
+ } else {
155
+ Object.setPrototypeOf(origState, __spreadProps(__spreadValues({}, Object.getPrototypeOf(origState)), {
156
+ __depends
157
+ }));
158
+ }
159
+ return dependentState;
160
+ };
161
+ const overwriteState = (state, obj, options = {}) => {
162
+ const { overwrite } = options;
163
+ if (!overwrite) return;
164
+ const shallow = overwrite === "shallow";
165
+ const merge = overwrite === "merge";
166
+ if (merge) {
167
+ deepMerge(state, obj, STATE_METHODS);
168
+ return;
169
+ }
170
+ const overwriteFunc = shallow ? overwriteShallow : overwriteDeep;
171
+ overwriteFunc(state, obj, STATE_METHODS);
172
+ };
173
+ export {
174
+ applyDependentState,
175
+ checkForStateTypes,
176
+ checkIfInherits,
177
+ createInheritedState,
178
+ createNestedObjectByKeyPath,
179
+ findInheritedState,
180
+ getChildStateInKey,
181
+ getParentStateInKey,
182
+ getRootStateInKey,
183
+ isState,
184
+ overwriteState
185
+ };
@@ -11,28 +11,37 @@ const trimStringFromSymbols = (str, characters) => {
11
11
  return str.replace(pattern, "");
12
12
  };
13
13
  const brackRegex = {
14
- 2: /\{\{\s*((?:\.\.\/)+)?([^}\s]+)\s*\}\}/g,
15
- 3: /\{\{\{\s*((?:\.\.\/)+)?([^}\s]+)\s*\}\}\}/g
14
+ 2: /{{\s*((?:\.\.\/)*)([\w\d.]+)\s*}}/g,
15
+ 3: /{{{(\s*(?:\.\.\/)*)([\w\d.]+)\s*}}}/g
16
16
  };
17
- function replaceLiteralsWithObjectFields(str, options = {}, forcedState) {
18
- if (!str.includes(options.bracketsLength === 3 ? "{{{" : "{{")) return str;
19
- const reg = brackRegex[options.bracketsLength || 2];
20
- const obj = forcedState || (this == null ? void 0 : this.state) || {};
17
+ const getNestedValue = (obj, path) => {
18
+ return path.split(".").reduce((acc, part) => {
19
+ return acc && acc[part] !== void 0 ? acc[part] : void 0;
20
+ }, obj);
21
+ };
22
+ function replaceLiteralsWithObjectFields(str, state = {}, options = {}) {
23
+ const { bracketsLength = 2 } = options;
24
+ const bracketPattern = bracketsLength === 3 ? "{{{" : "{{";
25
+ if (!str.includes(bracketPattern)) return str;
26
+ const reg = brackRegex[bracketsLength];
27
+ const obj = state || {};
21
28
  return str.replace(reg, (_, parentPath, variable) => {
22
29
  if (parentPath) {
23
- const parentLevels = parentPath.match(options.bracketsLength === 3 ? /\.\.\.\//g : /\.\.\//g).length;
30
+ const parentLevels = (parentPath.match(/\.\.\//g) || []).length;
24
31
  let parentState = obj;
25
32
  for (let i = 0; i < parentLevels; i++) {
33
+ if (!parentState || !parentState.parent) return "";
26
34
  parentState = parentState.parent;
27
- if (!parentState) {
28
- return "";
29
- }
30
35
  }
31
- const value = parentState[variable.trim()];
32
- return value !== void 0 ? `${value}` : "";
36
+ const key = variable.trim();
37
+ if (key === "parent") {
38
+ return parentState.value !== void 0 ? String(parentState.value) : "";
39
+ }
40
+ const value = getNestedValue(parentState, key);
41
+ return value !== void 0 ? String(value) : "";
33
42
  } else {
34
- const value = obj[variable.trim()];
35
- return value !== void 0 ? `${value}` : "";
43
+ const value = getNestedValue(obj, variable.trim());
44
+ return value !== void 0 ? String(value) : "";
36
45
  }
37
46
  });
38
47
  }
@@ -108,7 +117,10 @@ const customEncodeURIComponent = (str) => {
108
117
  }).join("");
109
118
  };
110
119
  const customDecodeURIComponent = (encodedStr) => {
111
- return encodedStr.replace(/%[0-9A-Fa-f]{2}/g, (match) => String.fromCharCode(parseInt(match.slice(1), 16)));
120
+ return encodedStr.replace(
121
+ /%[0-9A-Fa-f]{2}/g,
122
+ (match) => String.fromCharCode(parseInt(match.slice(1), 16))
123
+ );
112
124
  };
113
125
  export {
114
126
  customDecodeURIComponent,
@@ -0,0 +1,22 @@
1
+ import { createSnapshotId } from "./key.js";
2
+ const snapshot = {
3
+ snapshotId: createSnapshotId
4
+ };
5
+ const captureSnapshot = (element, options) => {
6
+ const ref = element.__ref;
7
+ const { currentSnapshot, calleeElement } = options;
8
+ const isCallee = calleeElement === element;
9
+ if (!calleeElement || isCallee) {
10
+ const createdStanpshot = snapshot.snapshotId();
11
+ ref.__currentSnapshot = createdStanpshot;
12
+ return [createdStanpshot, element];
13
+ }
14
+ const snapshotOnCallee = calleeElement.__ref.__currentSnapshot;
15
+ if (currentSnapshot < snapshotOnCallee) {
16
+ return [snapshotOnCallee, calleeElement, true];
17
+ }
18
+ return [snapshotOnCallee, calleeElement];
19
+ };
20
+ export {
21
+ captureSnapshot
22
+ };
package/element.js ADDED
@@ -0,0 +1,148 @@
1
+ 'use strict'
2
+
3
+ import { createExtends } from './extends.js'
4
+ import { createKey } from './key.js'
5
+ import { isNode } from './node.js'
6
+ import { createProps } from './props.js'
7
+ import { HTML_TAGS } from './tags.js'
8
+ import { is, isFunction } from './types.js'
9
+
10
+ const ENV = process.env.NODE_ENV
11
+
12
+ export const returnValueAsText = (element, parent, key) => {
13
+ const childExtendsTag = parent.childExtends && parent.childExtends.tag
14
+ const childPropsTag = parent.props.childProps && parent.props.childProps.tag
15
+ const isKeyValidHTMLTag = HTML_TAGS.body.indexOf(key) > -1 && key
16
+ return {
17
+ text: element,
18
+ tag: childExtendsTag || childPropsTag || isKeyValidHTMLTag || 'string'
19
+ }
20
+ }
21
+
22
+ export const createBasedOnType = (element, parent, key) => {
23
+ // if ELEMENT is not given
24
+ if (element === undefined) {
25
+ if (ENV === 'test' || ENV === 'development') {
26
+ console.warn(
27
+ key,
28
+ 'element is undefined in',
29
+ parent && parent.__ref && parent.__ref.path
30
+ )
31
+ }
32
+ return {}
33
+ }
34
+
35
+ if (element === null) return
36
+ if (element === true) return {}
37
+
38
+ if (is(element)('string', 'number')) {
39
+ return returnValueAsText(element, parent, key)
40
+ }
41
+
42
+ if (isFunction(element)) {
43
+ return { props: element }
44
+ }
45
+
46
+ return element
47
+ }
48
+
49
+ export const addRef = (element, parent) => {
50
+ const ref = {}
51
+ ref.origin = element
52
+ ref.parent = parent
53
+ return ref
54
+ }
55
+
56
+ export const createParent = (element, parent, key, options, root) => {
57
+ if (!parent) return root
58
+ if (isNode(parent)) {
59
+ const parentNodeWrapper = { key: ':root', node: parent }
60
+ root[`${key}_parent`] = parentNodeWrapper
61
+ return parentNodeWrapper
62
+ }
63
+ return parent
64
+ }
65
+
66
+ export const createRoot = (element, parent) => {
67
+ const { __ref: ref } = element
68
+ const { __ref: parentRef } = parent
69
+
70
+ const hasRoot = parent && parent.key === ':root'
71
+ if (!ref?.root) {
72
+ ref.root = hasRoot ? element : parentRef?.root
73
+ }
74
+ }
75
+
76
+ export const createPath = (element, parent, key) => {
77
+ let { __ref: parentRef } = parent
78
+ // set the PATH array
79
+ if (!parentRef) parentRef = parent.ref = {}
80
+ if (!parentRef.path) parentRef.path = []
81
+ return parentRef.path.concat(key)
82
+ }
83
+
84
+ export const addContext = (element, parent, key, options, root) => {
85
+ const forcedOptionsContext =
86
+ options.context && !root.context && !element.context
87
+ if (forcedOptionsContext) root.context = options.context
88
+
89
+ // inherit from parent or root
90
+ return options.context || parent.context || root.context || element.context
91
+ }
92
+
93
+ export const addCaching = (element, parent, key) => {
94
+ const ref = addRef(element, parent)
95
+
96
+ element.__ref = ref
97
+
98
+ // enable CACHING
99
+ if (!ref.__defineCache) ref.__defineCache = {}
100
+
101
+ // enable EXEC
102
+ if (!ref.__exec) ref.__exec = {}
103
+ if (!ref.__execProps) ref.__execProps = {}
104
+
105
+ // enable CLASS CACHING
106
+ if (!ref.__class) ref.__class = {}
107
+ if (!ref.__classNames) ref.__classNames = {}
108
+
109
+ // enable CLASS CACHING
110
+ if (!ref.__attr) ref.__attr = {}
111
+
112
+ // enable CHANGES storing
113
+ if (!ref.__changes) ref.__changes = []
114
+
115
+ // enable CHANGES storing
116
+ if (!ref.__children) ref.__children = []
117
+
118
+ ref.__extends = createExtends(element, parent, key)
119
+ ref.path = createPath(element, parent, key)
120
+
121
+ return ref
122
+ }
123
+
124
+ export const createElement = (passedProps, parentEl, passedKey, opts, root) => {
125
+ const hashed = passedProps?.__hash
126
+ const element = hashed
127
+ ? { extends: [passedProps] }
128
+ : createBasedOnType(passedProps, parentEl, passedKey)
129
+ if (!element) return
130
+
131
+ const parent = createParent(element, parentEl, passedKey, opts, root)
132
+ const key = createKey(element, parent, passedKey)
133
+
134
+ addCaching(element, parent, key)
135
+
136
+ const props = createProps(element, parent, key)
137
+ const context = addContext(element, parent, key, opts, root)
138
+ const on = element.on || {}
139
+
140
+ return {
141
+ ...element,
142
+ key,
143
+ props,
144
+ parent,
145
+ context,
146
+ on
147
+ }
148
+ }
package/env.js CHANGED
@@ -2,9 +2,12 @@
2
2
 
3
3
  export const NODE_ENV = process.env.NODE_ENV
4
4
 
5
- export const isProduction = (env = NODE_ENV) => (env === 'production' || env === 'prod') ||
5
+ export const isProduction = (env = NODE_ENV) =>
6
+ env === 'production' ||
7
+ env === 'prod' ||
6
8
  (env !== 'development' && env !== 'dev' && env !== 'test')
7
9
  export const isTest = (env = NODE_ENV) => env === 'test'
8
- export const isDevelopment = (env = NODE_ENV) => env === 'development' || env === 'dev'
10
+ export const isDevelopment = (env = NODE_ENV) =>
11
+ env === 'development' || env === 'dev'
9
12
 
10
13
  export const getNev = (key, env = NODE_ENV) => env[key]
package/events.js ADDED
@@ -0,0 +1,17 @@
1
+ 'use strict'
2
+
3
+ import { lowercaseFirstLetter } from './string.js'
4
+ import { isFunction } from './types.js'
5
+
6
+ export const addEventFromProps = (key, element) => {
7
+ const { props, on } = element
8
+ const eventName = lowercaseFirstLetter(key.split('on')[1])
9
+ const origEvent = on[eventName]
10
+ const funcFromProps = props[key]
11
+ if (isFunction(origEvent)) {
12
+ on[eventName] = (...args) => {
13
+ const originalEventRetunrs = origEvent(...args)
14
+ if (originalEventRetunrs !== false) funcFromProps(...args)
15
+ }
16
+ } else on[eventName] = funcFromProps
17
+ }