@tanstack/optimistic
Version:
Core optimistic updates library
141 lines (140 loc) • 3.45 kB
JavaScript
"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
class SortedMap {
/**
* Creates a new SortedMap instance
*
* @param comparator - Optional function to compare values for sorting
*/
constructor(comparator) {
this.map = /* @__PURE__ */ new Map();
this.sortedKeys = [];
this.comparator = comparator || this.defaultComparator;
}
/**
* Default comparator function used when none is provided
*
* @param a - First value to compare
* @param b - Second value to compare
* @returns -1 if a < b, 1 if a > b, 0 if equal
*/
defaultComparator(a, b) {
if (a < b) return -1;
if (a > b) return 1;
return 0;
}
/**
* Sets a key-value pair in the map and maintains sort order
*
* @param key - The key to set
* @param value - The value to associate with the key
* @returns This SortedMap instance for chaining
*/
set(key, value) {
this.map.set(key, value);
if (!this.sortedKeys.includes(key)) {
this.sortedKeys.push(key);
}
this.sortedKeys.sort((a, b) => {
const valueA = this.map.get(a);
const valueB = this.map.get(b);
return this.comparator(valueA, valueB);
});
return this;
}
/**
* Gets a value by its key
*
* @param key - The key to look up
* @returns The value associated with the key, or undefined if not found
*/
get(key) {
return this.map.get(key);
}
/**
* Removes a key-value pair from the map
*
* @param key - The key to remove
* @returns True if the key was found and removed, false otherwise
*/
delete(key) {
if (this.map.delete(key)) {
const index = this.sortedKeys.indexOf(key);
this.sortedKeys.splice(index, 1);
return true;
}
return false;
}
/**
* Checks if a key exists in the map
*
* @param key - The key to check
* @returns True if the key exists, false otherwise
*/
has(key) {
return this.map.has(key);
}
/**
* Removes all key-value pairs from the map
*/
clear() {
this.map.clear();
this.sortedKeys = [];
}
/**
* Gets the number of key-value pairs in the map
*/
get size() {
return this.map.size;
}
/**
* Default iterator that returns entries in sorted order
*
* @returns An iterator for the map's entries
*/
*[Symbol.iterator]() {
for (const key of this.sortedKeys) {
yield [key, this.map.get(key)];
}
}
/**
* Returns an iterator for the map's entries in sorted order
*
* @returns An iterator for the map's entries
*/
entries() {
return this[Symbol.iterator]();
}
/**
* Returns an iterator for the map's keys in sorted order
*
* @returns An iterator for the map's keys
*/
keys() {
return this.sortedKeys[Symbol.iterator]();
}
/**
* Returns an iterator for the map's values in sorted order
*
* @returns An iterator for the map's values
*/
values() {
return (function* () {
for (const key of this.sortedKeys) {
yield this.map.get(key);
}
}).call(this);
}
/**
* Executes a callback function for each key-value pair in the map in sorted order
*
* @param callbackfn - Function to execute for each entry
*/
forEach(callbackfn) {
for (const key of this.sortedKeys) {
callbackfn(this.map.get(key), key, this.map);
}
}
}
exports.SortedMap = SortedMap;
//# sourceMappingURL=SortedMap.cjs.map