@tanstack/db 0.0.11 → 0.0.13
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/dist/cjs/SortedMap.cjs +38 -11
- package/dist/cjs/SortedMap.cjs.map +1 -1
- package/dist/cjs/SortedMap.d.cts +10 -0
- package/dist/cjs/collection.cjs +476 -144
- package/dist/cjs/collection.cjs.map +1 -1
- package/dist/cjs/collection.d.cts +107 -32
- package/dist/cjs/index.cjs +2 -1
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +1 -0
- package/dist/cjs/optimistic-action.cjs +21 -0
- package/dist/cjs/optimistic-action.cjs.map +1 -0
- package/dist/cjs/optimistic-action.d.cts +39 -0
- package/dist/cjs/query/compiled-query.cjs +38 -16
- package/dist/cjs/query/compiled-query.cjs.map +1 -1
- package/dist/cjs/query/query-builder.cjs +2 -2
- package/dist/cjs/query/query-builder.cjs.map +1 -1
- package/dist/cjs/transactions.cjs +3 -1
- package/dist/cjs/transactions.cjs.map +1 -1
- package/dist/cjs/types.d.cts +83 -10
- package/dist/esm/SortedMap.d.ts +10 -0
- package/dist/esm/SortedMap.js +38 -11
- package/dist/esm/SortedMap.js.map +1 -1
- package/dist/esm/collection.d.ts +107 -32
- package/dist/esm/collection.js +477 -145
- package/dist/esm/collection.js.map +1 -1
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +3 -2
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/optimistic-action.d.ts +39 -0
- package/dist/esm/optimistic-action.js +21 -0
- package/dist/esm/optimistic-action.js.map +1 -0
- package/dist/esm/query/compiled-query.js +38 -16
- package/dist/esm/query/compiled-query.js.map +1 -1
- package/dist/esm/query/query-builder.js +2 -2
- package/dist/esm/query/query-builder.js.map +1 -1
- package/dist/esm/transactions.js +3 -1
- package/dist/esm/transactions.js.map +1 -1
- package/dist/esm/types.d.ts +83 -10
- package/package.json +1 -1
- package/src/SortedMap.ts +46 -13
- package/src/collection.ts +689 -239
- package/src/index.ts +1 -0
- package/src/optimistic-action.ts +65 -0
- package/src/query/compiled-query.ts +79 -21
- package/src/query/query-builder.ts +2 -2
- package/src/transactions.ts +6 -1
- package/src/types.ts +124 -8
package/dist/cjs/SortedMap.cjs
CHANGED
|
@@ -23,6 +23,33 @@ class SortedMap {
|
|
|
23
23
|
if (a > b) return 1;
|
|
24
24
|
return 0;
|
|
25
25
|
}
|
|
26
|
+
/**
|
|
27
|
+
* Finds the index where a key-value pair should be inserted to maintain sort order.
|
|
28
|
+
* Uses binary search to find the correct position based on the value.
|
|
29
|
+
* Hence, it is in O(log n) time.
|
|
30
|
+
*
|
|
31
|
+
* @param key - The key to find position for
|
|
32
|
+
* @param value - The value to compare against
|
|
33
|
+
* @returns The index where the key should be inserted
|
|
34
|
+
*/
|
|
35
|
+
indexOf(value) {
|
|
36
|
+
let left = 0;
|
|
37
|
+
let right = this.sortedKeys.length;
|
|
38
|
+
while (left < right) {
|
|
39
|
+
const mid = Math.floor((left + right) / 2);
|
|
40
|
+
const midKey = this.sortedKeys[mid];
|
|
41
|
+
const midValue = this.map.get(midKey);
|
|
42
|
+
const comparison = this.comparator(value, midValue);
|
|
43
|
+
if (comparison < 0) {
|
|
44
|
+
right = mid;
|
|
45
|
+
} else if (comparison > 0) {
|
|
46
|
+
left = mid + 1;
|
|
47
|
+
} else {
|
|
48
|
+
return mid;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return left;
|
|
52
|
+
}
|
|
26
53
|
/**
|
|
27
54
|
* Sets a key-value pair in the map and maintains sort order
|
|
28
55
|
*
|
|
@@ -31,15 +58,14 @@ class SortedMap {
|
|
|
31
58
|
* @returns This SortedMap instance for chaining
|
|
32
59
|
*/
|
|
33
60
|
set(key, value) {
|
|
34
|
-
this.map.
|
|
35
|
-
|
|
36
|
-
this.
|
|
61
|
+
if (this.map.has(key)) {
|
|
62
|
+
const oldValue = this.map.get(key);
|
|
63
|
+
const oldIndex = this.indexOf(oldValue);
|
|
64
|
+
this.sortedKeys.splice(oldIndex, 1);
|
|
37
65
|
}
|
|
38
|
-
this.
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
return this.comparator(valueA, valueB);
|
|
42
|
-
});
|
|
66
|
+
const index = this.indexOf(value);
|
|
67
|
+
this.sortedKeys.splice(index, 0, key);
|
|
68
|
+
this.map.set(key, value);
|
|
43
69
|
return this;
|
|
44
70
|
}
|
|
45
71
|
/**
|
|
@@ -58,10 +84,11 @@ class SortedMap {
|
|
|
58
84
|
* @returns True if the key was found and removed, false otherwise
|
|
59
85
|
*/
|
|
60
86
|
delete(key) {
|
|
61
|
-
if (this.map.
|
|
62
|
-
const
|
|
87
|
+
if (this.map.has(key)) {
|
|
88
|
+
const oldValue = this.map.get(key);
|
|
89
|
+
const index = this.indexOf(oldValue);
|
|
63
90
|
this.sortedKeys.splice(index, 1);
|
|
64
|
-
return
|
|
91
|
+
return this.map.delete(key);
|
|
65
92
|
}
|
|
66
93
|
return false;
|
|
67
94
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SortedMap.cjs","sources":["../../src/SortedMap.ts"],"sourcesContent":["/**\n * A Map implementation that keeps its entries sorted based on a comparator function\n * @template TKey - The type of keys in the map\n * @template TValue - The type of values in the map\n */\nexport class SortedMap<TKey, TValue> {\n private map: Map<TKey, TValue>\n private sortedKeys: Array<TKey>\n private comparator: (a: TValue, b: TValue) => number\n\n /**\n * Creates a new SortedMap instance\n *\n * @param comparator - Optional function to compare values for sorting\n */\n constructor(comparator?: (a: TValue, b: TValue) => number) {\n this.map = new Map<TKey, TValue>()\n this.sortedKeys = []\n this.comparator = comparator || this.defaultComparator\n }\n\n /**\n * Default comparator function used when none is provided\n *\n * @param a - First value to compare\n * @param b - Second value to compare\n * @returns -1 if a < b, 1 if a > b, 0 if equal\n */\n private defaultComparator(a: TValue, b: TValue): number {\n if (a < b) return -1\n if (a > b) return 1\n return 0\n }\n\n /**\n * Sets a key-value pair in the map and maintains sort order\n *\n * @param key - The key to set\n * @param value - The value to associate with the key\n * @returns This SortedMap instance for chaining\n */\n set(key: TKey, value: TValue): this {\n this.map.
|
|
1
|
+
{"version":3,"file":"SortedMap.cjs","sources":["../../src/SortedMap.ts"],"sourcesContent":["/**\n * A Map implementation that keeps its entries sorted based on a comparator function\n * @template TKey - The type of keys in the map\n * @template TValue - The type of values in the map\n */\nexport class SortedMap<TKey, TValue> {\n private map: Map<TKey, TValue>\n private sortedKeys: Array<TKey>\n private comparator: (a: TValue, b: TValue) => number\n\n /**\n * Creates a new SortedMap instance\n *\n * @param comparator - Optional function to compare values for sorting\n */\n constructor(comparator?: (a: TValue, b: TValue) => number) {\n this.map = new Map<TKey, TValue>()\n this.sortedKeys = []\n this.comparator = comparator || this.defaultComparator\n }\n\n /**\n * Default comparator function used when none is provided\n *\n * @param a - First value to compare\n * @param b - Second value to compare\n * @returns -1 if a < b, 1 if a > b, 0 if equal\n */\n private defaultComparator(a: TValue, b: TValue): number {\n if (a < b) return -1\n if (a > b) return 1\n return 0\n }\n\n /**\n * Finds the index where a key-value pair should be inserted to maintain sort order.\n * Uses binary search to find the correct position based on the value.\n * Hence, it is in O(log n) time.\n *\n * @param key - The key to find position for\n * @param value - The value to compare against\n * @returns The index where the key should be inserted\n */\n private indexOf(value: TValue): number {\n let left = 0\n let right = this.sortedKeys.length\n\n while (left < right) {\n const mid = Math.floor((left + right) / 2)\n const midKey = this.sortedKeys[mid]!\n const midValue = this.map.get(midKey)!\n const comparison = this.comparator(value, midValue)\n\n if (comparison < 0) {\n right = mid\n } else if (comparison > 0) {\n left = mid + 1\n } else {\n return mid\n }\n }\n\n return left\n }\n\n /**\n * Sets a key-value pair in the map and maintains sort order\n *\n * @param key - The key to set\n * @param value - The value to associate with the key\n * @returns This SortedMap instance for chaining\n */\n set(key: TKey, value: TValue): this {\n if (this.map.has(key)) {\n // Need to remove the old key from the sorted keys array\n const oldValue = this.map.get(key)!\n const oldIndex = this.indexOf(oldValue)\n this.sortedKeys.splice(oldIndex, 1)\n }\n\n // Insert the new key at the correct position\n const index = this.indexOf(value)\n this.sortedKeys.splice(index, 0, key)\n\n this.map.set(key, value)\n\n return this\n }\n\n /**\n * Gets a value by its key\n *\n * @param key - The key to look up\n * @returns The value associated with the key, or undefined if not found\n */\n get(key: TKey): TValue | undefined {\n return this.map.get(key)\n }\n\n /**\n * Removes a key-value pair from the map\n *\n * @param key - The key to remove\n * @returns True if the key was found and removed, false otherwise\n */\n delete(key: TKey): boolean {\n if (this.map.has(key)) {\n const oldValue = this.map.get(key)\n const index = this.indexOf(oldValue!)\n this.sortedKeys.splice(index, 1)\n return this.map.delete(key)\n }\n\n return false\n }\n\n /**\n * Checks if a key exists in the map\n *\n * @param key - The key to check\n * @returns True if the key exists, false otherwise\n */\n has(key: TKey): boolean {\n return this.map.has(key)\n }\n\n /**\n * Removes all key-value pairs from the map\n */\n clear(): void {\n this.map.clear()\n this.sortedKeys = []\n }\n\n /**\n * Gets the number of key-value pairs in the map\n */\n get size(): number {\n return this.map.size\n }\n\n /**\n * Default iterator that returns entries in sorted order\n *\n * @returns An iterator for the map's entries\n */\n *[Symbol.iterator](): IterableIterator<[TKey, TValue]> {\n for (const key of this.sortedKeys) {\n yield [key, this.map.get(key)!] as [TKey, TValue]\n }\n }\n\n /**\n * Returns an iterator for the map's entries in sorted order\n *\n * @returns An iterator for the map's entries\n */\n entries(): IterableIterator<[TKey, TValue]> {\n return this[Symbol.iterator]()\n }\n\n /**\n * Returns an iterator for the map's keys in sorted order\n *\n * @returns An iterator for the map's keys\n */\n keys(): IterableIterator<TKey> {\n return this.sortedKeys[Symbol.iterator]()\n }\n\n /**\n * Returns an iterator for the map's values in sorted order\n *\n * @returns An iterator for the map's values\n */\n values(): IterableIterator<TValue> {\n return function* (this: SortedMap<TKey, TValue>) {\n for (const key of this.sortedKeys) {\n yield this.map.get(key)!\n }\n }.call(this)\n }\n\n /**\n * Executes a callback function for each key-value pair in the map in sorted order\n *\n * @param callbackfn - Function to execute for each entry\n */\n forEach(\n callbackfn: (value: TValue, key: TKey, map: Map<TKey, TValue>) => void\n ): void {\n for (const key of this.sortedKeys) {\n callbackfn(this.map.get(key)!, key, this.map)\n }\n }\n}\n"],"names":[],"mappings":";;AAKO,MAAM,UAAwB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUnC,YAAY,YAA+C;AACpD,SAAA,0BAAU,IAAkB;AACjC,SAAK,aAAa,CAAC;AACd,SAAA,aAAa,cAAc,KAAK;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAU/B,kBAAkB,GAAW,GAAmB;AAClD,QAAA,IAAI,EAAU,QAAA;AACd,QAAA,IAAI,EAAU,QAAA;AACX,WAAA;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAYD,QAAQ,OAAuB;AACrC,QAAI,OAAO;AACP,QAAA,QAAQ,KAAK,WAAW;AAE5B,WAAO,OAAO,OAAO;AACnB,YAAM,MAAM,KAAK,OAAO,OAAO,SAAS,CAAC;AACnC,YAAA,SAAS,KAAK,WAAW,GAAG;AAClC,YAAM,WAAW,KAAK,IAAI,IAAI,MAAM;AACpC,YAAM,aAAa,KAAK,WAAW,OAAO,QAAQ;AAElD,UAAI,aAAa,GAAG;AACV,gBAAA;AAAA,MAAA,WACC,aAAa,GAAG;AACzB,eAAO,MAAM;AAAA,MAAA,OACR;AACE,eAAA;AAAA,MAAA;AAAA,IACT;AAGK,WAAA;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUT,IAAI,KAAW,OAAqB;AAClC,QAAI,KAAK,IAAI,IAAI,GAAG,GAAG;AAErB,YAAM,WAAW,KAAK,IAAI,IAAI,GAAG;AAC3B,YAAA,WAAW,KAAK,QAAQ,QAAQ;AACjC,WAAA,WAAW,OAAO,UAAU,CAAC;AAAA,IAAA;AAI9B,UAAA,QAAQ,K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,EAQ1C,SAAmC;AACjC,YAAO,aAA0C;AACpC,iBAAA,OAAO,KAAK,YAAY;AAC3B,cAAA,KAAK,IAAI,IAAI,GAAG;AAAA,MAAA;AAAA,IACxB,GACA,KAAK,IAAI;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQb,QACE,YACM;AACK,eAAA,OAAO,KAAK,YAAY;AACjC,iBAAW,KAAK,IAAI,IAAI,GAAG,GAAI,KAAK,KAAK,GAAG;AAAA,IAAA;AAAA,EAC9C;AAEJ;;"}
|
package/dist/cjs/SortedMap.d.cts
CHANGED
|
@@ -21,6 +21,16 @@ export declare class SortedMap<TKey, TValue> {
|
|
|
21
21
|
* @returns -1 if a < b, 1 if a > b, 0 if equal
|
|
22
22
|
*/
|
|
23
23
|
private defaultComparator;
|
|
24
|
+
/**
|
|
25
|
+
* Finds the index where a key-value pair should be inserted to maintain sort order.
|
|
26
|
+
* Uses binary search to find the correct position based on the value.
|
|
27
|
+
* Hence, it is in O(log n) time.
|
|
28
|
+
*
|
|
29
|
+
* @param key - The key to find position for
|
|
30
|
+
* @param value - The value to compare against
|
|
31
|
+
* @returns The index where the key should be inserted
|
|
32
|
+
*/
|
|
33
|
+
private indexOf;
|
|
24
34
|
/**
|
|
25
35
|
* Sets a key-value pair in the map and maintains sort order
|
|
26
36
|
*
|