@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.
Files changed (122) hide show
  1. package/LICENSE +25 -12
  2. package/README.md +106 -0
  3. package/array.js +162 -0
  4. package/cache.js +7 -0
  5. package/cdn.js +83 -0
  6. package/component.js +25 -0
  7. package/cookie.js +71 -0
  8. package/{src/detectHeight.js → detectHeight.js} +3 -3
  9. package/dist/cjs/array.js +168 -0
  10. package/dist/cjs/browser.js +0 -2
  11. package/dist/cjs/cache.js +26 -0
  12. package/dist/cjs/cdn.js +85 -0
  13. package/dist/cjs/component.js +45 -0
  14. package/dist/cjs/cookie.js +75 -0
  15. package/dist/cjs/date.js +1 -3
  16. package/dist/cjs/detectHeight.js +4 -6
  17. package/dist/cjs/element.js +136 -0
  18. package/dist/cjs/env.js +43 -0
  19. package/dist/cjs/events.js +39 -0
  20. package/dist/cjs/extends.js +434 -0
  21. package/dist/cjs/fibonacci.js +1 -3
  22. package/dist/cjs/files.js +1 -3
  23. package/dist/cjs/function.js +114 -0
  24. package/dist/cjs/globals.js +30 -0
  25. package/dist/cjs/if.js +48 -0
  26. package/dist/cjs/index.js +38 -425
  27. package/dist/cjs/key.js +38 -0
  28. package/dist/cjs/keys.js +201 -0
  29. package/dist/cjs/load.js +5 -7
  30. package/dist/cjs/log.js +34 -0
  31. package/dist/cjs/metadata.js +310 -0
  32. package/dist/cjs/methods.js +599 -0
  33. package/dist/cjs/node.js +35 -0
  34. package/dist/cjs/object.js +634 -0
  35. package/dist/cjs/projectKeys.js +35 -0
  36. package/dist/cjs/props.js +282 -0
  37. package/dist/cjs/scaling.js +5 -169
  38. package/dist/cjs/scope.js +44 -0
  39. package/dist/cjs/sharedLibraries.js +146 -0
  40. package/dist/cjs/state.js +173 -0
  41. package/dist/cjs/string-extra.js +72 -0
  42. package/dist/cjs/string.js +183 -0
  43. package/dist/cjs/style.js +1 -3
  44. package/dist/cjs/tags.js +220 -0
  45. package/dist/cjs/triggerEvent.js +112 -0
  46. package/dist/cjs/types.js +74 -0
  47. package/dist/cjs/update.js +42 -0
  48. package/dist/esm/array.js +148 -0
  49. package/dist/esm/browser.js +14 -0
  50. package/dist/esm/cache.js +6 -0
  51. package/dist/esm/cdn.js +65 -0
  52. package/dist/esm/component.js +25 -0
  53. package/dist/esm/cookie.js +55 -0
  54. package/dist/esm/date.js +11 -0
  55. package/dist/esm/detectHeight.js +42 -0
  56. package/dist/esm/element.js +116 -0
  57. package/dist/esm/env.js +23 -0
  58. package/dist/esm/events.js +19 -0
  59. package/dist/esm/extends.js +414 -0
  60. package/dist/esm/fibonacci.js +10 -0
  61. package/dist/esm/files.js +4 -0
  62. package/dist/esm/function.js +94 -0
  63. package/dist/esm/globals.js +10 -0
  64. package/dist/esm/if.js +28 -0
  65. package/dist/esm/index.js +37 -0
  66. package/dist/esm/key.js +18 -0
  67. package/dist/esm/keys.js +181 -0
  68. package/dist/esm/load.js +151 -0
  69. package/dist/esm/log.js +14 -0
  70. package/dist/esm/metadata.js +290 -0
  71. package/dist/esm/methods.js +569 -0
  72. package/dist/esm/node.js +15 -0
  73. package/dist/esm/object.js +621 -0
  74. package/dist/esm/projectKeys.js +16 -0
  75. package/dist/esm/props.js +262 -0
  76. package/dist/esm/scaling.js +15 -0
  77. package/dist/esm/scope.js +24 -0
  78. package/dist/esm/sharedLibraries.js +126 -0
  79. package/dist/esm/state.js +163 -0
  80. package/dist/esm/string-extra.js +52 -0
  81. package/dist/esm/string.js +163 -0
  82. package/dist/esm/style.js +20 -0
  83. package/dist/esm/tags.js +200 -0
  84. package/dist/esm/triggerEvent.js +92 -0
  85. package/dist/esm/types.js +54 -0
  86. package/dist/esm/update.js +22 -0
  87. package/dist/iife/index.js +5818 -0
  88. package/element.js +148 -0
  89. package/env.js +15 -0
  90. package/events.js +19 -0
  91. package/extends.js +519 -0
  92. package/function.js +145 -0
  93. package/globals.js +8 -0
  94. package/if.js +30 -0
  95. package/index.js +41 -0
  96. package/key.js +20 -0
  97. package/keys.js +176 -0
  98. package/log.js +12 -0
  99. package/metadata.js +392 -0
  100. package/methods.js +636 -0
  101. package/node.js +37 -0
  102. package/object.js +803 -0
  103. package/package.json +38 -22
  104. package/projectKeys.js +30 -0
  105. package/props.js +327 -0
  106. package/{src/scaling.js → scaling.js} +1 -1
  107. package/scope.js +32 -0
  108. package/sharedLibraries.js +184 -0
  109. package/state.js +203 -0
  110. package/{src/index.js → string-extra.js} +11 -17
  111. package/string.js +206 -0
  112. package/tags.js +176 -0
  113. package/triggerEvent.js +102 -0
  114. package/types.js +55 -0
  115. package/update.js +27 -0
  116. package/dist/cjs/package.json +0 -4
  117. /package/{src/browser.js → browser.js} +0 -0
  118. /package/{src/date.js → date.js} +0 -0
  119. /package/{src/fibonacci.js → fibonacci.js} +0 -0
  120. /package/{src/files.js → files.js} +0 -0
  121. /package/{src/load.js → load.js} +0 -0
  122. /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
@@ -0,0 +1,8 @@
1
+ 'use strict'
2
+
3
+ const global = globalThis
4
+ const self = globalThis
5
+ const window = globalThis
6
+ const document = (this || window).document // eslint-disable-line
7
+
8
+ export { global, self, window, document }
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
@@ -0,0 +1,12 @@
1
+ 'use strict'
2
+
3
+ export const logIf = (bool, ...arg) => {
4
+ if (bool) arg.map(v => console.log(v))
5
+ }
6
+ export const logGroupIf = (bool, key, ...arg) => {
7
+ if (bool) {
8
+ console.group(key)
9
+ arg.map(v => console.log(v))
10
+ console.groupEnd(key)
11
+ }
12
+ }