@adiwajshing/keyed-db
Advanced tools
Comparing version 0.1.1 to 0.1.2
@@ -18,2 +18,3 @@ export default class KeyedDB<T> { | ||
updateKey(value: T, update: (value: T) => void): void; | ||
filter(predicate: (value: T, index: number) => boolean): KeyedDB<T>; | ||
paginatedByValue(value: T | null, limit: number): T[]; | ||
@@ -20,0 +21,0 @@ paginated(cursor: number | null, limit: number): T[]; |
@@ -28,3 +28,3 @@ "use strict"; | ||
else | ||
throw new Error(`duplicate key: ${this.key(value)}, values: ${JSON.stringify(value)}, ${JSON.stringify(this.array[index])}`); | ||
throw new Error(`duplicate key: ${this.key(value)}, of inserting: ${this.idGetter(value)}, present: ${this.idGetter(this.array[index])}`); | ||
} | ||
@@ -59,2 +59,12 @@ else { | ||
} | ||
filter(predicate) { | ||
const db = new KeyedDB(this.key, this.idGetter); | ||
db.array = this.array.filter((value, index) => { | ||
if (predicate(value, index)) { | ||
db.dict[this.idGetter(value)] = value; | ||
return true; | ||
} | ||
}); | ||
return db; | ||
} | ||
paginatedByValue(value, limit) { | ||
@@ -61,0 +71,0 @@ return this.paginated(value && this.key(value), limit); |
{ | ||
"name": "@adiwajshing/keyed-db", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Lightweight library to store an in-memory DB", | ||
@@ -15,3 +15,2 @@ "homepage": "https://github.com/adiwajshing/keyed-db", | ||
"test": "mocha --timeout 60000 -r ts-node/register src/Tests.ts", | ||
"lint": "eslint '*/*.ts' --quiet --fix", | ||
"build": "tsc" | ||
@@ -26,10 +25,6 @@ }, | ||
"devDependencies": { | ||
"@types/mocha": "^7.0.2", | ||
"@types/node": "^14.0.14", | ||
"@types/ws": "^7.2.6", | ||
"assert": "^2.0.0", | ||
"mocha": "^8.0.1", | ||
"ts-node-dev": "^1.0.0-pre.49", | ||
"typescript": "^3.9.5" | ||
"mocha": "^8.0.1" | ||
} | ||
} |
@@ -29,3 +29,3 @@ export default class KeyedDB<T> { | ||
else if (this.key(value) !== this.key(this.array[index])) this.array.splice (index, 0, value) | ||
else throw new Error(`duplicate key: ${this.key(value)}, values: ${JSON.stringify(value)}, ${JSON.stringify(this.array[index])}`) | ||
else throw new Error(`duplicate key: ${this.key(value)}, of inserting: ${this.idGetter(value)}, present: ${this.idGetter(this.array[index])}`) | ||
@@ -61,2 +61,12 @@ } else { | ||
} | ||
filter (predicate: (value: T, index: number) => boolean) { | ||
const db = new KeyedDB (this.key, this.idGetter) | ||
db.array = this.array.filter ((value, index) => { | ||
if (predicate(value, index)) { | ||
db.dict[ this.idGetter (value) ] = value | ||
return true | ||
} | ||
}) | ||
return db | ||
} | ||
paginatedByValue(value: T | null, limit: number) { | ||
@@ -63,0 +73,0 @@ return this.paginated (value && this.key(value), limit) |
15264
3
335