react-hooks-global-states 16.0.0 → 16.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.
Files changed (46) hide show
  1. package/jest.config.js +50 -0
  2. package/package.json +52 -2
  3. package/GlobalStore.cjs +0 -525
  4. package/GlobalStore.d.ts +0 -160
  5. package/GlobalStore.debugProps.cjs +0 -28
  6. package/GlobalStore.debugProps.d.ts +0 -5
  7. package/GlobalStore.debugProps.js +0 -28
  8. package/GlobalStore.debugProps.mjs +0 -7
  9. package/GlobalStore.js +0 -525
  10. package/GlobalStore.mjs +0 -497
  11. package/actions.cjs +0 -61
  12. package/actions.d.ts +0 -32
  13. package/actions.js +0 -61
  14. package/actions.mjs +0 -30
  15. package/bundle.cjs +0 -40
  16. package/bundle.js +0 -40
  17. package/bundle.mjs +0 -19
  18. package/createContext.cjs +0 -253
  19. package/createContext.d.ts +0 -15
  20. package/createContext.js +0 -253
  21. package/createContext.mjs +0 -235
  22. package/createGlobalState.cjs +0 -29
  23. package/createGlobalState.d.ts +0 -7
  24. package/createGlobalState.js +0 -29
  25. package/createGlobalState.mjs +0 -8
  26. package/index.d.ts +0 -9
  27. package/isRecord.cjs +0 -46
  28. package/isRecord.d.ts +0 -2
  29. package/isRecord.js +0 -46
  30. package/isRecord.mjs +0 -15
  31. package/shallowCompare.cjs +0 -115
  32. package/shallowCompare.d.ts +0 -52
  33. package/shallowCompare.js +0 -115
  34. package/shallowCompare.mjs +0 -84
  35. package/throwWrongKeyOnActionCollectionConfig.cjs +0 -41
  36. package/throwWrongKeyOnActionCollectionConfig.d.ts +0 -2
  37. package/throwWrongKeyOnActionCollectionConfig.js +0 -41
  38. package/throwWrongKeyOnActionCollectionConfig.mjs +0 -20
  39. package/types.cjs +0 -18
  40. package/types.d.ts +0 -1250
  41. package/types.js +0 -18
  42. package/types.mjs +0 -0
  43. package/uniqueId.cjs +0 -72
  44. package/uniqueId.d.ts +0 -7
  45. package/uniqueId.js +0 -72
  46. package/uniqueId.mjs +0 -51
package/createContext.mjs DELETED
@@ -1,235 +0,0 @@
1
- var __defProp = Object.defineProperty;
2
- var __defProps = Object.defineProperties;
3
- var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
- var __spreadValues = (a, b) => {
9
- for (var prop in b || (b = {}))
10
- if (__hasOwnProp.call(b, prop))
11
- __defNormalProp(a, prop, b[prop]);
12
- if (__getOwnPropSymbols)
13
- for (var prop of __getOwnPropSymbols(b)) {
14
- if (__propIsEnum.call(b, prop))
15
- __defNormalProp(a, prop, b[prop]);
16
- }
17
- return a;
18
- };
19
- var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
- var __objRest = (source, exclude) => {
21
- var target = {};
22
- for (var prop in source)
23
- if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
24
- target[prop] = source[prop];
25
- if (source != null && __getOwnPropSymbols)
26
- for (var prop of __getOwnPropSymbols(source)) {
27
- if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
28
- target[prop] = source[prop];
29
- }
30
- return target;
31
- };
32
-
33
- // src/createContext.ts
34
- import {
35
- createContext as reactCreateContext,
36
- useContext,
37
- createElement as reactCreateElement,
38
- useMemo,
39
- useEffect,
40
- useRef,
41
- useDebugValue,
42
- useLayoutEffect,
43
- useState
44
- } from "react";
45
- import { createObservable, createSelectorHook as createSelectorHookBase, GlobalStore } from "./GlobalStore.mjs";
46
- import isFunction from "json-storage-formatter/isFunction";
47
- import uniqueId from "./uniqueId.mjs";
48
- var createContext = (valueArg, contextArgs = {}) => {
49
- var _a;
50
- const Context = reactCreateContext(null);
51
- const contextIdentifier = (_a = contextArgs.name) != null ? _a : uniqueId("ctx:");
52
- const getInheritedState = () => isFunction(valueArg) ? valueArg() : valueArg;
53
- const hasValueProp = (props) => {
54
- return Object.prototype.hasOwnProperty.call(props, "value");
55
- };
56
- const useProviderValue = (props) => {
57
- var _a2;
58
- useDebugValue(`${contextIdentifier}:Provider`);
59
- const [store] = useState(() => {
60
- var _a3, _b, _c, _d;
61
- const initialValue = (() => {
62
- if (hasValueProp(props))
63
- return isFunction(props.value) ? props.value(getInheritedState()) : props.value;
64
- return getInheritedState();
65
- })();
66
- const store2 = new GlobalStore(initialValue, __spreadProps(__spreadValues({}, contextArgs), {
67
- metadata: (_a3 = isFunction(contextArgs.metadata) ? contextArgs.metadata() : contextArgs.metadata) != null ? _a3 : {}
68
- }));
69
- const storeToolsExtensions = {
70
- use
71
- };
72
- Object.assign(store2.storeTools, storeToolsExtensions);
73
- (_c = (_b = contextArgs.callbacks) == null ? void 0 : _b.onCreated) == null ? void 0 : _c.call(
74
- _b,
75
- store2.storeTools,
76
- store2
77
- );
78
- (_d = props.onCreated) == null ? void 0 : _d.call(props, store2.storeTools, store2);
79
- return store2;
80
- });
81
- useLayoutEffect(() => {
82
- const shouldUpdateState = hasValueProp(props) && !isFunction(props.value);
83
- if (!shouldUpdateState) return;
84
- store.setState(props.value);
85
- }, [store, props.value]);
86
- useEffect(() => {
87
- var _a3, _b, _c;
88
- const unsubscribes = [
89
- (_b = (_a3 = contextArgs.callbacks) == null ? void 0 : _a3.onMounted) == null ? void 0 : _b.call(
90
- _a3,
91
- store.storeTools,
92
- store
93
- ),
94
- (_c = props.onMounted) == null ? void 0 : _c.call(props, store.storeTools, store)
95
- ].filter(Boolean);
96
- return () => {
97
- var _a4, _b2, _c2;
98
- (_b2 = (_a4 = store.callbacks) == null ? void 0 : _a4.onUnMount) == null ? void 0 : _b2.call(_a4, store);
99
- unsubscribes.forEach((unsubscribe) => unsubscribe == null ? void 0 : unsubscribe());
100
- (_c2 = store.__onUnMountContext) == null ? void 0 : _c2.call(
101
- store,
102
- store
103
- );
104
- };
105
- }, [store]);
106
- (_a2 = props.onRender) == null ? void 0 : _a2.call(props, store.storeTools, store);
107
- return store;
108
- };
109
- const Provider = (args) => {
110
- const store = useProviderValue(args);
111
- return reactCreateElement(Context.Provider, { value: store.use }, args.children);
112
- };
113
- Provider.displayName = `${contextIdentifier}:Provider`;
114
- Context.displayName = `${contextIdentifier}`;
115
- const providerExtensions = {
116
- makeProviderWrapper: (parentOptions) => {
117
- const context = {
118
- current: void 0,
119
- instance: void 0
120
- };
121
- const wrapper = (_a2) => {
122
- var _b = _a2, { children } = _b, options = __objRest(_b, ["children"]);
123
- return reactCreateElement(
124
- Provider,
125
- __spreadProps(__spreadValues(__spreadValues({}, parentOptions), options), {
126
- onCreated: (ctx, store) => {
127
- var _a3;
128
- context.current = ctx;
129
- context.instance = store;
130
- (_a3 = parentOptions == null ? void 0 : parentOptions.onCreated) == null ? void 0 : _a3.call(parentOptions, ctx, store);
131
- }
132
- }),
133
- children
134
- );
135
- };
136
- return { wrapper, context };
137
- }
138
- };
139
- const use = (...args) => {
140
- useDebugValue(`${contextIdentifier}:use`);
141
- const hook = useContext(Context);
142
- if (!hook) throw new Error("use hook must be used within a ContextProvider");
143
- return hook(...args);
144
- };
145
- const api = () => {
146
- useDebugValue(`${contextIdentifier}:api`);
147
- const hook = useContext(Context);
148
- if (!hook) throw new Error("api hook must be used within a ContextProvider");
149
- return hook;
150
- };
151
- const observable = (...args) => {
152
- useDebugValue(`${contextIdentifier}:observable`);
153
- const context = api();
154
- const props = useRef(args);
155
- props.current = args;
156
- return useMemo(() => {
157
- var _a2, _b, _c;
158
- return context.createObservable(
159
- (state) => {
160
- const selector = props.current[0];
161
- return selector(state);
162
- },
163
- {
164
- // we call the equality in this way to execute always the latest version of the functions
165
- // check if the root state changed
166
- isEqualRoot: !((_a2 = props.current[1]) == null ? void 0 : _a2.isEqualRoot) ? void 0 : (current, next) => {
167
- const isEqualRoot = props.current[1].isEqualRoot;
168
- return Boolean(isEqualRoot(current, next));
169
- },
170
- // check if the selection changed
171
- isEqual: !((_b = props.current[1]) == null ? void 0 : _b.isEqual) ? void 0 : (current, next) => {
172
- const isEqual = props.current[1].isEqual;
173
- return Boolean(isEqual(current, next));
174
- },
175
- name: (_c = props.current[1]) == null ? void 0 : _c.name
176
- }
177
- );
178
- }, [context]);
179
- };
180
- const useExtensions = {
181
- displayName: Context.displayName,
182
- createSelectorHook: createSelectorHook.bind(use),
183
- api,
184
- select: (...args) => use(...args)[0],
185
- observable,
186
- actions: () => {
187
- return api().actions;
188
- }
189
- };
190
- Object.assign(Provider, providerExtensions);
191
- Object.assign(use, useExtensions);
192
- return {
193
- use,
194
- Provider,
195
- Context
196
- };
197
- };
198
- function createSelectorHook(selector, hookConfig) {
199
- var _a;
200
- const selectorIdentifier = (_a = hookConfig == null ? void 0 : hookConfig.name) != null ? _a : uniqueId("sh:");
201
- const use = (...selectorArgs) => {
202
- const context = this.api();
203
- useDebugValue(`${this.displayName}:selector`);
204
- useDebugValue(`${selectorIdentifier}:use`);
205
- return useMemo(() => {
206
- return context.createSelectorHook(selector, hookConfig);
207
- }, [context])(...selectorArgs);
208
- };
209
- const api = () => {
210
- useDebugValue(`${this.displayName}:selector`);
211
- useDebugValue(`${selectorIdentifier}:api`);
212
- const context = this.api();
213
- return useMemo(() => {
214
- const observable = context.createObservable(selector, hookConfig);
215
- return __spreadProps(__spreadValues({}, context), {
216
- getState: () => observable.getState(),
217
- subscribe: observable.subscribe.bind(observable),
218
- createSelectorHook: createSelectorHookBase.bind(observable),
219
- createObservable: createObservable.bind(observable)
220
- });
221
- }, [context]);
222
- };
223
- const publicExtensions = {
224
- displayName: this.displayName,
225
- createSelectorHook: createSelectorHook.bind(use),
226
- api
227
- };
228
- Object.assign(use, publicExtensions);
229
- return use;
230
- }
231
- var createContext_default = createContext;
232
- export {
233
- createContext,
234
- createContext_default as default
235
- };
@@ -1,29 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/createGlobalState.ts
21
- var createGlobalState_exports = {};
22
- __export(createGlobalState_exports, {
23
- createGlobalState: () => createGlobalState,
24
- default: () => createGlobalState_default
25
- });
26
- module.exports = __toCommonJS(createGlobalState_exports);
27
- var import_GlobalStore = require("./GlobalStore.cjs");
28
- var createGlobalState = (...[state, args]) => new import_GlobalStore.GlobalStore(state, args).use;
29
- var createGlobalState_default = createGlobalState;
@@ -1,7 +0,0 @@
1
- import type { CreateGlobalState } from './types';
2
- /**
3
- * Creates a global state hook
4
- */
5
- export declare const createGlobalState: CreateGlobalState;
6
- export default createGlobalState;
7
- export type { InferActionsType, InferStateApi, AnyActions } from './types';
@@ -1,29 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/createGlobalState.ts
21
- var createGlobalState_exports = {};
22
- __export(createGlobalState_exports, {
23
- createGlobalState: () => createGlobalState,
24
- default: () => createGlobalState_default
25
- });
26
- module.exports = __toCommonJS(createGlobalState_exports);
27
- var import_GlobalStore = require("./GlobalStore.js");
28
- var createGlobalState = (...[state, args]) => new import_GlobalStore.GlobalStore(state, args).use;
29
- var createGlobalState_default = createGlobalState;
@@ -1,8 +0,0 @@
1
- // src/createGlobalState.ts
2
- import { GlobalStore } from "./GlobalStore.mjs";
3
- var createGlobalState = (...[state, args]) => new GlobalStore(state, args).use;
4
- var createGlobalState_default = createGlobalState;
5
- export {
6
- createGlobalState,
7
- createGlobalState_default as default
8
- };
package/index.d.ts DELETED
@@ -1,9 +0,0 @@
1
- export type { ActionCollectionConfig, ActionCollectionResult, AnyActions, AnyFunction, BaseMetadata, BrandedId, CleanupFunction, ContextActionCollectionConfig, ContextActionCollectionResult, ContextHook, ContextProvider, ContextProviderExtensions, ContextPublicApi, ContextStoreTools, ContextStoreToolsExtensions, CreateContext, CreateGlobalState, GlobalStoreCallbacks as GlobalStoreCallbacksForContext, GlobalStoreCallbacks, GlobalStoreContextCallbacks, InferAPI, InferActionsType, InferContextApi, InferStateApi, MetadataGetter, MetadataSetter, ObservableFragment, ReadonlyContextHook, ReadonlyContextPublicApi, ReadonlyHook, ReadonlyStateApi, SelectHook, SelectorCallback, StateApi, StateChanges, StateHook, StoreTools, SubscribeCallback, SubscribeCallbackConfig, SubscribeToState, SubscriberParameters, SubscriptionCallback, UniqueId, UnsubscribeCallback, UseHookOptions, Any, } from './types';
2
- export { GlobalStore } from './GlobalStore';
3
- export { createGlobalState } from './createGlobalState';
4
- export { shallowCompare } from './shallowCompare';
5
- export { uniqueId } from './uniqueId';
6
- export { throwWrongKeyOnActionCollectionConfig } from './throwWrongKeyOnActionCollectionConfig';
7
- export { isRecord } from './isRecord';
8
- export { actions } from './actions';
9
- export { createContext } from './createContext';
package/isRecord.cjs DELETED
@@ -1,46 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
-
30
- // src/isRecord.ts
31
- var isRecord_exports = {};
32
- __export(isRecord_exports, {
33
- default: () => isRecord_default,
34
- isRecord: () => isRecord
35
- });
36
- module.exports = __toCommonJS(isRecord_exports);
37
- var import_isNil = __toESM(require("json-storage-formatter/isNil"));
38
- var isRecord = (value) => {
39
- if ((0, import_isNil.default)(value) || typeof value !== "object") return false;
40
- if (Array.isArray(value)) return false;
41
- if (value instanceof Map) return false;
42
- if (value instanceof Set) return false;
43
- if (value instanceof Date) return false;
44
- return true;
45
- };
46
- var isRecord_default = isRecord;
package/isRecord.d.ts DELETED
@@ -1,2 +0,0 @@
1
- export declare const isRecord: (value: unknown) => value is Record<string, unknown>;
2
- export default isRecord;
package/isRecord.js DELETED
@@ -1,46 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
-
30
- // src/isRecord.ts
31
- var isRecord_exports = {};
32
- __export(isRecord_exports, {
33
- default: () => isRecord_default,
34
- isRecord: () => isRecord
35
- });
36
- module.exports = __toCommonJS(isRecord_exports);
37
- var import_isNil = __toESM(require("json-storage-formatter/isNil"));
38
- var isRecord = (value) => {
39
- if ((0, import_isNil.default)(value) || typeof value !== "object") return false;
40
- if (Array.isArray(value)) return false;
41
- if (value instanceof Map) return false;
42
- if (value instanceof Set) return false;
43
- if (value instanceof Date) return false;
44
- return true;
45
- };
46
- var isRecord_default = isRecord;
package/isRecord.mjs DELETED
@@ -1,15 +0,0 @@
1
- // src/isRecord.ts
2
- import isNil from "json-storage-formatter/isNil";
3
- var isRecord = (value) => {
4
- if (isNil(value) || typeof value !== "object") return false;
5
- if (Array.isArray(value)) return false;
6
- if (value instanceof Map) return false;
7
- if (value instanceof Set) return false;
8
- if (value instanceof Date) return false;
9
- return true;
10
- };
11
- var isRecord_default = isRecord;
12
- export {
13
- isRecord_default as default,
14
- isRecord
15
- };
@@ -1,115 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
-
30
- // src/shallowCompare.ts
31
- var shallowCompare_exports = {};
32
- __export(shallowCompare_exports, {
33
- canCheckSimpleEquality: () => canCheckSimpleEquality,
34
- default: () => shallowCompare_default,
35
- isArray: () => isArray,
36
- isEqualArray: () => isEqualArray,
37
- isEqualMap: () => isEqualMap,
38
- isEqualObject: () => isEqualObject,
39
- isEqualSet: () => isEqualSet,
40
- isMap: () => isMap,
41
- isSet: () => isSet,
42
- shallowCompare: () => shallowCompare
43
- });
44
- module.exports = __toCommonJS(shallowCompare_exports);
45
- var import_isPrimitive = __toESM(require("json-storage-formatter/isPrimitive"));
46
- var import_isDate = __toESM(require("json-storage-formatter/isDate"));
47
- var import_isNil = __toESM(require("json-storage-formatter/isNil"));
48
- var import_isRecord = __toESM(require("./isRecord.cjs"));
49
- var shallowCompare = (value1, value2) => {
50
- const isEqual = value1 === value2;
51
- if (isEqual) return true;
52
- if (canCheckSimpleEquality(value1, value2)) {
53
- return value1 === value2;
54
- }
55
- if (isArray(value1) && isArray(value2)) {
56
- return isEqualArray(value1, value2);
57
- }
58
- if (isMap(value1) && isMap(value2)) {
59
- return isEqualMap(value1, value2);
60
- }
61
- if (isSet(value1) && isSet(value2)) {
62
- return isEqualSet(value1, value2);
63
- }
64
- if (!(0, import_isRecord.default)(value1) || !(0, import_isRecord.default)(value2)) return value1 === value2;
65
- return isEqualObject(value1, value2);
66
- };
67
- var isArray = (value) => Array.isArray(value);
68
- var isMap = (value) => {
69
- return value instanceof Map;
70
- };
71
- var isSet = (value) => {
72
- return value instanceof Set;
73
- };
74
- var canCheckSimpleEquality = (value1, value2) => {
75
- const typeofValue1 = typeof value1;
76
- const typeofValue2 = typeof value2;
77
- if (typeofValue1 !== typeofValue2) return true;
78
- return (0, import_isNil.default)(value1) || (0, import_isNil.default)(value2) || (0, import_isPrimitive.default)(value1) && (0, import_isPrimitive.default)(value2) || (0, import_isDate.default)(value1) && (0, import_isDate.default)(value2) || typeofValue1 === "function" && typeofValue2 === "function";
79
- };
80
- var isEqualArray = (array1, array2) => {
81
- if (array1.length !== array2.length) return false;
82
- for (let i = 0; i < array1.length; i += 1) {
83
- const $value1 = array1[i];
84
- const $value2 = array2[i];
85
- if ($value1 !== $value2) return false;
86
- }
87
- return true;
88
- };
89
- var isEqualMap = (map1, map2) => {
90
- if (map1.size !== map2.size) return false;
91
- for (const [key, value] of map1) {
92
- const $value2 = map2.get(key);
93
- if (value !== $value2) return false;
94
- }
95
- return true;
96
- };
97
- var isEqualSet = (set1, set2) => {
98
- if (set1.size !== set2.size) return false;
99
- for (const value of set1) {
100
- if (!set2.has(value)) return false;
101
- }
102
- return true;
103
- };
104
- var isEqualObject = (value1, value2) => {
105
- const keys1 = Object.keys(value1);
106
- const keys2 = Object.keys(value2);
107
- if (keys1.length !== keys2.length) return false;
108
- for (const key of keys1) {
109
- const propObject1 = value1[key];
110
- const propObject2 = value2[key];
111
- if (propObject1 !== propObject2) return false;
112
- }
113
- return true;
114
- };
115
- var shallowCompare_default = shallowCompare;
@@ -1,52 +0,0 @@
1
- /**
2
- * @description It performs a shallow comparison of the values.
3
- * @param value1 - The first value to compare.
4
- * @param value2 - The second value to compare.
5
- * @returns True if the values are equal, false otherwise.
6
- */
7
- export declare const shallowCompare: <T>(value1: T, value2: T) => boolean;
8
- export declare const isArray: (value: unknown) => value is unknown[];
9
- export declare const isMap: (value: unknown) => value is Map<unknown, unknown>;
10
- export declare const isSet: (value: unknown) => value is Set<unknown>;
11
- /**
12
- * @description Determines whether a simple equality check (using `===`) is sufficient
13
- * to compare the provided values. This helps decide when a deep equality check
14
- * is unnecessary or inefficient.
15
- *
16
- * Simple equality checks are considered valid when:
17
- * - The values have different types (comparison will trivially return false)
18
- * - Either value is null or undefined
19
- * - Both values are primitive types (string, number, boolean, symbol, bigint)
20
- * - Both values are Date objects
21
- * - Both values are functions
22
- *
23
- * @param value1 - The first value to compare.
24
- * @param value2 - The second value to compare.
25
- * @returns `true` if a simple `===` check is sufficient, `false` if a deep comparison may be required.
26
- *
27
- * @example
28
- * ```ts
29
- * canCheckSimpleEquality(42, 42); // true (primitive numbers)
30
- * canCheckSimpleEquality({ a: 1 }, { a: 1 }); // false (objects)
31
- * canCheckSimpleEquality([1, 2], [1, 2]); // false (arrays)
32
- * canCheckSimpleEquality('a', 1); // true (different types, shallow check enough)
33
- * ```
34
- */
35
- export declare const canCheckSimpleEquality: (value1: unknown, value2: unknown) => boolean;
36
- /**
37
- * @description Performs a shallow comparison between two arrays.
38
- */
39
- export declare const isEqualArray: <T>(array1: T[], array2: T[]) => array1 is T[];
40
- /**
41
- * @description Performs a shallow comparison between two maps.
42
- */
43
- export declare const isEqualMap: <K, V>(map1: Map<K, V>, map2: Map<K, V>) => map1 is Map<K, V>;
44
- /**
45
- * @description Performs a shallow comparison between two sets.
46
- */
47
- export declare const isEqualSet: <T>(set1: Set<T>, set2: Set<T>) => set1 is Set<T>;
48
- /**
49
- * @description Performs a shallow comparison between two objects.
50
- */
51
- export declare const isEqualObject: <T extends Record<string, unknown>>(value1: T, value2: T) => boolean;
52
- export default shallowCompare;