@symbo.ls/utils 3.2.3 → 3.14.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/LICENSE +25 -12
- package/README.md +106 -0
- package/array.js +162 -0
- package/cache.js +7 -0
- package/cdn.js +83 -0
- package/component.js +25 -0
- package/cookie.js +71 -0
- package/{src/detectHeight.js → detectHeight.js} +3 -3
- package/dist/cjs/array.js +168 -0
- package/dist/cjs/browser.js +0 -2
- package/dist/cjs/cache.js +26 -0
- package/dist/cjs/cdn.js +85 -0
- package/dist/cjs/component.js +45 -0
- package/dist/cjs/cookie.js +75 -0
- package/dist/cjs/date.js +1 -3
- package/dist/cjs/detectHeight.js +4 -6
- package/dist/cjs/element.js +136 -0
- package/dist/cjs/env.js +43 -0
- package/dist/cjs/events.js +39 -0
- package/dist/cjs/extends.js +434 -0
- package/dist/cjs/fibonacci.js +1 -3
- package/dist/cjs/files.js +1 -3
- package/dist/cjs/function.js +114 -0
- package/dist/cjs/globals.js +30 -0
- package/dist/cjs/if.js +48 -0
- package/dist/cjs/index.js +38 -425
- package/dist/cjs/key.js +38 -0
- package/dist/cjs/keys.js +201 -0
- package/dist/cjs/load.js +5 -7
- package/dist/cjs/log.js +34 -0
- package/dist/cjs/metadata.js +310 -0
- package/dist/cjs/methods.js +599 -0
- package/dist/cjs/node.js +35 -0
- package/dist/cjs/object.js +634 -0
- package/dist/cjs/projectKeys.js +35 -0
- package/dist/cjs/props.js +282 -0
- package/dist/cjs/scaling.js +5 -169
- package/dist/cjs/scope.js +44 -0
- package/dist/cjs/sharedLibraries.js +146 -0
- package/dist/cjs/state.js +173 -0
- package/dist/cjs/string-extra.js +72 -0
- package/dist/cjs/string.js +183 -0
- package/dist/cjs/style.js +1 -3
- package/dist/cjs/tags.js +220 -0
- package/dist/cjs/triggerEvent.js +112 -0
- package/dist/cjs/types.js +74 -0
- package/dist/cjs/update.js +42 -0
- package/dist/esm/array.js +148 -0
- package/dist/esm/browser.js +14 -0
- package/dist/esm/cache.js +6 -0
- package/dist/esm/cdn.js +65 -0
- package/dist/esm/component.js +25 -0
- package/dist/esm/cookie.js +55 -0
- package/dist/esm/date.js +11 -0
- package/dist/esm/detectHeight.js +42 -0
- package/dist/esm/element.js +116 -0
- package/dist/esm/env.js +23 -0
- package/dist/esm/events.js +19 -0
- package/dist/esm/extends.js +414 -0
- package/dist/esm/fibonacci.js +10 -0
- package/dist/esm/files.js +4 -0
- package/dist/esm/function.js +94 -0
- package/dist/esm/globals.js +10 -0
- package/dist/esm/if.js +28 -0
- package/dist/esm/index.js +37 -0
- package/dist/esm/key.js +18 -0
- package/dist/esm/keys.js +181 -0
- package/dist/esm/load.js +151 -0
- package/dist/esm/log.js +14 -0
- package/dist/esm/metadata.js +290 -0
- package/dist/esm/methods.js +569 -0
- package/dist/esm/node.js +15 -0
- package/dist/esm/object.js +621 -0
- package/dist/esm/projectKeys.js +16 -0
- package/dist/esm/props.js +262 -0
- package/dist/esm/scaling.js +15 -0
- package/dist/esm/scope.js +24 -0
- package/dist/esm/sharedLibraries.js +126 -0
- package/dist/esm/state.js +163 -0
- package/dist/esm/string-extra.js +52 -0
- package/dist/esm/string.js +163 -0
- package/dist/esm/style.js +20 -0
- package/dist/esm/tags.js +200 -0
- package/dist/esm/triggerEvent.js +92 -0
- package/dist/esm/types.js +54 -0
- package/dist/esm/update.js +22 -0
- package/dist/iife/index.js +5818 -0
- package/element.js +148 -0
- package/env.js +15 -0
- package/events.js +19 -0
- package/extends.js +519 -0
- package/function.js +145 -0
- package/globals.js +8 -0
- package/if.js +30 -0
- package/index.js +41 -0
- package/key.js +20 -0
- package/keys.js +176 -0
- package/log.js +12 -0
- package/metadata.js +392 -0
- package/methods.js +636 -0
- package/node.js +37 -0
- package/object.js +803 -0
- package/package.json +38 -22
- package/projectKeys.js +30 -0
- package/props.js +327 -0
- package/{src/scaling.js → scaling.js} +1 -1
- package/scope.js +32 -0
- package/sharedLibraries.js +184 -0
- package/state.js +203 -0
- package/{src/index.js → string-extra.js} +11 -17
- package/string.js +206 -0
- package/tags.js +176 -0
- package/triggerEvent.js +102 -0
- package/types.js +55 -0
- package/update.js +27 -0
- package/dist/cjs/package.json +0 -4
- /package/{src/browser.js → browser.js} +0 -0
- /package/{src/date.js → date.js} +0 -0
- /package/{src/fibonacci.js → fibonacci.js} +0 -0
- /package/{src/files.js → files.js} +0 -0
- /package/{src/load.js → load.js} +0 -0
- /package/{src/style.js → style.js} +0 -0
package/function.js
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Debounces a function, ensuring that it is only executed after a specified timeout
|
|
5
|
+
* period has elapsed since the last invocation.
|
|
6
|
+
*
|
|
7
|
+
* @param {function} func - The function to be debounced.
|
|
8
|
+
* @param {number} [timeout=300] - The time (in milliseconds) to wait after the last call to
|
|
9
|
+
* `debounce` before executing the `func`.
|
|
10
|
+
* @returns {function} - A debounced version of the input function `func`.
|
|
11
|
+
* @example
|
|
12
|
+
* // Usage example:
|
|
13
|
+
* const debouncedFunction = debounce(this, myFunction, 500);
|
|
14
|
+
* window.addEventListener('resize', debouncedFunction);
|
|
15
|
+
*/
|
|
16
|
+
export function debounce (func, wait, immediate) {
|
|
17
|
+
let timeout
|
|
18
|
+
return function () {
|
|
19
|
+
const context = this
|
|
20
|
+
const args = arguments
|
|
21
|
+
const later = function () {
|
|
22
|
+
timeout = null
|
|
23
|
+
if (!immediate) func.apply(context, args)
|
|
24
|
+
}
|
|
25
|
+
const callNow = immediate && !timeout
|
|
26
|
+
clearTimeout(timeout)
|
|
27
|
+
timeout = setTimeout(later, wait)
|
|
28
|
+
if (callNow) func.apply(context, args)
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Debounces a function, ensuring that it is only executed after a specified timeout
|
|
34
|
+
* period has elapsed since the last invocation.
|
|
35
|
+
*
|
|
36
|
+
* @param {Object} element - The context (this) to which the debounced function will be applied.
|
|
37
|
+
* @param {function} func - The function to be debounced.
|
|
38
|
+
* @param {number} [timeout=300] - The time (in milliseconds) to wait after the last call to
|
|
39
|
+
* `debounce` before executing the `func`.
|
|
40
|
+
* @returns {function} - A debounced version of the input function `func`.
|
|
41
|
+
* @example
|
|
42
|
+
* // Usage example:
|
|
43
|
+
* const debouncedFunction = debounce(this, myFunction, 500);
|
|
44
|
+
* window.addEventListener('resize', debouncedFunction);
|
|
45
|
+
*/
|
|
46
|
+
export const debounceOnContext = (element, func, timeout = 300) => {
|
|
47
|
+
let timer
|
|
48
|
+
return (...args) => {
|
|
49
|
+
clearTimeout(timer)
|
|
50
|
+
timer = setTimeout(() => {
|
|
51
|
+
func.apply(element, args)
|
|
52
|
+
}, timeout)
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export const memoize = fn => {
|
|
57
|
+
const cache = {}
|
|
58
|
+
return (...args) => {
|
|
59
|
+
const n = args[0]
|
|
60
|
+
if (n in cache) {
|
|
61
|
+
return cache[n]
|
|
62
|
+
} else {
|
|
63
|
+
const result = fn(n)
|
|
64
|
+
cache[n] = result
|
|
65
|
+
return result
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const RE_STRING_FUNCTION = /^((function\s*\([^)]*\)\s*\{[^}]*\})|(\([^)]*\)\s*=>))/
|
|
71
|
+
|
|
72
|
+
export const isStringFunction = inputString => {
|
|
73
|
+
return RE_STRING_FUNCTION.test(inputString)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Resolve an event handler through context plugins.
|
|
78
|
+
*
|
|
79
|
+
* If `handler` is already a function it is returned as-is.
|
|
80
|
+
* Otherwise, iterates `context.plugins` looking for a plugin
|
|
81
|
+
* with a `resolveHandler` method that can compile the handler
|
|
82
|
+
* (e.g. a funcql schema) into a callable function.
|
|
83
|
+
*
|
|
84
|
+
* @param {*} handler - event handler (function, object, array, string)
|
|
85
|
+
* @param {object} element - domql element
|
|
86
|
+
* @returns {function|*} resolved handler
|
|
87
|
+
*/
|
|
88
|
+
/**
|
|
89
|
+
* Check if context has any plugin that can resolve non-function handlers.
|
|
90
|
+
*/
|
|
91
|
+
export const hasHandlerPlugin = (ctx) => {
|
|
92
|
+
const plugins = ctx?.plugins
|
|
93
|
+
if (!plugins || !plugins.length) return false
|
|
94
|
+
for (const plugin of plugins) {
|
|
95
|
+
if (plugin.resolveHandler) return true
|
|
96
|
+
}
|
|
97
|
+
return false
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export const resolveHandler = (handler, element) => {
|
|
101
|
+
if (typeof handler === 'function') return handler
|
|
102
|
+
const plugins = element?.context?.plugins
|
|
103
|
+
if (!plugins) return handler
|
|
104
|
+
for (const plugin of plugins) {
|
|
105
|
+
if (plugin.resolveHandler) {
|
|
106
|
+
const resolved = plugin.resolveHandler(handler, element)
|
|
107
|
+
if (typeof resolved === 'function') return resolved
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return handler
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Run a named hook across all context plugins.
|
|
115
|
+
*
|
|
116
|
+
* @param {string} hookName - lifecycle hook name
|
|
117
|
+
* @param {object} element - domql element
|
|
118
|
+
* @param {*[]} args - additional arguments forwarded to each plugin hook
|
|
119
|
+
*/
|
|
120
|
+
export const runPluginHook = (hookName, element, ...args) => {
|
|
121
|
+
const plugins = element?.context?.plugins
|
|
122
|
+
if (!plugins) return false
|
|
123
|
+
let handled = false
|
|
124
|
+
for (const plugin of plugins) {
|
|
125
|
+
if (typeof plugin[hookName] === 'function') {
|
|
126
|
+
plugin[hookName](element, ...args)
|
|
127
|
+
handled = true
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return handled
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export function cloneFunction (fn, win = window) {
|
|
134
|
+
const temp = function () {
|
|
135
|
+
return fn.apply(win, arguments)
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// Copy properties from original function
|
|
139
|
+
for (const key in fn) {
|
|
140
|
+
if (Object.prototype.hasOwnProperty.call(fn, key)) {
|
|
141
|
+
temp[key] = fn[key]
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return temp
|
|
145
|
+
}
|
package/globals.js
ADDED
package/if.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
import { isFunction } from './types.js'
|
|
4
|
+
import { exec } from './object.js'
|
|
5
|
+
|
|
6
|
+
export const createIfConditionFlag = (element, parent) => {
|
|
7
|
+
const { __ref: ref } = element
|
|
8
|
+
const ifVal = element.if
|
|
9
|
+
|
|
10
|
+
if (isFunction(ifVal)) {
|
|
11
|
+
let result
|
|
12
|
+
try {
|
|
13
|
+
result = ifVal(element, element.state, element.context)
|
|
14
|
+
} catch (e) {
|
|
15
|
+
if (typeof console !== 'undefined') console.warn('[DOMQL] if condition error:', e)
|
|
16
|
+
delete ref.__if
|
|
17
|
+
return
|
|
18
|
+
}
|
|
19
|
+
if (!result) {
|
|
20
|
+
delete ref.__if
|
|
21
|
+
} else ref.__if = true
|
|
22
|
+
} else if (ifVal != null && typeof ifVal === 'object') {
|
|
23
|
+
// funcql schema — evaluate via exec() which delegates to plugins
|
|
24
|
+
const result = exec(ifVal, element)
|
|
25
|
+
if (!result) delete ref.__if
|
|
26
|
+
else ref.__if = true
|
|
27
|
+
} else {
|
|
28
|
+
ref.__if = true
|
|
29
|
+
}
|
|
30
|
+
}
|
package/index.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
export * from './key.js'
|
|
4
|
+
export * from './env.js'
|
|
5
|
+
export * from './types.js'
|
|
6
|
+
export * from './object.js'
|
|
7
|
+
export * from './function.js'
|
|
8
|
+
export * from './array.js'
|
|
9
|
+
export * from './node.js'
|
|
10
|
+
export * from './if.js'
|
|
11
|
+
export * from './log.js'
|
|
12
|
+
export * from './string.js'
|
|
13
|
+
export * from './globals.js'
|
|
14
|
+
export * from './cookie.js'
|
|
15
|
+
export * from './tags.js'
|
|
16
|
+
export * from './component.js'
|
|
17
|
+
export * from './props.js'
|
|
18
|
+
export * from './extends.js'
|
|
19
|
+
export * from './element.js'
|
|
20
|
+
export * from './state.js'
|
|
21
|
+
export * from './keys.js'
|
|
22
|
+
export * from './scope.js'
|
|
23
|
+
export * from './methods.js'
|
|
24
|
+
export * from './cache.js'
|
|
25
|
+
export * from './update.js'
|
|
26
|
+
export * from './triggerEvent.js'
|
|
27
|
+
export * from './projectKeys.js'
|
|
28
|
+
|
|
29
|
+
// ── Merged from the former @symbo.ls/smbls-utils package (4.x) ────────────
|
|
30
|
+
export * from './browser.js'
|
|
31
|
+
export * from './cdn.js'
|
|
32
|
+
export * from './date.js'
|
|
33
|
+
export * from './detectHeight.js'
|
|
34
|
+
export * from './fibonacci.js'
|
|
35
|
+
export * from './files.js'
|
|
36
|
+
export * from './load.js'
|
|
37
|
+
export * from './metadata.js'
|
|
38
|
+
export * from './scaling.js'
|
|
39
|
+
export * from './sharedLibraries.js'
|
|
40
|
+
export * from './string-extra.js'
|
|
41
|
+
export * from './style.js'
|
package/key.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
import { exec } from './object.js'
|
|
4
|
+
|
|
5
|
+
export const generateKey = (function () {
|
|
6
|
+
let index = 0
|
|
7
|
+
|
|
8
|
+
function newId () {
|
|
9
|
+
index++
|
|
10
|
+
return index
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
return newId
|
|
14
|
+
})()
|
|
15
|
+
|
|
16
|
+
export const createSnapshotId = generateKey
|
|
17
|
+
|
|
18
|
+
export const createKey = (element, parent, key) => {
|
|
19
|
+
return (exec(key, element) || key || element.key || generateKey()).toString()
|
|
20
|
+
}
|
package/keys.js
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
export const DOMQ_PROPERTIES = new Set([
|
|
4
|
+
'attr',
|
|
5
|
+
'style',
|
|
6
|
+
'text',
|
|
7
|
+
'html',
|
|
8
|
+
'content',
|
|
9
|
+
'data',
|
|
10
|
+
'classlist',
|
|
11
|
+
'state',
|
|
12
|
+
'scope',
|
|
13
|
+
'root',
|
|
14
|
+
'deps',
|
|
15
|
+
'extend',
|
|
16
|
+
'extends',
|
|
17
|
+
'$router',
|
|
18
|
+
'routes',
|
|
19
|
+
'children',
|
|
20
|
+
'childExtend',
|
|
21
|
+
'childExtends',
|
|
22
|
+
'childExtendRecursive',
|
|
23
|
+
'childExtendsRecursive',
|
|
24
|
+
'props',
|
|
25
|
+
'if',
|
|
26
|
+
'define',
|
|
27
|
+
'__name',
|
|
28
|
+
'__ref',
|
|
29
|
+
'__hash',
|
|
30
|
+
'__text',
|
|
31
|
+
'key',
|
|
32
|
+
'tag',
|
|
33
|
+
'query',
|
|
34
|
+
'parent',
|
|
35
|
+
'node',
|
|
36
|
+
'variables',
|
|
37
|
+
'on',
|
|
38
|
+
'fetch',
|
|
39
|
+
'component',
|
|
40
|
+
'context',
|
|
41
|
+
'preventContentUpdate',
|
|
42
|
+
'preventContentRecreate'
|
|
43
|
+
])
|
|
44
|
+
|
|
45
|
+
export const PARSED_DOMQ_PROPERTIES = new Set([
|
|
46
|
+
'attr',
|
|
47
|
+
'style',
|
|
48
|
+
'text',
|
|
49
|
+
'html',
|
|
50
|
+
'content',
|
|
51
|
+
'data',
|
|
52
|
+
'class',
|
|
53
|
+
'state',
|
|
54
|
+
'scope',
|
|
55
|
+
'children',
|
|
56
|
+
'props',
|
|
57
|
+
'if',
|
|
58
|
+
'key',
|
|
59
|
+
'tag',
|
|
60
|
+
'query',
|
|
61
|
+
'on',
|
|
62
|
+
'context'
|
|
63
|
+
])
|
|
64
|
+
|
|
65
|
+
export const STATE_PROPERTIES = [
|
|
66
|
+
'ref',
|
|
67
|
+
'parent',
|
|
68
|
+
'__element',
|
|
69
|
+
'__depends',
|
|
70
|
+
'__ref',
|
|
71
|
+
'__children',
|
|
72
|
+
'root'
|
|
73
|
+
]
|
|
74
|
+
|
|
75
|
+
export const STATE_METHODS = new Set([
|
|
76
|
+
'update',
|
|
77
|
+
'parse',
|
|
78
|
+
'clean',
|
|
79
|
+
'create',
|
|
80
|
+
'destroy',
|
|
81
|
+
'add',
|
|
82
|
+
'toggle',
|
|
83
|
+
'remove',
|
|
84
|
+
'apply',
|
|
85
|
+
'set',
|
|
86
|
+
'reset',
|
|
87
|
+
'replace',
|
|
88
|
+
'quietReplace',
|
|
89
|
+
'quietUpdate',
|
|
90
|
+
'applyReplace',
|
|
91
|
+
'applyFunction',
|
|
92
|
+
'keys',
|
|
93
|
+
'values',
|
|
94
|
+
'ref',
|
|
95
|
+
'rootUpdate',
|
|
96
|
+
'parentUpdate',
|
|
97
|
+
'parent',
|
|
98
|
+
'__element',
|
|
99
|
+
'__depends',
|
|
100
|
+
'__ref',
|
|
101
|
+
'__children',
|
|
102
|
+
'root',
|
|
103
|
+
'setByPath',
|
|
104
|
+
'setPathCollection',
|
|
105
|
+
'removeByPath',
|
|
106
|
+
'removePathCollection',
|
|
107
|
+
'getByPath'
|
|
108
|
+
])
|
|
109
|
+
|
|
110
|
+
export const PROPS_METHODS = new Set(['update', '__element'])
|
|
111
|
+
|
|
112
|
+
export const METHODS = new Set([
|
|
113
|
+
'set',
|
|
114
|
+
'reset',
|
|
115
|
+
'update',
|
|
116
|
+
'remove',
|
|
117
|
+
'updateContent',
|
|
118
|
+
'removeContent',
|
|
119
|
+
'lookup',
|
|
120
|
+
'lookdown',
|
|
121
|
+
'lookdownAll',
|
|
122
|
+
'getRef',
|
|
123
|
+
'getPath',
|
|
124
|
+
'setNodeStyles',
|
|
125
|
+
'spotByPath',
|
|
126
|
+
'keys',
|
|
127
|
+
'parse',
|
|
128
|
+
'setProps',
|
|
129
|
+
'parseDeep',
|
|
130
|
+
'variables',
|
|
131
|
+
'if',
|
|
132
|
+
'log',
|
|
133
|
+
'verbose',
|
|
134
|
+
'warn',
|
|
135
|
+
'error',
|
|
136
|
+
'call',
|
|
137
|
+
'nextElement',
|
|
138
|
+
'previousElement',
|
|
139
|
+
'getRootState',
|
|
140
|
+
'getRoot',
|
|
141
|
+
'getRootData',
|
|
142
|
+
'getRootContext',
|
|
143
|
+
'getContext',
|
|
144
|
+
'getQuery',
|
|
145
|
+
'getDB',
|
|
146
|
+
'getChildren',
|
|
147
|
+
'preventContentUpdate',
|
|
148
|
+
'preventContentRecreate'
|
|
149
|
+
])
|
|
150
|
+
|
|
151
|
+
export const METHODS_EXL = new Set([
|
|
152
|
+
'node', 'context', 'extends', '__element', '__ref',
|
|
153
|
+
...METHODS,
|
|
154
|
+
...STATE_METHODS,
|
|
155
|
+
...PROPS_METHODS
|
|
156
|
+
])
|
|
157
|
+
|
|
158
|
+
export const DOMQL_EVENTS = new Set([
|
|
159
|
+
'init',
|
|
160
|
+
'beforeClassAssign',
|
|
161
|
+
'render',
|
|
162
|
+
'renderRouter',
|
|
163
|
+
'attachNode',
|
|
164
|
+
'stateInit',
|
|
165
|
+
'stateCreated',
|
|
166
|
+
'beforeStateUpdate',
|
|
167
|
+
'stateUpdate',
|
|
168
|
+
'beforeUpdate',
|
|
169
|
+
'done',
|
|
170
|
+
'create',
|
|
171
|
+
'complete',
|
|
172
|
+
'frame',
|
|
173
|
+
'update',
|
|
174
|
+
'fetchError',
|
|
175
|
+
'fetchComplete'
|
|
176
|
+
])
|
package/log.js
ADDED