@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/element.js CHANGED
@@ -12,7 +12,7 @@ const ENV = process.env.NODE_ENV
12
12
  export const returnValueAsText = (element, parent, key) => {
13
13
  const childExtendsTag = parent.childExtends && parent.childExtends.tag
14
14
  const childPropsTag = parent.props.childProps && parent.props.childProps.tag
15
- const isKeyValidHTMLTag = HTML_TAGS.body.indexOf(key) > -1 && key
15
+ const isKeyValidHTMLTag = HTML_TAGS.body.has(key) && key
16
16
  return {
17
17
  text: element,
18
18
  tag: childExtendsTag || childPropsTag || isKeyValidHTMLTag || 'string'
@@ -26,7 +26,7 @@ export const createBasedOnType = (element, parent, key) => {
26
26
  console.warn(
27
27
  key,
28
28
  'element is undefined in',
29
- parent && parent.__ref && parent.__ref.path
29
+ parent?.__ref?.path
30
30
  )
31
31
  }
32
32
  return {}
package/extends.js CHANGED
@@ -94,7 +94,7 @@ export const setHashedExtend = (extend, stack) => {
94
94
  if (!isString(extend)) {
95
95
  extend.__hash = hash
96
96
  }
97
- if (!['__proto__', 'constructor', 'prototype'].includes(hash)) {
97
+ if (hash !== '__proto__' && hash !== 'constructor' && hash !== 'prototype') {
98
98
  extendStackRegistry[hash] = stack
99
99
  }
100
100
  return stack
@@ -125,11 +125,14 @@ export const extractArrayExtend = (
125
125
  }
126
126
 
127
127
  export const deepExtend = (extend, stack, context, processed = new Set()) => {
128
- const extendOflattenExtend = extend.extends
129
- // Remove extends property before adding to stack
128
+ const extendOflattenExtend = extend.extends || extend.extend
129
+ // Remove extends/extend properties before adding to stack
130
130
  const cleanExtend = { ...extend }
131
131
  delete cleanExtend.extends
132
- if (Object.keys(cleanExtend).length > 0) {
132
+ delete cleanExtend.extend
133
+ let hasKeys = false
134
+ for (const _k in cleanExtend) { hasKeys = true; break } // eslint-disable-line
135
+ if (hasKeys) {
133
136
  stack.push(cleanExtend)
134
137
  }
135
138
  if (extendOflattenExtend) {
@@ -157,7 +160,7 @@ export const flattenExtend = (
157
160
 
158
161
  processed.add(extend)
159
162
 
160
- if (extend?.extends) {
163
+ if (extend?.extends || extend?.extend) {
161
164
  deepExtend(extend, stack, context, processed)
162
165
  } else if (extend) {
163
166
  stack.push(extend)
@@ -166,12 +169,16 @@ export const flattenExtend = (
166
169
  return stack
167
170
  }
168
171
 
172
+ const MERGE_EXTENDS_SKIP = new Set([
173
+ 'parent', 'node', '__ref', '__proto__', 'extend', 'childExtend', 'childExtendRecursive'
174
+ ])
175
+
169
176
  export const deepMergeExtends = (element, extend) => {
170
177
  // Clone extend to prevent mutations
171
178
  extend = deepClone(extend)
172
179
 
173
180
  for (const e in extend) {
174
- if (['parent', 'node', '__ref', '__proto__'].indexOf(e) > -1) continue
181
+ if (MERGE_EXTENDS_SKIP.has(e)) continue
175
182
 
176
183
  const elementProp = element[e]
177
184
  const extendProp = extend[e]
@@ -182,7 +189,7 @@ export const deepMergeExtends = (element, extend) => {
182
189
  // Handle only properties that exist in the extend object
183
190
  if (
184
191
  Object.prototype.hasOwnProperty.call(extend, e) &&
185
- !['__proto__', 'constructor', 'prototype'].includes(e)
192
+ e !== '__proto__' && e !== 'constructor' && e !== 'prototype'
186
193
  ) {
187
194
  if (elementProp === undefined) {
188
195
  // For undefined properties in element, copy from extend
@@ -233,15 +240,15 @@ export const mapStringsWithContextComponents = (
233
240
  options = {},
234
241
  variant
235
242
  ) => {
236
- const COMPONENTS = (context && context.components) || options.components
237
- const PAGES = (context && context.pages) || options.pages
243
+ const COMPONENTS = context?.components || options.components
244
+ const PAGES = context?.pages || options.pages
238
245
  if (isString(extend)) {
239
246
  const componentExists =
240
247
  COMPONENTS &&
241
248
  (COMPONENTS[extend + '.' + variant] ||
242
249
  COMPONENTS[extend] ||
243
250
  COMPONENTS['smbls.' + extend])
244
- const pageExists = PAGES && extend.startsWith('/') && PAGES[extend]
251
+ const pageExists = PAGES && extend.charCodeAt(0) === 47 && PAGES[extend]
245
252
  if (componentExists) return componentExists
246
253
  else if (pageExists) return pageExists
247
254
  else {
@@ -283,7 +290,7 @@ export const getExtendsInElement = obj => {
283
290
 
284
291
  function traverse (o) {
285
292
  for (const key in o) {
286
- if (Object.hasOwnProperty.call(o, key)) {
293
+ if (Object.prototype.hasOwnProperty.call(o, key)) {
287
294
  // Check if the key starts with a capital letter and exclude keys like @mobileL, $propsCollection
288
295
  if (matchesComponentNaming(key)) {
289
296
  result.push(key)
@@ -316,7 +323,10 @@ export const createElementExtends = (element, parent, options = {}) => {
316
323
  const context = element.context || parent.context
317
324
  const variant = element.props?.variant
318
325
 
319
- // Add the extends property from element if it exists
326
+ // Add the extends property from element if it exists (supports v2 `extend` fallback)
327
+ if (element.extend && !element.extends) element.extends = element.extend
328
+ delete element.extend
329
+ if (!element.extends && element.props?.extends) element.extends = element.props.extends
320
330
  if (element.extends) {
321
331
  if (Array.isArray(element.extends) && element.extends.length > 0) {
322
332
  // Handle first item in array specially for variant
@@ -379,16 +389,18 @@ export const inheritChildExtends = (element, parent, options = {}) => {
379
389
  const ignoreChildExtends =
380
390
  options.ignoreChildExtends || props?.ignoreChildExtends
381
391
 
382
- if (!ignoreChildExtends && parent.childExtends) {
383
- // Use else if to avoid double-adding
384
- addExtends(parent.childExtends, element)
392
+ // Supports v2 `childExtend` fallback
393
+ const childExtends = parent.childExtends || parent.childExtend
394
+ if (!ignoreChildExtends && childExtends) {
395
+ addExtends(childExtends, element)
385
396
  }
386
397
  return ref.__extends
387
398
  }
388
399
 
389
400
  export const inheritRecursiveChildExtends = (element, parent, options = {}) => {
390
401
  const { props, __ref: ref } = element
391
- const childExtendsRecursive = parent.childExtendsRecursive
402
+ // Supports v2 `childExtendRecursive` fallback
403
+ const childExtendsRecursive = parent.childExtendsRecursive || parent.childExtendRecursive
392
404
  const ignoreChildExtendsRecursive =
393
405
  options.ignoreChildExtendsRecursive || props?.ignoreChildExtendsRecursive
394
406
  const isText = element.key === '__text'
@@ -402,7 +414,6 @@ export const createExtendsStack = (element, parent, options = {}) => {
402
414
  const { props, __ref: ref } = element
403
415
  const context = element.context || parent.context
404
416
 
405
- // if (ENV !== 'test' && ENV !== 'development') delete element.extends
406
417
  const variant = element.variant || props?.variant
407
418
 
408
419
  const __extends = removeDuplicatesInArray(
package/function.js CHANGED
@@ -67,12 +67,10 @@ export const memoize = fn => {
67
67
  }
68
68
  }
69
69
 
70
- export const isStringFunction = inputString => {
71
- // Regular expression to match both regular and arrow function declarations
72
- const functionRegex = /^((function\s*\([^)]*\)\s*\{[^}]*\})|(\([^)]*\)\s*=>))/
70
+ const RE_STRING_FUNCTION = /^((function\s*\([^)]*\)\s*\{[^}]*\})|(\([^)]*\)\s*=>))/
73
71
 
74
- // Use the regex to test if the inputString matches the function pattern
75
- return functionRegex.test(inputString)
72
+ export const isStringFunction = inputString => {
73
+ return RE_STRING_FUNCTION.test(inputString)
76
74
  }
77
75
 
78
76
  export function cloneFunction (fn, win = window) {
@@ -82,7 +80,7 @@ export function cloneFunction (fn, win = window) {
82
80
 
83
81
  // Copy properties from original function
84
82
  for (const key in fn) {
85
- if (Object.hasOwnProperty.call(fn, key)) {
83
+ if (Object.prototype.hasOwnProperty.call(fn, key)) {
86
84
  temp[key] = fn[key]
87
85
  }
88
86
  }
package/index.js CHANGED
@@ -23,3 +23,4 @@ export * from './scope.js'
23
23
  export * from './methods.js'
24
24
  export * from './cache.js'
25
25
  export * from './update.js'
26
+ export * from './triggerEvent.js'
package/keys.js CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict'
2
2
 
3
- export const DOMQ_PROPERTIES = [
3
+ export const DOMQ_PROPERTIES = new Set([
4
4
  'attr',
5
5
  'style',
6
6
  'text',
@@ -12,11 +12,14 @@ export const DOMQ_PROPERTIES = [
12
12
  'scope',
13
13
  'root',
14
14
  'deps',
15
+ 'extend',
15
16
  'extends',
16
17
  '$router',
17
18
  'routes',
18
19
  'children',
20
+ 'childExtend',
19
21
  'childExtends',
22
+ 'childExtendRecursive',
20
23
  'childExtendsRecursive',
21
24
  'props',
22
25
  'if',
@@ -34,9 +37,9 @@ export const DOMQ_PROPERTIES = [
34
37
  'on',
35
38
  'component',
36
39
  'context'
37
- ]
40
+ ])
38
41
 
39
- export const PARSED_DOMQ_PROPERTIES = [
42
+ export const PARSED_DOMQ_PROPERTIES = new Set([
40
43
  'attr',
41
44
  'style',
42
45
  'text',
@@ -52,8 +55,9 @@ export const PARSED_DOMQ_PROPERTIES = [
52
55
  'key',
53
56
  'tag',
54
57
  'query',
55
- 'on'
56
- ]
58
+ 'on',
59
+ 'context'
60
+ ])
57
61
 
58
62
  export const STATE_PROPERTIES = [
59
63
  'ref',
@@ -65,7 +69,7 @@ export const STATE_PROPERTIES = [
65
69
  'root'
66
70
  ]
67
71
 
68
- export const STATE_METHODS = [
72
+ export const STATE_METHODS = new Set([
69
73
  'update',
70
74
  'parse',
71
75
  'clean',
@@ -98,11 +102,11 @@ export const STATE_METHODS = [
98
102
  'removeByPath',
99
103
  'removePathCollection',
100
104
  'getByPath'
101
- ]
105
+ ])
102
106
 
103
- export const PROPS_METHODS = ['update', '__element']
107
+ export const PROPS_METHODS = new Set(['update', '__element'])
104
108
 
105
- export const METHODS = [
109
+ export const METHODS = new Set([
106
110
  'set',
107
111
  'reset',
108
112
  'update',
@@ -128,17 +132,23 @@ export const METHODS = [
128
132
  'error',
129
133
  'call',
130
134
  'nextElement',
131
- 'previousElement'
132
- ]
135
+ 'previousElement',
136
+ 'getRootState',
137
+ 'getRoot',
138
+ 'getRootData',
139
+ 'getRootContext',
140
+ 'getContext',
141
+ 'getChildren'
142
+ ])
133
143
 
134
- export const METHODS_EXL = [
135
- ...['node', 'context', 'extends', '__element', '__ref'],
144
+ export const METHODS_EXL = new Set([
145
+ 'node', 'context', 'extends', '__element', '__ref',
136
146
  ...METHODS,
137
147
  ...STATE_METHODS,
138
148
  ...PROPS_METHODS
139
- ]
149
+ ])
140
150
 
141
- export const DOMQL_EVENTS = [
151
+ export const DOMQL_EVENTS = new Set([
142
152
  'init',
143
153
  'beforeClassAssign',
144
154
  'render',
@@ -154,4 +164,4 @@ export const DOMQL_EVENTS = [
154
164
  'complete',
155
165
  'frame',
156
166
  'update'
157
- ]
167
+ ])
package/methods.js CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict'
2
2
 
3
- import { triggerEventOn } from '@domql/event'
3
+ import { triggerEventOn } from './triggerEvent.js'
4
4
  import { DOMQ_PROPERTIES, METHODS, PARSED_DOMQ_PROPERTIES } from './keys.js'
5
5
  import { isDefined, isFunction, isObject, isObjectLike } from './types.js'
6
6
  import { deepClone } from './object.js'
@@ -156,11 +156,53 @@ export function getPath () {
156
156
  return this.getRef().path
157
157
  }
158
158
 
159
- // export function set () {
160
- // }
159
+ export function getRootState (param) {
160
+ let state = null
161
+ const hasRootState = (obj) => obj.__element && obj.root?.isRootState
162
+ if (!this) {
163
+ state = window.platformState || window.smblsApp?.state
164
+ } else if (hasRootState(this)) {
165
+ state = this.root
166
+ } else if (this.__ref && this.__ref.path) {
167
+ const hasPlatformState = this.state && hasRootState(this.state)
168
+ const hasPlatformStateOnParent =
169
+ isFunction(this.state) &&
170
+ this.parent.state &&
171
+ hasRootState(this.parent.state)
172
+ if (hasPlatformState || hasPlatformStateOnParent) {
173
+ state = this.state.root || this.parent.state.root
174
+ }
175
+ }
176
+ if (!state) {
177
+ state = window.platformState || window.smblsApp?.state
178
+ }
179
+ return param ? state?.[param] : state
180
+ }
181
+
182
+ export function getRoot (key) {
183
+ const rootElem = this.getRootState()?.__element
184
+ return rootElem && Object.keys(rootElem).length > 0 && key
185
+ ? rootElem[key]
186
+ : rootElem
187
+ }
161
188
 
162
- // export function update () {
163
- // }
189
+ export function getRootData (key) {
190
+ return this.getRoot('data') &&
191
+ Object.keys(this.getRoot('data')).length > 0 &&
192
+ key
193
+ ? this.getRoot('data')[key]
194
+ : this.getRoot('data')
195
+ }
196
+
197
+ export function getRootContext (key) {
198
+ const ctx = this.getRoot()?.context
199
+ return key ? ctx[key] : ctx
200
+ }
201
+
202
+ export function getContext (key) {
203
+ const ctx = this.context
204
+ return key ? ctx[key] : ctx
205
+ }
164
206
 
165
207
  export const defineSetter = (element, key, get, set) =>
166
208
  Object.defineProperty(element, key, { get, set })
@@ -170,10 +212,10 @@ export function keys () {
170
212
  const keys = []
171
213
  for (const param in element) {
172
214
  if (
173
- // (REGISTRY[param] && !DOMQ_PROPERTIES.includes(param)) ||
174
- !Object.hasOwnProperty.call(element, param) ||
175
- (DOMQ_PROPERTIES.includes(param) &&
176
- !PARSED_DOMQ_PROPERTIES.includes(param))
215
+ // (REGISTRY[param] && !DOMQ_PROPERTIES.has(param)) ||
216
+ !Object.prototype.hasOwnProperty.call(element, param) ||
217
+ (DOMQ_PROPERTIES.has(param) &&
218
+ !PARSED_DOMQ_PROPERTIES.has(param))
177
219
  ) {
178
220
  continue
179
221
  }
@@ -187,26 +229,28 @@ export function parse (excl = []) {
187
229
  const obj = {}
188
230
  const keyList = keys.call(element)
189
231
  const hasChildren = keyList.includes('children')
190
- keyList.forEach(v => {
191
- if (excl.includes(v) || !Object.hasOwnProperty.call(element, v)) return
192
- if (hasChildren && v === 'content') return
232
+ const exclSet = excl.length ? new Set(excl) : null
233
+ for (let i = 0; i < keyList.length; i++) {
234
+ const v = keyList[i]
235
+ if ((exclSet && exclSet.has(v)) || !Object.prototype.hasOwnProperty.call(element, v)) continue
236
+ if (hasChildren && v === 'content') continue
193
237
  const val = element[v]
194
238
  if (v === 'state') {
195
- if (element.__ref && !element.__ref.__hasRootState) return
239
+ if (element.__ref && !element.__ref.__hasRootState) continue
196
240
  const parsedVal = isFunction(val && val.parse) ? val.parse() : val
197
241
  obj[v] = isFunction(parsedVal)
198
242
  ? parsedVal
199
243
  : JSON.parse(JSON.stringify(parsedVal || {}))
200
244
  } else if (v === 'scope') {
201
- if (element.__ref && !element.__ref.__hasRootScope) return
245
+ if (element.__ref && !element.__ref.__hasRootScope) continue
202
246
  obj[v] = JSON.parse(JSON.stringify(val || {}))
203
247
  } else if (v === 'props') {
204
248
  const { __element, update, ...props } = element[v]
205
249
  obj[v] = props
206
- } else if (isDefined(val) && Object.hasOwnProperty.call(element, v)) {
250
+ } else if (isDefined(val) && Object.prototype.hasOwnProperty.call(element, v)) {
207
251
  obj[v] = val
208
252
  }
209
- })
253
+ }
210
254
  return obj
211
255
  }
212
256
 
@@ -215,8 +259,9 @@ export function parseDeep (excl = [], visited = new WeakSet()) {
215
259
  if (visited.has(element)) return undefined
216
260
  visited.add(element)
217
261
  const obj = parse.call(element, excl)
262
+ const exclSet = excl.length ? new Set(excl) : null
218
263
  for (const v in obj) {
219
- if (excl.includes(v) || !Object.hasOwnProperty.call(element, v)) continue
264
+ if ((exclSet && exclSet.has(v)) || !Object.prototype.hasOwnProperty.call(element, v)) continue
220
265
  const val = obj[v]
221
266
  if (Array.isArray(val)) {
222
267
  obj[v] = val.map(item =>
@@ -341,5 +386,5 @@ export function call (fnKey, ...args) {
341
386
  }
342
387
 
343
388
  export function isMethod (param, element) {
344
- return Boolean(METHODS.includes(param) || element?.context?.methods?.[param])
389
+ return Boolean(METHODS.has(param) || element?.context?.methods?.[param])
345
390
  }