@signaldb/core 1.7.2 → 1.8.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 (95) hide show
  1. package/dist/.vite/manifest.json +88 -59
  2. package/dist/AutoFetchCollection.d.ts +3 -3
  3. package/dist/Collection/Cursor.d.ts +10 -7
  4. package/dist/Collection/index.d.ts +27 -11
  5. package/dist/Collection/types.d.ts +1 -0
  6. package/dist/ReplicatedCollection.d.ts +4 -4
  7. package/dist/index.cjs.js +26 -27
  8. package/dist/index.d.ts +1 -1
  9. package/dist/index.mjs +14 -28
  10. package/dist/index10.cjs.js +265 -0
  11. package/dist/index10.mjs +263 -28
  12. package/dist/index11.cjs.js +125 -0
  13. package/dist/index11.mjs +124 -12
  14. package/dist/index12.cjs.js +15 -0
  15. package/dist/index12.mjs +14 -5
  16. package/dist/index13.cjs.js +23 -0
  17. package/dist/index13.mjs +22 -133
  18. package/dist/index14.cjs.js +12 -0
  19. package/dist/index14.mjs +11 -73
  20. package/dist/index15.cjs.js +22 -0
  21. package/dist/index15.mjs +21 -10
  22. package/dist/index16.cjs.js +33 -0
  23. package/dist/index16.mjs +32 -27
  24. package/dist/index17.cjs.js +15 -0
  25. package/dist/index17.mjs +14 -142
  26. package/dist/index18.cjs.js +98 -0
  27. package/dist/index18.mjs +97 -7
  28. package/dist/index19.cjs.js +11 -0
  29. package/dist/index19.mjs +10 -41
  30. package/dist/index2.cjs.js +23 -0
  31. package/dist/index2.mjs +22 -260
  32. package/dist/index20.cjs.js +45 -0
  33. package/dist/index20.mjs +44 -15
  34. package/dist/index21.cjs.js +45 -0
  35. package/dist/index21.mjs +44 -26
  36. package/dist/index22.cjs.js +69 -0
  37. package/dist/index22.mjs +66 -77
  38. package/dist/index23.cjs.js +778 -0
  39. package/dist/index23.mjs +775 -81
  40. package/dist/index24.cjs.js +11 -0
  41. package/dist/index24.mjs +10 -18
  42. package/dist/index25.cjs.js +81 -0
  43. package/dist/index25.mjs +80 -29
  44. package/dist/index26.cjs.js +98 -0
  45. package/dist/index26.mjs +96 -8
  46. package/dist/index27.cjs.js +154 -0
  47. package/dist/index27.mjs +153 -7
  48. package/dist/index28.cjs.js +11 -0
  49. package/dist/index28.mjs +10 -29
  50. package/dist/index29.cjs.js +11 -0
  51. package/dist/index29.mjs +10 -42
  52. package/dist/index3.cjs.js +19 -0
  53. package/dist/index3.mjs +18 -788
  54. package/dist/index4.cjs.js +37 -0
  55. package/dist/index4.mjs +36 -167
  56. package/dist/index5.cjs.js +35 -0
  57. package/dist/index5.mjs +34 -67
  58. package/dist/index6.cjs.js +47 -0
  59. package/dist/index6.mjs +46 -5
  60. package/dist/index7.cjs.js +33 -0
  61. package/dist/index7.mjs +32 -5
  62. package/dist/index8.cjs.js +19 -0
  63. package/dist/index8.mjs +18 -5
  64. package/dist/index9.cjs.js +139 -0
  65. package/dist/index9.mjs +138 -5
  66. package/dist/utils/deepClone.d.ts +1 -1
  67. package/package.json +3 -3
  68. package/dist/index.cjs10.js +0 -29
  69. package/dist/index.cjs11.js +0 -12
  70. package/dist/index.cjs12.js +0 -5
  71. package/dist/index.cjs13.js +0 -133
  72. package/dist/index.cjs14.js +0 -74
  73. package/dist/index.cjs15.js +0 -10
  74. package/dist/index.cjs16.js +0 -27
  75. package/dist/index.cjs17.js +0 -142
  76. package/dist/index.cjs18.js +0 -7
  77. package/dist/index.cjs19.js +0 -42
  78. package/dist/index.cjs2.js +0 -261
  79. package/dist/index.cjs20.js +0 -15
  80. package/dist/index.cjs21.js +0 -26
  81. package/dist/index.cjs22.js +0 -79
  82. package/dist/index.cjs23.js +0 -84
  83. package/dist/index.cjs24.js +0 -18
  84. package/dist/index.cjs25.js +0 -29
  85. package/dist/index.cjs26.js +0 -9
  86. package/dist/index.cjs27.js +0 -7
  87. package/dist/index.cjs28.js +0 -29
  88. package/dist/index.cjs29.js +0 -42
  89. package/dist/index.cjs3.js +0 -788
  90. package/dist/index.cjs4.js +0 -167
  91. package/dist/index.cjs5.js +0 -68
  92. package/dist/index.cjs6.js +0 -5
  93. package/dist/index.cjs7.js +0 -5
  94. package/dist/index.cjs8.js +0 -5
  95. package/dist/index.cjs9.js +0 -5
@@ -0,0 +1,139 @@
1
+ const require_isEqual = require("./index7.cjs.js");
2
+ const require_uniqueBy = require("./index8.cjs.js");
3
+ //#region src/Collection/Observer.ts
4
+ /**
5
+ * Represents an observer that tracks changes in a collection of items and triggers
6
+ * callbacks for various events such as addition, removal, and modification of items.
7
+ * @template T - The type of the items being observed, which must include an `id` field.
8
+ */
9
+ var Observer = class {
10
+ previousItems = [];
11
+ callbacks;
12
+ unbindEvents;
13
+ /**
14
+ * Creates a new instance of the `Observer` class.
15
+ * Sets up event bindings and initializes the callbacks for tracking changes in a collection.
16
+ * @param bindEvents - A function to bind external events to the observer. Must return a cleanup function to unbind those events.
17
+ */
18
+ constructor(bindEvents) {
19
+ this.callbacks = {
20
+ added: [],
21
+ addedBefore: [],
22
+ changed: [],
23
+ changedField: [],
24
+ movedBefore: [],
25
+ removed: []
26
+ };
27
+ this.unbindEvents = bindEvents();
28
+ }
29
+ call(event, ...args) {
30
+ this.callbacks[event].forEach(({ callback, options }) => {
31
+ if (!options.skipInitial || !options.isInitial) callback(...args);
32
+ });
33
+ }
34
+ hasCallbacks(events) {
35
+ return events.some((event) => this.callbacks[event].length > 0);
36
+ }
37
+ /**
38
+ * Determines if the observer has no active callbacks registered for any events.
39
+ * @returns A boolean indicating whether the observer is empty (i.e., no callbacks are registered).
40
+ */
41
+ isEmpty() {
42
+ return !this.hasCallbacks([
43
+ "added",
44
+ "addedBefore",
45
+ "changed",
46
+ "changedField",
47
+ "movedBefore",
48
+ "removed"
49
+ ]);
50
+ }
51
+ /**
52
+ * Compares the previous state of items with the new state and triggers the appropriate callbacks
53
+ * for events such as added, removed, changed, or moved items.
54
+ * @param newItems - The new list of items to compare against the previous state.
55
+ */
56
+ runChecks(newItems) {
57
+ const oldItemsMap = new Map(this.previousItems.map((item, index) => [item.id, {
58
+ item,
59
+ index,
60
+ beforeItem: this.previousItems[index + 1] || null
61
+ }]));
62
+ const newItemsMap = new Map(newItems.map((item, index) => [item.id, {
63
+ item,
64
+ index,
65
+ beforeItem: newItems[index + 1] || null
66
+ }]));
67
+ if (this.hasCallbacks([
68
+ "changed",
69
+ "changedField",
70
+ "movedBefore",
71
+ "removed"
72
+ ])) oldItemsMap.forEach(({ item: oldItem, index, beforeItem: oldBeforeItem }) => {
73
+ const newItem = newItemsMap.get(oldItem.id);
74
+ if (newItem) {
75
+ if (this.hasCallbacks(["changed", "changedField"]) && !require_isEqual.default(newItem.item, oldItem)) {
76
+ this.call("changed", newItem.item);
77
+ if (this.hasCallbacks(["changedField"])) require_uniqueBy.default([...Object.keys(newItem.item), ...Object.keys(oldItem)], (value) => value).forEach((key) => {
78
+ if (require_isEqual.default(newItem.item[key], oldItem[key])) return;
79
+ this.call("changedField", newItem.item, key, oldItem[key], newItem.item[key]);
80
+ });
81
+ }
82
+ if (newItem.index !== index && newItem.beforeItem?.id !== oldBeforeItem?.id) this.call("movedBefore", newItem.item, newItem.beforeItem);
83
+ } else this.call("removed", oldItem);
84
+ });
85
+ if (this.hasCallbacks(["added", "addedBefore"])) newItems.forEach((newItem, index) => {
86
+ if (oldItemsMap.get(newItem.id)) return;
87
+ this.call("added", newItem);
88
+ this.call("addedBefore", newItem, newItems[index + 1] || null);
89
+ });
90
+ this.previousItems = newItems;
91
+ Object.keys(this.callbacks).forEach((key) => {
92
+ const event = key;
93
+ const callbacks = this.callbacks[event];
94
+ this.callbacks[event] = callbacks.map((callback) => ({
95
+ ...callback,
96
+ options: {
97
+ ...callback.options,
98
+ isInitial: false
99
+ }
100
+ }));
101
+ });
102
+ }
103
+ /**
104
+ * Stops the observer by unbinding all events and cleaning up resources.
105
+ */
106
+ stop() {
107
+ this.unbindEvents();
108
+ }
109
+ /**
110
+ * Registers callbacks for specific events to observe changes in the collection.
111
+ * @param callbacks - An object containing the callbacks for various events (e.g., 'added', 'removed').
112
+ * @param skipInitial - A boolean indicating whether to skip invoking the callbacks for the initial state of the collection.
113
+ */
114
+ addCallbacks(callbacks, skipInitial = false) {
115
+ Object.keys(callbacks).forEach((key) => {
116
+ const typedKey = key;
117
+ this.callbacks[typedKey].push({
118
+ callback: callbacks[typedKey],
119
+ options: {
120
+ skipInitial,
121
+ isInitial: true
122
+ }
123
+ });
124
+ });
125
+ }
126
+ /**
127
+ * Removes the specified callbacks for specific events, unregistering them from the observer.
128
+ * @param callbacks - An object containing the callbacks to be removed for various events.
129
+ */
130
+ removeCallbacks(callbacks) {
131
+ Object.keys(callbacks).forEach((key) => {
132
+ const typedKey = key;
133
+ const index = this.callbacks[typedKey].findIndex(({ callback }) => callback === callbacks[typedKey]);
134
+ this.callbacks[typedKey].splice(index, 1);
135
+ });
136
+ }
137
+ };
138
+ //#endregion
139
+ exports.default = Observer;
package/dist/index9.mjs CHANGED
@@ -1,6 +1,139 @@
1
- function createReactivityAdapter(definition) {
2
- return definition;
3
- }
4
- export {
5
- createReactivityAdapter as default
1
+ import isEqual from "./index7.mjs";
2
+ import uniqueBy from "./index8.mjs";
3
+ //#region src/Collection/Observer.ts
4
+ /**
5
+ * Represents an observer that tracks changes in a collection of items and triggers
6
+ * callbacks for various events such as addition, removal, and modification of items.
7
+ * @template T - The type of the items being observed, which must include an `id` field.
8
+ */
9
+ var Observer = class {
10
+ previousItems = [];
11
+ callbacks;
12
+ unbindEvents;
13
+ /**
14
+ * Creates a new instance of the `Observer` class.
15
+ * Sets up event bindings and initializes the callbacks for tracking changes in a collection.
16
+ * @param bindEvents - A function to bind external events to the observer. Must return a cleanup function to unbind those events.
17
+ */
18
+ constructor(bindEvents) {
19
+ this.callbacks = {
20
+ added: [],
21
+ addedBefore: [],
22
+ changed: [],
23
+ changedField: [],
24
+ movedBefore: [],
25
+ removed: []
26
+ };
27
+ this.unbindEvents = bindEvents();
28
+ }
29
+ call(event, ...args) {
30
+ this.callbacks[event].forEach(({ callback, options }) => {
31
+ if (!options.skipInitial || !options.isInitial) callback(...args);
32
+ });
33
+ }
34
+ hasCallbacks(events) {
35
+ return events.some((event) => this.callbacks[event].length > 0);
36
+ }
37
+ /**
38
+ * Determines if the observer has no active callbacks registered for any events.
39
+ * @returns A boolean indicating whether the observer is empty (i.e., no callbacks are registered).
40
+ */
41
+ isEmpty() {
42
+ return !this.hasCallbacks([
43
+ "added",
44
+ "addedBefore",
45
+ "changed",
46
+ "changedField",
47
+ "movedBefore",
48
+ "removed"
49
+ ]);
50
+ }
51
+ /**
52
+ * Compares the previous state of items with the new state and triggers the appropriate callbacks
53
+ * for events such as added, removed, changed, or moved items.
54
+ * @param newItems - The new list of items to compare against the previous state.
55
+ */
56
+ runChecks(newItems) {
57
+ const oldItemsMap = new Map(this.previousItems.map((item, index) => [item.id, {
58
+ item,
59
+ index,
60
+ beforeItem: this.previousItems[index + 1] || null
61
+ }]));
62
+ const newItemsMap = new Map(newItems.map((item, index) => [item.id, {
63
+ item,
64
+ index,
65
+ beforeItem: newItems[index + 1] || null
66
+ }]));
67
+ if (this.hasCallbacks([
68
+ "changed",
69
+ "changedField",
70
+ "movedBefore",
71
+ "removed"
72
+ ])) oldItemsMap.forEach(({ item: oldItem, index, beforeItem: oldBeforeItem }) => {
73
+ const newItem = newItemsMap.get(oldItem.id);
74
+ if (newItem) {
75
+ if (this.hasCallbacks(["changed", "changedField"]) && !isEqual(newItem.item, oldItem)) {
76
+ this.call("changed", newItem.item);
77
+ if (this.hasCallbacks(["changedField"])) uniqueBy([...Object.keys(newItem.item), ...Object.keys(oldItem)], (value) => value).forEach((key) => {
78
+ if (isEqual(newItem.item[key], oldItem[key])) return;
79
+ this.call("changedField", newItem.item, key, oldItem[key], newItem.item[key]);
80
+ });
81
+ }
82
+ if (newItem.index !== index && newItem.beforeItem?.id !== oldBeforeItem?.id) this.call("movedBefore", newItem.item, newItem.beforeItem);
83
+ } else this.call("removed", oldItem);
84
+ });
85
+ if (this.hasCallbacks(["added", "addedBefore"])) newItems.forEach((newItem, index) => {
86
+ if (oldItemsMap.get(newItem.id)) return;
87
+ this.call("added", newItem);
88
+ this.call("addedBefore", newItem, newItems[index + 1] || null);
89
+ });
90
+ this.previousItems = newItems;
91
+ Object.keys(this.callbacks).forEach((key) => {
92
+ const event = key;
93
+ const callbacks = this.callbacks[event];
94
+ this.callbacks[event] = callbacks.map((callback) => ({
95
+ ...callback,
96
+ options: {
97
+ ...callback.options,
98
+ isInitial: false
99
+ }
100
+ }));
101
+ });
102
+ }
103
+ /**
104
+ * Stops the observer by unbinding all events and cleaning up resources.
105
+ */
106
+ stop() {
107
+ this.unbindEvents();
108
+ }
109
+ /**
110
+ * Registers callbacks for specific events to observe changes in the collection.
111
+ * @param callbacks - An object containing the callbacks for various events (e.g., 'added', 'removed').
112
+ * @param skipInitial - A boolean indicating whether to skip invoking the callbacks for the initial state of the collection.
113
+ */
114
+ addCallbacks(callbacks, skipInitial = false) {
115
+ Object.keys(callbacks).forEach((key) => {
116
+ const typedKey = key;
117
+ this.callbacks[typedKey].push({
118
+ callback: callbacks[typedKey],
119
+ options: {
120
+ skipInitial,
121
+ isInitial: true
122
+ }
123
+ });
124
+ });
125
+ }
126
+ /**
127
+ * Removes the specified callbacks for specific events, unregistering them from the observer.
128
+ * @param callbacks - An object containing the callbacks to be removed for various events.
129
+ */
130
+ removeCallbacks(callbacks) {
131
+ Object.keys(callbacks).forEach((key) => {
132
+ const typedKey = key;
133
+ const index = this.callbacks[typedKey].findIndex(({ callback }) => callback === callbacks[typedKey]);
134
+ this.callbacks[typedKey].splice(index, 1);
135
+ });
136
+ }
6
137
  };
138
+ //#endregion
139
+ export { Observer as default };
@@ -4,7 +4,7 @@
4
4
  * @template T - The type of the value to clone.
5
5
  * @param value - The value to deep clone.
6
6
  * @returns A deep copy of the provided value.
7
- * @throws An error if the value is a function, as cloning functions is not supported.
7
+ * @throws {Error} An error if the value is a function, as cloning functions is not supported.
8
8
  */
9
9
  export declare function clone<T>(value: T): T;
10
10
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@signaldb/core",
3
- "version": "1.7.2",
3
+ "version": "1.8.1",
4
4
  "description": "SignalDB is a client-side database that provides a simple MongoDB-like interface to the data with first-class typescript support to achieve an optimistic UI. Data persistence can be achieved by using storage providers that store the data through a JSON interface to places such as localStorage.",
5
5
  "scripts": {
6
6
  "build": "rimraf dist && vite build",
@@ -55,9 +55,9 @@
55
55
  ],
56
56
  "dependencies": {
57
57
  "fast-sort": "^3.4.1",
58
- "mingo": "^7.0.0"
58
+ "mingo": "^7.1.1"
59
59
  },
60
60
  "devDependencies": {
61
- "zod": "^4.0.14"
61
+ "zod": "^4.3.5"
62
62
  }
63
63
  }
@@ -1,29 +0,0 @@
1
- "use strict";
2
- function isEqual(a, b) {
3
- if (Object.is(a, b))
4
- return true;
5
- if (a instanceof RegExp && b instanceof RegExp)
6
- return a.toString() === b.toString();
7
- if (a instanceof Date && b instanceof Date)
8
- return a.getTime() === b.getTime();
9
- if (typeof a !== "object")
10
- return false;
11
- if (typeof b !== "object")
12
- return false;
13
- if (a === null)
14
- return false;
15
- if (b === null)
16
- return false;
17
- const aKeys = Object.keys(a);
18
- const bKeys = Object.keys(b);
19
- if (aKeys.length !== bKeys.length)
20
- return false;
21
- for (const key of aKeys) {
22
- if (!bKeys.includes(key))
23
- return false;
24
- if (!isEqual(a[key], b[key]))
25
- return false;
26
- }
27
- return true;
28
- }
29
- module.exports = isEqual;
@@ -1,12 +0,0 @@
1
- "use strict";
2
- const mingo = require("mingo");
3
- const deepClone = require("./index.cjs19.js");
4
- function modify(item, modifier) {
5
- const hasOperators = Object.keys(modifier).some((key) => key.startsWith("$"));
6
- if (!hasOperators)
7
- return modifier;
8
- const clonedItem = deepClone.default(item);
9
- mingo.update(clonedItem, modifier);
10
- return clonedItem;
11
- }
12
- module.exports = modify;
@@ -1,5 +0,0 @@
1
- "use strict";
2
- function randomId() {
3
- return Math.floor(Math.random() * 1e17).toString(16);
4
- }
5
- module.exports = randomId;
@@ -1,133 +0,0 @@
1
- "use strict";
2
- class EventEmitter {
3
- _maxListeners = 100;
4
- /**
5
- * We store a set of the listeners for each event.
6
- */
7
- _listenerStore = /* @__PURE__ */ new Map();
8
- setMaxListeners(max) {
9
- this._maxListeners = max;
10
- return this;
11
- }
12
- /**
13
- * Subscribe to an event with a listener function.
14
- * @param eventName The event name (key of E).
15
- * @param listener A function that receives the emitted arguments.
16
- * @returns The emitter instance (for chaining).
17
- */
18
- on(eventName, listener) {
19
- let listenersSet = this._listenerStore.get(eventName);
20
- if (!listenersSet) {
21
- listenersSet = /* @__PURE__ */ new Set();
22
- this._listenerStore.set(eventName, listenersSet);
23
- }
24
- listenersSet.add(listener);
25
- if (listenersSet.size > this._maxListeners) {
26
- console.warn(`Possible EventEmitter memory leak detected. ${listenersSet.size} ${String(eventName)} listeners added. Use emitter.setMaxListeners() to increase limit.`);
27
- }
28
- return this;
29
- }
30
- /**
31
- * Subscribe to an event with a listener function.
32
- * @param eventName The event name (key of E).
33
- * @param listener A function that receives the emitted arguments.
34
- * @returns The emitter instance (for chaining).
35
- */
36
- addListener(eventName, listener) {
37
- return this.on(eventName, listener);
38
- }
39
- /**
40
- * Subscribe to an event, handling it only once. Automatically removes
41
- * the listener after it fires the first time.
42
- * @param eventName The event name (key of E).
43
- * @param listener A function that receives the emitted arguments.
44
- * @returns The emitter instance (for chaining).
45
- */
46
- once(eventName, listener) {
47
- const onceWrapper = ((...args) => {
48
- listener(...args);
49
- this.off(eventName, onceWrapper);
50
- });
51
- return this.on(eventName, onceWrapper);
52
- }
53
- /**
54
- * Unsubscribe a previously subscribed listener.
55
- * @param eventName The event name (key of E).
56
- * @param listener The original function passed to `on` or `once`.
57
- * @returns The emitter instance (for chaining).
58
- */
59
- off(eventName, listener) {
60
- const listenersSet = this._listenerStore.get(eventName);
61
- if (!listenersSet)
62
- return this;
63
- listenersSet.delete(listener);
64
- if (listenersSet.size === 0) {
65
- this._listenerStore.delete(eventName);
66
- }
67
- return this;
68
- }
69
- /**
70
- * Unsubscribe a previously subscribed listener.
71
- * @param eventName The event name (key of E).
72
- * @param listener The original function passed to `on` or `once`.
73
- * @returns The emitter instance (for chaining).
74
- */
75
- removeListener(eventName, listener) {
76
- return this.off(eventName, listener);
77
- }
78
- /**
79
- * Emit (dispatch) an event with a variable number of arguments.
80
- * @param eventName The event name (key of E).
81
- * @param args The arguments to pass to subscribed listeners.
82
- */
83
- emit(eventName, ...args) {
84
- this.listeners(eventName).forEach((listener) => {
85
- listener(...args);
86
- });
87
- }
88
- /**
89
- * Returns the array of listener functions currently registered for a given event.
90
- * @param eventName The event name (key of E).
91
- * @returns An array of listener functions.
92
- */
93
- listeners(eventName) {
94
- const listenersSet = this._listenerStore.get(eventName);
95
- if (!listenersSet)
96
- return [];
97
- return [...listenersSet.values()];
98
- }
99
- /**
100
- * Returns the number of listeners for a given event.
101
- * @param eventName The event name (key of E).
102
- * @returns The number of listeners.
103
- */
104
- listenerCount(eventName) {
105
- const listenersSet = this._listenerStore.get(eventName);
106
- return listenersSet ? listenersSet.size : 0;
107
- }
108
- /**
109
- * Removes all listeners for a given event, or all events if none is specified.
110
- * @param eventName Optional. If omitted, clears all events’ listeners.
111
- * @returns The emitter instance (for chaining).
112
- */
113
- removeAllListeners(eventName) {
114
- if (eventName === void 0) {
115
- for (const [eventName_, listenersSet] of this._listenerStore.entries()) {
116
- for (const listener of listenersSet.values()) {
117
- this.off(eventName_, listener);
118
- }
119
- }
120
- this._listenerStore.clear();
121
- } else {
122
- const listenersSet = this._listenerStore.get(eventName);
123
- if (listenersSet) {
124
- for (const listener of listenersSet.values()) {
125
- this.off(eventName, listener);
126
- }
127
- this._listenerStore.delete(eventName);
128
- }
129
- }
130
- return this;
131
- }
132
- }
133
- module.exports = EventEmitter;
@@ -1,74 +0,0 @@
1
- "use strict";
2
- Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
3
- const createIndexProvider = require("./index.cjs6.js");
4
- const get = require("./index.cjs24.js");
5
- const getMatchingKeys = require("./index.cjs28.js");
6
- const serializeValue = require("./index.cjs20.js");
7
- function createExternalIndex(field, index) {
8
- return createIndexProvider({
9
- query(selector) {
10
- if (!Object.hasOwnProperty.call(selector, field)) {
11
- return { matched: false };
12
- }
13
- const fieldSelector = selector[field];
14
- const filteresForNull = fieldSelector == null || fieldSelector.$exists === false;
15
- const keys = filteresForNull ? { include: null, exclude: [...index.keys()].filter((key) => key != null) } : getMatchingKeys(field, selector);
16
- if (keys.include == null && keys.exclude == null)
17
- return { matched: false };
18
- let includedPositions = [];
19
- if (keys.include == null) {
20
- for (const set of index.values()) {
21
- for (const pos of set) {
22
- includedPositions.push(pos);
23
- }
24
- }
25
- } else {
26
- for (const key of keys.include) {
27
- const posSet = index.get(key);
28
- if (posSet) {
29
- for (const pos of posSet) {
30
- includedPositions.push(pos);
31
- }
32
- }
33
- }
34
- }
35
- if (keys.exclude != null) {
36
- const excludeSet = /* @__PURE__ */ new Set();
37
- for (const key of keys.exclude) {
38
- const posSet = index.get(key);
39
- if (posSet) {
40
- for (const pos of posSet) {
41
- excludeSet.add(pos);
42
- }
43
- }
44
- }
45
- includedPositions = includedPositions.filter((pos) => !excludeSet.has(pos));
46
- }
47
- return {
48
- matched: true,
49
- positions: includedPositions,
50
- fields: [field],
51
- keepSelector: filteresForNull
52
- };
53
- },
54
- rebuild() {
55
- }
56
- });
57
- }
58
- function createIndex(field) {
59
- const index = /* @__PURE__ */ new Map();
60
- return {
61
- ...createExternalIndex(field, index),
62
- rebuild(items) {
63
- index.clear();
64
- items.forEach((item, i) => {
65
- const value = serializeValue(get(item, field));
66
- const current = index.get(value) || /* @__PURE__ */ new Set();
67
- current.add(i);
68
- index.set(value, current);
69
- });
70
- }
71
- };
72
- }
73
- exports.createExternalIndex = createExternalIndex;
74
- exports.default = createIndex;
@@ -1,10 +0,0 @@
1
- "use strict";
2
- const fastSort = require("fast-sort");
3
- const get = require("./index.cjs24.js");
4
- function sortItems(items, sortFields) {
5
- return fastSort.sort(items).by(Object.entries(sortFields).map(([key, value]) => {
6
- const order = value === 1 ? "asc" : "desc";
7
- return { [order]: (i) => get(i, key) };
8
- }));
9
- }
10
- module.exports = sortItems;
@@ -1,27 +0,0 @@
1
- "use strict";
2
- const get = require("./index.cjs24.js");
3
- const set = require("./index.cjs25.js");
4
- function project(item, fields) {
5
- const allFieldsDeactivated = Object.values(fields).every((value) => value === 0);
6
- if (allFieldsDeactivated) {
7
- const result2 = { ...item };
8
- Object.keys(fields).forEach((key) => {
9
- const fieldValue = get(item, key);
10
- if (fieldValue === void 0)
11
- return;
12
- set(result2, key, void 0, true);
13
- });
14
- return result2;
15
- }
16
- const result = {};
17
- Object.entries(fields).forEach(([key, value]) => {
18
- const fieldValue = get(item, key);
19
- if (fieldValue === void 0)
20
- return;
21
- if (fieldValue == null && value !== 1)
22
- return;
23
- set(result, key, value === 1 ? fieldValue : void 0);
24
- });
25
- return result;
26
- }
27
- module.exports = project;