@stacksjs/utils 0.64.5 → 0.65.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/src/promise.ts DELETED
@@ -1,116 +0,0 @@
1
- import { remove } from '@stacksjs/arrays'
2
-
3
- export interface SingletonPromiseReturn<T> {
4
- (): Promise<T>
5
- /**
6
- * Reset current staled promise.
7
- * Await it to have proper shutdown.
8
- */
9
- reset: () => Promise<void>
10
- }
11
-
12
- // export { peek } from 'bun'
13
-
14
- /**
15
- * Promise with `resolve` and `reject` methods of itself
16
- */
17
- export interface ControlledPromise<T = void> extends Promise<T> {
18
- resolve: (value: T | PromiseLike<T>) => void
19
- reject: (reason?: any) => void
20
- }
21
-
22
- /**
23
- * Create singleton promise function.
24
- *
25
- * @category Promise
26
- */
27
- export function createSingletonPromise<T>(fn: () => Promise<T>): SingletonPromiseReturn<T> {
28
- let _promise: Promise<T> | undefined
29
-
30
- function wrapper() {
31
- if (!_promise) _promise = fn()
32
- return _promise
33
- }
34
- wrapper.reset = async () => {
35
- const _prev = _promise
36
- _promise = undefined
37
- if (_prev) await _prev
38
- }
39
-
40
- return wrapper
41
- }
42
-
43
- /**
44
- * Create a promise lock.
45
- *
46
- * @category Promise
47
- * @example
48
- * ```
49
- * const lock = createPromiseLock()
50
- *
51
- * lock.run(async () => {
52
- * await doSomething()
53
- * })
54
- *
55
- * // in anther context:
56
- * await lock.wait() // it will wait all tasking finished
57
- * ```
58
- */
59
- export function createPromiseLock() {
60
- const locks: Promise<any>[] = []
61
-
62
- return {
63
- async run<T = void>(fn: () => Promise<T>): Promise<T> {
64
- const p = fn()
65
- locks.push(p)
66
- try {
67
- return await p
68
- } finally {
69
- remove(locks, p)
70
- }
71
- },
72
- async wait(): Promise<void> {
73
- await Promise.allSettled(locks)
74
- },
75
- isWaiting() {
76
- return Boolean(locks.length)
77
- },
78
- clear() {
79
- locks.length = 0
80
- },
81
- }
82
- }
83
-
84
- /**
85
- * Return a Promise with `resolve` and `reject` methods
86
- *
87
- * @category Promise
88
- * @example
89
- * ```
90
- * const promise = createControlledPromise()
91
- *
92
- * await promise
93
- *
94
- * // in anther context:
95
- * promise.resolve(data)
96
- * ```
97
- */
98
- export function createControlledPromise<T>(): ControlledPromise<T> {
99
- let resolve: any
100
- let reject: any
101
-
102
- const promise = new Promise<T>((_resolve, _reject) => {
103
- resolve = _resolve
104
- reject = _reject
105
- }) as ControlledPromise<T>
106
-
107
- promise.resolve = resolve
108
-
109
- promise.reject = reject
110
- return promise
111
- }
112
-
113
- /**
114
- * Create a promise that will be resolved after `ms` milliseconds.
115
- */
116
- // export { sleep, sleepSync } from 'bun'
package/src/regex.ts DELETED
@@ -1,35 +0,0 @@
1
- export type {
2
- Flag,
3
- MagicRegExp,
4
- MagicRegExpMatchArray,
5
- Input,
6
- MapToStringCapturedBy,
7
- StringCapturedBy,
8
- } from 'magic-regexp'
9
- export {
10
- createRegExp,
11
- caseInsensitive,
12
- anyOf,
13
- carriageReturn,
14
- char,
15
- charIn,
16
- charNotIn,
17
- digit,
18
- dotAll,
19
- exactly,
20
- global,
21
- letter,
22
- linefeed,
23
- maybe,
24
- multiline,
25
- not,
26
- oneOrMore,
27
- sticky,
28
- tab,
29
- unicode,
30
- whitespace,
31
- withIndices,
32
- word,
33
- wordBoundary,
34
- wordChar,
35
- } from 'magic-regexp'
package/src/retry.ts DELETED
@@ -1,36 +0,0 @@
1
- export function retry(fn: Function, options: any): Promise<any> {
2
- const { retries = 3, initialDelay = 1000, backoffFactor = 2, jitter = true } = options
3
-
4
- return new Promise((resolve, reject) => {
5
- let attemptCount = 0
6
- const attempt = async () => {
7
- try {
8
- resolve(await fn())
9
- } catch (err) {
10
- if (attemptCount >= retries) {
11
- reject(err)
12
- } else {
13
- const delay = calculateDelay(attemptCount, initialDelay, backoffFactor, jitter)
14
- setTimeout(() => attempt(), delay)
15
- attemptCount++
16
- }
17
- }
18
- }
19
- attempt()
20
- })
21
- }
22
-
23
- export function calculateDelay(
24
- attemptCount: number,
25
- initialDelay: number,
26
- backoffFactor: number,
27
- jitter?: boolean,
28
- ): number {
29
- let delay = initialDelay * backoffFactor ** attemptCount
30
- if (jitter) {
31
- const random = Math.random() // Generates a number between 0 and 1
32
- const jitterValue = delay * 0.3 // Jitter will be up to 30% of the delay
33
- delay = delay + jitterValue * (random - 0.5) * 2 // Adjust delay randomly within ±30%
34
- }
35
- return delay
36
- }
package/src/sleep.ts DELETED
@@ -1,60 +0,0 @@
1
- /**
2
- * Pauses execution for a specified number of milliseconds.
3
- * @param ms The number of milliseconds to pause execution.
4
- * @returns A promise that resolves after the specified delay.
5
- */
6
- export function sleep(ms: number): Promise<void> {
7
- return new Promise((resolve) => setTimeout(resolve, ms))
8
- }
9
-
10
- /**
11
- * Pauses execution for a specified number of milliseconds.
12
- * @param ms The number of milliseconds to pause execution.
13
- * @returns A promise that resolves after the specified delay.
14
- */
15
- export function wait(ms: number): Promise<void> {
16
- return new Promise((resolve) => setTimeout(resolve, ms))
17
- }
18
-
19
- /**
20
- * Pauses execution for a specified number of milliseconds.
21
- * @param ms The number of milliseconds to pause execution.
22
- * @returns A promise that resolves after the specified delay.
23
- */
24
- export function delay(ms: number): Promise<void> {
25
- return new Promise((resolve) => setTimeout(resolve, ms))
26
- }
27
-
28
- /**
29
- * Pauses execution until a specified condition is met.
30
- * @param condition The condition to wait for.
31
- * @param interval The interval at which to check the condition, in milliseconds. Defaults to 1000.
32
- * @returns A promise that resolves when the condition is met.
33
- */
34
- export function waitUntil(condition: () => boolean, interval = 1000): Promise<void> {
35
- return new Promise((resolve) => {
36
- const check = setInterval(() => {
37
- if (condition()) {
38
- clearInterval(check)
39
- resolve()
40
- }
41
- }, interval)
42
- })
43
- }
44
-
45
- /**
46
- * Pauses execution while a specified condition is met.
47
- * @param condition The condition to wait while.
48
- * @param interval The interval at which to check the condition, in milliseconds. Defaults to 1000.
49
- * @returns A promise that resolves when the condition is no longer met.
50
- */
51
- export function waitWhile(condition: () => boolean, interval = 1000): Promise<void> {
52
- return new Promise((resolve) => {
53
- const check = setInterval(() => {
54
- if (!condition()) {
55
- clearInterval(check)
56
- resolve()
57
- }
58
- }, interval)
59
- })
60
- }
package/src/throttle.ts DELETED
@@ -1,46 +0,0 @@
1
- /**
2
- * Throttle a function.
3
- *
4
- * @param {Function} fn - The function to throttle.
5
- * @param {number} [wait] - The time to wait between function calls.
6
- * @returns {Function} - A new function that throttles the calls to the original function.
7
- *
8
- * @example
9
- * ```ts
10
- * // logs window dimensions at most every 250ms
11
- * window.addEventListener(
12
- * "resize",
13
- * throttle(function (evt) {
14
- * console.log(window.innerWidth)
15
- * console.log(window.innerHeight)
16
- * }, 250)
17
- * )
18
- * ```
19
- */
20
-
21
- export function throttle(fn: Function, wait = 300) {
22
- let inThrottle: boolean
23
- let lastFn: ReturnType<typeof setTimeout>
24
- let lastTime: number
25
-
26
- return function (this: unknown, ...args: any[]) {
27
- if (!inThrottle) {
28
- fn.apply(this, args)
29
-
30
- lastTime = Date.now()
31
- inThrottle = true
32
- } else {
33
- clearTimeout(lastFn)
34
-
35
- lastFn = setTimeout(
36
- () => {
37
- if (Date.now() - lastTime >= wait) {
38
- fn.apply(this, args) // Use 'this' directly
39
- lastTime = Date.now()
40
- }
41
- },
42
- Math.max(wait - (Date.now() - lastTime), 0),
43
- )
44
- }
45
- }
46
- }
package/src/vendors.ts DELETED
@@ -1,258 +0,0 @@
1
- export {
2
- asyncComputed,
3
- breakpointsAntDesign,
4
- breakpointsBootstrapV5,
5
- breakpointsMasterCss,
6
- breakpointsQuasar,
7
- breakpointsSematic,
8
- breakpointsTailwind,
9
- breakpointsVuetify,
10
- cloneFnJSON,
11
- computedAsync,
12
- computedInject,
13
- createFetch,
14
- createReusableTemplate,
15
- createTemplatePromise,
16
- createUnrefFn,
17
- customStorageEventName,
18
- defaultDocument,
19
- defaultLocation,
20
- defaultNavigator,
21
- defaultWindow,
22
- executeTransition,
23
- formatTimeAgo,
24
- getSSRHandler,
25
- mapGamepadToXbox360Controller,
26
- onClickOutside,
27
- onKeyDown,
28
- onKeyPressed,
29
- onKeyStroke,
30
- onKeyUp,
31
- onLongPress,
32
- onStartTyping,
33
- setSSRHandler,
34
- templateRef,
35
- unrefElement,
36
- useActiveElement,
37
- useAnimate,
38
- useAsyncQueue,
39
- useAsyncState,
40
- useBase64,
41
- useBattery,
42
- useBluetooth,
43
- useBreakpoints,
44
- useBroadcastChannel,
45
- useBrowserLocation,
46
- useCached,
47
- useClipboard,
48
- useCloned,
49
- useColorMode,
50
- useConfirmDialog,
51
- useCssVar,
52
- useCurrentElement,
53
- useCycleList,
54
- useDark,
55
- useDebouncedRefHistory,
56
- useDeviceMotion,
57
- useDeviceOrientation,
58
- useDevicePixelRatio,
59
- useDevicesList,
60
- useDisplayMedia,
61
- useDocumentVisibility,
62
- useDraggable,
63
- useDropZone,
64
- useElementBounding,
65
- useElementByPoint,
66
- useElementHover,
67
- useElementSize,
68
- useElementVisibility,
69
- useEventBus,
70
- useEventListener,
71
- useEventSource,
72
- useEyeDropper,
73
- useFavicon,
74
- useFetch,
75
- useFileDialog,
76
- useFileSystemAccess,
77
- useFocus,
78
- useFocusWithin,
79
- useFps,
80
- useFullscreen,
81
- useGamepad,
82
- useGeolocation,
83
- useIdle,
84
- useImage,
85
- useInfiniteScroll,
86
- useIntersectionObserver,
87
- useKeyModifier,
88
- useLocalStorage,
89
- useMagicKeys,
90
- useManualRefHistory,
91
- useMediaControls,
92
- useMediaQuery,
93
- useMemoize,
94
- useMemory,
95
- useMounted,
96
- useMouse,
97
- useMouseInElement,
98
- useMousePressed,
99
- useMutationObserver,
100
- useNavigatorLanguage,
101
- useNetwork,
102
- useObjectUrl,
103
- useOffsetPagination,
104
- useOnline,
105
- usePageLeave,
106
- useParallax,
107
- useParentElement,
108
- usePerformanceObserver,
109
- usePermission,
110
- usePointer,
111
- usePointerLock,
112
- usePointerSwipe,
113
- usePreferredColorScheme,
114
- usePreferredContrast,
115
- usePreferredDark,
116
- usePreferredLanguages,
117
- usePreferredReducedMotion,
118
- usePrevious,
119
- useRafFn,
120
- useRefHistory,
121
- useResizeObserver,
122
- useScreenOrientation,
123
- useScreenSafeArea,
124
- useScriptTag,
125
- useScroll,
126
- useScrollLock,
127
- useSessionStorage,
128
- useShare,
129
- useSorted,
130
- useSpeechRecognition,
131
- useSpeechSynthesis,
132
- useStepper,
133
- useStorage,
134
- useStorageAsync,
135
- useStyleTag,
136
- useSupported,
137
- useSwipe,
138
- useTemplateRefsList,
139
- useTextDirection,
140
- useTextSelection,
141
- useTextareaAutosize,
142
- useThrottledRefHistory,
143
- useTimeAgo,
144
- useTimeoutPoll,
145
- useTimestamp,
146
- useTitle,
147
- useTransition,
148
- useToggle,
149
- useUrlSearchParams,
150
- useUserMedia,
151
- useVModel,
152
- useVModels,
153
- useVibrate,
154
- useVirtualList,
155
- useWakeLock,
156
- useWebNotification,
157
- useWebSocket,
158
- useWebWorker,
159
- useWebWorkerFn,
160
- useWindowFocus,
161
- useWindowScroll,
162
- useWindowSize,
163
- useDateFormat,
164
- useNow,
165
- watchArray,
166
- watchAtMost,
167
- watchDebounced,
168
- watchDeep,
169
- watchIgnorable,
170
- watchImmediate,
171
- watchOnce,
172
- watchPausable,
173
- watchThrottled,
174
- watchTriggerable,
175
- watchWithFilter,
176
- whenever,
177
- syncRef,
178
- syncRefs,
179
- throttledRef,
180
- throttledWatch,
181
- toReactive,
182
- tryOnBeforeMount,
183
- tryOnBeforeUnmount,
184
- tryOnMounted,
185
- tryOnScopeDispose,
186
- tryOnUnmounted,
187
- until,
188
- isDefined,
189
- ignorableWatch,
190
- extendRef,
191
- eagerComputed,
192
- debouncedRef,
193
- debouncedWatch,
194
- createReactiveFn,
195
- createSharedComposable,
196
- createInjectionState,
197
- createGlobalState,
198
- createEventHook,
199
- controlledRef,
200
- controlledComputed,
201
- computedEager,
202
- computedWithControl,
203
- autoResetRef,
204
- injectLocal,
205
- makeDestructurable,
206
- pausableWatch,
207
- provideLocal,
208
- reactify,
209
- reactifyObject,
210
- reactiveComputed,
211
- reactiveOmit,
212
- reactivePick,
213
- refAutoReset,
214
- refDebounced,
215
- refDefault,
216
- refThrottled,
217
- refWithControl,
218
- resolveRef,
219
- resolveUnref,
220
- useArrayDifference,
221
- useArrayEvery,
222
- useArrayFilter,
223
- useArrayFind,
224
- useArrayFindIndex,
225
- useArrayIncludes,
226
- useArrayMap,
227
- useArrayReduce,
228
- useArraySome,
229
- useArrayUnique,
230
- useClipboardItems,
231
- useCounter,
232
- useDebounce,
233
- useDebounceFn,
234
- useInterval,
235
- useIntervalFn,
236
- useLastChanged,
237
- useThrottle,
238
- useThrottleFn,
239
- useTimeout,
240
- useTimeoutFn,
241
- useToNumber,
242
- useToString,
243
- } from '@vueuse/core'
244
-
245
- export type {
246
- // Head,
247
- HeadObject,
248
- HeadObjectPlain,
249
- } from '@vueuse/head'
250
-
251
- export {
252
- Head,
253
- HeadVuePlugin,
254
- createHead,
255
- renderHeadToString,
256
- } from '@vueuse/head'
257
-
258
- export { default as readableSize } from 'pretty-bytes'