@domql/utils 2.5.187 → 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 (64) 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 +30 -16
  6. package/dist/cjs/cache.js +26 -0
  7. package/dist/cjs/component.js +16 -226
  8. package/dist/cjs/cookie.js +19 -24
  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/function.js +2 -4
  13. package/dist/cjs/if.js +30 -0
  14. package/dist/cjs/index.js +25 -15
  15. package/dist/cjs/key.js +6 -1
  16. package/dist/cjs/keys.js +178 -0
  17. package/dist/cjs/log.js +1 -2
  18. package/dist/cjs/methods.js +305 -0
  19. package/dist/cjs/object.js +89 -237
  20. package/dist/cjs/props.js +220 -0
  21. package/dist/cjs/scope.js +28 -0
  22. package/dist/cjs/state.js +175 -0
  23. package/dist/cjs/string.js +27 -16
  24. package/dist/cjs/types.js +2 -4
  25. package/dist/cjs/update.js +42 -0
  26. package/dist/esm/array.js +30 -16
  27. package/dist/esm/cache.js +6 -0
  28. package/dist/esm/component.js +17 -245
  29. package/dist/esm/cookie.js +19 -24
  30. package/dist/esm/element.js +135 -0
  31. package/dist/esm/events.js +17 -0
  32. package/dist/esm/extends.js +349 -0
  33. package/dist/esm/function.js +2 -4
  34. package/dist/esm/if.js +10 -0
  35. package/dist/esm/index.js +10 -0
  36. package/dist/esm/key.js +6 -1
  37. package/dist/esm/keys.js +158 -0
  38. package/dist/esm/log.js +1 -2
  39. package/dist/esm/methods.js +285 -0
  40. package/dist/esm/object.js +90 -239
  41. package/dist/esm/props.js +216 -0
  42. package/dist/esm/scope.js +8 -0
  43. package/dist/esm/state.js +185 -0
  44. package/dist/esm/string.js +27 -16
  45. package/dist/esm/types.js +2 -4
  46. package/dist/esm/update.js +22 -0
  47. package/element.js +149 -0
  48. package/env.js +5 -2
  49. package/events.js +17 -0
  50. package/extends.js +425 -0
  51. package/if.js +14 -0
  52. package/index.js +10 -0
  53. package/key.js +6 -0
  54. package/keys.js +157 -0
  55. package/log.js +4 -1
  56. package/methods.js +315 -0
  57. package/node.js +21 -13
  58. package/object.js +121 -235
  59. package/package.json +3 -3
  60. package/props.js +249 -0
  61. package/scope.js +8 -0
  62. package/state.js +208 -0
  63. package/string.js +66 -30
  64. package/update.js +27 -0
@@ -0,0 +1,216 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
3
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
4
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
5
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
+ var __spreadValues = (a, b) => {
7
+ for (var prop in b || (b = {}))
8
+ if (__hasOwnProp.call(b, prop))
9
+ __defNormalProp(a, prop, b[prop]);
10
+ if (__getOwnPropSymbols)
11
+ for (var prop of __getOwnPropSymbols(b)) {
12
+ if (__propIsEnum.call(b, prop))
13
+ __defNormalProp(a, prop, b[prop]);
14
+ }
15
+ return a;
16
+ };
17
+ import { DOMQ_PROPERTIES, PROPS_METHODS } from "./keys.js";
18
+ import { addEventFromProps } from "./events.js";
19
+ import { deepClone, deepMerge, exec } from "./object.js";
20
+ import { is, isArray, isFunction, isObject, isObjectLike } from "./types.js";
21
+ const createProps = (element, parent, key) => {
22
+ const { props, __ref: ref } = element;
23
+ ref.__propsStack = [];
24
+ if (props) ref.__initialProps = props;
25
+ else return {};
26
+ if (!isObjectLike(props)) {
27
+ ref.__propsStack.push(props);
28
+ return {};
29
+ }
30
+ return __spreadValues({}, props);
31
+ };
32
+ function pickupPropsFromElement(element, opts = {}) {
33
+ var _a, _b, _c;
34
+ const cachedKeys = opts.cachedKeys || [];
35
+ for (const key in element) {
36
+ const value = element[key];
37
+ const hasDefine = isObject((_a = element.define) == null ? void 0 : _a[key]);
38
+ const hasGlobalDefine = isObject((_c = (_b = element.context) == null ? void 0 : _b.define) == null ? void 0 : _c[key]);
39
+ const isElement = /^[A-Z]/.test(key) || /^\d+$/.test(key);
40
+ const isBuiltin = DOMQ_PROPERTIES.includes(key);
41
+ if (!isElement && !isBuiltin && !hasDefine && !hasGlobalDefine) {
42
+ element.props[key] = value;
43
+ delete element[key];
44
+ cachedKeys.push(key);
45
+ }
46
+ }
47
+ return element;
48
+ }
49
+ function pickupElementFromProps(element, opts) {
50
+ var _a, _b, _c;
51
+ const cachedKeys = opts.cachedKeys || [];
52
+ for (const key in element.props) {
53
+ const value = element.props[key];
54
+ const isEvent = key.startsWith("on") && key.length > 2;
55
+ const isFn = isFunction(value);
56
+ if (isEvent && isFn) {
57
+ addEventFromProps(key, element);
58
+ delete element.props[key];
59
+ continue;
60
+ }
61
+ if (cachedKeys.includes(key)) continue;
62
+ const hasDefine = isObject((_a = element.define) == null ? void 0 : _a[key]);
63
+ const hasGlobalDefine = isObject((_c = (_b = element.context) == null ? void 0 : _b.define) == null ? void 0 : _c[key]);
64
+ const isElement = /^[A-Z]/.test(key) || /^\d+$/.test(key);
65
+ const isBuiltin = DOMQ_PROPERTIES.includes(key);
66
+ if (isElement || isBuiltin || hasDefine || hasGlobalDefine) {
67
+ element[key] = value;
68
+ delete element.props[key];
69
+ }
70
+ }
71
+ return element;
72
+ }
73
+ function propertizeElement(element, opts = {}) {
74
+ const cachedKeys = [];
75
+ pickupPropsFromElement(element, { cachedKeys });
76
+ pickupElementFromProps(element, { cachedKeys });
77
+ return element;
78
+ }
79
+ const objectizeStringProperty = (propValue) => {
80
+ if (is(propValue)("string", "number")) {
81
+ return { inheritedString: propValue };
82
+ }
83
+ return propValue;
84
+ };
85
+ const propExists = (prop, stack) => {
86
+ if (!prop || !stack.length) return false;
87
+ const key = isObject(prop) ? JSON.stringify(prop) : prop;
88
+ return stack.some((existing) => {
89
+ const existingKey = isObject(existing) ? JSON.stringify(existing) : existing;
90
+ return existingKey === key;
91
+ });
92
+ };
93
+ const inheritParentProps = (element, parent) => {
94
+ var _a;
95
+ const { __ref: ref } = element;
96
+ const propsStack = ref.__propsStack || [];
97
+ const parentProps = parent.props;
98
+ if (!parentProps) return propsStack;
99
+ const matchParentKeyProps = parentProps[element.key];
100
+ const matchParentChildProps = parentProps.childProps;
101
+ const ignoreChildProps = (_a = element.props) == null ? void 0 : _a.ignoreChildProps;
102
+ if (matchParentChildProps && !ignoreChildProps) {
103
+ const childProps = objectizeStringProperty(matchParentChildProps);
104
+ propsStack.unshift(childProps);
105
+ }
106
+ if (matchParentKeyProps) {
107
+ const keyProps = objectizeStringProperty(matchParentKeyProps);
108
+ propsStack.unshift(keyProps);
109
+ }
110
+ return propsStack;
111
+ };
112
+ async function update(props, options) {
113
+ const element = this.__element;
114
+ await element.update({ props }, options);
115
+ }
116
+ function setPropsPrototype(element) {
117
+ const methods = { update: update.bind(element.props), __element: element };
118
+ Object.setPrototypeOf(element.props, methods);
119
+ }
120
+ const removeDuplicateProps = (propsStack) => {
121
+ const seen = /* @__PURE__ */ new Set();
122
+ return propsStack.filter((prop) => {
123
+ if (!prop || PROPS_METHODS.includes(prop)) return false;
124
+ const key = isObject(prop) ? JSON.stringify(prop) : prop;
125
+ if (seen.has(key)) return false;
126
+ seen.add(key);
127
+ return true;
128
+ });
129
+ };
130
+ const syncProps = (propsStack, element, opts) => {
131
+ element.props = propsStack.reduce((mergedProps, v) => {
132
+ if (PROPS_METHODS.includes(v)) return mergedProps;
133
+ while (isFunction(v)) v = exec(v, element);
134
+ return deepMerge(mergedProps, deepClone(v, { exclude: PROPS_METHODS }));
135
+ }, {});
136
+ setPropsPrototype(element);
137
+ return element.props;
138
+ };
139
+ const createPropsStack = (element, parent) => {
140
+ const { props, __ref: ref } = element;
141
+ let propsStack = ref.__propsStack || [];
142
+ if (parent && parent.props) {
143
+ const parentStack = inheritParentProps(element, parent);
144
+ propsStack = [...parentStack];
145
+ }
146
+ if (isObject(props)) propsStack.push(props);
147
+ else if (props === "inherit" && (parent == null ? void 0 : parent.props)) propsStack.push(parent.props);
148
+ else if (props) propsStack.push(props);
149
+ if (isArray(ref.__extendsStack)) {
150
+ ref.__extendsStack.forEach((_extends) => {
151
+ if (_extends.props && _extends.props !== props) {
152
+ propsStack.push(_extends.props);
153
+ }
154
+ });
155
+ }
156
+ ref.__propsStack = removeDuplicateProps(propsStack);
157
+ return ref.__propsStack;
158
+ };
159
+ const applyProps = (element, parent) => {
160
+ const { __ref: ref } = element;
161
+ const propsStack = createPropsStack(element, parent);
162
+ if (propsStack.length) {
163
+ syncProps(propsStack, element);
164
+ } else {
165
+ ref.__propsStack = [];
166
+ element.props = {};
167
+ }
168
+ };
169
+ const initProps = function(element, parent, options) {
170
+ const { __ref: ref } = element;
171
+ if (ref.__if) applyProps(element, parent);
172
+ else {
173
+ try {
174
+ applyProps(element, parent);
175
+ } catch (e) {
176
+ element.props = {};
177
+ ref.__propsStack = [];
178
+ }
179
+ }
180
+ setPropsPrototype(element);
181
+ return element;
182
+ };
183
+ const updateProps = (newProps, element, parent) => {
184
+ const { __ref: ref } = element;
185
+ const propsStack = ref.__propsStack || [];
186
+ let newStack = [...propsStack];
187
+ const parentProps = inheritParentProps(element, parent);
188
+ if (parentProps.length) {
189
+ newStack = [...parentProps, ...newStack];
190
+ }
191
+ if (newProps) {
192
+ newStack = [newProps, ...newStack];
193
+ }
194
+ ref.__propsStack = removeDuplicateProps(newStack);
195
+ if (ref.__propsStack.length) {
196
+ syncProps(ref.__propsStack, element);
197
+ }
198
+ return element;
199
+ };
200
+ export {
201
+ applyProps,
202
+ createProps,
203
+ createPropsStack,
204
+ inheritParentProps,
205
+ initProps,
206
+ objectizeStringProperty,
207
+ pickupElementFromProps,
208
+ pickupPropsFromElement,
209
+ propExists,
210
+ propertizeElement,
211
+ removeDuplicateProps,
212
+ setPropsPrototype,
213
+ syncProps,
214
+ update,
215
+ updateProps
216
+ };
@@ -0,0 +1,8 @@
1
+ const createScope = (element, parent) => {
2
+ var _a;
3
+ const { __ref: ref } = element;
4
+ if (!element.scope) element.scope = parent.scope || ((_a = ref.root) == null ? void 0 : _a.scope) || {};
5
+ };
6
+ export {
7
+ createScope
8
+ };
@@ -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,29 +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 ? "{{{" : "{{"))
19
- return str;
20
- const reg = brackRegex[options.bracketsLength || 2];
21
- 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 || {};
22
28
  return str.replace(reg, (_, parentPath, variable) => {
23
29
  if (parentPath) {
24
- const parentLevels = parentPath.match(options.bracketsLength === 3 ? /\.\.\.\//g : /\.\.\//g).length;
30
+ const parentLevels = (parentPath.match(/\.\.\//g) || []).length;
25
31
  let parentState = obj;
26
32
  for (let i = 0; i < parentLevels; i++) {
33
+ if (!parentState || !parentState.parent) return "";
27
34
  parentState = parentState.parent;
28
- if (!parentState) {
29
- return "";
30
- }
31
35
  }
32
- const value = parentState[variable.trim()];
33
- 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) : "";
34
42
  } else {
35
- const value = obj[variable.trim()];
36
- return value !== void 0 ? `${value}` : "";
43
+ const value = getNestedValue(obj, variable.trim());
44
+ return value !== void 0 ? String(value) : "";
37
45
  }
38
46
  });
39
47
  }
@@ -109,7 +117,10 @@ const customEncodeURIComponent = (str) => {
109
117
  }).join("");
110
118
  };
111
119
  const customDecodeURIComponent = (encodedStr) => {
112
- 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
+ );
113
124
  };
114
125
  export {
115
126
  customDecodeURIComponent,
package/dist/esm/types.js CHANGED
@@ -1,7 +1,6 @@
1
1
  import { isHtmlElement, isNode } from "./node.js";
2
2
  const isObject = (arg) => {
3
- if (arg === null)
4
- return false;
3
+ if (arg === null) return false;
5
4
  return typeof arg === "object" && arg.constructor === Object;
6
5
  };
7
6
  const isString = (arg) => typeof arg === "string";
@@ -12,8 +11,7 @@ const isNull = (arg) => arg === null;
12
11
  const isArray = (arg) => Array.isArray(arg);
13
12
  const isDate = (d) => d instanceof Date;
14
13
  const isObjectLike = (arg) => {
15
- if (arg === null)
16
- return false;
14
+ if (arg === null) return false;
17
15
  return typeof arg === "object";
18
16
  };
19
17
  const isDefined = (arg) => {
@@ -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,149 @@
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
+ return 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
+ ref.root = createRoot(element, parent) // Call createRoot after addCaching
121
+
122
+ return ref
123
+ }
124
+
125
+ export const createElement = (passedProps, parentEl, passedKey, opts, root) => {
126
+ const hashed = passedProps?.__hash
127
+ const element = hashed
128
+ ? { extends: [passedProps] }
129
+ : createBasedOnType(passedProps, parentEl, passedKey)
130
+ if (!element) return
131
+
132
+ const parent = createParent(element, parentEl, passedKey, opts, root)
133
+ const key = createKey(element, parent, passedKey)
134
+
135
+ addCaching(element, parent, key)
136
+
137
+ const props = createProps(element, parent, key)
138
+ const context = addContext(element, parent, key, opts, root)
139
+ const on = element.on || {}
140
+
141
+ return {
142
+ ...element,
143
+ key,
144
+ props,
145
+ parent,
146
+ context,
147
+ on
148
+ }
149
+ }
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]