Adds clear to levelup + updates tests (#40792)

* feat: adds clear to levelup

Signed-off-by: Carson Farmer <carson.farmer@gmail.com>

* docs: update contributors

Signed-off-by: Carson Farmer <carson.farmer@gmail.com>
This commit is contained in:
Carson Farmer
2019-12-05 18:56:15 -08:00
committed by Ron Buckton
parent eee201b2ad
commit 331b9e134c
2 changed files with 34 additions and 1 deletions
+26 -1
View File
@@ -1,7 +1,8 @@
// Type definitions for levelup 3.1
// Type definitions for levelup 4.3
// Project: https://github.com/Level/levelup
// Definitions by: Meirion Hughes <https://github.com/MeirionHughes>
// Daniel Byrne <https://github.com/danwbyrne>
// Carson Farmer <https://github.com/carsonfarmer>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.8
@@ -25,6 +26,11 @@ type LevelUpDel<K, O> =
((key: K, options: O, callback: ErrorCallback) => void) &
((key: K, options?: O) => Promise<void>);
type LevelUpClear<O> =
((callback: ErrorCallback) => void) &
((options: O, callback: ErrorCallback) => void) &
((options?: O) => Promise<void>);
type LevelUpBatch<K, O> =
((key: K, callback: ErrorCallback) => void) &
((key: K, options: O, callback: ErrorCallback) => void) &
@@ -45,6 +51,20 @@ type InferDBDel<DB> =
LevelUpDel<K, O> :
LevelUpDel<any, AbstractOptions>;
type InferDBClear<DB> =
DB extends { clear: (options: infer O, callback: ErrorCallback) => void } ?
LevelUpClear<O> :
LevelUpClear<AbstractClearOptions>;
interface AbstractClearOptions<K = any> extends AbstractOptions {
gt?: K;
gte?: K;
lt?: K;
lte?: K;
reverse?: boolean;
limit?: number;
}
export interface LevelUp<DB = AbstractLevelDOWN, Iterator = AbstractIterator<any, any>> extends EventEmitter {
open(): Promise<void>;
open(callback?: ErrorCallback): void;
@@ -54,6 +74,7 @@ export interface LevelUp<DB = AbstractLevelDOWN, Iterator = AbstractIterator<any
put: InferDBPut<DB>;
get: InferDBGet<DB>;
del: InferDBDel<DB>;
clear: InferDBClear<DB>;
batch(array: AbstractBatch[], options?: any): Promise<void>;
batch(array: AbstractBatch[], options: any, callback: (err?: any) => any): void;
@@ -82,6 +103,10 @@ export interface LevelUp<DB = AbstractLevelDOWN, Iterator = AbstractIterator<any
*/
on(event: 'batch', cb: (ary: any[]) => void): this;
/*
emitted when clear is called
*/
on(event: 'clear', cb: (opts: any) => void): this;
/*
emitted on given event
*/
on(event: 'open' | 'ready' | 'closed' | 'opening' | 'closing', cb: () => void): this;
+8
View File
@@ -68,3 +68,11 @@ db.createReadStream()
.on('end', () => {
console.log('Stream closed');
});
db.clear((error) => {
});
db.clear({ gt: 'hello' }, (error) => {
});
db.clear().then(() => console.log('cleared'));