@o/helpers 0.16.19 → 0.16.20
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/README.md +2 -0
- package/dist/array/getRandomItem.d.ts +2 -0
- package/dist/array/getRandomItem.d.ts.map +1 -0
- package/dist/array/getRandomItem.js +7 -0
- package/dist/array/getRandomItem.js.map +1 -0
- package/dist/array/takeLast.d.ts +2 -0
- package/dist/array/takeLast.d.ts.map +1 -0
- package/dist/array/takeLast.js +4 -0
- package/dist/array/takeLast.js.map +1 -0
- package/dist/array/uniqBy.d.ts +8 -0
- package/dist/array/uniqBy.d.ts.map +1 -0
- package/dist/array/uniqBy.js +17 -0
- package/dist/array/uniqBy.js.map +1 -0
- package/dist/async/asyncContext.d.ts +15 -0
- package/dist/async/asyncContext.d.ts.map +1 -0
- package/dist/async/asyncContext.js +115 -0
- package/dist/async/asyncContext.js.map +1 -0
- package/dist/async/asyncContext.native.d.ts +8 -0
- package/dist/async/asyncContext.native.d.ts.map +1 -0
- package/dist/async/asyncContext.native.js +63 -0
- package/dist/async/asyncContext.native.js.map +1 -0
- package/dist/async/idle.d.ts +7 -0
- package/dist/async/idle.d.ts.map +1 -0
- package/dist/async/idle.js +46 -0
- package/dist/async/idle.js.map +1 -0
- package/dist/async/interval.d.ts +2 -0
- package/dist/async/interval.d.ts.map +1 -0
- package/dist/async/interval.js +8 -0
- package/dist/async/interval.js.map +1 -0
- package/dist/async/isAborted.d.ts +2 -0
- package/dist/async/isAborted.d.ts.map +1 -0
- package/dist/async/isAborted.js +2 -0
- package/dist/async/isAborted.js.map +1 -0
- package/dist/async/useAsync.d.ts +2 -0
- package/dist/async/useAsync.d.ts.map +1 -0
- package/dist/async/useAsync.js +31 -0
- package/dist/async/useAsync.js.map +1 -0
- package/dist/async/useAsyncEffect.native.js +99 -0
- package/dist/async/useLazyMount.d.ts +8 -0
- package/dist/async/useLazyMount.d.ts.map +1 -0
- package/dist/async/useLazyMount.js +20 -0
- package/dist/async/useLazyMount.js.map +1 -0
- package/dist/async/useLazyMount.native.js +20 -0
- package/dist/async/useLazyValue.d.ts +5 -0
- package/dist/async/useLazyValue.d.ts.map +1 -0
- package/dist/async/useLazyValue.js +18 -0
- package/dist/async/useLazyValue.js.map +1 -0
- package/dist/async/useLazyValue.native.js +18 -0
- package/dist/browser/clearIndexedDB.d.ts +2 -0
- package/dist/browser/clearIndexedDB.d.ts.map +1 -0
- package/dist/browser/clearIndexedDB.js +17 -0
- package/dist/browser/clearIndexedDB.js.map +1 -0
- package/dist/client-only.d.ts +2 -0
- package/dist/client-only.d.ts.map +1 -0
- package/dist/client-only.js +5 -0
- package/dist/client-only.js.map +1 -0
- package/dist/clipboard/clipboard.d.ts +2 -0
- package/dist/clipboard/clipboard.d.ts.map +1 -0
- package/dist/clipboard/clipboard.js +10 -0
- package/dist/clipboard/clipboard.js.map +1 -0
- package/dist/clipboard/clipboard.native.d.ts +2 -0
- package/dist/clipboard/clipboard.native.d.ts.map +1 -0
- package/dist/clipboard/clipboard.native.js +11 -0
- package/dist/clipboard/clipboard.native.js.map +1 -0
- package/dist/color/extractOpacityFromColor.d.ts +2 -0
- package/dist/color/extractOpacityFromColor.d.ts.map +1 -0
- package/dist/color/extractOpacityFromColor.js +18 -0
- package/dist/color/extractOpacityFromColor.js.map +1 -0
- package/dist/color/generateColors.d.ts +10 -0
- package/dist/color/generateColors.d.ts.map +1 -0
- package/dist/color/generateColors.js +48 -0
- package/dist/color/generateColors.js.map +1 -0
- package/dist/color/lum.d.ts +2 -0
- package/dist/color/lum.d.ts.map +1 -0
- package/dist/color/lum.js +67 -0
- package/dist/color/lum.js.map +1 -0
- package/dist/color/toHex.d.ts +3 -0
- package/dist/color/toHex.d.ts.map +1 -0
- package/dist/color/toHex.js +10 -0
- package/dist/color/toHex.js.map +1 -0
- package/dist/constants.native.js +22 -0
- package/dist/debug/debugLog.d.ts +10 -0
- package/dist/debug/debugLog.d.ts.map +1 -0
- package/dist/debug/debugLog.js +14 -0
- package/dist/debug/debugLog.js.map +1 -0
- package/dist/debug/debugUseState.d.ts +2 -0
- package/dist/debug/debugUseState.d.ts.map +1 -0
- package/dist/debug/debugUseState.js +29 -0
- package/dist/debug/debugUseState.js.map +1 -0
- package/dist/emitter.d.ts.map +1 -1
- package/dist/emitter.js +18 -2
- package/dist/emitter.js.map +1 -1
- package/dist/emitter.native.js +289 -0
- package/dist/ensure/ensure.d.ts +5 -0
- package/dist/ensure/ensure.d.ts.map +1 -0
- package/dist/ensure/ensure.js +22 -0
- package/dist/ensure/ensure.js.map +1 -0
- package/dist/ensure/ensureOne.d.ts +2 -0
- package/dist/ensure/ensureOne.d.ts.map +1 -0
- package/dist/ensure/ensureOne.js +14 -0
- package/dist/ensure/ensureOne.js.map +1 -0
- package/dist/function/emptyFn.d.ts +2 -0
- package/dist/function/emptyFn.d.ts.map +1 -0
- package/dist/function/emptyFn.js +2 -0
- package/dist/function/emptyFn.js.map +1 -0
- package/dist/function/identityFn.d.ts +2 -0
- package/dist/function/identityFn.d.ts.map +1 -0
- package/dist/function/identityFn.js +2 -0
- package/dist/function/identityFn.js.map +1 -0
- package/dist/function/throttle.d.ts +22 -0
- package/dist/function/throttle.d.ts.map +1 -0
- package/dist/function/throttle.js +40 -0
- package/dist/function/throttle.js.map +1 -0
- package/dist/global/globalEffect.d.ts +2 -0
- package/dist/global/globalEffect.d.ts.map +1 -0
- package/dist/global/globalEffect.js +12 -0
- package/dist/global/globalEffect.js.map +1 -0
- package/dist/index.native.js +1 -1
- package/dist/number/formatNumber.d.ts +12 -0
- package/dist/number/formatNumber.d.ts.map +1 -0
- package/dist/number/formatNumber.js +39 -0
- package/dist/number/formatNumber.js.map +1 -0
- package/dist/object/decorateObject.d.ts +2 -0
- package/dist/object/decorateObject.d.ts.map +1 -0
- package/dist/object/decorateObject.js +15 -0
- package/dist/object/decorateObject.js.map +1 -0
- package/dist/object/isEqualDeep.d.ts +5 -0
- package/dist/object/isEqualDeep.d.ts.map +1 -0
- package/dist/object/isEqualDeep.js +5 -0
- package/dist/object/isEqualDeep.js.map +1 -0
- package/dist/object/isEqualJSON.d.ts +2 -0
- package/dist/object/isEqualJSON.d.ts.map +1 -0
- package/dist/object/isEqualJSON.js +2 -0
- package/dist/object/isEqualJSON.js.map +1 -0
- package/dist/object/mapObject.d.ts +4 -0
- package/dist/object/mapObject.d.ts.map +1 -0
- package/dist/object/mapObject.js +10 -0
- package/dist/object/mapObject.js.map +1 -0
- package/dist/object/object.d.ts +10 -0
- package/dist/object/object.d.ts.map +1 -0
- package/dist/object/object.js +13 -0
- package/dist/object/object.js.map +1 -0
- package/dist/object/objectUniqueKey.d.ts +2 -0
- package/dist/object/objectUniqueKey.d.ts.map +1 -0
- package/dist/object/objectUniqueKey.js +10 -0
- package/dist/object/objectUniqueKey.js.map +1 -0
- package/dist/server/ensureEnv.d.ts +2 -0
- package/dist/server/ensureEnv.d.ts.map +1 -0
- package/dist/server/ensureEnv.js +20 -0
- package/dist/server/ensureEnv.js.map +1 -0
- package/dist/server/getHeaders.d.ts +2 -0
- package/dist/server/getHeaders.d.ts.map +1 -0
- package/dist/server/getHeaders.js +8 -0
- package/dist/server/getHeaders.js.map +1 -0
- package/dist/server/prettyPrintRequest.d.ts +2 -0
- package/dist/server/prettyPrintRequest.d.ts.map +1 -0
- package/dist/server/prettyPrintRequest.js +14 -0
- package/dist/server/prettyPrintRequest.js.map +1 -0
- package/dist/server/prettyPrintResponse.d.ts +2 -0
- package/dist/server/prettyPrintResponse.d.ts.map +1 -0
- package/dist/server/prettyPrintResponse.js +13 -0
- package/dist/server/prettyPrintResponse.js.map +1 -0
- package/dist/server/streamToString.d.ts +2 -0
- package/dist/server/streamToString.d.ts.map +1 -0
- package/dist/server/streamToString.js +21 -0
- package/dist/server/streamToString.js.map +1 -0
- package/dist/server-only.d.ts +2 -0
- package/dist/server-only.d.ts.map +1 -0
- package/dist/server-only.js +5 -0
- package/dist/server-only.js.map +1 -0
- package/dist/shared.d.ts +59 -4
- package/dist/shared.d.ts.map +1 -1
- package/dist/shared.js +59 -3
- package/dist/shared.js.map +1 -1
- package/dist/shared.native.js +73 -0
- package/dist/string/dedent.d.ts +2 -0
- package/dist/string/dedent.d.ts.map +1 -0
- package/dist/string/dedent.js +24 -0
- package/dist/string/dedent.js.map +1 -0
- package/dist/string/ellipsis.d.ts +2 -0
- package/dist/string/ellipsis.d.ts.map +1 -0
- package/dist/string/ellipsis.js +10 -0
- package/dist/string/ellipsis.js.map +1 -0
- package/dist/string/hash.d.ts +3 -0
- package/dist/string/hash.d.ts.map +1 -0
- package/dist/string/hash.js +16 -0
- package/dist/string/hash.js.map +1 -0
- package/dist/string/insertAtIndex.d.ts +2 -0
- package/dist/string/insertAtIndex.d.ts.map +1 -0
- package/dist/string/insertAtIndex.js +7 -0
- package/dist/string/insertAtIndex.js.map +1 -0
- package/dist/string/nbspLastWord.d.ts +6 -0
- package/dist/string/nbspLastWord.d.ts.map +1 -0
- package/dist/string/nbspLastWord.js +13 -0
- package/dist/string/nbspLastWord.js.map +1 -0
- package/dist/string/pickLast.d.ts +2 -0
- package/dist/string/pickLast.d.ts.map +1 -0
- package/dist/string/pickLast.js +10 -0
- package/dist/string/pickLast.js.map +1 -0
- package/dist/string/pluralize.d.ts +4 -0
- package/dist/string/pluralize.d.ts.map +1 -0
- package/dist/string/pluralize.js +19 -0
- package/dist/string/pluralize.js.map +1 -0
- package/dist/string/pluralize.native.d.ts +3 -0
- package/dist/string/pluralize.native.d.ts.map +1 -0
- package/dist/string/pluralize.native.js +5 -0
- package/dist/string/pluralize.native.js.map +1 -0
- package/dist/string/truncateList.d.ts +16 -0
- package/dist/string/truncateList.d.ts.map +1 -0
- package/dist/string/truncateList.js +23 -0
- package/dist/string/truncateList.js.map +1 -0
- package/dist/time/formatDate.d.ts +4 -0
- package/dist/time/formatDate.d.ts.map +1 -0
- package/dist/time/formatDate.js +40 -0
- package/dist/time/formatDate.js.map +1 -0
- package/dist/time/formatDateRelative.d.ts +8 -0
- package/dist/time/formatDateRelative.d.ts.map +1 -0
- package/dist/time/formatDateRelative.js +55 -0
- package/dist/time/formatDateRelative.js.map +1 -0
- package/dist/time/formatDistanceToNow.d.ts +2 -0
- package/dist/time/formatDistanceToNow.d.ts.map +1 -0
- package/dist/time/formatDistanceToNow.js +21 -0
- package/dist/time/formatDistanceToNow.js.map +1 -0
- package/dist/time/useTimer.d.ts +10 -0
- package/dist/time/useTimer.d.ts.map +1 -0
- package/dist/time/useTimer.js +60 -0
- package/dist/time/useTimer.js.map +1 -0
- package/dist/types/NullToOptional.d.ts +6 -0
- package/dist/types/NullToOptional.d.ts.map +1 -0
- package/dist/types/NullToOptional.js +2 -0
- package/dist/types/NullToOptional.js.map +1 -0
- package/dist/types/object.d.ts +24 -0
- package/dist/types/object.d.ts.map +1 -0
- package/dist/types/object.js +3 -0
- package/dist/types/object.js.map +1 -0
- package/dist/types/react.d.ts +2 -0
- package/dist/types/react.d.ts.map +1 -0
- package/dist/types/react.js +2 -0
- package/dist/types/react.js.map +1 -0
- package/dist/types/timer.d.ts +2 -0
- package/dist/types/timer.d.ts.map +1 -0
- package/dist/types/timer.js +2 -0
- package/dist/types/timer.js.map +1 -0
- package/dist/types/tuple.d.ts +2 -0
- package/dist/types/tuple.d.ts.map +1 -0
- package/dist/types/tuple.js +2 -0
- package/dist/types/tuple.js.map +1 -0
- package/dist/url/urlSanitize.d.ts +2 -0
- package/dist/url/urlSanitize.d.ts.map +1 -0
- package/dist/url/urlSanitize.js +21 -0
- package/dist/url/urlSanitize.js.map +1 -0
- package/dist/url/urlValidate.d.ts +2 -0
- package/dist/url/urlValidate.d.ts.map +1 -0
- package/dist/url/urlValidate.js +8 -0
- package/dist/url/urlValidate.js.map +1 -0
- package/package.json +30 -4
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { dequal } from 'dequal';
|
|
3
|
+
import * as React from 'react';
|
|
4
|
+
import { use, useLayoutEffect, useState } from 'react';
|
|
5
|
+
import { handleAbortError } from './async/abortable.js';
|
|
6
|
+
import { DEBUG_LEVEL, EMPTY_ARRAY } from './constants.native.js';
|
|
7
|
+
import { AbortError } from './error/errors.js';
|
|
8
|
+
import { globalValue } from './global/globalValue.js';
|
|
9
|
+
import { createGlobalContext } from './react/createGlobalContext.js';
|
|
10
|
+
// keeps a reference to the current value easily
|
|
11
|
+
function useGet(currentValue, initialValue, forwardToFunction) {
|
|
12
|
+
const curRef = React.useRef(initialValue ?? currentValue);
|
|
13
|
+
useLayoutEffect(() => {
|
|
14
|
+
curRef.current = currentValue;
|
|
15
|
+
});
|
|
16
|
+
// oxlint-disable-next-line exhaustive-deps
|
|
17
|
+
return React.useCallback(forwardToFunction
|
|
18
|
+
? (...args) => curRef.current?.apply(null, args)
|
|
19
|
+
: () => curRef.current, [curRef, forwardToFunction]);
|
|
20
|
+
}
|
|
21
|
+
export class Emitter {
|
|
22
|
+
disposables = new Set();
|
|
23
|
+
value;
|
|
24
|
+
options;
|
|
25
|
+
constructor(value, options) {
|
|
26
|
+
this.value = value;
|
|
27
|
+
this.options = options;
|
|
28
|
+
}
|
|
29
|
+
listen = (disposable) => {
|
|
30
|
+
this.disposables.add(disposable);
|
|
31
|
+
return () => {
|
|
32
|
+
this.disposables.delete(disposable);
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
emit = (next) => {
|
|
36
|
+
if (process.env.NODE_ENV === 'development') {
|
|
37
|
+
setCache(this, next);
|
|
38
|
+
}
|
|
39
|
+
const compare = this.options?.comparator;
|
|
40
|
+
if (compare) {
|
|
41
|
+
if (this.value && compare(this.value, next)) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
if (this.value === next) {
|
|
47
|
+
if (process.env.NODE_ENV === 'development') {
|
|
48
|
+
console.warn(`[emitter] ${this.options?.name} no comparator option but received same value!
|
|
49
|
+
|
|
50
|
+
this will emit the same value again, which can be desirable, but we warn to ensure it's not unintended:
|
|
51
|
+
|
|
52
|
+
- if you want this behavior, add { comparator: isEqualNever }
|
|
53
|
+
- if you want only non-equal values: { comparator: isEqualIdentity }
|
|
54
|
+
- if you want only deeply non-equal values: { comparator: isEqualDeep }`);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
this.value = next;
|
|
59
|
+
if (DEBUG_LEVEL > 1) {
|
|
60
|
+
if (!this.options?.silent) {
|
|
61
|
+
const name = this.options?.name;
|
|
62
|
+
console.groupCollapsed(`📣 ${name}`);
|
|
63
|
+
console.info(next);
|
|
64
|
+
console.trace(`trace >`);
|
|
65
|
+
console.groupEnd();
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
this.disposables.forEach((cb) => cb(next));
|
|
69
|
+
};
|
|
70
|
+
nextValue = () => {
|
|
71
|
+
return new Promise((res) => {
|
|
72
|
+
const dispose = this.listen((val) => {
|
|
73
|
+
dispose();
|
|
74
|
+
res(val);
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
// just createEmitter but ensures it doesn't mess up on HMR
|
|
80
|
+
export function createGlobalEmitter(name, defaultValue, options) {
|
|
81
|
+
return globalValue(name, () => createEmitter(name, defaultValue, options));
|
|
82
|
+
}
|
|
83
|
+
export function createEmitter(name, defaultValue, options) {
|
|
84
|
+
const existing = createOrUpdateCache(name, defaultValue);
|
|
85
|
+
return new Emitter(existing || defaultValue, { name, ...options });
|
|
86
|
+
}
|
|
87
|
+
export const useEmitter = (emitter, cb, args) => {
|
|
88
|
+
const getCallback = useGet(cb);
|
|
89
|
+
useLayoutEffect(() => {
|
|
90
|
+
return emitter.listen((val) => {
|
|
91
|
+
try {
|
|
92
|
+
getCallback()(val);
|
|
93
|
+
}
|
|
94
|
+
catch (err) {
|
|
95
|
+
handleAbortError(err);
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
}, [emitter, getCallback]);
|
|
99
|
+
};
|
|
100
|
+
// i think this was useSyncExternalStore but removed for concurrent rendering improvements
|
|
101
|
+
// wondering if we could just always return a deferred value? or default to it?
|
|
102
|
+
export const useEmitterValue = (emitter, options) => {
|
|
103
|
+
const disabled = options?.disable;
|
|
104
|
+
// use a function initializer to get current emitter value
|
|
105
|
+
const [state, setState] = useState(() => emitter.value);
|
|
106
|
+
useLayoutEffect(() => {
|
|
107
|
+
if (disabled)
|
|
108
|
+
return;
|
|
109
|
+
// sync immediately in case emitter changed between render and effect
|
|
110
|
+
if (emitter.value !== state) {
|
|
111
|
+
setState(emitter.value);
|
|
112
|
+
}
|
|
113
|
+
return emitter.listen(setState);
|
|
114
|
+
// intentionally omit state from deps - we only want to re-subscribe when emitter changes.
|
|
115
|
+
// including state caused unsubscribe/resubscribe churn on every change, which under a
|
|
116
|
+
// rapidly-firing emitter (better-auth session refetches during ios oauth handoff)
|
|
117
|
+
// cascades into "Maximum update depth" via consumers that depend on the returned ref.
|
|
118
|
+
// oxlint-disable-next-line exhaustive-deps
|
|
119
|
+
}, [disabled, emitter]);
|
|
120
|
+
return state;
|
|
121
|
+
};
|
|
122
|
+
/**
|
|
123
|
+
* By default selectors run every render, as well as when emitters update. This is a change
|
|
124
|
+
* from the previous behavior where they only ran when emitters changed value.
|
|
125
|
+
*
|
|
126
|
+
* The reason for this is because emitters capture the variables in scope of the component
|
|
127
|
+
* each render already by using "useGet" by default, which makes them easier to use - you
|
|
128
|
+
* don't need to pass an args[] array except for edge cases.
|
|
129
|
+
*
|
|
130
|
+
* Before explaining why we switched to the default, understand the different uses:
|
|
131
|
+
*
|
|
132
|
+
* - Default behavior - selector is updated every render, and ran every render, as well as
|
|
133
|
+
* when emitter value changes, so you basically are always up to date.
|
|
134
|
+
*
|
|
135
|
+
* - Set an args[] array as the fourth argument - this will stop the automatic capturing
|
|
136
|
+
* and instead update selector only when you change args[] yourself. This is good for when you
|
|
137
|
+
* want explicit control over re-selection and rendering.
|
|
138
|
+
*
|
|
139
|
+
* - With { lazy: true }, the selector only runs when the emitter value changes. If used with
|
|
140
|
+
* args[], you capture the context of the selector based on args[], if not, it's based on the
|
|
141
|
+
* current render.
|
|
142
|
+
*
|
|
143
|
+
* I made this change when we had 16 usages of useEmitterSelector and 100% of them are doing very
|
|
144
|
+
* cheap calculations, so this feels like the right pattern. For the rare case of a heavy selector,
|
|
145
|
+
* you have the option to control it.
|
|
146
|
+
*
|
|
147
|
+
*/
|
|
148
|
+
export const useEmitterSelector = (emitter, selector, options, args = EMPTY_ARRAY) => {
|
|
149
|
+
const [state, setState] = useState(() => selector(emitter.value));
|
|
150
|
+
const disabled = options?.disable;
|
|
151
|
+
const getSelector = useGet(selector);
|
|
152
|
+
if (options?.lazy !== true) {
|
|
153
|
+
const next = selector(emitter.value);
|
|
154
|
+
if (next !== state) {
|
|
155
|
+
setState(next);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
const prevArgsRef = React.useRef(null);
|
|
159
|
+
const argsCountRef = React.useRef(0);
|
|
160
|
+
if (prevArgsRef.current === null ||
|
|
161
|
+
prevArgsRef.current.length !== args.length ||
|
|
162
|
+
prevArgsRef.current.some((a, i) => !Object.is(a, args[i]))) {
|
|
163
|
+
prevArgsRef.current = [...args];
|
|
164
|
+
argsCountRef.current++;
|
|
165
|
+
}
|
|
166
|
+
useLayoutEffect(() => {
|
|
167
|
+
if (disabled)
|
|
168
|
+
return;
|
|
169
|
+
return emitter.listen((val) => {
|
|
170
|
+
try {
|
|
171
|
+
const selectorFn = args !== EMPTY_ARRAY ? selector : getSelector();
|
|
172
|
+
const next = selectorFn(val);
|
|
173
|
+
setState(next);
|
|
174
|
+
}
|
|
175
|
+
catch (error) {
|
|
176
|
+
if (error instanceof AbortError) {
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
throw error;
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
// oxlint-disable-next-line exhaustive-deps
|
|
183
|
+
}, [disabled, emitter, getSelector, argsCountRef.current]);
|
|
184
|
+
return state;
|
|
185
|
+
};
|
|
186
|
+
export const useEmittersSelector = (emitters, selector, options) => {
|
|
187
|
+
const getSelector = useGet(selector);
|
|
188
|
+
const disabled = options?.disable;
|
|
189
|
+
const [state, setState] = useState(() => {
|
|
190
|
+
const values = emitters.map((e) => e.value);
|
|
191
|
+
return getSelector()(values);
|
|
192
|
+
});
|
|
193
|
+
const prevEmittersRef = React.useRef(null);
|
|
194
|
+
const emittersCountRef = React.useRef(0);
|
|
195
|
+
if (prevEmittersRef.current === null ||
|
|
196
|
+
prevEmittersRef.current.length !== emitters.length ||
|
|
197
|
+
prevEmittersRef.current.some((e, i) => !Object.is(e, emitters[i]))) {
|
|
198
|
+
prevEmittersRef.current = [...emitters];
|
|
199
|
+
emittersCountRef.current++;
|
|
200
|
+
}
|
|
201
|
+
useLayoutEffect(() => {
|
|
202
|
+
if (disabled) {
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
const handler = () => {
|
|
206
|
+
const values = emitters.map((e) => e.value);
|
|
207
|
+
try {
|
|
208
|
+
const next = getSelector()(values);
|
|
209
|
+
setState((prev) => {
|
|
210
|
+
if (options?.isEqual?.(prev, next)) {
|
|
211
|
+
return prev;
|
|
212
|
+
}
|
|
213
|
+
if (dequal(prev, next)) {
|
|
214
|
+
return prev;
|
|
215
|
+
}
|
|
216
|
+
return next;
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
catch (error) {
|
|
220
|
+
if (error instanceof AbortError) {
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
throw error;
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
const disposals = emitters.map((emitter) => emitter.listen(handler));
|
|
227
|
+
return () => {
|
|
228
|
+
disposals.forEach((d) => d());
|
|
229
|
+
};
|
|
230
|
+
// oxlint-disable-next-line exhaustive-deps
|
|
231
|
+
}, [disabled, getSelector, emittersCountRef.current]);
|
|
232
|
+
return state;
|
|
233
|
+
};
|
|
234
|
+
export const createUseEmitter = (emitter) => {
|
|
235
|
+
return (cb, args) => useEmitter(emitter, cb, args);
|
|
236
|
+
};
|
|
237
|
+
export const createUseSelector = (emitter) => {
|
|
238
|
+
return (selector, options, args) => {
|
|
239
|
+
return useEmitterSelector(emitter, selector, options, args);
|
|
240
|
+
};
|
|
241
|
+
};
|
|
242
|
+
export function createContextualEmitter(name, defaultValue, defaultOptions) {
|
|
243
|
+
const id = Math.random().toString(36);
|
|
244
|
+
const EmitterContext = createGlobalContext(`contextual-emitter/${id}`, null);
|
|
245
|
+
const useContextEmitter = () => {
|
|
246
|
+
const emitter = use(EmitterContext);
|
|
247
|
+
if (!emitter) {
|
|
248
|
+
throw new Error('useContextEmitter must be used within an EmitterProvider');
|
|
249
|
+
}
|
|
250
|
+
return emitter;
|
|
251
|
+
};
|
|
252
|
+
const ProvideEmitter = (props) => {
|
|
253
|
+
const { children, value, silent } = props;
|
|
254
|
+
const [emitter] = useState(() => new Emitter(value ?? defaultValue, { name, silent, ...defaultOptions }));
|
|
255
|
+
useLayoutEffect(() => {
|
|
256
|
+
if (value !== undefined && value !== emitter.value) {
|
|
257
|
+
emitter.emit(value);
|
|
258
|
+
}
|
|
259
|
+
}, [value, emitter]);
|
|
260
|
+
return _jsx(EmitterContext.Provider, { value: emitter, children: children });
|
|
261
|
+
};
|
|
262
|
+
return [useContextEmitter, ProvideEmitter];
|
|
263
|
+
}
|
|
264
|
+
const HMRCache = process.env.NODE_ENV === 'development'
|
|
265
|
+
? new Map()
|
|
266
|
+
: null;
|
|
267
|
+
function setCache(emitter, value) {
|
|
268
|
+
const name = emitter.options?.name;
|
|
269
|
+
if (!name)
|
|
270
|
+
return;
|
|
271
|
+
const cache = HMRCache?.get(name);
|
|
272
|
+
if (!cache)
|
|
273
|
+
return;
|
|
274
|
+
cache.currentValue = value;
|
|
275
|
+
}
|
|
276
|
+
function createOrUpdateCache(name, defaultValueProp) {
|
|
277
|
+
const existing = HMRCache?.get(name);
|
|
278
|
+
const defaultValue = dequal(existing?.originalDefaultValue, defaultValueProp)
|
|
279
|
+
? existing?.currentValue
|
|
280
|
+
: defaultValueProp;
|
|
281
|
+
if (!existing) {
|
|
282
|
+
HMRCache?.set(name, {
|
|
283
|
+
originalDefaultValue: defaultValueProp,
|
|
284
|
+
currentValue: defaultValue,
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
return defaultValue;
|
|
288
|
+
}
|
|
289
|
+
//# sourceMappingURL=emitter.js.map
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare function ensureExists<T>(value: T | undefined | null, msg?: string): asserts value is T;
|
|
2
|
+
export declare function ensure<T>(value: T, msg?: string): asserts value is Exclude<T, null | undefined | false>;
|
|
3
|
+
export declare function ensureError(val: unknown): asserts val is Error;
|
|
4
|
+
export declare function ensureString(val: unknown, name?: string): asserts val is string;
|
|
5
|
+
//# sourceMappingURL=ensure.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ensure.d.ts","sourceRoot":"","sources":["../../src/ensure/ensure.ts"],"names":[],"mappings":"AAEA,wBAAgB,YAAY,CAAC,CAAC,EAC5B,KAAK,EAAE,CAAC,GAAG,SAAS,GAAG,IAAI,EAC3B,GAAG,SAAK,GACP,OAAO,CAAC,KAAK,IAAI,CAAC,CAIpB;AAED,wBAAgB,MAAM,CAAC,CAAC,EACtB,KAAK,EAAE,CAAC,EACR,GAAG,SAAK,GACP,OAAO,CAAC,KAAK,IAAI,OAAO,CAAC,CAAC,EAAE,IAAI,GAAG,SAAS,GAAG,KAAK,CAAC,CAIvD;AAED,wBAAgB,WAAW,CAAC,GAAG,EAAE,OAAO,GAAG,OAAO,CAAC,GAAG,IAAI,KAAK,CAI9D;AAED,wBAAgB,YAAY,CAAC,GAAG,EAAE,OAAO,EAAE,IAAI,SAAc,GAAG,OAAO,CAAC,GAAG,IAAI,MAAM,CAIpF"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { EnsureError } from '../error/errors.js';
|
|
2
|
+
export function ensureExists(value, msg = '') {
|
|
3
|
+
if (value === undefined || value === null) {
|
|
4
|
+
throw new EnsureError(`Invalid nullish value (${value}): ${msg}`);
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
export function ensure(value, msg = '') {
|
|
8
|
+
if (!value) {
|
|
9
|
+
throw new EnsureError(`ensure() invalid: (${value}): ${msg} ${new Error().stack}`);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export function ensureError(val) {
|
|
13
|
+
if (!(val instanceof Error)) {
|
|
14
|
+
throw val;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
export function ensureString(val, name = '(unnamed)') {
|
|
18
|
+
if (typeof val !== 'string') {
|
|
19
|
+
throw new EnsureError(`No string ${name}`);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=ensure.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ensure.js","sourceRoot":"","sources":["../../src/ensure/ensure.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAA;AAEhD,MAAM,UAAU,YAAY,CAC1B,KAA2B,EAC3B,GAAG,GAAG,EAAE;IAER,IAAI,KAAK,KAAK,SAAS,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;QAC1C,MAAM,IAAI,WAAW,CAAC,0BAA0B,KAAK,MAAM,GAAG,EAAE,CAAC,CAAA;IACnE,CAAC;AACH,CAAC;AAED,MAAM,UAAU,MAAM,CACpB,KAAQ,EACR,GAAG,GAAG,EAAE;IAER,IAAI,CAAC,KAAK,EAAE,CAAC;QACX,MAAM,IAAI,WAAW,CAAC,sBAAsB,KAAK,MAAM,GAAG,IAAI,IAAI,KAAK,EAAE,CAAC,KAAK,EAAE,CAAC,CAAA;IACpF,CAAC;AACH,CAAC;AAED,MAAM,UAAU,WAAW,CAAC,GAAY;IACtC,IAAI,CAAC,CAAC,GAAG,YAAY,KAAK,CAAC,EAAE,CAAC;QAC5B,MAAM,GAAG,CAAA;IACX,CAAC;AACH,CAAC;AAED,MAAM,UAAU,YAAY,CAAC,GAAY,EAAE,IAAI,GAAG,WAAW;IAC3D,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE,CAAC;QAC5B,MAAM,IAAI,WAAW,CAAC,aAAa,IAAI,EAAE,CAAC,CAAA;IAC5C,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ensureOne.d.ts","sourceRoot":"","sources":["../../src/ensure/ensureOne.ts"],"names":[],"mappings":"AAIA,wBAAgB,SAAS,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,MAAM,EAAE,WAAW,KAAK,CAAC,GAAG,CAAC,CAahF"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// Ensures only one instance ever exists, but replaces it every time it's called
|
|
2
|
+
const Controllers = new Map();
|
|
3
|
+
export function ensureOne(key, factory) {
|
|
4
|
+
const symbolKey = Symbol.for(key);
|
|
5
|
+
const g = globalThis;
|
|
6
|
+
if (g[symbolKey]) {
|
|
7
|
+
Controllers.get(symbolKey)?.abort();
|
|
8
|
+
}
|
|
9
|
+
const controller = new AbortController();
|
|
10
|
+
Controllers.set(symbolKey, controller);
|
|
11
|
+
g[symbolKey] = factory(controller.signal);
|
|
12
|
+
return g[symbolKey];
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=ensureOne.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ensureOne.js","sourceRoot":"","sources":["../../src/ensure/ensureOne.ts"],"names":[],"mappings":"AAAA,gFAAgF;AAEhF,MAAM,WAAW,GAAG,IAAI,GAAG,EAA2B,CAAA;AAEtD,MAAM,UAAU,SAAS,CAAI,GAAW,EAAE,OAAmC;IAC3E,MAAM,SAAS,GAAG,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;IACjC,MAAM,CAAC,GAAG,UAAqC,CAAA;IAE/C,IAAI,CAAC,CAAC,SAAS,CAAC,EAAE,CAAC;QACjB,WAAW,CAAC,GAAG,CAAC,SAAS,CAAC,EAAE,KAAK,EAAE,CAAA;IACrC,CAAC;IAED,MAAM,UAAU,GAAG,IAAI,eAAe,EAAE,CAAA;IACxC,WAAW,CAAC,GAAG,CAAC,SAAS,EAAE,UAAU,CAAC,CAAA;IACtC,CAAC,CAAC,SAAS,CAAC,GAAG,OAAO,CAAC,UAAU,CAAC,MAAM,CAAC,CAAA;IAEzC,OAAO,CAAC,CAAC,SAAS,CAAM,CAAA;AAC1B,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"emptyFn.d.ts","sourceRoot":"","sources":["../../src/function/emptyFn.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,OAAO,QAAO,IAAU,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"emptyFn.js","sourceRoot":"","sources":["../../src/function/emptyFn.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,OAAO,GAAG,GAAS,EAAE,GAAE,CAAC,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"identityFn.d.ts","sourceRoot":"","sources":["../../src/function/identityFn.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,UAAU,GAAI,CAAC,EAAE,MAAM,CAAC,KAAG,CAAS,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"identityFn.js","sourceRoot":"","sources":["../../src/function/identityFn.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,UAAU,GAAG,CAAI,IAAO,EAAK,EAAE,CAAC,IAAI,CAAA"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export interface ThrottleOptions {
|
|
2
|
+
/**
|
|
3
|
+
* Fire immediately on the first call.
|
|
4
|
+
*/
|
|
5
|
+
start?: boolean;
|
|
6
|
+
/**
|
|
7
|
+
* Fire as soon as `wait` has passed.
|
|
8
|
+
*/
|
|
9
|
+
middle?: boolean;
|
|
10
|
+
/**
|
|
11
|
+
* Cancel after the first successful call.
|
|
12
|
+
*/
|
|
13
|
+
once?: boolean;
|
|
14
|
+
}
|
|
15
|
+
interface Throttler<T extends unknown[]> {
|
|
16
|
+
(...args: T): void;
|
|
17
|
+
cancel(): void;
|
|
18
|
+
}
|
|
19
|
+
export declare function throttle<T extends unknown[]>(callback: (...args: T) => unknown, wait?: number, { start, middle, once }?: ThrottleOptions): Throttler<T>;
|
|
20
|
+
export declare function debounce<T extends unknown[]>(callback: (...args: T) => unknown, wait?: number, { start, middle, once }?: ThrottleOptions): Throttler<T>;
|
|
21
|
+
export {};
|
|
22
|
+
//# sourceMappingURL=throttle.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"throttle.d.ts","sourceRoot":"","sources":["../../src/function/throttle.ts"],"names":[],"mappings":"AAIA,MAAM,WAAW,eAAe;IAC9B;;OAEG;IACH,KAAK,CAAC,EAAE,OAAO,CAAA;IACf;;OAEG;IACH,MAAM,CAAC,EAAE,OAAO,CAAA;IAChB;;OAEG;IACH,IAAI,CAAC,EAAE,OAAO,CAAA;CACf;AAED,UAAU,SAAS,CAAC,CAAC,SAAS,OAAO,EAAE;IACrC,CAAC,GAAG,IAAI,EAAE,CAAC,GAAG,IAAI,CAAA;IAClB,MAAM,IAAI,IAAI,CAAA;CACf;AAED,wBAAgB,QAAQ,CAAC,CAAC,SAAS,OAAO,EAAE,EAC1C,QAAQ,EAAE,CAAC,GAAG,IAAI,EAAE,CAAC,KAAK,OAAO,EACjC,IAAI,SAAI,EACR,EAAE,KAAY,EAAE,MAAa,EAAE,IAAY,EAAE,GAAE,eAAoB,GAClE,SAAS,CAAC,CAAC,CAAC,CAmCd;AAED,wBAAgB,QAAQ,CAAC,CAAC,SAAS,OAAO,EAAE,EAC1C,QAAQ,EAAE,CAAC,GAAG,IAAI,EAAE,CAAC,KAAK,OAAO,EACjC,IAAI,SAAI,EACR,EAAE,KAAa,EAAE,MAAc,EAAE,IAAY,EAAE,GAAE,eAAoB,GACpE,SAAS,CAAC,CAAC,CAAC,CAEd"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// via @github/mini-throttle https://raw.githubusercontent.com/github/mini-throttle/refs/heads/main/index.ts
|
|
2
|
+
export function throttle(callback, wait = 0, { start = true, middle = true, once = false } = {}) {
|
|
3
|
+
let innerStart = start;
|
|
4
|
+
let last = 0;
|
|
5
|
+
let timer;
|
|
6
|
+
let cancelled = false;
|
|
7
|
+
function fn(...args) {
|
|
8
|
+
if (cancelled)
|
|
9
|
+
return;
|
|
10
|
+
const delta = Date.now() - last;
|
|
11
|
+
last = Date.now();
|
|
12
|
+
if (start && middle && delta >= wait) {
|
|
13
|
+
innerStart = true;
|
|
14
|
+
}
|
|
15
|
+
if (innerStart) {
|
|
16
|
+
innerStart = false;
|
|
17
|
+
callback.apply(this, args);
|
|
18
|
+
if (once)
|
|
19
|
+
fn.cancel();
|
|
20
|
+
}
|
|
21
|
+
else if ((middle && delta < wait) || !middle) {
|
|
22
|
+
clearTimeout(timer);
|
|
23
|
+
timer = setTimeout(() => {
|
|
24
|
+
last = Date.now();
|
|
25
|
+
callback.apply(this, args);
|
|
26
|
+
if (once)
|
|
27
|
+
fn.cancel();
|
|
28
|
+
}, !middle ? wait : wait - delta);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
fn.cancel = () => {
|
|
32
|
+
clearTimeout(timer);
|
|
33
|
+
cancelled = true;
|
|
34
|
+
};
|
|
35
|
+
return fn;
|
|
36
|
+
}
|
|
37
|
+
export function debounce(callback, wait = 0, { start = false, middle = false, once = false } = {}) {
|
|
38
|
+
return throttle(callback, wait, { start, middle, once });
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=throttle.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"throttle.js","sourceRoot":"","sources":["../../src/function/throttle.ts"],"names":[],"mappings":"AAAA,4GAA4G;AAwB5G,MAAM,UAAU,QAAQ,CACtB,QAAiC,EACjC,IAAI,GAAG,CAAC,EACR,EAAE,KAAK,GAAG,IAAI,EAAE,MAAM,GAAG,IAAI,EAAE,IAAI,GAAG,KAAK,KAAsB,EAAE;IAEnE,IAAI,UAAU,GAAG,KAAK,CAAA;IACtB,IAAI,IAAI,GAAG,CAAC,CAAA;IACZ,IAAI,KAAY,CAAA;IAChB,IAAI,SAAS,GAAG,KAAK,CAAA;IACrB,SAAS,EAAE,CAAgB,GAAG,IAAO;QACnC,IAAI,SAAS;YAAE,OAAM;QACrB,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,EAAE,GAAG,IAAI,CAAA;QAC/B,IAAI,GAAG,IAAI,CAAC,GAAG,EAAE,CAAA;QAEjB,IAAI,KAAK,IAAI,MAAM,IAAI,KAAK,IAAI,IAAI,EAAE,CAAC;YACrC,UAAU,GAAG,IAAI,CAAA;QACnB,CAAC;QAED,IAAI,UAAU,EAAE,CAAC;YACf,UAAU,GAAG,KAAK,CAAA;YAClB,QAAQ,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAA;YAC1B,IAAI,IAAI;gBAAE,EAAE,CAAC,MAAM,EAAE,CAAA;QACvB,CAAC;aAAM,IAAI,CAAC,MAAM,IAAI,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC;YAC/C,YAAY,CAAC,KAAK,CAAC,CAAA;YACnB,KAAK,GAAG,UAAU,CAChB,GAAG,EAAE;gBACH,IAAI,GAAG,IAAI,CAAC,GAAG,EAAE,CAAA;gBACjB,QAAQ,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAA;gBAC1B,IAAI,IAAI;oBAAE,EAAE,CAAC,MAAM,EAAE,CAAA;YACvB,CAAC,EACD,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,GAAG,KAAK,CAC9B,CAAA;QACH,CAAC;IACH,CAAC;IACD,EAAE,CAAC,MAAM,GAAG,GAAG,EAAE;QACf,YAAY,CAAC,KAAK,CAAC,CAAA;QACnB,SAAS,GAAG,IAAI,CAAA;IAClB,CAAC,CAAA;IACD,OAAO,EAAE,CAAA;AACX,CAAC;AAED,MAAM,UAAU,QAAQ,CACtB,QAAiC,EACjC,IAAI,GAAG,CAAC,EACR,EAAE,KAAK,GAAG,KAAK,EAAE,MAAM,GAAG,KAAK,EAAE,IAAI,GAAG,KAAK,KAAsB,EAAE;IAErE,OAAO,QAAQ,CAAC,QAAQ,EAAE,IAAI,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAA;AAC1D,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"globalEffect.d.ts","sourceRoot":"","sources":["../../src/global/globalEffect.ts"],"names":[],"mappings":"AAAA,wBAAgB,YAAY,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,IAAI,GAAG,CAAC,MAAM,IAAI,CAAC,GAAG,IAAI,CAalF"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export function globalEffect(key, factory) {
|
|
2
|
+
const disposeKey = Symbol.for(key);
|
|
3
|
+
const g = globalThis;
|
|
4
|
+
if (g[disposeKey]) {
|
|
5
|
+
g[disposeKey]?.();
|
|
6
|
+
}
|
|
7
|
+
const dispose = factory();
|
|
8
|
+
if (dispose) {
|
|
9
|
+
g[disposeKey] = dispose;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=globalEffect.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"globalEffect.js","sourceRoot":"","sources":["../../src/global/globalEffect.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,YAAY,CAAC,GAAW,EAAE,OAAkC;IAC1E,MAAM,UAAU,GAAG,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;IAClC,MAAM,CAAC,GAAG,UAAwC,CAAA;IAElD,IAAI,CAAC,CAAC,UAAU,CAAC,EAAE,CAAC;QAClB,CAAC,CAAC,UAAU,CAAC,EAAE,EAAE,CAAA;IACnB,CAAC;IAED,MAAM,OAAO,GAAG,OAAO,EAAE,CAAA;IAEzB,IAAI,OAAO,EAAE,CAAC;QACZ,CAAC,CAAC,UAAU,CAAC,GAAG,OAAO,CAAA;IACzB,CAAC;AACH,CAAC"}
|
package/dist/index.native.js
CHANGED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export interface FormatNumberOptions {
|
|
2
|
+
locale?: string;
|
|
3
|
+
maximumFractionDigits?: number;
|
|
4
|
+
minimumFractionDigits?: number;
|
|
5
|
+
forceCompact?: boolean;
|
|
6
|
+
}
|
|
7
|
+
export declare function formatNumber(value: number, options?: FormatNumberOptions): string;
|
|
8
|
+
export declare function abbreviateNumber(value: number): string;
|
|
9
|
+
export declare function formatCount(value: number): string;
|
|
10
|
+
export declare function formatReactionCount(value: number | string): string;
|
|
11
|
+
export declare function formatPhoneNumber(value: string): string;
|
|
12
|
+
//# sourceMappingURL=formatNumber.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"formatNumber.d.ts","sourceRoot":"","sources":["../../src/number/formatNumber.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,mBAAmB;IAClC,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,qBAAqB,CAAC,EAAE,MAAM,CAAA;IAC9B,qBAAqB,CAAC,EAAE,MAAM,CAAA;IAC9B,YAAY,CAAC,EAAE,OAAO,CAAA;CACvB;AAED,wBAAgB,YAAY,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,GAAE,mBAAwB,GAAG,MAAM,CAqBrF;AAED,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAEtD;AAED,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAKjD;AAED,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,CASlE;AAED,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAEvD"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
export function formatNumber(value, options = {}) {
|
|
2
|
+
const { locale = 'en-US', maximumFractionDigits = 1, minimumFractionDigits = 0, forceCompact = false, } = options;
|
|
3
|
+
if (!forceCompact && Math.abs(value) < 1000) {
|
|
4
|
+
return new Intl.NumberFormat(locale, {
|
|
5
|
+
maximumFractionDigits,
|
|
6
|
+
minimumFractionDigits,
|
|
7
|
+
}).format(value);
|
|
8
|
+
}
|
|
9
|
+
return new Intl.NumberFormat(locale, {
|
|
10
|
+
notation: 'compact',
|
|
11
|
+
compactDisplay: 'short',
|
|
12
|
+
maximumFractionDigits,
|
|
13
|
+
minimumFractionDigits,
|
|
14
|
+
}).format(value);
|
|
15
|
+
}
|
|
16
|
+
export function abbreviateNumber(value) {
|
|
17
|
+
return formatNumber(value, { maximumFractionDigits: 1 });
|
|
18
|
+
}
|
|
19
|
+
export function formatCount(value) {
|
|
20
|
+
return formatNumber(value, {
|
|
21
|
+
maximumFractionDigits: 0,
|
|
22
|
+
forceCompact: value >= 1000,
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
export function formatReactionCount(value) {
|
|
26
|
+
if (typeof value === 'string')
|
|
27
|
+
return value;
|
|
28
|
+
if (value >= 1000000) {
|
|
29
|
+
return `${(value / 1000000).toFixed(1)}M`;
|
|
30
|
+
}
|
|
31
|
+
if (value >= 1000) {
|
|
32
|
+
return `${(value / 1000).toFixed(1)}K`;
|
|
33
|
+
}
|
|
34
|
+
return value.toString();
|
|
35
|
+
}
|
|
36
|
+
export function formatPhoneNumber(value) {
|
|
37
|
+
return value;
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=formatNumber.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"formatNumber.js","sourceRoot":"","sources":["../../src/number/formatNumber.ts"],"names":[],"mappings":"AAOA,MAAM,UAAU,YAAY,CAAC,KAAa,EAAE,UAA+B,EAAE;IAC3E,MAAM,EACJ,MAAM,GAAG,OAAO,EAChB,qBAAqB,GAAG,CAAC,EACzB,qBAAqB,GAAG,CAAC,EACzB,YAAY,GAAG,KAAK,GACrB,GAAG,OAAO,CAAA;IAEX,IAAI,CAAC,YAAY,IAAI,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,IAAI,EAAE,CAAC;QAC5C,OAAO,IAAI,IAAI,CAAC,YAAY,CAAC,MAAM,EAAE;YACnC,qBAAqB;YACrB,qBAAqB;SACtB,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;IAClB,CAAC;IAED,OAAO,IAAI,IAAI,CAAC,YAAY,CAAC,MAAM,EAAE;QACnC,QAAQ,EAAE,SAAS;QACnB,cAAc,EAAE,OAAO;QACvB,qBAAqB;QACrB,qBAAqB;KACtB,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;AAClB,CAAC;AAED,MAAM,UAAU,gBAAgB,CAAC,KAAa;IAC5C,OAAO,YAAY,CAAC,KAAK,EAAE,EAAE,qBAAqB,EAAE,CAAC,EAAE,CAAC,CAAA;AAC1D,CAAC;AAED,MAAM,UAAU,WAAW,CAAC,KAAa;IACvC,OAAO,YAAY,CAAC,KAAK,EAAE;QACzB,qBAAqB,EAAE,CAAC;QACxB,YAAY,EAAE,KAAK,IAAI,IAAI;KAC5B,CAAC,CAAA;AACJ,CAAC;AAED,MAAM,UAAU,mBAAmB,CAAC,KAAsB;IACxD,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,KAAK,CAAA;IAC3C,IAAI,KAAK,IAAI,OAAO,EAAE,CAAC;QACrB,OAAO,GAAG,CAAC,KAAK,GAAG,OAAO,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,CAAA;IAC3C,CAAC;IACD,IAAI,KAAK,IAAI,IAAI,EAAE,CAAC;QAClB,OAAO,GAAG,CAAC,KAAK,GAAG,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,CAAA;IACxC,CAAC;IACD,OAAO,KAAK,CAAC,QAAQ,EAAE,CAAA;AACzB,CAAC;AAED,MAAM,UAAU,iBAAiB,CAAC,KAAa;IAC7C,OAAO,KAAK,CAAA;AACd,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decorateObject.d.ts","sourceRoot":"","sources":["../../src/object/decorateObject.ts"],"names":[],"mappings":"AAAA,wBAAgB,cAAc,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAC1D,GAAG,EAAE,CAAC,EACN,SAAS,EAAE,CAAC,EAAE,EAAE,QAAQ,KAAK,QAAQ,GACpC,CAAC,CAYH"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export function decorateObject(obj, decorator) {
|
|
2
|
+
const decorated = {};
|
|
3
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
4
|
+
if (typeof value === 'function') {
|
|
5
|
+
;
|
|
6
|
+
decorated[key] = decorator(value);
|
|
7
|
+
}
|
|
8
|
+
else {
|
|
9
|
+
;
|
|
10
|
+
decorated[key] = value;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
return decorated;
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=decorateObject.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decorateObject.js","sourceRoot":"","sources":["../../src/object/decorateObject.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,cAAc,CAC5B,GAAM,EACN,SAAqC;IAErC,MAAM,SAAS,GAAG,EAAO,CAAA;IAEzB,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC;QAC/C,IAAI,OAAO,KAAK,KAAK,UAAU,EAAE,CAAC;YAChC,CAAC;YAAC,SAAiB,CAAC,GAAG,CAAC,GAAG,SAAS,CAAC,KAAK,CAAC,CAAA;QAC7C,CAAC;aAAM,CAAC;YACN,CAAC;YAAC,SAAiB,CAAC,GAAG,CAAC,GAAG,KAAK,CAAA;QAClC,CAAC;IACH,CAAC;IAED,OAAO,SAAS,CAAA;AAClB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"isEqualDeep.d.ts","sourceRoot":"","sources":["../../src/object/isEqualDeep.ts"],"names":[],"mappings":"AAGA,KAAK,UAAU,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,KAAK,OAAO,CAAA;AAEjD,eAAO,MAAM,WAAW,EAAE,UAAmB,CAAA;AAC7C,eAAO,MAAM,eAAe,EAAE,UAAuB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"isEqualDeep.js","sourceRoot":"","sources":["../../src/object/isEqualDeep.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AAC/B,OAAO,EAAE,MAAM,IAAI,UAAU,EAAE,MAAM,aAAa,CAAA;AAIlD,MAAM,CAAC,MAAM,WAAW,GAAe,MAAM,CAAA;AAC7C,MAAM,CAAC,MAAM,eAAe,GAAe,UAAU,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"isEqualJSON.d.ts","sourceRoot":"","sources":["../../src/object/isEqualJSON.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,WAAW,GAAI,GAAG,GAAG,EAAE,GAAG,GAAG,KAAG,OACJ,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"isEqualJSON.js","sourceRoot":"","sources":["../../src/object/isEqualJSON.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,CAAM,EAAE,CAAM,EAAW,EAAE,CACrD,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mapObject.d.ts","sourceRoot":"","sources":["../../src/object/mapObject.ts"],"names":[],"mappings":"AAAA,wBAAgB,SAAS,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,CAAC,EACxD,GAAG,EAAE,CAAC,EACN,EAAE,EAAE,CAAC,CAAC,SAAS,MAAM,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,KAAK,CAAC,GAChD;KAAG,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC;CAAE,CAUvB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mapObject.js","sourceRoot":"","sources":["../../src/object/mapObject.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,SAAS,CACvB,GAAM,EACN,EAAiD;IAEjD,MAAM,MAAM,GAAG,EAA2B,CAAA;IAE1C,KAAK,MAAM,GAAG,IAAI,GAAG,EAAE,CAAC;QACtB,IAAI,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,GAAG,CAAC,EAAE,CAAC;YAC5B,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,GAAG,CAAC,CAAA;QACjC,CAAC;IACH,CAAC;IAED,OAAO,MAAM,CAAA;AACf,CAAC"}
|