@composy/store-core 0.0.1
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/CHANGELOG.md +5 -0
- package/README.md +172 -0
- package/dist/index.js +1852 -0
- package/dist/index.js.map +1 -0
- package/dist/index.min.js +1082 -0
- package/dist/index.min.js.map +1 -0
- package/es/core/async-action.d.ts +95 -0
- package/es/core/async-action.d.ts.map +1 -0
- package/es/core/async-action.js +116 -0
- package/es/core/async-action.js.map +1 -0
- package/es/core/base-store.d.ts +85 -0
- package/es/core/base-store.d.ts.map +1 -0
- package/es/core/base-store.js +185 -0
- package/es/core/base-store.js.map +1 -0
- package/es/core/batch-manager.d.ts +46 -0
- package/es/core/batch-manager.d.ts.map +1 -0
- package/es/core/batch-manager.js +64 -0
- package/es/core/batch-manager.js.map +1 -0
- package/es/core/create-store.d.ts +66 -0
- package/es/core/create-store.d.ts.map +1 -0
- package/es/core/create-store.js +29 -0
- package/es/core/create-store.js.map +1 -0
- package/es/core/event-bus.d.ts +34 -0
- package/es/core/event-bus.d.ts.map +1 -0
- package/es/core/event-bus.js +71 -0
- package/es/core/event-bus.js.map +1 -0
- package/es/core/index.d.ts +14 -0
- package/es/core/index.d.ts.map +1 -0
- package/es/core/index.js +17 -0
- package/es/core/pub-sub.d.ts +41 -0
- package/es/core/pub-sub.d.ts.map +1 -0
- package/es/core/pub-sub.js +71 -0
- package/es/core/pub-sub.js.map +1 -0
- package/es/engine/index.d.ts +10 -0
- package/es/engine/index.d.ts.map +1 -0
- package/es/engine/index.js +12 -0
- package/es/engine/plugin.d.ts +20 -0
- package/es/engine/plugin.d.ts.map +1 -0
- package/es/engine/plugin.js +153 -0
- package/es/engine/plugin.js.map +1 -0
- package/es/engine/types.d.ts +81 -0
- package/es/engine/types.d.ts.map +1 -0
- package/es/engine/types.js +10 -0
- package/es/errors/index.d.ts +208 -0
- package/es/errors/index.d.ts.map +1 -0
- package/es/errors/index.js +217 -0
- package/es/errors/index.js.map +1 -0
- package/es/index.d.ts +27 -0
- package/es/index.d.ts.map +1 -0
- package/es/index.js +34 -0
- package/es/index.js.map +1 -0
- package/es/middleware/index.d.ts +13 -0
- package/es/middleware/index.d.ts.map +1 -0
- package/es/middleware/index.js +16 -0
- package/es/middleware/logger.d.ts +67 -0
- package/es/middleware/logger.d.ts.map +1 -0
- package/es/middleware/logger.js +136 -0
- package/es/middleware/logger.js.map +1 -0
- package/es/middleware/middleware-manager.d.ts +40 -0
- package/es/middleware/middleware-manager.d.ts.map +1 -0
- package/es/middleware/middleware-manager.js +79 -0
- package/es/middleware/middleware-manager.js.map +1 -0
- package/es/middleware/persist.d.ts +34 -0
- package/es/middleware/persist.d.ts.map +1 -0
- package/es/middleware/persist.js +102 -0
- package/es/middleware/persist.js.map +1 -0
- package/es/middleware/time-travel.d.ts +30 -0
- package/es/middleware/time-travel.d.ts.map +1 -0
- package/es/middleware/time-travel.js +151 -0
- package/es/middleware/time-travel.js.map +1 -0
- package/es/middleware/validation.d.ts +100 -0
- package/es/middleware/validation.d.ts.map +1 -0
- package/es/middleware/validation.js +74 -0
- package/es/middleware/validation.js.map +1 -0
- package/es/plugins/define-plugin.d.ts +74 -0
- package/es/plugins/define-plugin.d.ts.map +1 -0
- package/es/plugins/define-plugin.js +52 -0
- package/es/plugins/define-plugin.js.map +1 -0
- package/es/plugins/index.d.ts +8 -0
- package/es/plugins/index.d.ts.map +1 -0
- package/es/plugins/index.js +13 -0
- package/es/plugins/plugin-manager.d.ts +35 -0
- package/es/plugins/plugin-manager.d.ts.map +1 -0
- package/es/plugins/plugin-manager.js +77 -0
- package/es/plugins/plugin-manager.js.map +1 -0
- package/es/types/index.d.ts +12 -0
- package/es/types/index.d.ts.map +1 -0
- package/es/types/index.js +10 -0
- package/es/types/middleware.d.ts +172 -0
- package/es/types/middleware.d.ts.map +1 -0
- package/es/types/middleware.js +10 -0
- package/es/types/plugin.d.ts +136 -0
- package/es/types/plugin.d.ts.map +1 -0
- package/es/types/plugin.js +10 -0
- package/es/types/store.d.ts +198 -0
- package/es/types/store.d.ts.map +1 -0
- package/es/types/store.js +10 -0
- package/es/types/subscription.d.ts +158 -0
- package/es/types/subscription.d.ts.map +1 -0
- package/es/types/subscription.js +10 -0
- package/es/types/utility.d.ts +312 -0
- package/es/types/utility.d.ts.map +1 -0
- package/es/types/utility.js +10 -0
- package/es/utils/debounce-throttle.d.ts +86 -0
- package/es/utils/debounce-throttle.d.ts.map +1 -0
- package/es/utils/debounce-throttle.js +86 -0
- package/es/utils/debounce-throttle.js.map +1 -0
- package/es/utils/deep-clone.d.ts +143 -0
- package/es/utils/deep-clone.d.ts.map +1 -0
- package/es/utils/deep-clone.js +193 -0
- package/es/utils/deep-clone.js.map +1 -0
- package/es/utils/index.d.ts +13 -0
- package/es/utils/index.d.ts.map +1 -0
- package/es/utils/index.js +14 -0
- package/es/utils/selector.d.ts +213 -0
- package/es/utils/selector.d.ts.map +1 -0
- package/es/utils/selector.js +151 -0
- package/es/utils/selector.js.map +1 -0
- package/esm/core/async-action.d.ts +95 -0
- package/esm/core/async-action.js +116 -0
- package/esm/core/async-action.js.map +1 -0
- package/esm/core/base-store.d.ts +85 -0
- package/esm/core/base-store.js +185 -0
- package/esm/core/base-store.js.map +1 -0
- package/esm/core/batch-manager.d.ts +46 -0
- package/esm/core/batch-manager.js +64 -0
- package/esm/core/batch-manager.js.map +1 -0
- package/esm/core/create-store.d.ts +66 -0
- package/esm/core/create-store.js +29 -0
- package/esm/core/create-store.js.map +1 -0
- package/esm/core/event-bus.d.ts +34 -0
- package/esm/core/event-bus.js +71 -0
- package/esm/core/event-bus.js.map +1 -0
- package/esm/core/index.d.ts +14 -0
- package/esm/core/index.js +17 -0
- package/esm/core/pub-sub.d.ts +41 -0
- package/esm/core/pub-sub.js +71 -0
- package/esm/core/pub-sub.js.map +1 -0
- package/esm/engine/index.d.ts +10 -0
- package/esm/engine/index.js +12 -0
- package/esm/engine/plugin.d.ts +20 -0
- package/esm/engine/plugin.js +153 -0
- package/esm/engine/plugin.js.map +1 -0
- package/esm/engine/types.d.ts +81 -0
- package/esm/errors/index.d.ts +208 -0
- package/esm/errors/index.js +217 -0
- package/esm/errors/index.js.map +1 -0
- package/esm/index.d.ts +27 -0
- package/esm/index.js +36 -0
- package/esm/index.js.map +1 -0
- package/esm/middleware/index.d.ts +13 -0
- package/esm/middleware/logger.d.ts +67 -0
- package/esm/middleware/logger.js +119 -0
- package/esm/middleware/logger.js.map +1 -0
- package/esm/middleware/middleware-manager.d.ts +40 -0
- package/esm/middleware/middleware-manager.js +79 -0
- package/esm/middleware/middleware-manager.js.map +1 -0
- package/esm/middleware/persist.d.ts +34 -0
- package/esm/middleware/persist.js +102 -0
- package/esm/middleware/persist.js.map +1 -0
- package/esm/middleware/time-travel.d.ts +30 -0
- package/esm/middleware/time-travel.js +151 -0
- package/esm/middleware/time-travel.js.map +1 -0
- package/esm/middleware/validation.d.ts +100 -0
- package/esm/middleware/validation.js +74 -0
- package/esm/middleware/validation.js.map +1 -0
- package/esm/plugins/define-plugin.d.ts +74 -0
- package/esm/plugins/define-plugin.js +52 -0
- package/esm/plugins/define-plugin.js.map +1 -0
- package/esm/plugins/index.d.ts +8 -0
- package/esm/plugins/plugin-manager.d.ts +35 -0
- package/esm/plugins/plugin-manager.js +77 -0
- package/esm/plugins/plugin-manager.js.map +1 -0
- package/esm/types/index.d.ts +12 -0
- package/esm/types/middleware.d.ts +172 -0
- package/esm/types/plugin.d.ts +136 -0
- package/esm/types/store.d.ts +198 -0
- package/esm/types/subscription.d.ts +158 -0
- package/esm/types/utility.d.ts +312 -0
- package/esm/utils/debounce-throttle.d.ts +86 -0
- package/esm/utils/debounce-throttle.js +86 -0
- package/esm/utils/debounce-throttle.js.map +1 -0
- package/esm/utils/deep-clone.d.ts +143 -0
- package/esm/utils/deep-clone.js +193 -0
- package/esm/utils/deep-clone.js.map +1 -0
- package/esm/utils/index.d.ts +13 -0
- package/esm/utils/selector.d.ts +213 -0
- package/esm/utils/selector.js +151 -0
- package/esm/utils/selector.js.map +1 -0
- package/lib/core/async-action.cjs +118 -0
- package/lib/core/async-action.cjs.map +1 -0
- package/lib/core/async-action.d.ts +95 -0
- package/lib/core/base-store.cjs +187 -0
- package/lib/core/base-store.cjs.map +1 -0
- package/lib/core/base-store.d.ts +85 -0
- package/lib/core/batch-manager.cjs +69 -0
- package/lib/core/batch-manager.cjs.map +1 -0
- package/lib/core/batch-manager.d.ts +46 -0
- package/lib/core/create-store.cjs +31 -0
- package/lib/core/create-store.cjs.map +1 -0
- package/lib/core/create-store.d.ts +66 -0
- package/lib/core/event-bus.cjs +74 -0
- package/lib/core/event-bus.cjs.map +1 -0
- package/lib/core/event-bus.d.ts +34 -0
- package/lib/core/index.cjs +29 -0
- package/lib/core/index.d.ts +14 -0
- package/lib/core/pub-sub.cjs +74 -0
- package/lib/core/pub-sub.cjs.map +1 -0
- package/lib/core/pub-sub.d.ts +41 -0
- package/lib/engine/index.cjs +16 -0
- package/lib/engine/index.d.ts +10 -0
- package/lib/engine/plugin.cjs +157 -0
- package/lib/engine/plugin.cjs.map +1 -0
- package/lib/engine/plugin.d.ts +20 -0
- package/lib/engine/types.cjs +12 -0
- package/lib/engine/types.d.ts +81 -0
- package/lib/errors/index.cjs +233 -0
- package/lib/errors/index.cjs.map +1 -0
- package/lib/errors/index.d.ts +208 -0
- package/lib/index.cjs +101 -0
- package/lib/index.cjs.map +1 -0
- package/lib/index.d.ts +27 -0
- package/lib/middleware/index.cjs +28 -0
- package/lib/middleware/index.d.ts +13 -0
- package/lib/middleware/logger.cjs +140 -0
- package/lib/middleware/logger.cjs.map +1 -0
- package/lib/middleware/logger.d.ts +67 -0
- package/lib/middleware/middleware-manager.cjs +83 -0
- package/lib/middleware/middleware-manager.cjs.map +1 -0
- package/lib/middleware/middleware-manager.d.ts +40 -0
- package/lib/middleware/persist.cjs +105 -0
- package/lib/middleware/persist.cjs.map +1 -0
- package/lib/middleware/persist.d.ts +34 -0
- package/lib/middleware/time-travel.cjs +154 -0
- package/lib/middleware/time-travel.cjs.map +1 -0
- package/lib/middleware/time-travel.d.ts +30 -0
- package/lib/middleware/validation.cjs +76 -0
- package/lib/middleware/validation.cjs.map +1 -0
- package/lib/middleware/validation.d.ts +100 -0
- package/lib/package.json +3 -0
- package/lib/plugins/define-plugin.cjs +56 -0
- package/lib/plugins/define-plugin.cjs.map +1 -0
- package/lib/plugins/define-plugin.d.ts +74 -0
- package/lib/plugins/index.cjs +20 -0
- package/lib/plugins/index.d.ts +8 -0
- package/lib/plugins/plugin-manager.cjs +81 -0
- package/lib/plugins/plugin-manager.cjs.map +1 -0
- package/lib/plugins/plugin-manager.d.ts +35 -0
- package/lib/types/index.cjs +12 -0
- package/lib/types/index.d.ts +12 -0
- package/lib/types/middleware.cjs +12 -0
- package/lib/types/middleware.d.ts +172 -0
- package/lib/types/plugin.cjs +12 -0
- package/lib/types/plugin.d.ts +136 -0
- package/lib/types/store.cjs +12 -0
- package/lib/types/store.d.ts +198 -0
- package/lib/types/subscription.cjs +12 -0
- package/lib/types/subscription.d.ts +158 -0
- package/lib/types/utility.cjs +12 -0
- package/lib/types/utility.d.ts +312 -0
- package/lib/utils/debounce-throttle.cjs +89 -0
- package/lib/utils/debounce-throttle.cjs.map +1 -0
- package/lib/utils/debounce-throttle.d.ts +86 -0
- package/lib/utils/deep-clone.cjs +202 -0
- package/lib/utils/deep-clone.cjs.map +1 -0
- package/lib/utils/deep-clone.d.ts +143 -0
- package/lib/utils/index.cjs +34 -0
- package/lib/utils/index.d.ts +13 -0
- package/lib/utils/selector.cjs +161 -0
- package/lib/utils/selector.cjs.map +1 -0
- package/lib/utils/selector.d.ts +213 -0
- package/package.json +155 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"debounce-throttle.js","names":[],"sources":["../../src/utils/debounce-throttle.ts"],"sourcesContent":["/**\n * 防抖和节流工具函数\n *\n * 提供性能优化的防抖、节流功能\n * @module utils/debounce-throttle\n */\n\n/**\n * 防抖函数返回类型\n */\nexport interface DebouncedFunction<T extends (...args: never[]) => void> {\n (...args: Parameters<T>): void\n /** 取消待执行的调用 */\n cancel: () => void\n /** 立即执行待处理的调用 */\n flush: () => void\n /** 是否有待处理的调用 */\n pending: () => boolean\n}\n\n/**\n * 节流函数返回类型\n */\nexport interface ThrottledFunction<T extends (...args: never[]) => void> {\n (...args: Parameters<T>): void\n /** 取消待执行的调用 */\n cancel: () => void\n}\n\n/**\n * 创建防抖函数\n *\n * 在连续调用停止后,延迟指定时间执行函数\n *\n * @template T - 函数类型\n * @param fn - 需要防抖的函数\n * @param delay - 延迟时间(毫秒)\n * @param immediate - 是否在首次调用时立即执行\n * @returns 防抖后的函数\n *\n * @example\n * ```typescript\n * const debouncedSearch = debounce((query: string) => {\n * fetchResults(query)\n * }, 300)\n *\n * // 连续调用只会在最后一次调用后 300ms 执行\n * debouncedSearch('a')\n * debouncedSearch('ab')\n * debouncedSearch('abc') // 只有这次会触发\n *\n * // 取消待执行的调用\n * debouncedSearch.cancel()\n * ```\n */\nexport function debounce<T extends (...args: never[]) => void>(\n fn: T,\n delay: number,\n immediate = false,\n): DebouncedFunction<T> {\n let timer: ReturnType<typeof setTimeout> | null = null\n let lastArgs: Parameters<T> | null = null\n\n const debounced = ((...args: Parameters<T>) => {\n lastArgs = args\n\n if (timer) {\n clearTimeout(timer)\n }\n\n if (immediate && !timer) {\n fn(...args)\n timer = setTimeout(() => {\n timer = null\n lastArgs = null\n }, delay)\n }\n else {\n timer = setTimeout(() => {\n fn(...args)\n timer = null\n lastArgs = null\n }, delay)\n }\n }) as DebouncedFunction<T>\n\n debounced.cancel = () => {\n if (timer) {\n clearTimeout(timer)\n timer = null\n lastArgs = null\n }\n }\n\n debounced.flush = () => {\n if (timer && lastArgs) {\n clearTimeout(timer)\n fn(...lastArgs)\n timer = null\n lastArgs = null\n }\n }\n\n debounced.pending = () => timer !== null\n\n return debounced\n}\n\n/**\n * 创建节流函数\n *\n * 在指定时间间隔内最多执行一次函数\n *\n * @template T - 函数类型\n * @param fn - 需要节流的函数\n * @param interval - 间隔时间(毫秒)\n * @param options - 节流选项\n * @param options.leading - 是否在时间窗开始时立即执行\n * @param options.trailing - 是否在时间窗结束时补执行最后一次调用\n * @returns 节流后的函数\n *\n * @example\n * ```typescript\n * const throttledUpdate = throttle((x: number, y: number) => {\n * updatePosition(x, y)\n * }, 100)\n *\n * // 在 100ms 内多次调用,只会执行一次\n * window.addEventListener('mousemove', (e) => {\n * throttledUpdate(e.clientX, e.clientY)\n * })\n *\n * // 取消\n * throttledUpdate.cancel()\n * ```\n */\nexport function throttle<T extends (...args: never[]) => void>(\n fn: T,\n interval: number,\n options: { leading?: boolean, trailing?: boolean } = {},\n): ThrottledFunction<T> {\n const { leading = true, trailing = true } = options\n let timer: ReturnType<typeof setTimeout> | null = null\n let lastTime = 0\n let lastArgs: Parameters<T> | null = null\n\n const throttled = ((...args: Parameters<T>) => {\n const now = Date.now()\n const remaining = interval - (now - lastTime)\n\n lastArgs = args\n\n if (remaining <= 0 || remaining > interval) {\n if (timer) {\n clearTimeout(timer)\n timer = null\n }\n lastTime = now\n if (leading || lastTime !== 0) {\n fn(...args)\n }\n lastArgs = null\n }\n else if (!timer && trailing) {\n timer = setTimeout(() => {\n lastTime = trailing ? Date.now() : 0\n timer = null\n if (lastArgs) {\n fn(...lastArgs)\n lastArgs = null\n }\n }, remaining)\n }\n }) as ThrottledFunction<T>\n\n throttled.cancel = () => {\n if (timer) {\n clearTimeout(timer)\n timer = null\n }\n lastTime = 0\n lastArgs = null\n }\n\n return throttled\n}\n"],"mappings":";;;;;;;;;;AAuDO,SAAS,SACd,IACA,OACA,YAAY,OACU;CACtB,IAAI,QAA8C;CAClD,IAAI,WAAiC;CAErC,MAAM,cAAa,GAAI,SAAwB;EAC7C,WAAW;EAEX,IAAI,OACF,aAAa,KAAK;EAGpB,IAAI,aAAa,CAAC,OAAO;GACvB,GAAG,GAAG,IAAI;GACV,QAAQ,iBAAiB;IACvB,QAAQ;I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-clone
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* 判断是否为对象
|
|
9
|
+
*
|
|
10
|
+
* @param value - 待检查的值
|
|
11
|
+
* @returns 是否为对象
|
|
12
|
+
*/
|
|
13
|
+
export declare function isObject(value: unknown): value is Record<string, unknown>;
|
|
14
|
+
/**
|
|
15
|
+
* 判断是否为普通对象(非数组、非内置对象)
|
|
16
|
+
*
|
|
17
|
+
* @param value - 待检查的值
|
|
18
|
+
* @returns 是否为普通对象
|
|
19
|
+
*/
|
|
20
|
+
export declare function isPlainObject(value: unknown): value is Record<string, unknown>;
|
|
21
|
+
/**
|
|
22
|
+
* 深拷贝配置选项
|
|
23
|
+
*/
|
|
24
|
+
export interface DeepCloneOptions {
|
|
25
|
+
/** 是否优先使用 structuredClone(默认 true) */
|
|
26
|
+
useStructuredClone?: boolean;
|
|
27
|
+
/** 是否克隆函数(默认 false,函数保持引用) */
|
|
28
|
+
cloneFunctions?: boolean;
|
|
29
|
+
/** 是否克隆 Symbol 属性(默认 false) */
|
|
30
|
+
cloneSymbols?: boolean;
|
|
31
|
+
/** 最大深度(默认无限制) */
|
|
32
|
+
maxDepth?: number;
|
|
33
|
+
/** 自定义克隆器 */
|
|
34
|
+
customCloner?: <T>(value: T, clone: <U>(v: U) => U) => T | undefined;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* 深拷贝
|
|
38
|
+
*
|
|
39
|
+
* 高性能深拷贝实现,优先使用原生 structuredClone(如果可用)
|
|
40
|
+
*
|
|
41
|
+
* @template T - 值类型
|
|
42
|
+
* @param value - 要拷贝的值
|
|
43
|
+
* @param options - 配置选项
|
|
44
|
+
* @returns 拷贝后的值
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```typescript
|
|
48
|
+
* const obj = { a: 1, b: { c: 2 } }
|
|
49
|
+
* const cloned = deepClone(obj)
|
|
50
|
+
* cloned.b.c = 3
|
|
51
|
+
* console.log(obj.b.c) // 2
|
|
52
|
+
* ```
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```typescript
|
|
56
|
+
* // 带配置的深拷贝
|
|
57
|
+
* const cloned = deepClone(obj, {
|
|
58
|
+
* maxDepth: 5,
|
|
59
|
+
* cloneSymbols: true
|
|
60
|
+
* })
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
export declare function deepClone<T>(value: T, options?: DeepCloneOptions): T;
|
|
64
|
+
/**
|
|
65
|
+
* 浅拷贝
|
|
66
|
+
*
|
|
67
|
+
* @template T - 值类型
|
|
68
|
+
* @param value - 要拷贝的值
|
|
69
|
+
* @returns 拷贝后的值
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```typescript
|
|
73
|
+
* const arr = [1, 2, { a: 3 }]
|
|
74
|
+
* const cloned = shallowClone(arr)
|
|
75
|
+
* cloned[2].a = 4
|
|
76
|
+
* console.log(arr[2].a) // 4 (浅拷贝,嵌套对象共享引用)
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
export declare function shallowClone<T>(value: T): T;
|
|
80
|
+
/**
|
|
81
|
+
* 深度合并配置选项
|
|
82
|
+
*/
|
|
83
|
+
export interface DeepMergeOptions {
|
|
84
|
+
/** 数组合并策略:'replace' 替换 | 'concat' 连接 | 'merge' 按索引合并 */
|
|
85
|
+
arrayStrategy?: 'replace' | 'concat' | 'merge';
|
|
86
|
+
/** 是否克隆源对象(默认 true) */
|
|
87
|
+
clone?: boolean;
|
|
88
|
+
/** 最大深度 */
|
|
89
|
+
maxDepth?: number;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* 深度合并对象
|
|
93
|
+
*
|
|
94
|
+
* 将多个源对象深度合并到目标对象
|
|
95
|
+
*
|
|
96
|
+
* @template T - 目标对象类型
|
|
97
|
+
* @param target - 目标对象
|
|
98
|
+
* @param sources - 源对象列表
|
|
99
|
+
* @returns 合并后的对象
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* ```typescript
|
|
103
|
+
* const target = { a: 1, b: { c: 2 } }
|
|
104
|
+
* const source = { b: { d: 3 }, e: 4 }
|
|
105
|
+
* const result = deepMerge(target, source)
|
|
106
|
+
* // { a: 1, b: { c: 2, d: 3 }, e: 4 }
|
|
107
|
+
* ```
|
|
108
|
+
*/
|
|
109
|
+
export declare function deepMerge<T extends Record<string, unknown>>(target: T, ...sources: Array<Partial<T>>): T;
|
|
110
|
+
/**
|
|
111
|
+
* 带配置的深度合并
|
|
112
|
+
*
|
|
113
|
+
* @template T - 目标对象类型
|
|
114
|
+
* @param target - 目标对象
|
|
115
|
+
* @param options - 合并配置
|
|
116
|
+
* @param sources - 源对象列表
|
|
117
|
+
* @returns 合并后的对象
|
|
118
|
+
*/
|
|
119
|
+
export declare function deepMergeWithOptions<T extends Record<string, unknown>>(target: T, options: DeepMergeOptions, ...sources: Array<Partial<T>>): T;
|
|
120
|
+
/**
|
|
121
|
+
* 比较两个值是否深度相等
|
|
122
|
+
*
|
|
123
|
+
* @param a - 第一个值
|
|
124
|
+
* @param b - 第二个值
|
|
125
|
+
* @returns 是否相等
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* ```typescript
|
|
129
|
+
* deepEqual({ a: 1 }, { a: 1 }) // true
|
|
130
|
+
* deepEqual([1, 2], [1, 2]) // true
|
|
131
|
+
* deepEqual({ a: { b: 1 } }, { a: { b: 2 } }) // false
|
|
132
|
+
* ```
|
|
133
|
+
*/
|
|
134
|
+
export declare function deepEqual(a: unknown, b: unknown): boolean;
|
|
135
|
+
/**
|
|
136
|
+
* 冻结对象(深度)
|
|
137
|
+
*
|
|
138
|
+
* @template T - 对象类型
|
|
139
|
+
* @param obj - 要冻结的对象
|
|
140
|
+
* @returns 冻结后的对象
|
|
141
|
+
*/
|
|
142
|
+
export declare function deepFreeze<T extends object>(obj: T): Readonly<T>;
|
|
143
|
+
//# sourceMappingURL=deep-clone.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
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AED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,SAAS,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACzD,MAAM,EAAE,CAAC,EACT,GAAG,OAAO,EAAE,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,GAC5B,CAAC,CAEH;AAED;;;;;;;;GAQG;AACH,wBAAgB,oBAAoB,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACpE,MAAM,EAAE,CAAC,EACT,OAAO,EAAE,gBAAgB,EACzB,GAAG,OAAO,EAAE,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,GAC5B,CAAC,CAwEH;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,OAAO,GAAG,OAAO,CAmEzD;AAED;;;;;;GAMG;AACH,wBAAgB,UAAU,CAAC,CAAC,SAAS,MAAM,EAAE,GAAG,EAAE,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAWhE"}
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* ***********************************
|
|
3
|
+
* @composy/store-core v0.0.1 *
|
|
4
|
+
* Built with rollup *
|
|
5
|
+
* Build time: 2024-08-17 11:27:33 *
|
|
6
|
+
* Build mode: production *
|
|
7
|
+
* Minified: No *
|
|
8
|
+
* ***********************************
|
|
9
|
+
*/
|
|
10
|
+
//#region src/utils/deep-clone.ts
|
|
11
|
+
const hasOwnProperty = Object.prototype.hasOwnProperty;
|
|
12
|
+
function hasOwnKey(value, key) {
|
|
13
|
+
return hasOwnProperty.call(value, key);
|
|
14
|
+
}
|
|
15
|
+
function isObject(value) {
|
|
16
|
+
return value !== null && typeof value === "object";
|
|
17
|
+
}
|
|
18
|
+
function isPlainObject(value) {
|
|
19
|
+
if (!isObject(value)) return false;
|
|
20
|
+
const proto = Object.getPrototypeOf(value);
|
|
21
|
+
return proto === null || proto === Object.prototype;
|
|
22
|
+
}
|
|
23
|
+
function isStructuredCloneable(value) {
|
|
24
|
+
if (value === null || value === void 0) return true;
|
|
25
|
+
if (typeof value === "boolean" || typeof value === "number" || typeof value === "string") return true;
|
|
26
|
+
if (typeof value === "bigint") return true;
|
|
27
|
+
if (typeof value === "function" || typeof value === "symbol") return false;
|
|
28
|
+
if (value instanceof Date || value instanceof RegExp) return true;
|
|
29
|
+
if (value instanceof ArrayBuffer || value instanceof DataView) return true;
|
|
30
|
+
if (value instanceof Map || value instanceof Set) return true;
|
|
31
|
+
if (ArrayBuffer.isView(value)) return true;
|
|
32
|
+
if (Array.isArray(value)) return value.every((item) => isStructuredCloneable(item));
|
|
33
|
+
if (isPlainObject(value)) return Object.values(value).every((v) => isStructuredCloneable(v));
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
function deepClone(value, options = {}) {
|
|
37
|
+
const { useStructuredClone: preferStructuredClone = true, cloneFunctions = false, cloneSymbols = false, maxDepth = Infinity, customCloner } = options;
|
|
38
|
+
if (preferStructuredClone && typeof structuredClone === "function" && isStructuredCloneable(value)) try {
|
|
39
|
+
return structuredClone(value);
|
|
40
|
+
} catch {}
|
|
41
|
+
const cache = /* @__PURE__ */ new WeakMap();
|
|
42
|
+
function cloneInternal(val, depth) {
|
|
43
|
+
if (val === null || val === void 0) return val;
|
|
44
|
+
if (typeof val === "boolean" || typeof val === "number" || typeof val === "string") return val;
|
|
45
|
+
if (typeof val === "bigint") return val;
|
|
46
|
+
if (typeof val === "symbol") return cloneSymbols ? Symbol(val.description) : val;
|
|
47
|
+
if (typeof val === "function") return cloneFunctions ? val.bind({}) : val;
|
|
48
|
+
if (depth > maxDepth) return val;
|
|
49
|
+
if (customCloner) {
|
|
50
|
+
const customResult = customCloner(val, (v) => cloneInternal(v, depth + 1));
|
|
51
|
+
if (customResult !== void 0) return customResult;
|
|
52
|
+
}
|
|
53
|
+
if (typeof val === "object" && val !== null) {
|
|
54
|
+
if (cache.has(val)) return cache.get(val);
|
|
55
|
+
}
|
|
56
|
+
if (val instanceof Date) return new Date(val.getTime());
|
|
57
|
+
if (val instanceof RegExp) {
|
|
58
|
+
const result = new RegExp(val.source, val.flags);
|
|
59
|
+
result.lastIndex = val.lastIndex;
|
|
60
|
+
return result;
|
|
61
|
+
}
|
|
62
|
+
if (val instanceof Error) {
|
|
63
|
+
const result = new val.constructor(val.message);
|
|
64
|
+
result.stack = val.stack;
|
|
65
|
+
result.name = val.name;
|
|
66
|
+
return result;
|
|
67
|
+
}
|
|
68
|
+
if (val instanceof Map) {
|
|
69
|
+
const result = /* @__PURE__ */ new Map();
|
|
70
|
+
cache.set(val, result);
|
|
71
|
+
val.forEach((v, k) => {
|
|
72
|
+
result.set(cloneInternal(k, depth + 1), cloneInternal(v, depth + 1));
|
|
73
|
+
});
|
|
74
|
+
return result;
|
|
75
|
+
}
|
|
76
|
+
if (val instanceof Set) {
|
|
77
|
+
const result = /* @__PURE__ */ new Set();
|
|
78
|
+
cache.set(val, result);
|
|
79
|
+
val.forEach((v) => {
|
|
80
|
+
result.add(cloneInternal(v, depth + 1));
|
|
81
|
+
});
|
|
82
|
+
return result;
|
|
83
|
+
}
|
|
84
|
+
if (val instanceof ArrayBuffer) return val.slice(0);
|
|
85
|
+
if (ArrayBuffer.isView(val) && !(val instanceof DataView)) {
|
|
86
|
+
const TypedArrayConstructor = val.constructor;
|
|
87
|
+
return new TypedArrayConstructor(val.buffer.slice(0));
|
|
88
|
+
}
|
|
89
|
+
if (Array.isArray(val)) {
|
|
90
|
+
const result = [];
|
|
91
|
+
cache.set(val, result);
|
|
92
|
+
for (let i = 0; i < val.length; i++) result[i] = cloneInternal(val[i], depth + 1);
|
|
93
|
+
return result;
|
|
94
|
+
}
|
|
95
|
+
if (isPlainObject(val)) {
|
|
96
|
+
const result = {};
|
|
97
|
+
cache.set(val, result);
|
|
98
|
+
const keys = Object.keys(val);
|
|
99
|
+
for (const key of keys) result[key] = cloneInternal(val[key], depth + 1);
|
|
100
|
+
if (cloneSymbols) {
|
|
101
|
+
const symbolKeys = Object.getOwnPropertySymbols(val);
|
|
102
|
+
for (const sym of symbolKeys) result[sym] = cloneInternal(val[sym], depth + 1);
|
|
103
|
+
}
|
|
104
|
+
return result;
|
|
105
|
+
}
|
|
106
|
+
return val;
|
|
107
|
+
}
|
|
108
|
+
return cloneInternal(value, 0);
|
|
109
|
+
}
|
|
110
|
+
function shallowClone(value) {
|
|
111
|
+
if (value === null || value === void 0) return value;
|
|
112
|
+
if (typeof value !== "object") return value;
|
|
113
|
+
if (Array.isArray(value)) return [...value];
|
|
114
|
+
if (value instanceof Map) return new Map(value);
|
|
115
|
+
if (value instanceof Set) return new Set(value);
|
|
116
|
+
if (value instanceof Date) return new Date(value.getTime());
|
|
117
|
+
return { ...value };
|
|
118
|
+
}
|
|
119
|
+
function deepMerge(target, ...sources) {
|
|
120
|
+
return deepMergeWithOptions(target, {}, ...sources);
|
|
121
|
+
}
|
|
122
|
+
function deepMergeWithOptions(target, options, ...sources) {
|
|
123
|
+
const { arrayStrategy = "replace", clone = true, maxDepth = Infinity } = options;
|
|
124
|
+
function mergeInternal(targetObj, sourceObj, depth) {
|
|
125
|
+
if (depth > maxDepth) return targetObj;
|
|
126
|
+
for (const key of Object.keys(sourceObj)) {
|
|
127
|
+
const sourceValue = sourceObj[key];
|
|
128
|
+
const targetValue = targetObj[key];
|
|
129
|
+
if (Array.isArray(sourceValue)) if (arrayStrategy === "concat" && Array.isArray(targetValue)) targetObj[key] = [...targetValue, ...sourceValue];
|
|
130
|
+
else if (arrayStrategy === "merge" && Array.isArray(targetValue)) {
|
|
131
|
+
const result2 = [...targetValue];
|
|
132
|
+
sourceValue.forEach((item, index) => {
|
|
133
|
+
if (isPlainObject(item) && isPlainObject(result2[index])) result2[index] = mergeInternal(result2[index], item, depth + 1);
|
|
134
|
+
else result2[index] = clone ? deepClone(item) : item;
|
|
135
|
+
});
|
|
136
|
+
targetObj[key] = result2;
|
|
137
|
+
} else targetObj[key] = clone ? deepClone(sourceValue) : sourceValue;
|
|
138
|
+
else if (isPlainObject(sourceValue)) if (isPlainObject(targetValue)) targetObj[key] = mergeInternal(targetValue, sourceValue, depth + 1);
|
|
139
|
+
else targetObj[key] = clone ? deepClone(sourceValue) : sourceValue;
|
|
140
|
+
else targetObj[key] = clone && isObject(sourceValue) ? deepClone(sourceValue) : sourceValue;
|
|
141
|
+
}
|
|
142
|
+
return targetObj;
|
|
143
|
+
}
|
|
144
|
+
let result = target;
|
|
145
|
+
for (const source of sources) if (source !== null && source !== void 0) result = mergeInternal(result, source, 0);
|
|
146
|
+
return result;
|
|
147
|
+
}
|
|
148
|
+
function deepEqual(a, b) {
|
|
149
|
+
if (a === b) return true;
|
|
150
|
+
if (a === null || b === null || a === void 0 || b === void 0) return a === b;
|
|
151
|
+
if (typeof a !== typeof b) return false;
|
|
152
|
+
if (typeof a !== "object") return a === b;
|
|
153
|
+
if (a instanceof Date && b instanceof Date) return a.getTime() === b.getTime();
|
|
154
|
+
if (a instanceof RegExp && b instanceof RegExp) return a.source === b.source && a.flags === b.flags;
|
|
155
|
+
if (a instanceof Map && b instanceof Map) {
|
|
156
|
+
if (a.size !== b.size) return false;
|
|
157
|
+
for (const [key, value] of a) if (!b.has(key) || !deepEqual(value, b.get(key))) return false;
|
|
158
|
+
return true;
|
|
159
|
+
}
|
|
160
|
+
if (a instanceof Set && b instanceof Set) {
|
|
161
|
+
if (a.size !== b.size) return false;
|
|
162
|
+
for (const value of a) if (!b.has(value)) return false;
|
|
163
|
+
return true;
|
|
164
|
+
}
|
|
165
|
+
if (Array.isArray(a) && Array.isArray(b)) {
|
|
166
|
+
if (a.length !== b.length) return false;
|
|
167
|
+
for (let i = 0; i < a.length; i++) if (!deepEqual(a[i], b[i])) return false;
|
|
168
|
+
return true;
|
|
169
|
+
}
|
|
170
|
+
if (isPlainObject(a) && isPlainObject(b)) {
|
|
171
|
+
const keysA = Object.keys(a);
|
|
172
|
+
const keysB = Object.keys(b);
|
|
173
|
+
if (keysA.length !== keysB.length) return false;
|
|
174
|
+
for (const key of keysA) {
|
|
175
|
+
if (!hasOwnKey(b, key)) return false;
|
|
176
|
+
if (!deepEqual(a[key], b[key])) return false;
|
|
177
|
+
}
|
|
178
|
+
return true;
|
|
179
|
+
}
|
|
180
|
+
return false;
|
|
181
|
+
}
|
|
182
|
+
function deepFreeze(obj) {
|
|
183
|
+
const propNames = Object.getOwnPropertyNames(obj);
|
|
184
|
+
for (const name of propNames) {
|
|
185
|
+
const value = obj[name];
|
|
186
|
+
if (value && typeof value === "object") deepFreeze(value);
|
|
187
|
+
}
|
|
188
|
+
return Object.freeze(obj);
|
|
189
|
+
}
|
|
190
|
+
//#endregion
|
|
191
|
+
export { deepClone, deepEqual, deepFreeze, deepMerge, deepMergeWithOptions, isObject, isPlainObject, shallowClone };
|
|
192
|
+
/*! End of @composy/store-core | Powered by @composy/builder */
|
|
193
|
+
//# sourceMappingURL=deep-clone.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"deep-clone.js","names":["result"],"sources":["../../src/utils/deep-clone.ts"],"sourcesContent":["/**\n * 深拷贝工具\n *\n * 提供高性能的深拷贝、浅拷贝和深度合并功能\n * @module utils/deep-clone\n */\n\nconst hasOwnProperty = Object.prototype.hasOwnProperty\n\nfunction hasOwnKey(value: object, key: PropertyKey): boolean {\n return hasOwnProperty.call(value, key)\n}\n\n/**\n * 判断是否为对象\n *\n * @param value - 待检查的值\n * @returns 是否为对象\n */\nexport function isObject(value: unknown): value is Record<string, unknown> {\n return value !== null && typeof value === 'object'\n}\n\n/**\n * 判断是否为普通对象(非数组、非内置对象)\n *\n * @param value - 待检查的值\n * @returns 是否为普通对象\n */\nexport function isPlainObject(value: unknown): value is Record<string, unknown> {\n if (!isObject(value)) return false\n const proto = Object.getPrototypeOf(value)\n return proto === null || proto === Object.prototype\n}\n\n/**\n * 判断值是否可以被 structuredClone 处理\n *\n * @param value - 待检查的值\n * @returns 是否可以使用 structuredClone\n */\nfunction isStructuredCloneable(value: unknown): boolean {\n // 基础类型\n if (value === null || value === undefined) return true\n if (typeof value === 'boolean' || typeof value === 'number' || typeof value === 'string') return true\n if (typeof value === 'bigint') return true\n\n // 函数和 Symbol 不可克隆\n if (typeof value === 'function' || typeof value === 'symbol') return false\n\n // 内置可克隆对象\n if (value instanceof Date || value instanceof RegExp) return true\n if (value instanceof ArrayBuffer || value instanceof DataView) return true\n if (value instanceof Map || value instanceof Set) return true\n if (ArrayBuffer.isView(value)) return true\n\n // 普通对象和数组递归检查\n if (Array.isArray(value)) {\n return value.every(item => isStructuredCloneable(item))\n }\n\n if (isPlainObject(value)) {\n return Object.values(value).every(v => isStructuredCloneable(v))\n }\n\n return false\n}\n\n/**\n * 深拷贝配置选项\n */\nexport interface DeepCloneOptions {\n /** 是否优先使用 structuredClone(默认 true) */\n useStructuredClone?: boolean\n /** 是否克隆函数(默认 false,函数保持引用) */\n cloneFunctions?: boolean\n /** 是否克隆 Symbol 属性(默认 false) */\n cloneSymbols?: boolean\n /** 最大深度(默认无限制) */\n maxDepth?: number\n /** 自定义克隆器 */\n customCloner?: <T>(value: T, clone: <U>(v: U) => U) => T | undefined\n}\n\n/**\n * 深拷贝\n *\n * 高性能深拷贝实现,优先使用原生 structuredClone(如果可用)\n *\n * @template T - 值类型\n * @param value - 要拷贝的值\n * @param options - 配置选项\n * @returns 拷贝后的值\n *\n * @example\n * ```typescript\n * const obj = { a: 1, b: { c: 2 } }\n * const cloned = deepClone(obj)\n * cloned.b.c = 3\n * console.log(obj.b.c) // 2\n * ```\n *\n * @example\n * ```typescript\n * // 带配置的深拷贝\n * const cloned = deepClone(obj, {\n * maxDepth: 5,\n * cloneSymbols: true\n * })\n * ```\n */\nexport function deepClone<T>(value: T, options: DeepCloneOptions = {}): T {\n const {\n useStructuredClone: preferStructuredClone = true,\n cloneFunctions = false,\n cloneSymbols = false,\n maxDepth = Infinity,\n customCloner,\n } = options\n\n // 尝试使用 structuredClone(性能更好)\n if (preferStructuredClone && typeof structuredClone === 'function' && isStructuredCloneable(value)) {\n try {\n return structuredClone(value)\n }\n catch {\n // structuredClone 失败,回退到手动实现\n }\n }\n\n const cache = new WeakMap<object, unknown>()\n\n function cloneInternal<U>(val: U, depth: number): U {\n // 基础类型直接返回\n if (val === null || val === undefined) return val\n if (typeof val === 'boolean' || typeof val === 'number' || typeof val === 'string') return val\n if (typeof val === 'bigint') return val\n\n // Symbol 处理\n if (typeof val === 'symbol') {\n return cloneSymbols ? Symbol(val.description) as U : val\n }\n\n // 函数处理\n if (typeof val === 'function') {\n return cloneFunctions ? (val.bind({}) as U) : val\n }\n\n // 深度检查\n if (depth > maxDepth) {\n return val\n }\n\n // 自定义克隆器\n if (customCloner) {\n const customResult = customCloner(val, v => cloneInternal(v, depth + 1))\n if (customResult !== undefined) {\n return customResult\n }\n }\n\n // 检查循环引用\n if (typeof val === 'object' && val !== null) {\n if (cache.has(val)) {\n return cache.get(val) as U\n }\n }\n\n // 处理日期\n if (val instanceof Date) {\n return new Date(val.getTime()) as U\n }\n\n // 处理正则\n if (val instanceof RegExp) {\n const result = new RegExp(val.source, val.flags)\n ;(result as RegExp).lastIndex = val.lastIndex\n return result as U\n }\n\n // 处理 Error\n if (val instanceof Error) {\n const result = new (val.constructor as ErrorConstructor)(val.message)\n result.stack = val.stack\n result.name = val.name\n return result as U\n }\n\n // 处理 Map\n if (val instanceof Map) {\n const result = new Map()\n cache.set(val, result)\n val.forEach((v, k) => {\n result.set(cloneInternal(k, depth + 1), cloneInternal(v, depth + 1))\n })\n return result as U\n }\n\n // 处理 Set\n if (val instanceof Set) {\n const result = new Set()\n cache.set(val, result)\n val.forEach((v) => {\n result.add(cloneInternal(v, depth + 1))\n })\n return result as U\n }\n\n // 处理 ArrayBuffer\n if (val instanceof ArrayBuffer) {\n return val.slice(0) as U\n }\n\n // 处理 TypedArray\n if (ArrayBuffer.isView(val) && !(val instanceof DataView)) {\n const TypedArrayConstructor = val.constructor as new (buffer: ArrayBuffer) => typeof val\n return new TypedArrayConstructor((val as unknown as { buffer: ArrayBuffer }).buffer.slice(0)) as U\n }\n\n // 处理数组\n if (Array.isArray(val)) {\n const result: unknown[] = []\n cache.set(val, result)\n for (let i = 0; i < val.length; i++) {\n result[i] = cloneInternal(val[i], depth + 1)\n }\n return result as U\n }\n\n // 处理普通对象\n if (isPlainObject(val)) {\n const result: Record<string | symbol, unknown> = {}\n cache.set(val, result)\n\n // 克隆字符串键\n const keys = Object.keys(val)\n for (const key of keys) {\n result[key] = cloneInternal((val as Record<string, unknown>)[key], depth + 1)\n }\n\n // 克隆 Symbol 键\n if (cloneSymbols) {\n const symbolKeys = Object.getOwnPropertySymbols(val)\n for (const sym of symbolKeys) {\n result[sym] = cloneInternal((val as Record<symbol, unknown>)[sym], depth + 1)\n }\n }\n\n return result as U\n }\n\n // 其他对象类型(如自定义类实例)直接返回\n return val\n }\n\n return cloneInternal(value, 0)\n}\n\n/**\n * 浅拷贝\n *\n * @template T - 值类型\n * @param value - 要拷贝的值\n * @returns 拷贝后的值\n *\n * @example\n * ```typescript\n * const arr = [1, 2, { a: 3 }]\n * const cloned = shallowClone(arr)\n * cloned[2].a = 4\n * console.log(arr[2].a) // 4 (浅拷贝,嵌套对象共享引用)\n * ```\n */\nexport function shallowClone<T>(value: T): T {\n if (value === null || value === undefined) return value\n if (typeof value !== 'object') return value\n\n if (Array.isArray(value)) {\n return [...value] as T\n }\n\n if (value instanceof Map) {\n return new Map(value) as T\n }\n\n if (value instanceof Set) {\n return new Set(value) as T\n }\n\n if (value instanceof Date) {\n return new Date(value.getTime()) as T\n }\n\n return { ...value }\n}\n\n/**\n * 深度合并配置选项\n */\nexport interface DeepMergeOptions {\n /** 数组合并策略:'replace' 替换 | 'concat' 连接 | 'merge' 按索引合并 */\n arrayStrategy?: 'replace' | 'concat' | 'merge'\n /** 是否克隆源对象(默认 true) */\n clone?: boolean\n /** 最大深度 */\n maxDepth?: number\n}\n\n/**\n * 深度合并对象\n *\n * 将多个源对象深度合并到目标对象\n *\n * @template T - 目标对象类型\n * @param target - 目标对象\n * @param sources - 源对象列表\n * @returns 合并后的对象\n *\n * @example\n * ```typescript\n * const target = { a: 1, b: { c: 2 } }\n * const source = { b: { d: 3 }, e: 4 }\n * const result = deepMerge(target, source)\n * // { a: 1, b: { c: 2, d: 3 }, e: 4 }\n * ```\n */\nexport function deepMerge<T extends Record<string, unknown>>(\n target: T,\n ...sources: Array<Partial<T>>\n): T {\n return deepMergeWithOptions(target, {}, ...sources)\n}\n\n/**\n * 带配置的深度合并\n *\n * @template T - 目标对象类型\n * @param target - 目标对象\n * @param options - 合并配置\n * @param sources - 源对象列表\n * @returns 合并后的对象\n */\nexport function deepMergeWithOptions<T extends Record<string, unknown>>(\n target: T,\n options: DeepMergeOptions,\n ...sources: Array<Partial<T>>\n): T {\n const {\n arrayStrategy = 'replace',\n clone = true,\n maxDepth = Infinity,\n } = options\n\n function mergeInternal(\n targetObj: Record<string, unknown>,\n sourceObj: Record<string, unknown>,\n depth: number,\n ): Record<string, unknown> {\n if (depth > maxDepth) {\n return targetObj\n }\n\n for (const key of Object.keys(sourceObj)) {\n const sourceValue = sourceObj[key]\n const targetValue = targetObj[key]\n\n // 处理数组\n if (Array.isArray(sourceValue)) {\n if (arrayStrategy === 'concat' && Array.isArray(targetValue)) {\n targetObj[key] = [...targetValue, ...sourceValue]\n }\n else if (arrayStrategy === 'merge' && Array.isArray(targetValue)) {\n const result = [...targetValue]\n sourceValue.forEach((item, index) => {\n if (isPlainObject(item) && isPlainObject(result[index])) {\n result[index] = mergeInternal(result[index] as Record<string, unknown>, item, depth + 1)\n }\n else {\n result[index] = clone ? deepClone(item) : item\n }\n })\n targetObj[key] = result\n }\n else {\n targetObj[key] = clone ? deepClone(sourceValue) : sourceValue\n }\n }\n // 处理普通对象\n else if (isPlainObject(sourceValue)) {\n if (isPlainObject(targetValue)) {\n targetObj[key] = mergeInternal(\n targetValue as Record<string, unknown>,\n sourceValue,\n depth + 1,\n )\n }\n else {\n targetObj[key] = clone ? deepClone(sourceValue) : sourceValue\n }\n }\n // 其他类型直接赋值\n else {\n targetObj[key] = clone && isObject(sourceValue) ? deepClone(sourceValue) : sourceValue\n }\n }\n\n return targetObj\n }\n\n let result = target\n\n for (const source of sources) {\n if (source !== null && source !== undefined) {\n result = mergeInternal(result, source, 0) as T\n }\n }\n\n return result\n}\n\n/**\n * 比较两个值是否深度相等\n *\n * @param a - 第一个值\n * @param b - 第二个值\n * @returns 是否相等\n *\n * @example\n * ```typescript\n * deepEqual({ a: 1 }, { a: 1 }) // true\n * deepEqual([1, 2], [1, 2]) // true\n * deepEqual({ a: { b: 1 } }, { a: { b: 2 } }) // false\n * ```\n */\nexport function deepEqual(a: unknown, b: unknown): boolean {\n // 严格相等\n if (a === b) return true\n\n // null/undefined 检查\n if (a === null || b === null || a === undefined || b === undefined) {\n return a === b\n }\n\n // 类型不同\n if (typeof a !== typeof b) return false\n\n // 基础类型\n if (typeof a !== 'object') return a === b\n\n // 日期比较\n if (a instanceof Date && b instanceof Date) {\n return a.getTime() === b.getTime()\n }\n\n // 正则比较\n if (a instanceof RegExp && b instanceof RegExp) {\n return a.source === b.source && a.flags === b.flags\n }\n\n // Map 比较\n if (a instanceof Map && b instanceof Map) {\n if (a.size !== b.size) return false\n for (const [key, value] of a) {\n if (!b.has(key) || !deepEqual(value, b.get(key))) {\n return false\n }\n }\n return true\n }\n\n // Set 比较\n if (a instanceof Set && b instanceof Set) {\n if (a.size !== b.size) return false\n for (const value of a) {\n if (!b.has(value)) return false\n }\n return true\n }\n\n // 数组比较\n if (Array.isArray(a) && Array.isArray(b)) {\n if (a.length !== b.length) return false\n for (let i = 0; i < a.length; i++) {\n if (!deepEqual(a[i], b[i])) return false\n }\n return true\n }\n\n // 对象比较\n if (isPlainObject(a) && isPlainObject(b)) {\n const keysA = Object.keys(a)\n const keysB = Object.keys(b)\n if (keysA.length !== keysB.length) return false\n for (const key of keysA) {\n if (!hasOwnKey(b, key)) return false\n if (!deepEqual(a[key], b[key])) return false\n }\n return true\n }\n\n return false\n}\n\n/**\n * 冻结对象(深度)\n *\n * @template T - 对象类型\n * @param obj - 要冻结的对象\n * @returns 冻结后的对象\n */\nexport function deepFreeze<T extends object>(obj: T): Readonly<T> {\n const propNames = Object.getOwnPropertyNames(obj)\n\n for (const name of propNames) {\n const value = (obj as Record<string, unknown>)[name]\n if (value && typeof value === 'object') {\n deepFreeze(value)\n }\n }\n\n return Object.freeze(obj)\n}\n\n"],"mappings":";;;;;;;;;;AAOA,MAAM,iBAAiB,OAAO,UAAU;AAExC,SAAS,UAAU,OAAe,KAA2B;CAC3D,OAAO,eAAe,KAAK,OAAO,GAAG;AACvC;AAQO,SAAS,SAAS,OAAkD;CACzE,OAAO,UAAU,QAAQ,OAAO,UAAU;AAC5C;AAQO,SAAS,cAAc,OAAkD;CAC9E,IAAI,CAAC,SAAS,KAAK,GAAG,OAAO;CAC7B,MAAM,QAAQ,OAAO,eAAe,KAAK;CACzC,OAAO,UAAU,QAAQ,UAAU,OAAO;AAC5C;AAQA,SAAS,sBAAsB,OAAyB;CAEtD,IAAI,UAAU,QAAQ,UAAU,KAAA,GAAW,OAAO;CAClD,IAAI,OAAO,UAAU,aAAa,OAAO,UAAU,YAAY,OAAO,UAAU,UAAU,OAAO;CACjG,IAAI,OAAO,UAAU,UAAU,OAAO;CAGtC,IAAI,OAAO,UAAU,cAAc,OAAO,UAAU,UAAU,OAAO;CAGrE,IAAI,iBAAiB,QAAQ,iBAAiB,QAAQ,OAAO;CAC7D,IAAI,iBAAiB,eAAe,iBAAiB,UAAU,OAAO;CACtE,IAAI,iBAAiB,OAAO,iBAAiB,KAAK,OAAO;CACzD,IAAI,YAAY,OAAO,KAAK,GAAG,OAAO;CAGtC,IAAI,MAAM,QAAQ,KAAK,GACrB,OAAO,MAAM,OAAM,SAAQ,sBAAsB,IAAI,CAAC;CAGxD,IAAI,cAAc,KAAK,GACrB,OAAO,OAAO,OAAO,KAAK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xC,IAAI,EAAE,WAAW,EAAE,QAAQ,OAAO;EAClC,KAAA,IAAS,IAAI,GAAG,IAAI,EAAE,QAAQ,KAC5B,IAAI,CAAC,UAAU,EAAE,IAAI,EAAE,EAAE,GAAG,OAAO;EAErC,OAAO;CACT;CAGA,IAAI,cAAc,CAAC,KAAK,cAAc,CAAC,GAAG;EACxC,MAAM,QAAQ,OAAO,KAAK,CAAC;EAC3B,MAAM,QAAQ,OAAO,KAAK,CAAC;EAC3B,IAAI,MAAM,WAAW,MAAM,QAAQ,OAAO;EAC1C,KAAA,MAAW,OAAO,OAAO;GACvB,IAAI,CAAC,UAAU,GAAG,GAAG,GAAG,OAAO;GAC/B,IAAI,CAAC,UAAU,EAAE,MAAM,EAAE,IAAI,GAAG,OAAO;EACzC;EACA,OAAO;CACT;CAEA,OAAO;AACT;AASO,SAAS,WAA6B,KAAqB;CAChE,MAAM,YAAY,OAAO,oBAAoB,GAAG;CAEhD,KAAA,MAAW,QAAQ,WAAW;EAC5B,MAAM,QAAS,IAAgC;EAC/C,IAAI,SAAS,OAAO,UAAU,UAC5B,WAAW,KAAK;CAEpB;CAEA,OAAO,OAAO,OAAO,GAAG;AAC1B"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 工具函数模块
|
|
3
|
+
*
|
|
4
|
+
* 提供深拷贝、选择器、路径操作等工具函数
|
|
5
|
+
* @module utils
|
|
6
|
+
*/
|
|
7
|
+
export { debounce, throttle, } from './debounce-throttle';
|
|
8
|
+
export type { DebouncedFunction, ThrottledFunction, } from './debounce-throttle';
|
|
9
|
+
export { deepClone, deepEqual, deepFreeze, deepMerge, deepMergeWithOptions, isObject, isPlainObject, shallowClone, } from './deep-clone';
|
|
10
|
+
export type { DeepCloneOptions, DeepMergeOptions, } from './deep-clone';
|
|
11
|
+
export { combineSelectors, createCachedSelector, createMemoizedSelector, createSelector, createSelectorWithDeps, deleteByPath, getByPath, hasPath, setByPath, } from './selector';
|
|
12
|
+
export type { MemoizedSelectorOptions, SelectorEqualityFn, } from './selector';
|
|
13
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EACL,QAAQ,EACR,QAAQ,GACT,MAAM,qBAAqB,CAAA;AAE5B,YAAY,EACV,iBAAiB,EACjB,iBAAiB,GAClB,MAAM,qBAAqB,CAAA;AAE5B,OAAO,EACL,SAAS,EACT,SAAS,EACT,UAAU,EACV,SAAS,EACT,oBAAoB,EACpB,QAAQ,EACR,aAAa,EACb,YAAY,GACb,MAAM,cAAc,CAAA;AAErB,YAAY,EACV,gBAAgB,EAChB,gBAAgB,GACjB,MAAM,cAAc,CAAA;AAErB,OAAO,EACL,gBAAgB,EAChB,oBAAoB,EACpB,sBAAsB,EACtB,cAAc,EACd,sBAAsB,EACtB,YAAY,EACZ,SAAS,EACT,OAAO,EACP,SAAS,GACV,MAAM,YAAY,CAAA;AAEnB,YAAY,EACV,uBAAuB,EACvB,kBAAkB,GACnB,MAAM,YAAY,CAAA"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* ***********************************
|
|
3
|
+
* @composy/store-core v0.0.1 *
|
|
4
|
+
* Built with rollup *
|
|
5
|
+
* Build time: 2024-08-17 11:27:33 *
|
|
6
|
+
* Build mode: production *
|
|
7
|
+
* Minified: No *
|
|
8
|
+
* ***********************************
|
|
9
|
+
*/
|
|
10
|
+
import { debounce, throttle } from "./debounce-throttle.js";
|
|
11
|
+
import { deepClone, deepEqual, deepFreeze, deepMerge, deepMergeWithOptions, isObject, isPlainObject, shallowClone } from "./deep-clone.js";
|
|
12
|
+
import { combineSelectors, createCachedSelector, createMemoizedSelector, createSelector, createSelectorWithDeps, deleteByPath, getByPath, hasPath, setByPath } from "./selector.js";
|
|
13
|
+
export { combineSelectors, createCachedSelector, createMemoizedSelector, createSelector, createSelectorWithDeps, debounce, deepClone, deepEqual, deepFreeze, deepMerge, deepMergeWithOptions, deleteByPath, getByPath, hasPath, isObject, isPlainObject, setByPath, shallowClone, throttle };
|
|
14
|
+
/*! End of @composy/store-core | Powered by @composy/builder */
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 状态选择器工具
|
|
3
|
+
*
|
|
4
|
+
* 提供高性能的状态选择器和路径操作工具
|
|
5
|
+
* @module utils/selector
|
|
6
|
+
*/
|
|
7
|
+
import type { Selector, StateTree } from '../types';
|
|
8
|
+
/**
|
|
9
|
+
* 选择器相等性比较函数类型
|
|
10
|
+
*/
|
|
11
|
+
export type SelectorEqualityFn<R> = (a: R, b: R) => boolean;
|
|
12
|
+
/**
|
|
13
|
+
* 记忆化选择器配置选项
|
|
14
|
+
*/
|
|
15
|
+
export interface MemoizedSelectorOptions<R> {
|
|
16
|
+
/** 相等性比较函数 */
|
|
17
|
+
equalityFn?: SelectorEqualityFn<R>;
|
|
18
|
+
/** 最大缓存大小(默认 1) */
|
|
19
|
+
maxSize?: number;
|
|
20
|
+
/** 是否在开发模式下记录缓存命中率 */
|
|
21
|
+
trackHitRate?: boolean;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* 创建记忆化选择器
|
|
25
|
+
*
|
|
26
|
+
* 基本的记忆化选择器,只缓存最后一次结果
|
|
27
|
+
*
|
|
28
|
+
* @template S - 状态类型
|
|
29
|
+
* @template R - 返回值类型
|
|
30
|
+
* @param selector - 选择器函数
|
|
31
|
+
* @param equalityFn - 相等性比较函数(默认使用 Object.is)
|
|
32
|
+
* @returns 记忆化的选择器
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```typescript
|
|
36
|
+
* const selectTodos = createSelector(
|
|
37
|
+
* (state: AppState) => state.todos,
|
|
38
|
+
* )
|
|
39
|
+
*
|
|
40
|
+
* const selectCompletedTodos = createSelector(
|
|
41
|
+
* (state: AppState) => state.todos.filter(t => t.completed),
|
|
42
|
+
* (a, b) => a.length === b.length && a.every((v, i) => v === b[i])
|
|
43
|
+
* )
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
export declare function createSelector<S extends StateTree, R>(selector: Selector<S, R>, equalityFn?: SelectorEqualityFn<R>): Selector<S, R>;
|
|
47
|
+
/**
|
|
48
|
+
* 创建带依赖的选择器
|
|
49
|
+
*
|
|
50
|
+
* 类似 reselect 的实现,支持多个输入选择器
|
|
51
|
+
*
|
|
52
|
+
* @template S - 状态类型
|
|
53
|
+
* @template Deps - 依赖结果类型
|
|
54
|
+
* @template R - 返回值类型
|
|
55
|
+
* @param dependencies - 依赖选择器数组
|
|
56
|
+
* @param combiner - 组合函数
|
|
57
|
+
* @param options - 配置选项
|
|
58
|
+
* @returns 记忆化的选择器
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```typescript
|
|
62
|
+
* const selectVisibleTodos = createSelectorWithDeps(
|
|
63
|
+
* [
|
|
64
|
+
* (state) => state.todos,
|
|
65
|
+
* (state) => state.visibilityFilter,
|
|
66
|
+
* ],
|
|
67
|
+
* (todos, filter) => {
|
|
68
|
+
* switch (filter) {
|
|
69
|
+
* case 'SHOW_COMPLETED': return todos.filter(t => t.completed)
|
|
70
|
+
* case 'SHOW_ACTIVE': return todos.filter(t => !t.completed)
|
|
71
|
+
* default: return todos
|
|
72
|
+
* }
|
|
73
|
+
* }
|
|
74
|
+
* )
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
export declare function createSelectorWithDeps<S extends StateTree, Deps extends unknown[], R>(dependencies: {
|
|
78
|
+
[K in keyof Deps]: Selector<S, Deps[K]>;
|
|
79
|
+
}, combiner: (...args: Deps) => R, options?: MemoizedSelectorOptions<R>): Selector<S, R>;
|
|
80
|
+
/**
|
|
81
|
+
* 组合多个选择器
|
|
82
|
+
*
|
|
83
|
+
* 简单的选择器组合,不带记忆化
|
|
84
|
+
*
|
|
85
|
+
* @template S - 状态类型
|
|
86
|
+
* @template R - 结果类型数组
|
|
87
|
+
* @template C - 组合结果类型
|
|
88
|
+
* @param selectors - 选择器数组
|
|
89
|
+
* @param combiner - 组合函数
|
|
90
|
+
* @returns 组合后的选择器
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```typescript
|
|
94
|
+
* const selectUserAndTodos = combineSelectors(
|
|
95
|
+
* [
|
|
96
|
+
* (state) => state.user,
|
|
97
|
+
* (state) => state.todos,
|
|
98
|
+
* ],
|
|
99
|
+
* (user, todos) => ({ user, todos })
|
|
100
|
+
* )
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
103
|
+
export declare function combineSelectors<S extends StateTree, R extends unknown[], C>(selectors: {
|
|
104
|
+
[K in keyof R]: Selector<S, R[K]>;
|
|
105
|
+
}, combiner: (...results: R) => C): Selector<S, C>;
|
|
106
|
+
/**
|
|
107
|
+
* 创建带高级缓存的选择器
|
|
108
|
+
*
|
|
109
|
+
* 支持多个缓存条目,使用 LRU 策略
|
|
110
|
+
*
|
|
111
|
+
* @template S - 状态类型
|
|
112
|
+
* @template R - 返回值类型
|
|
113
|
+
* @param selector - 选择器函数
|
|
114
|
+
* @param options - 配置选项
|
|
115
|
+
* @returns 带缓存的选择器
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```typescript
|
|
119
|
+
* const selectFilteredItems = createCachedSelector(
|
|
120
|
+
* (state, filter) => state.items.filter(item => item.type === filter),
|
|
121
|
+
* { maxSize: 5 }
|
|
122
|
+
* )
|
|
123
|
+
* ```
|
|
124
|
+
*/
|
|
125
|
+
export declare function createCachedSelector<S extends StateTree, R>(selector: Selector<S, R>, options?: MemoizedSelectorOptions<R>): Selector<S, R> & {
|
|
126
|
+
clearCache: () => void;
|
|
127
|
+
getCacheSize: () => number;
|
|
128
|
+
};
|
|
129
|
+
/**
|
|
130
|
+
* 创建记忆化选择器(别名,同 createSelector)
|
|
131
|
+
*
|
|
132
|
+
* @template S - 状态类型
|
|
133
|
+
* @template R - 返回值类型
|
|
134
|
+
* @param selector - 选择器函数
|
|
135
|
+
* @param options - 配置选项
|
|
136
|
+
* @returns 记忆化的选择器
|
|
137
|
+
*/
|
|
138
|
+
export declare function createMemoizedSelector<S extends StateTree, R>(selector: Selector<S, R>, options?: MemoizedSelectorOptions<R>): Selector<S, R>;
|
|
139
|
+
/**
|
|
140
|
+
* 获取嵌套对象的值
|
|
141
|
+
*
|
|
142
|
+
* 支持点表示法和数组索引
|
|
143
|
+
*
|
|
144
|
+
* @template T - 返回值类型
|
|
145
|
+
* @param obj - 对象
|
|
146
|
+
* @param path - 路径(支持 'a.b.c' 或 'a[0].b')
|
|
147
|
+
* @param defaultValue - 默认值(当路径不存在时返回)
|
|
148
|
+
* @returns 值
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
* ```typescript
|
|
152
|
+
* const value = getByPath({ a: { b: { c: 1 } } }, 'a.b.c')
|
|
153
|
+
* // value: 1
|
|
154
|
+
*
|
|
155
|
+
* const arr = getByPath({ items: [{ name: 'test' }] }, 'items[0].name')
|
|
156
|
+
* // arr: 'test'
|
|
157
|
+
*
|
|
158
|
+
* const missing = getByPath({ a: 1 }, 'b.c', 'default')
|
|
159
|
+
* // missing: 'default'
|
|
160
|
+
* ```
|
|
161
|
+
*/
|
|
162
|
+
export declare function getByPath<T = unknown>(obj: unknown, path: string, defaultValue?: T): T | undefined;
|
|
163
|
+
/**
|
|
164
|
+
* 设置嵌套对象的值
|
|
165
|
+
*
|
|
166
|
+
* 支持自动创建中间路径
|
|
167
|
+
*
|
|
168
|
+
* @param obj - 对象
|
|
169
|
+
* @param path - 路径
|
|
170
|
+
* @param value - 值
|
|
171
|
+
* @returns 是否成功
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* ```typescript
|
|
175
|
+
* const obj = {}
|
|
176
|
+
* setByPath(obj, 'a.b.c', 1)
|
|
177
|
+
* // obj: { a: { b: { c: 1 } } }
|
|
178
|
+
*
|
|
179
|
+
* setByPath(obj, 'items[0].name', 'test')
|
|
180
|
+
* // obj: { a: { b: { c: 1 } }, items: [{ name: 'test' }] }
|
|
181
|
+
* ```
|
|
182
|
+
*/
|
|
183
|
+
export declare function setByPath(obj: Record<string, unknown>, path: string, value: unknown): boolean;
|
|
184
|
+
/**
|
|
185
|
+
* 删除嵌套对象的值
|
|
186
|
+
*
|
|
187
|
+
* @param obj - 对象
|
|
188
|
+
* @param path - 路径
|
|
189
|
+
* @returns 是否成功删除
|
|
190
|
+
*
|
|
191
|
+
* @example
|
|
192
|
+
* ```typescript
|
|
193
|
+
* const obj = { a: { b: { c: 1 } } }
|
|
194
|
+
* deleteByPath(obj, 'a.b.c')
|
|
195
|
+
* // obj: { a: { b: {} } }
|
|
196
|
+
* ```
|
|
197
|
+
*/
|
|
198
|
+
export declare function deleteByPath(obj: Record<string, unknown>, path: string): boolean;
|
|
199
|
+
/**
|
|
200
|
+
* 检查嵌套对象中是否存在指定路径
|
|
201
|
+
*
|
|
202
|
+
* @param obj - 对象
|
|
203
|
+
* @param path - 路径
|
|
204
|
+
* @returns 是否存在
|
|
205
|
+
*
|
|
206
|
+
* @example
|
|
207
|
+
* ```typescript
|
|
208
|
+
* hasPath({ a: { b: 1 } }, 'a.b') // true
|
|
209
|
+
* hasPath({ a: { b: 1 } }, 'a.c') // false
|
|
210
|
+
* ```
|
|
211
|
+
*/
|
|
212
|
+
export declare function hasPath(obj: unknown, path: string): boolean;
|
|
213
|
+
//# sourceMappingURL=selector.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"selector.d.ts","sourceRoot":"","sources":["../../src/utils/selector.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,UAAU,CAAA;AAInD;;GAEG;AACH,MAAM,MAAM,kBAAkB,CAAC,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,EA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