@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.
- package/array.js +26 -13
- package/cache.js +4 -0
- package/component.js +10 -227
- package/cookie.js +27 -24
- package/dist/cjs/array.js +30 -16
- package/dist/cjs/cache.js +26 -0
- package/dist/cjs/component.js +16 -226
- package/dist/cjs/cookie.js +19 -24
- package/dist/cjs/element.js +137 -0
- package/dist/cjs/events.js +37 -0
- package/dist/cjs/extends.js +351 -0
- package/dist/cjs/function.js +2 -4
- package/dist/cjs/if.js +30 -0
- package/dist/cjs/index.js +25 -15
- package/dist/cjs/key.js +6 -1
- package/dist/cjs/keys.js +178 -0
- package/dist/cjs/log.js +1 -2
- package/dist/cjs/methods.js +305 -0
- package/dist/cjs/object.js +89 -237
- package/dist/cjs/props.js +220 -0
- package/dist/cjs/scope.js +28 -0
- package/dist/cjs/state.js +175 -0
- package/dist/cjs/string.js +27 -16
- package/dist/cjs/types.js +2 -4
- package/dist/cjs/update.js +42 -0
- package/dist/esm/array.js +30 -16
- package/dist/esm/cache.js +6 -0
- package/dist/esm/component.js +17 -245
- package/dist/esm/cookie.js +19 -24
- package/dist/esm/element.js +135 -0
- package/dist/esm/events.js +17 -0
- package/dist/esm/extends.js +349 -0
- package/dist/esm/function.js +2 -4
- package/dist/esm/if.js +10 -0
- package/dist/esm/index.js +10 -0
- package/dist/esm/key.js +6 -1
- package/dist/esm/keys.js +158 -0
- package/dist/esm/log.js +1 -2
- package/dist/esm/methods.js +285 -0
- package/dist/esm/object.js +90 -239
- package/dist/esm/props.js +216 -0
- package/dist/esm/scope.js +8 -0
- package/dist/esm/state.js +185 -0
- package/dist/esm/string.js +27 -16
- package/dist/esm/types.js +2 -4
- package/dist/esm/update.js +22 -0
- package/element.js +149 -0
- package/env.js +5 -2
- package/events.js +17 -0
- package/extends.js +425 -0
- package/if.js +14 -0
- package/index.js +10 -0
- package/key.js +6 -0
- package/keys.js +157 -0
- package/log.js +4 -1
- package/methods.js +315 -0
- package/node.js +21 -13
- package/object.js +121 -235
- package/package.json +3 -3
- package/props.js +249 -0
- package/scope.js +8 -0
- package/state.js +208 -0
- package/string.js +66 -30
- package/update.js +27 -0
package/methods.js
ADDED
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
import { triggerEventOn } from '@domql/event'
|
|
4
|
+
import { DOMQ_PROPERTIES, METHODS, PARSED_DOMQ_PROPERTIES } from './keys.js'
|
|
5
|
+
import { isDefined, isFunction, isObject, isObjectLike } from './types.js'
|
|
6
|
+
import { deepClone } from './object.js'
|
|
7
|
+
import { isProduction } from './env.js'
|
|
8
|
+
import { removeValueFromArray } from './array.js'
|
|
9
|
+
const ENV = process.env.NODE_ENV
|
|
10
|
+
|
|
11
|
+
// TODO: update these files
|
|
12
|
+
export function spotByPath (path) {
|
|
13
|
+
const element = this
|
|
14
|
+
const { __ref: ref } = element
|
|
15
|
+
const arr = [].concat(path)
|
|
16
|
+
let foundelement = ref.root[arr[0]]
|
|
17
|
+
|
|
18
|
+
if (!arr || !arr.length) {
|
|
19
|
+
return console.log(arr, 'on', element.key, 'is undefined')
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
while (foundelement.key === arr[0]) {
|
|
23
|
+
arr.shift()
|
|
24
|
+
if (!arr.length) break
|
|
25
|
+
foundelement = foundelement[arr[0]]
|
|
26
|
+
if (!foundelement) return
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return foundelement
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// TODO: update these files
|
|
33
|
+
export function lookup (param) {
|
|
34
|
+
const el = this
|
|
35
|
+
let { parent } = el
|
|
36
|
+
|
|
37
|
+
if (isFunction(param)) {
|
|
38
|
+
if (parent.state && param(parent, parent.state, parent.context)) {
|
|
39
|
+
return parent
|
|
40
|
+
} else if (parent.parent) return parent.lookup(param)
|
|
41
|
+
else return
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (el[param]) return el[param]
|
|
45
|
+
|
|
46
|
+
while (parent.param !== param) {
|
|
47
|
+
if (parent[param]) return parent[param]
|
|
48
|
+
parent = parent.parent
|
|
49
|
+
if (!parent) return
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return parent
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function lookdown (param) {
|
|
56
|
+
const el = this
|
|
57
|
+
const { __ref: ref } = el
|
|
58
|
+
const children = ref.__children
|
|
59
|
+
|
|
60
|
+
for (let i = 0; i < children.length; i++) {
|
|
61
|
+
const v = children[i]
|
|
62
|
+
const childElem = el[v]
|
|
63
|
+
|
|
64
|
+
if (v === param) return childElem
|
|
65
|
+
else if (isFunction(param)) {
|
|
66
|
+
const exec = param(childElem, childElem.state, childElem.context)
|
|
67
|
+
if (childElem.state && exec) {
|
|
68
|
+
return childElem
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
const lookdown = childElem?.lookdown?.(param)
|
|
72
|
+
if (lookdown) return lookdown
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function lookdownAll (param, results = []) {
|
|
77
|
+
const el = this
|
|
78
|
+
const { __ref: ref } = el
|
|
79
|
+
const children = ref.__children
|
|
80
|
+
|
|
81
|
+
for (let i = 0; i < children.length; i++) {
|
|
82
|
+
const v = children[i]
|
|
83
|
+
const childElem = el[v]
|
|
84
|
+
|
|
85
|
+
if (v === param) results.push(childElem)
|
|
86
|
+
else if (isFunction(param)) {
|
|
87
|
+
const exec = param(childElem, childElem.state, childElem.context)
|
|
88
|
+
if (childElem.state && exec) results.push(childElem)
|
|
89
|
+
}
|
|
90
|
+
childElem?.lookdownAll?.(param, results)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return results.length ? results : undefined
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function setNodeStyles (params = {}) {
|
|
97
|
+
const el = this
|
|
98
|
+
if (!el.node?.style) return
|
|
99
|
+
|
|
100
|
+
for (const param in params) {
|
|
101
|
+
const value = params[param]
|
|
102
|
+
const childElem = el[param]
|
|
103
|
+
if (isObject(value) && childElem) setNodeStyles.call(childElem, value)
|
|
104
|
+
else el.node.style[param] = value
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return el
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function remove (opts) {
|
|
111
|
+
const element = this
|
|
112
|
+
const beforeRemoveReturns = triggerEventOn('beforeRemove', element, opts)
|
|
113
|
+
if (beforeRemoveReturns === false) return element
|
|
114
|
+
if (isFunction(element.node.remove)) element.node.remove()
|
|
115
|
+
else if (!isProduction()) {
|
|
116
|
+
console.warn('This item cant be removed')
|
|
117
|
+
element.log()
|
|
118
|
+
}
|
|
119
|
+
delete element.parent[element.key]
|
|
120
|
+
if (element.parent.__ref) {
|
|
121
|
+
element.parent.__ref.__children = removeValueFromArray(
|
|
122
|
+
element.parent.__ref.__children,
|
|
123
|
+
element.key
|
|
124
|
+
)
|
|
125
|
+
}
|
|
126
|
+
triggerEventOn('remove', element, opts)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export function get (param) {
|
|
130
|
+
const element = this
|
|
131
|
+
return element[param]
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export function setProps (param, options) {
|
|
135
|
+
const element = this
|
|
136
|
+
if (!param || !element.props) return
|
|
137
|
+
element.update({ props: param }, options)
|
|
138
|
+
return element
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export function getRef () {
|
|
142
|
+
return this.__ref
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export function getPath () {
|
|
146
|
+
return this.getRef().path
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// export function set () {
|
|
150
|
+
// }
|
|
151
|
+
|
|
152
|
+
// export function update () {
|
|
153
|
+
// }
|
|
154
|
+
|
|
155
|
+
export const defineSetter = (element, key, get, set) =>
|
|
156
|
+
Object.defineProperty(element, key, { get, set })
|
|
157
|
+
|
|
158
|
+
export function keys () {
|
|
159
|
+
const element = this
|
|
160
|
+
const keys = []
|
|
161
|
+
for (const param in element) {
|
|
162
|
+
if (
|
|
163
|
+
// (REGISTRY[param] && !DOMQ_PROPERTIES.includes(param)) ||
|
|
164
|
+
!Object.hasOwnProperty.call(element, param) ||
|
|
165
|
+
(DOMQ_PROPERTIES.includes(param) &&
|
|
166
|
+
!PARSED_DOMQ_PROPERTIES.includes(param))
|
|
167
|
+
) {
|
|
168
|
+
continue
|
|
169
|
+
}
|
|
170
|
+
keys.push(param)
|
|
171
|
+
}
|
|
172
|
+
return keys
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export function parse (excl = []) {
|
|
176
|
+
const element = this
|
|
177
|
+
const { __ref: ref } = element
|
|
178
|
+
const obj = {}
|
|
179
|
+
const keyList = keys.call(element)
|
|
180
|
+
keyList.forEach(v => {
|
|
181
|
+
if (excl.includes(v)) return
|
|
182
|
+
const val = element[v]
|
|
183
|
+
if (v === 'state') {
|
|
184
|
+
if (!ref?.__hasRootState) return
|
|
185
|
+
const parsedVal = isFunction(val && val.parse) ? val.parse() : val
|
|
186
|
+
obj[v] = isFunction(parsedVal)
|
|
187
|
+
? parsedVal
|
|
188
|
+
: JSON.parse(JSON.stringify(parsedVal || {}))
|
|
189
|
+
} else if (v === 'scope') {
|
|
190
|
+
if (!ref?.__hasRootScope) return
|
|
191
|
+
obj[v] = JSON.parse(JSON.stringify(val || {}))
|
|
192
|
+
} else if (isDefined(val) && Object.hasOwnProperty.call(element, v)) {
|
|
193
|
+
obj[v] = val
|
|
194
|
+
}
|
|
195
|
+
})
|
|
196
|
+
return obj
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export function parseDeep (excl = []) {
|
|
200
|
+
const element = this
|
|
201
|
+
const obj = parse.call(element, excl)
|
|
202
|
+
for (const v in obj) {
|
|
203
|
+
if (excl.includes(v)) return
|
|
204
|
+
if (isObjectLike(obj[v])) {
|
|
205
|
+
obj[v] = parseDeep.call(obj[v], excl)
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
return obj
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export function verbose (...args) {
|
|
212
|
+
if (ENV !== 'test' && ENV !== 'development') return
|
|
213
|
+
|
|
214
|
+
const element = this
|
|
215
|
+
const { __ref: ref } = element
|
|
216
|
+
console.groupCollapsed(element.key)
|
|
217
|
+
if (args.length) {
|
|
218
|
+
args.forEach(v => console.log(`%c${v}:\n`, 'font-weight: bold', element[v]))
|
|
219
|
+
} else {
|
|
220
|
+
console.log(ref.path)
|
|
221
|
+
const keys = element.keys()
|
|
222
|
+
keys.forEach(v => console.log(`%c${v}:`, 'font-weight: bold', element[v]))
|
|
223
|
+
}
|
|
224
|
+
console.log(element)
|
|
225
|
+
console.groupEnd(element.key)
|
|
226
|
+
return element
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
export function log (...params) {
|
|
230
|
+
if (ENV === 'test' || ENV === 'development') {
|
|
231
|
+
console.log(...params)
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
export function warn (...params) {
|
|
236
|
+
if (ENV === 'test' || ENV === 'development') {
|
|
237
|
+
console.warn(...params)
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
export function error (...params) {
|
|
242
|
+
if (ENV === 'test' || ENV === 'development') {
|
|
243
|
+
if (params[params.length - 1]?.debugger) debugger // eslint-disable-line
|
|
244
|
+
if (params[params.length - 1]?.verbose) verbose.call(this)
|
|
245
|
+
throw new Error(...params)
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
export function nextElement () {
|
|
250
|
+
const element = this
|
|
251
|
+
const { key, parent } = element
|
|
252
|
+
const { __children } = parent.__ref
|
|
253
|
+
|
|
254
|
+
const currentIndex = __children.indexOf(key)
|
|
255
|
+
const nextChild = __children[currentIndex + 1]
|
|
256
|
+
|
|
257
|
+
return parent[nextChild]
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
export function previousElement (el) {
|
|
261
|
+
const element = el || this
|
|
262
|
+
const { key, parent } = element
|
|
263
|
+
const { __children } = parent.__ref
|
|
264
|
+
|
|
265
|
+
if (!__children) return
|
|
266
|
+
|
|
267
|
+
const currentIndex = __children.indexOf(key)
|
|
268
|
+
return parent[__children[currentIndex - 1]]
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
export function variables (obj = {}) {
|
|
272
|
+
const element = this
|
|
273
|
+
if (!element.data) element.data = {}
|
|
274
|
+
if (!element.data.varCaches) element.data.varCaches = {}
|
|
275
|
+
const varCaches = element.data.varCaches
|
|
276
|
+
const changes = {}
|
|
277
|
+
let changed
|
|
278
|
+
for (const key in obj) {
|
|
279
|
+
if (obj[key] !== varCaches[key]) {
|
|
280
|
+
changed = true
|
|
281
|
+
changes[key] = obj[key]
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
return {
|
|
285
|
+
changed: cb => {
|
|
286
|
+
if (!changed) return
|
|
287
|
+
const returns = cb(changes, deepClone(varCaches))
|
|
288
|
+
for (const key in changes) {
|
|
289
|
+
varCaches[key] = changes[key]
|
|
290
|
+
}
|
|
291
|
+
return returns
|
|
292
|
+
},
|
|
293
|
+
timeout: (cb, timeout) => {
|
|
294
|
+
if (!changed) return
|
|
295
|
+
const t = setTimeout(() => {
|
|
296
|
+
cb(changes)
|
|
297
|
+
clearTimeout(t)
|
|
298
|
+
}, timeout)
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
export function call (fnKey, ...args) {
|
|
304
|
+
const context = this.context
|
|
305
|
+
return (
|
|
306
|
+
context.utils?.[fnKey] ||
|
|
307
|
+
context.functions?.[fnKey] ||
|
|
308
|
+
context.methods?.[fnKey] ||
|
|
309
|
+
context.snippets?.[fnKey]
|
|
310
|
+
)?.call(this, ...args)
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
export function isMethod (param, element) {
|
|
314
|
+
return Boolean(METHODS.includes(param) || element?.context?.methods?.[param])
|
|
315
|
+
}
|
package/node.js
CHANGED
|
@@ -2,28 +2,36 @@
|
|
|
2
2
|
|
|
3
3
|
import { window } from './globals.js'
|
|
4
4
|
|
|
5
|
-
export const isNode =
|
|
5
|
+
export const isNode = obj => {
|
|
6
6
|
return (
|
|
7
|
-
typeof Node === 'object'
|
|
7
|
+
(typeof Node === 'object'
|
|
8
8
|
? obj instanceof window.Node
|
|
9
|
-
: obj &&
|
|
10
|
-
|
|
9
|
+
: obj &&
|
|
10
|
+
typeof obj === 'object' &&
|
|
11
|
+
typeof obj.nodeType === 'number' &&
|
|
12
|
+
typeof obj.nodeName === 'string') || false
|
|
13
|
+
)
|
|
11
14
|
}
|
|
12
15
|
|
|
13
16
|
// Returns true if it is a DOM element
|
|
14
17
|
export const isHtmlElement = obj => {
|
|
15
18
|
return (
|
|
16
|
-
typeof HTMLElement === 'object'
|
|
19
|
+
(typeof HTMLElement === 'object'
|
|
17
20
|
? obj instanceof window.HTMLElement // DOM2
|
|
18
|
-
: obj &&
|
|
19
|
-
|
|
21
|
+
: obj &&
|
|
22
|
+
typeof obj === 'object' &&
|
|
23
|
+
obj !== null &&
|
|
24
|
+
obj.nodeType === 1 &&
|
|
25
|
+
typeof obj.nodeName === 'string') || false
|
|
26
|
+
)
|
|
20
27
|
}
|
|
21
28
|
|
|
22
|
-
export const isDOMNode =
|
|
23
|
-
return
|
|
24
|
-
|
|
25
|
-
obj instanceof window.
|
|
26
|
-
|
|
27
|
-
|
|
29
|
+
export const isDOMNode = obj => {
|
|
30
|
+
return (
|
|
31
|
+
typeof window !== 'undefined' &&
|
|
32
|
+
(obj instanceof window.Node ||
|
|
33
|
+
obj instanceof window.Window ||
|
|
34
|
+
obj === window ||
|
|
35
|
+
obj === document)
|
|
28
36
|
)
|
|
29
37
|
}
|