@adiwajshing/keyed-db
Advanced tools
Comparing version 0.2.3 to 0.2.4
@@ -49,3 +49,13 @@ import { IKeyedDB, Identifiable, Comparable, PaginationMode } from "./Types"; | ||
all(): T[]; | ||
updateKey(value: T, update: (value: T) => void): void; | ||
/** | ||
* Updates a value specified by the ID | ||
* and adjusts its position in the DB after an update if required | ||
* @param id | ||
* @param update | ||
*/ | ||
update(id: string, update: (value: T) => void): 1 | 2; | ||
/** | ||
* @deprecated see `update` | ||
*/ | ||
updateKey(value: T, update: (value: T) => void): 1 | 2; | ||
filter(predicate: (value: T, index: number) => boolean): KeyedDB<T, K>; | ||
@@ -52,0 +62,0 @@ /** |
@@ -121,6 +121,31 @@ "use strict"; | ||
} | ||
/** | ||
* Updates a value specified by the ID | ||
* and adjusts its position in the DB after an update if required | ||
* @param id | ||
* @param update | ||
*/ | ||
update(id, update) { | ||
const value = this.get(id); | ||
if (value) { | ||
const idx = this.firstIndex(value); | ||
if (idx >= 0 && idx < this.array.length && this.idGetter(this.array[idx]) === id) { | ||
const oldKey = this.key.key(value); | ||
update(value); | ||
const newKey = this.key.key(value); | ||
if (newKey !== oldKey) { | ||
delete this.dict[id]; | ||
this.array.splice(idx, 1); | ||
this._insertSingle(value); | ||
return 2; | ||
} | ||
return 1; | ||
} | ||
} | ||
} | ||
/** | ||
* @deprecated see `update` | ||
*/ | ||
updateKey(value, update) { | ||
this.delete(value); | ||
update(value); | ||
this.insert(value); | ||
return this.update(this.idGetter(value), update); | ||
} | ||
@@ -218,5 +243,6 @@ filter(predicate) { | ||
firstIndex(value) { | ||
return BinarySearch_1.default(this.array, v => this.key.compare(this.key.key(value), this.key.key(v))); | ||
const valueKey = this.key.key(value); | ||
return BinarySearch_1.default(this.array, v => this.key.compare(valueKey, this.key.key(v))); | ||
} | ||
} | ||
exports.default = KeyedDB; |
{ | ||
"name": "@adiwajshing/keyed-db", | ||
"version": "0.2.3", | ||
"version": "0.2.4", | ||
"description": "Lightweight library to store an in-memory DB", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/adiwajshing/keyed-db", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17723
387