Skip to content

Commit 6b8fb4f

Browse files
committed
Implement has() and hasMany()
Adds support of two methods: ```js await db.put('love', 'u') await db.has('love') // true await db.hasMany(['love', 'hate']) // [true, false] ``` Ref: Level/community#142 Category: addition
1 parent 946a57f commit 6b8fb4f

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

index.js

+19
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ class MemoryLevel extends AbstractLevel {
284284
permanence: false,
285285
createIfMissing: false,
286286
errorIfExists: false,
287+
has: true,
287288
encodings: { [storeEncoding]: true },
288289
signals: {
289290
// Would have no value here because the operations are synchronous
@@ -313,6 +314,20 @@ class MemoryLevel extends AbstractLevel {
313314
return keys.map(key => this[kTree].get(key))
314315
}
315316

317+
async _has (key, options) {
318+
const tree = options.snapshot != null
319+
? options.snapshot[kTree]
320+
: this[kTree]
321+
return tree.get(key) !== undefined
322+
}
323+
324+
async _hasMany (keys, options) {
325+
const tree = options.snapshot != null
326+
? options.snapshot[kTree]
327+
: this[kTree]
328+
return keys.map(has, tree)
329+
}
330+
316331
async _del (key, options) {
317332
this[kTree] = this[kTree].remove(key)
318333
}
@@ -394,3 +409,7 @@ if (typeof process !== 'undefined' && !process.browser && typeof global !== 'und
394409
function isRangeOption (k) {
395410
return rangeOptions.has(k)
396411
}
412+
413+
function has (key) {
414+
return this.get(key) !== undefined
415+
}

0 commit comments

Comments
 (0)