@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,202 @@
|
|
|
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
|
+
"use strict";
|
|
11
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
12
|
+
//#region src/utils/deep-clone.ts
|
|
13
|
+
const hasOwnProperty = Object.prototype.hasOwnProperty;
|
|
14
|
+
function hasOwnKey(value, key) {
|
|
15
|
+
return hasOwnProperty.call(value, key);
|
|
16
|
+
}
|
|
17
|
+
function isObject(value) {
|
|
18
|
+
return value !== null && typeof value === "object";
|
|
19
|
+
}
|
|
20
|
+
function isPlainObject(value) {
|
|
21
|
+
if (!isObject(value)) return false;
|
|
22
|
+
const proto = Object.getPrototypeOf(value);
|
|
23
|
+
return proto === null || proto === Object.prototype;
|
|
24
|
+
}
|
|
25
|
+
function isStructuredCloneable(value) {
|
|
26
|
+
if (value === null || value === void 0) return true;
|
|
27
|
+
if (typeof value === "boolean" || typeof value === "number" || typeof value === "string") return true;
|
|
28
|
+
if (typeof value === "bigint") return true;
|
|
29
|
+
if (typeof value === "function" || typeof value === "symbol") return false;
|
|
30
|
+
if (value instanceof Date || value instanceof RegExp) return true;
|
|
31
|
+
if (value instanceof ArrayBuffer || value instanceof DataView) return true;
|
|
32
|
+
if (value instanceof Map || value instanceof Set) return true;
|
|
33
|
+
if (ArrayBuffer.isView(value)) return true;
|
|
34
|
+
if (Array.isArray(value)) return value.every((item) => isStructuredCloneable(item));
|
|
35
|
+
if (isPlainObject(value)) return Object.values(value).every((v) => isStructuredCloneable(v));
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
function deepClone(value, options = {}) {
|
|
39
|
+
const { useStructuredClone: preferStructuredClone = true, cloneFunctions = false, cloneSymbols = false, maxDepth = Infinity, customCloner } = options;
|
|
40
|
+
if (preferStructuredClone && typeof structuredClone === "function" && isStructuredCloneable(value)) try {
|
|
41
|
+
return structuredClone(value);
|
|
42
|
+
} catch {}
|
|
43
|
+
const cache = /* @__PURE__ */ new WeakMap();
|
|
44
|
+
function cloneInternal(val, depth) {
|
|
45
|
+
if (val === null || val === void 0) return val;
|
|
46
|
+
if (typeof val === "boolean" || typeof val === "number" || typeof val === "string") return val;
|
|
47
|
+
if (typeof val === "bigint") return val;
|
|
48
|
+
if (typeof val === "symbol") return cloneSymbols ? Symbol(val.description) : val;
|
|
49
|
+
if (typeof val === "function") return cloneFunctions ? val.bind({}) : val;
|
|
50
|
+
if (depth > maxDepth) return val;
|
|
51
|
+
if (customCloner) {
|
|
52
|
+
const customResult = customCloner(val, (v) => cloneInternal(v, depth + 1));
|
|
53
|
+
if (customResult !== void 0) return customResult;
|
|
54
|
+
}
|
|
55
|
+
if (typeof val === "object" && val !== null) {
|
|
56
|
+
if (cache.has(val)) return cache.get(val);
|
|
57
|
+
}
|
|
58
|
+
if (val instanceof Date) return new Date(val.getTime());
|
|
59
|
+
if (val instanceof RegExp) {
|
|
60
|
+
const result = new RegExp(val.source, val.flags);
|
|
61
|
+
result.lastIndex = val.lastIndex;
|
|
62
|
+
return result;
|
|
63
|
+
}
|
|
64
|
+
if (val instanceof Error) {
|
|
65
|
+
const result = new val.constructor(val.message);
|
|
66
|
+
result.stack = val.stack;
|
|
67
|
+
result.name = val.name;
|
|
68
|
+
return result;
|
|
69
|
+
}
|
|
70
|
+
if (val instanceof Map) {
|
|
71
|
+
const result = /* @__PURE__ */ new Map();
|
|
72
|
+
cache.set(val, result);
|
|
73
|
+
val.forEach((v, k) => {
|
|
74
|
+
result.set(cloneInternal(k, depth + 1), cloneInternal(v, depth + 1));
|
|
75
|
+
});
|
|
76
|
+
return result;
|
|
77
|
+
}
|
|
78
|
+
if (val instanceof Set) {
|
|
79
|
+
const result = /* @__PURE__ */ new Set();
|
|
80
|
+
cache.set(val, result);
|
|
81
|
+
val.forEach((v) => {
|
|
82
|
+
result.add(cloneInternal(v, depth + 1));
|
|
83
|
+
});
|
|
84
|
+
return result;
|
|
85
|
+
}
|
|
86
|
+
if (val instanceof ArrayBuffer) return val.slice(0);
|
|
87
|
+
if (ArrayBuffer.isView(val) && !(val instanceof DataView)) {
|
|
88
|
+
const TypedArrayConstructor = val.constructor;
|
|
89
|
+
return new TypedArrayConstructor(val.buffer.slice(0));
|
|
90
|
+
}
|
|
91
|
+
if (Array.isArray(val)) {
|
|
92
|
+
const result = [];
|
|
93
|
+
cache.set(val, result);
|
|
94
|
+
for (let i = 0; i < val.length; i++) result[i] = cloneInternal(val[i], depth + 1);
|
|
95
|
+
return result;
|
|
96
|
+
}
|
|
97
|
+
if (isPlainObject(val)) {
|
|
98
|
+
const result = {};
|
|
99
|
+
cache.set(val, result);
|
|
100
|
+
const keys = Object.keys(val);
|
|
101
|
+
for (const key of keys) result[key] = cloneInternal(val[key], depth + 1);
|
|
102
|
+
if (cloneSymbols) {
|
|
103
|
+
const symbolKeys = Object.getOwnPropertySymbols(val);
|
|
104
|
+
for (const sym of symbolKeys) result[sym] = cloneInternal(val[sym], depth + 1);
|
|
105
|
+
}
|
|
106
|
+
return result;
|
|
107
|
+
}
|
|
108
|
+
return val;
|
|
109
|
+
}
|
|
110
|
+
return cloneInternal(value, 0);
|
|
111
|
+
}
|
|
112
|
+
function shallowClone(value) {
|
|
113
|
+
if (value === null || value === void 0) return value;
|
|
114
|
+
if (typeof value !== "object") return value;
|
|
115
|
+
if (Array.isArray(value)) return [...value];
|
|
116
|
+
if (value instanceof Map) return new Map(value);
|
|
117
|
+
if (value instanceof Set) return new Set(value);
|
|
118
|
+
if (value instanceof Date) return new Date(value.getTime());
|
|
119
|
+
return { ...value };
|
|
120
|
+
}
|
|
121
|
+
function deepMerge(target, ...sources) {
|
|
122
|
+
return deepMergeWithOptions(target, {}, ...sources);
|
|
123
|
+
}
|
|
124
|
+
function deepMergeWithOptions(target, options, ...sources) {
|
|
125
|
+
const { arrayStrategy = "replace", clone = true, maxDepth = Infinity } = options;
|
|
126
|
+
function mergeInternal(targetObj, sourceObj, depth) {
|
|
127
|
+
if (depth > maxDepth) return targetObj;
|
|
128
|
+
for (const key of Object.keys(sourceObj)) {
|
|
129
|
+
const sourceValue = sourceObj[key];
|
|
130
|
+
const targetValue = targetObj[key];
|
|
131
|
+
if (Array.isArray(sourceValue)) if (arrayStrategy === "concat" && Array.isArray(targetValue)) targetObj[key] = [...targetValue, ...sourceValue];
|
|
132
|
+
else if (arrayStrategy === "merge" && Array.isArray(targetValue)) {
|
|
133
|
+
const result2 = [...targetValue];
|
|
134
|
+
sourceValue.forEach((item, index) => {
|
|
135
|
+
if (isPlainObject(item) && isPlainObject(result2[index])) result2[index] = mergeInternal(result2[index], item, depth + 1);
|
|
136
|
+
else result2[index] = clone ? deepClone(item) : item;
|
|
137
|
+
});
|
|
138
|
+
targetObj[key] = result2;
|
|
139
|
+
} else targetObj[key] = clone ? deepClone(sourceValue) : sourceValue;
|
|
140
|
+
else if (isPlainObject(sourceValue)) if (isPlainObject(targetValue)) targetObj[key] = mergeInternal(targetValue, sourceValue, depth + 1);
|
|
141
|
+
else targetObj[key] = clone ? deepClone(sourceValue) : sourceValue;
|
|
142
|
+
else targetObj[key] = clone && isObject(sourceValue) ? deepClone(sourceValue) : sourceValue;
|
|
143
|
+
}
|
|
144
|
+
return targetObj;
|
|
145
|
+
}
|
|
146
|
+
let result = target;
|
|
147
|
+
for (const source of sources) if (source !== null && source !== void 0) result = mergeInternal(result, source, 0);
|
|
148
|
+
return result;
|
|
149
|
+
}
|
|
150
|
+
function deepEqual(a, b) {
|
|
151
|
+
if (a === b) return true;
|
|
152
|
+
if (a === null || b === null || a === void 0 || b === void 0) return a === b;
|
|
153
|
+
if (typeof a !== typeof b) return false;
|
|
154
|
+
if (typeof a !== "object") return a === b;
|
|
155
|
+
if (a instanceof Date && b instanceof Date) return a.getTime() === b.getTime();
|
|
156
|
+
if (a instanceof RegExp && b instanceof RegExp) return a.source === b.source && a.flags === b.flags;
|
|
157
|
+
if (a instanceof Map && b instanceof Map) {
|
|
158
|
+
if (a.size !== b.size) return false;
|
|
159
|
+
for (const [key, value] of a) if (!b.has(key) || !deepEqual(value, b.get(key))) return false;
|
|
160
|
+
return true;
|
|
161
|
+
}
|
|
162
|
+
if (a instanceof Set && b instanceof Set) {
|
|
163
|
+
if (a.size !== b.size) return false;
|
|
164
|
+
for (const value of a) if (!b.has(value)) return false;
|
|
165
|
+
return true;
|
|
166
|
+
}
|
|
167
|
+
if (Array.isArray(a) && Array.isArray(b)) {
|
|
168
|
+
if (a.length !== b.length) return false;
|
|
169
|
+
for (let i = 0; i < a.length; i++) if (!deepEqual(a[i], b[i])) return false;
|
|
170
|
+
return true;
|
|
171
|
+
}
|
|
172
|
+
if (isPlainObject(a) && isPlainObject(b)) {
|
|
173
|
+
const keysA = Object.keys(a);
|
|
174
|
+
const keysB = Object.keys(b);
|
|
175
|
+
if (keysA.length !== keysB.length) return false;
|
|
176
|
+
for (const key of keysA) {
|
|
177
|
+
if (!hasOwnKey(b, key)) return false;
|
|
178
|
+
if (!deepEqual(a[key], b[key])) return false;
|
|
179
|
+
}
|
|
180
|
+
return true;
|
|
181
|
+
}
|
|
182
|
+
return false;
|
|
183
|
+
}
|
|
184
|
+
function deepFreeze(obj) {
|
|
185
|
+
const propNames = Object.getOwnPropertyNames(obj);
|
|
186
|
+
for (const name of propNames) {
|
|
187
|
+
const value = obj[name];
|
|
188
|
+
if (value && typeof value === "object") deepFreeze(value);
|
|
189
|
+
}
|
|
190
|
+
return Object.freeze(obj);
|
|
191
|
+
}
|
|
192
|
+
//#endregion
|
|
193
|
+
exports.deepClone = deepClone;
|
|
194
|
+
exports.deepEqual = deepEqual;
|
|
195
|
+
exports.deepFreeze = deepFreeze;
|
|
196
|
+
exports.deepMerge = deepMerge;
|
|
197
|
+
exports.deepMergeWithOptions = deepMergeWithOptions;
|
|
198
|
+
exports.isObject = isObject;
|
|
199
|
+
exports.isPlainObject = isPlainObject;
|
|
200
|
+
exports.shallowClone = shallowClone;
|
|
201
|
+
/*! End of @composy/store-core | Powered by @composy/builder */
|
|
202
|
+
//# sourceMappingURL=deep-clone.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"deep-clone.cjs","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,eAA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;EAC5B,MAAM,QAAS,IAAgC;EAC/C,IAAI,SAAS,OAAO,UAAU,UAC5B,WAAW,KAAK;CAEpB;CAEA,OAAO,OAAO,OAAO,GAAG;AAC1B"}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 深拷贝工具
|
|
3
|
+
*
|
|
4
|
+
* 提供高性能的深拷贝、浅拷贝和深度合并功能
|
|
5
|
+
* @module utils/deep-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,34 @@
|
|
|
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
|
+
"use strict";
|
|
11
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
12
|
+
const require_debounce_throttle = require("./debounce-throttle.cjs");
|
|
13
|
+
const require_deep_clone = require("./deep-clone.cjs");
|
|
14
|
+
const require_selector = require("./selector.cjs");
|
|
15
|
+
exports.combineSelectors = require_selector.combineSelectors;
|
|
16
|
+
exports.createCachedSelector = require_selector.createCachedSelector;
|
|
17
|
+
exports.createMemoizedSelector = require_selector.createMemoizedSelector;
|
|
18
|
+
exports.createSelector = require_selector.createSelector;
|
|
19
|
+
exports.createSelectorWithDeps = require_selector.createSelectorWithDeps;
|
|
20
|
+
exports.debounce = require_debounce_throttle.debounce;
|
|
21
|
+
exports.deepClone = require_deep_clone.deepClone;
|
|
22
|
+
exports.deepEqual = require_deep_clone.deepEqual;
|
|
23
|
+
exports.deepFreeze = require_deep_clone.deepFreeze;
|
|
24
|
+
exports.deepMerge = require_deep_clone.deepMerge;
|
|
25
|
+
exports.deepMergeWithOptions = require_deep_clone.deepMergeWithOptions;
|
|
26
|
+
exports.deleteByPath = require_selector.deleteByPath;
|
|
27
|
+
exports.getByPath = require_selector.getByPath;
|
|
28
|
+
exports.hasPath = require_selector.hasPath;
|
|
29
|
+
exports.isObject = require_deep_clone.isObject;
|
|
30
|
+
exports.isPlainObject = require_deep_clone.isPlainObject;
|
|
31
|
+
exports.setByPath = require_selector.setByPath;
|
|
32
|
+
exports.shallowClone = require_deep_clone.shallowClone;
|
|
33
|
+
exports.throttle = require_debounce_throttle.throttle;
|
|
34
|
+
/*! End of @composy/store-core | Powered by @composy/builder */
|
|
@@ -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,161 @@
|
|
|
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
|
+
"use strict";
|
|
11
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
12
|
+
//#region src/utils/selector.ts
|
|
13
|
+
const ARRAY_INDEX_RE = /^\d+$/;
|
|
14
|
+
function createSelector(selector, equalityFn = Object.is) {
|
|
15
|
+
let lastState;
|
|
16
|
+
let lastResult;
|
|
17
|
+
let initialized = false;
|
|
18
|
+
return (state) => {
|
|
19
|
+
if (initialized && lastState === state) return lastResult;
|
|
20
|
+
const result = selector(state);
|
|
21
|
+
if (!initialized || !equalityFn(result, lastResult)) lastResult = result;
|
|
22
|
+
lastState = state;
|
|
23
|
+
initialized = true;
|
|
24
|
+
return lastResult;
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function createSelectorWithDeps(dependencies, combiner, options = {}) {
|
|
28
|
+
const { equalityFn = Object.is, trackHitRate = false } = options;
|
|
29
|
+
let lastDeps;
|
|
30
|
+
let lastResult;
|
|
31
|
+
let initialized = false;
|
|
32
|
+
let hits = 0;
|
|
33
|
+
let calls = 0;
|
|
34
|
+
const selector = (state) => {
|
|
35
|
+
if (trackHitRate) calls++;
|
|
36
|
+
const deps = dependencies.map((dep) => dep(state));
|
|
37
|
+
if (initialized && lastDeps) {
|
|
38
|
+
let changed = false;
|
|
39
|
+
for (let i = 0; i < deps.length; i++) if (!Object.is(deps[i], lastDeps[i])) {
|
|
40
|
+
changed = true;
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
if (!changed) {
|
|
44
|
+
if (trackHitRate) hits++;
|
|
45
|
+
return lastResult;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
const result = combiner(...deps);
|
|
49
|
+
if (!initialized || !equalityFn(result, lastResult)) lastResult = result;
|
|
50
|
+
lastDeps = deps;
|
|
51
|
+
initialized = true;
|
|
52
|
+
return lastResult;
|
|
53
|
+
};
|
|
54
|
+
if (trackHitRate) selector.getHitRate = () => {
|
|
55
|
+
return calls > 0 ? hits / calls : 0;
|
|
56
|
+
};
|
|
57
|
+
return selector;
|
|
58
|
+
}
|
|
59
|
+
function combineSelectors(selectors, combiner) {
|
|
60
|
+
return (state) => {
|
|
61
|
+
return combiner(...selectors.map((selector) => selector(state)));
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
function createCachedSelector(selector, options = {}) {
|
|
65
|
+
const { maxSize = 10 } = options;
|
|
66
|
+
const cache = /* @__PURE__ */ new Map();
|
|
67
|
+
const memoizedSelector = (state) => {
|
|
68
|
+
if (cache.has(state)) {
|
|
69
|
+
const cached = cache.get(state);
|
|
70
|
+
cache.delete(state);
|
|
71
|
+
cache.set(state, cached);
|
|
72
|
+
return cached;
|
|
73
|
+
}
|
|
74
|
+
const result = selector(state);
|
|
75
|
+
if (cache.size >= maxSize) {
|
|
76
|
+
const firstKey = cache.keys().next().value;
|
|
77
|
+
if (firstKey !== void 0) cache.delete(firstKey);
|
|
78
|
+
}
|
|
79
|
+
cache.set(state, result);
|
|
80
|
+
return result;
|
|
81
|
+
};
|
|
82
|
+
memoizedSelector.clearCache = () => cache.clear();
|
|
83
|
+
memoizedSelector.getCacheSize = () => cache.size;
|
|
84
|
+
return memoizedSelector;
|
|
85
|
+
}
|
|
86
|
+
function createMemoizedSelector(selector, options = {}) {
|
|
87
|
+
return createSelector(selector, options.equalityFn);
|
|
88
|
+
}
|
|
89
|
+
function getByPath(obj, path, defaultValue) {
|
|
90
|
+
if (!path || path.length === 0) return obj;
|
|
91
|
+
const keys = path.replace(/\[([^\]]+)\]/g, ".$1").split(".").filter(Boolean);
|
|
92
|
+
let result = obj;
|
|
93
|
+
for (const key of keys) {
|
|
94
|
+
if (result === null || result === void 0) return defaultValue;
|
|
95
|
+
if (typeof result !== "object") return defaultValue;
|
|
96
|
+
result = result[key];
|
|
97
|
+
}
|
|
98
|
+
return result === void 0 ? defaultValue : result;
|
|
99
|
+
}
|
|
100
|
+
function setByPath(obj, path, value) {
|
|
101
|
+
if (!path || path.length === 0) return false;
|
|
102
|
+
const keys = path.replace(/\[([^\]]+)\]/g, ".$1").split(".").filter(Boolean);
|
|
103
|
+
let current = obj;
|
|
104
|
+
for (let i = 0; i < keys.length - 1; i++) {
|
|
105
|
+
const key = keys[i];
|
|
106
|
+
const nextKey = keys[i + 1] ?? "";
|
|
107
|
+
const nextValue = current[key] ?? (ARRAY_INDEX_RE.test(nextKey) ? [] : {});
|
|
108
|
+
current[key] = nextValue;
|
|
109
|
+
current = current[key];
|
|
110
|
+
}
|
|
111
|
+
const lastKey = keys[keys.length - 1];
|
|
112
|
+
current[lastKey] = value;
|
|
113
|
+
return true;
|
|
114
|
+
}
|
|
115
|
+
function deleteByPath(obj, path) {
|
|
116
|
+
if (!path || path.length === 0) return false;
|
|
117
|
+
const keys = path.replace(/\[([^\]]+)\]/g, ".$1").split(".").filter(Boolean);
|
|
118
|
+
let current = obj;
|
|
119
|
+
for (let i = 0; i < keys.length - 1; i++) {
|
|
120
|
+
const key = keys[i];
|
|
121
|
+
if (current[key] === void 0 || current[key] === null) return false;
|
|
122
|
+
current = current[key];
|
|
123
|
+
}
|
|
124
|
+
const lastKey = keys[keys.length - 1];
|
|
125
|
+
if (lastKey in current) {
|
|
126
|
+
if (Array.isArray(current)) {
|
|
127
|
+
const index = Number.parseInt(lastKey, 10);
|
|
128
|
+
if (!Number.isNaN(index)) {
|
|
129
|
+
current.splice(index, 1);
|
|
130
|
+
return true;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
delete current[lastKey];
|
|
134
|
+
return true;
|
|
135
|
+
}
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
function hasPath(obj, path) {
|
|
139
|
+
if (!path || path.length === 0) return true;
|
|
140
|
+
const keys = path.replace(/\[([^\]]+)\]/g, ".$1").split(".").filter(Boolean);
|
|
141
|
+
let current = obj;
|
|
142
|
+
for (const key of keys) {
|
|
143
|
+
if (current === null || current === void 0) return false;
|
|
144
|
+
if (typeof current !== "object") return false;
|
|
145
|
+
if (!(key in current)) return false;
|
|
146
|
+
current = current[key];
|
|
147
|
+
}
|
|
148
|
+
return true;
|
|
149
|
+
}
|
|
150
|
+
//#endregion
|
|
151
|
+
exports.combineSelectors = combineSelectors;
|
|
152
|
+
exports.createCachedSelector = createCachedSelector;
|
|
153
|
+
exports.createMemoizedSelector = createMemoizedSelector;
|
|
154
|
+
exports.createSelector = createSelector;
|
|
155
|
+
exports.createSelectorWithDeps = createSelectorWithDeps;
|
|
156
|
+
exports.deleteByPath = deleteByPath;
|
|
157
|
+
exports.getByPath = getByPath;
|
|
158
|
+
exports.hasPath = hasPath;
|
|
159
|
+
exports.setByPath = setByPath;
|
|
160
|
+
/*! End of @composy/store-core | Powered by @composy/builder */
|
|
161
|
+
//# sourceMappingURL=selector.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"selector.cjs","names":[],"sources":["../../src/utils/selector.ts"],"sourcesContent":["/**\n * 状态选择器工具\n *\n * 提供高性能的状态选择器和路径操作工具\n * @module utils/selector\n */\n\nimport type { Selector, StateTree } from '../types'\n\nconst ARRAY_INDEX_RE = /^\\d+$/\n\n/**\n * 选择器相等性比较函数类型\n */\nexport type SelectorEqualityFn<R> = (a: R, b: R) => boolean\n\n/**\n * 记忆化选择器配置选项\n */\nexport interface MemoizedSelectorOptions<R> {\n /** 相等性比较函数 */\n equalityFn?: SelectorEqualityFn<R>\n /** 最大缓存大小(默认 1) */\n maxSize?: number\n /** 是否在开发模式下记录缓存命中率 */\n trackHitRate?: boolean\n}\n\n/**\n * 创建记忆化选择器\n *\n * 基本的记忆化选择器,只缓存最后一次结果\n *\n * @template S - 状态类型\n * @template R - 返回值类型\n * @param selector - 选择器函数\n * @param equalityFn - 相等性比较函数(默认使用 Object.is)\n * @returns 记忆化的选择器\n *\n * @example\n * ```typescript\n * const selectTodos = createSelector(\n * (state: AppState) => state.todos,\n * )\n *\n * const selectCompletedTodos = createSelector(\n * (state: AppState) => state.todos.filter(t => t.completed),\n * (a, b) => a.length === b.length && a.every((v, i) => v === b[i])\n * )\n * ```\n */\nexport function createSelector<S extends StateTree, R>(\n selector: Selector<S, R>,\n equalityFn: SelectorEqualityFn<R> = Object.is\n): Selector<S, R> {\n let lastState: S | undefined\n let lastResult: R | undefined\n let initialized = false\n\n return (state: S): R => {\n // 如果状态引用未变,直接返回缓存结果\n if (initialized && lastState === state) {\n return lastResult as R\n }\n\n const result = selector(state)\n\n // 检查结果是否改变\n if (!initialized || !equalityFn(result, lastResult as R)) {\n lastResult = result\n }\n\n lastState = state\n initialized = true\n\n return lastResult as R\n }\n}\n\n/**\n * 创建带依赖的选择器\n *\n * 类似 reselect 的实现,支持多个输入选择器\n *\n * @template S - 状态类型\n * @template Deps - 依赖结果类型\n * @template R - 返回值类型\n * @param dependencies - 依赖选择器数组\n * @param combiner - 组合函数\n * @param options - 配置选项\n * @returns 记忆化的选择器\n *\n * @example\n * ```typescript\n * const selectVisibleTodos = createSelectorWithDeps(\n * [\n * (state) => state.todos,\n * (state) => state.visibilityFilter,\n * ],\n * (todos, filter) => {\n * switch (filter) {\n * case 'SHOW_COMPLETED': return todos.filter(t => t.completed)\n * case 'SHOW_ACTIVE': return todos.filter(t => !t.completed)\n * default: return todos\n * }\n * }\n * )\n * ```\n */\nexport function createSelectorWithDeps<\n S extends StateTree,\n Deps extends unknown[],\n R,\n>(\n dependencies: { [K in keyof Deps]: Selector<S, Deps[K]> },\n combiner: (...args: Deps) => R,\n options: MemoizedSelectorOptions<R> = {}\n): Selector<S, R> {\n const { equalityFn = Object.is, trackHitRate = false } = options\n\n let lastDeps: Deps | undefined\n let lastResult: R | undefined\n let initialized = false\n\n // 缓存统计(仅开发模式)\n let hits = 0\n let calls = 0\n\n const selector = (state: S): R => {\n if (trackHitRate) calls++\n\n // 计算所有依赖的结果\n const deps = dependencies.map(dep => dep(state)) as Deps\n\n // 检查依赖是否改变\n if (initialized && lastDeps) {\n let changed = false\n for (let i = 0; i < deps.length; i++) {\n if (!Object.is(deps[i], lastDeps[i])) {\n changed = true\n break\n }\n }\n\n if (!changed) {\n if (trackHitRate) hits++\n return lastResult as R\n }\n }\n\n // 计算新结果\n const result = combiner(...deps)\n\n // 检查结果是否改变\n if (!initialized || !equalityFn(result, lastResult as R)) {\n lastResult = result\n }\n\n lastDeps = deps\n initialized = true\n\n return lastResult as R\n }\n\n // 添加缓存统计方法\n if (trackHitRate) {\n ;(selector as Selector<S, R> & { getHitRate: () => number }).getHitRate =\n () => {\n return calls > 0 ? hits / calls : 0\n }\n }\n\n return selector\n}\n\n/**\n * 组合多个选择器\n *\n * 简单的选择器组合,不带记忆化\n *\n * @template S - 状态类型\n * @template R - 结果类型数组\n * @template C - 组合结果类型\n * @param selectors - 选择器数组\n * @param combiner - 组合函数\n * @returns 组合后的选择器\n *\n * @example\n * ```typescript\n * const selectUserAndTodos = combineSelectors(\n * [\n * (state) => state.user,\n * (state) => state.todos,\n * ],\n * (user, todos) => ({ user, todos })\n * )\n * ```\n */\nexport function combineSelectors<S extends StateTree, R extends unknown[], C>(\n selectors: { [K in keyof R]: Selector<S, R[K]> },\n combiner: (...results: R) => C\n): Selector<S, C> {\n return (state: S): C => {\n const results = selectors.map(selector => selector(state)) as R\n return combiner(...results)\n }\n}\n\n/**\n * 创建带高级缓存的选择器\n *\n * 支持多个缓存条目,使用 LRU 策略\n *\n * @template S - 状态类型\n * @template R - 返回值类型\n * @param selector - 选择器函数\n * @param options - 配置选项\n * @returns 带缓存的选择器\n *\n * @example\n * ```typescript\n * const selectFilteredItems = createCachedSelector(\n * (state, filter) => state.items.filter(item => item.type === filter),\n * { maxSize: 5 }\n * )\n * ```\n */\nexport function createCachedSelector<S extends StateTree, R>(\n selector: Selector<S, R>,\n options: MemoizedSelectorOptions<R> = {}\n): Selector<S, R> & { clearCache: () => void; getCacheSize: () => number } {\n const { maxSize = 10 } = options\n\n // 使用 Map 保持插入顺序(LRU 策略)\n const cache = new Map<S, R>()\n\n const memoizedSelector = (state: S): R => {\n // 检查缓存\n if (cache.has(state)) {\n const cached = cache.get(state) as R\n // 移到末尾(最近使用)\n cache.delete(state)\n cache.set(state, cached)\n return cached\n }\n\n // 计算结果\n const result = selector(state)\n\n // 管理缓存大小(LRU 淘汰)\n if (cache.size >= maxSize) {\n const firstKey = cache.keys().next().value\n if (firstKey !== undefined) {\n cache.delete(firstKey)\n }\n }\n\n cache.set(state, result)\n return result\n }\n\n // 添加缓存管理方法\n memoizedSelector.clearCache = () => cache.clear()\n memoizedSelector.getCacheSize = () => cache.size\n\n return memoizedSelector\n}\n\n/**\n * 创建记忆化选择器(别名,同 createSelector)\n *\n * @template S - 状态类型\n * @template R - 返回值类型\n * @param selector - 选择器函数\n * @param options - 配置选项\n * @returns 记忆化的选择器\n */\nexport function createMemoizedSelector<S extends StateTree, R>(\n selector: Selector<S, R>,\n options: MemoizedSelectorOptions<R> = {}\n): Selector<S, R> {\n return createSelector(selector, options.equalityFn)\n}\n\n/**\n * 获取嵌套对象的值\n *\n * 支持点表示法和数组索引\n *\n * @template T - 返回值类型\n * @param obj - 对象\n * @param path - 路径(支持 'a.b.c' 或 'a[0].b')\n * @param defaultValue - 默认值(当路径不存在时返回)\n * @returns 值\n *\n * @example\n * ```typescript\n * const value = getByPath({ a: { b: { c: 1 } } }, 'a.b.c')\n * // value: 1\n *\n * const arr = getByPath({ items: [{ name: 'test' }] }, 'items[0].name')\n * // arr: 'test'\n *\n * const missing = getByPath({ a: 1 }, 'b.c', 'default')\n * // missing: 'default'\n * ```\n */\nexport function getByPath<T = unknown>(\n obj: unknown,\n path: string,\n defaultValue?: T\n): T | undefined {\n if (!path || path.length === 0) {\n return obj as T\n }\n\n // 解析路径(支持 'a.b.c' 和 'a[0].b')\n const keys = path\n .replace(/\\[([^\\]]+)\\]/g, '.$1') // 将 [0] 转换为 .0\n .split('.')\n .filter(Boolean)\n\n let result: unknown = obj\n\n for (const key of keys) {\n if (result === null || result === undefined) {\n return defaultValue\n }\n\n if (typeof result !== 'object') {\n return defaultValue\n }\n\n result = (result as Record<string, unknown>)[key]\n }\n\n return result === undefined ? defaultValue : (result as T)\n}\n\n/**\n * 设置嵌套对象的值\n *\n * 支持自动创建中间路径\n *\n * @param obj - 对象\n * @param path - 路径\n * @param value - 值\n * @returns 是否成功\n *\n * @example\n * ```typescript\n * const obj = {}\n * setByPath(obj, 'a.b.c', 1)\n * // obj: { a: { b: { c: 1 } } }\n *\n * setByPath(obj, 'items[0].name', 'test')\n * // obj: { a: { b: { c: 1 } }, items: [{ name: 'test' }] }\n * ```\n */\nexport function setByPath(\n obj: Record<string, unknown>,\n path: string,\n value: unknown\n): boolean {\n if (!path || path.length === 0) {\n return false\n }\n\n // 解析路径\n const keys = path\n .replace(/\\[([^\\]]+)\\]/g, '.$1')\n .split('.')\n .filter(Boolean)\n\n let current: Record<string, unknown> = obj\n\n for (let i = 0; i < keys.length - 1; i++) {\n const key = keys[i]\n const nextKey = keys[i + 1] ?? ''\n\n const nextValue = current[key] ?? (ARRAY_INDEX_RE.test(nextKey) ? [] : {})\n // 根据下一个键决定创建对象还是数组\n current[key] = nextValue\n current = current[key] as Record<string, unknown>\n }\n\n const lastKey = keys[keys.length - 1]\n current[lastKey] = value\n return true\n}\n\n/**\n * 删除嵌套对象的值\n *\n * @param obj - 对象\n * @param path - 路径\n * @returns 是否成功删除\n *\n * @example\n * ```typescript\n * const obj = { a: { b: { c: 1 } } }\n * deleteByPath(obj, 'a.b.c')\n * // obj: { a: { b: {} } }\n * ```\n */\nexport function deleteByPath(\n obj: Record<string, unknown>,\n path: string\n): boolean {\n if (!path || path.length === 0) {\n return false\n }\n\n const keys = path\n .replace(/\\[([^\\]]+)\\]/g, '.$1')\n .split('.')\n .filter(Boolean)\n\n let current: Record<string, unknown> = obj\n\n for (let i = 0; i < keys.length - 1; i++) {\n const key = keys[i]\n if (current[key] === undefined || current[key] === null) {\n return false\n }\n current = current[key] as Record<string, unknown>\n }\n\n const lastKey = keys[keys.length - 1]\n if (lastKey in current) {\n if (Array.isArray(current)) {\n const index = Number.parseInt(lastKey, 10)\n if (!Number.isNaN(index)) {\n current.splice(index, 1)\n return true\n }\n }\n delete current[lastKey]\n return true\n }\n\n return false\n}\n\n/**\n * 检查嵌套对象中是否存在指定路径\n *\n * @param obj - 对象\n * @param path - 路径\n * @returns 是否存在\n *\n * @example\n * ```typescript\n * hasPath({ a: { b: 1 } }, 'a.b') // true\n * hasPath({ a: { b: 1 } }, 'a.c') // false\n * ```\n */\nexport function hasPath(obj: unknown, path: string): boolean {\n if (!path || path.length === 0) {\n return true\n }\n\n const keys = path\n .replace(/\\[([^\\]]+)\\]/g, '.$1')\n .split('.')\n .filter(Boolean)\n\n let current: unknown = obj\n\n for (const key of keys) {\n if (current === null || current === undefined) {\n return false\n }\n\n if (typeof current !== 'object') {\n return false\n }\n\n if (!(key in (current as Record<string, unknown>))) {\n return false\n }\n\n current = (current as Record<string, unknown>)[key]\n }\n\n return true\n}\n"],"mappings":";;;;;;;;;;;;AASA,MAAM,iBAAiB;AA0ChB,SAAS,eACd,UACA,aAAoC,OAAO,IAC3B;CAChB,IAAI;CACJ,IAAI;CACJ,IAAI,cAAc;CAElB,QAAQ,UAAgB;EAEtB,IAAI,eAAe,cAAc,OAC/B,OAAO;EAGT,MAAM,SAAS,SAAS,KAAK;EAG7B,IAAI,CAAC,eAAe,CAAC,WAAW,QAAQ,UAAe,GACrD,aAAa;EAGf,YAAY;EACZ,cAAc;EAEd,OAAO;CACT;AACF;AAgCO,SAAS,uBAKd,cACA,UACA,UAAsC,CAAC,GACvB;CAChB,MAAM,EAAE,aAAa,OAAO,IAAI,eAAe,UAAU;CAEzD,IAAI;CACJ,IAAI;CACJ,IAAI,cAAc;CAGlB,IAAI,OAAO;CACX,IAAI,QAAQ;CAEZ,MAAM,YAAY,UAAgB;EAChC,IAAI,cAAc;EAGlB,MAAM,OAAO,aAAa,KAAI,QAAO,IAAI,KAAK,CAAC;EAG/C,IAAI,eAAe,UAAU;GAC3B,IAAI,UAAU;GACd,KAAA,IAAS,IAAI,GAAG,IAAI,KAAK,QAAQ,KAC/B,IAAI,CAAC,OAAO,GAAG,KAAK,IAAI,SAAS,EAAE,GAAG;IACpC,UAAU;IACV;GACF;GAGF,IAAI,CAAC,SAAS;IACZ,IAAI,cAAc;IAClB,OAAO;GACT;EACF;EAGA,MA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