From fbad5275d8e4946e55c5477c62e202563c3880af Mon Sep 17 00:00:00 2001 From: Vincent Weevers Date: Sun, 5 Jan 2025 23:52:24 +0100 Subject: [PATCH] Implement `has()` and `hasMany()` (#18) Ref: https://github.com/Level/community/issues/142 Category: addition --- index.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/index.js b/index.js index 5eb27d1..3af8640 100644 --- a/index.js +++ b/index.js @@ -290,6 +290,7 @@ class MemoryLevel extends AbstractLevel { permanence: false, createIfMissing: false, errorIfExists: false, + has: true, encodings: { [storeEncoding]: true }, signals: { // Would have no value here because the operations are synchronous @@ -327,6 +328,20 @@ class MemoryLevel extends AbstractLevel { return keys.map(getFromThis, tree) } + async _has (key, options) { + const tree = options.snapshot != null + ? options.snapshot[kTree] + : this[kTree] + return tree.get(key) !== undefined + } + + async _hasMany (keys, options) { + const tree = options.snapshot != null + ? options.snapshot[kTree] + : this[kTree] + return keys.map(has, tree) + } + async _del (key, options) { this[kTree] = this[kTree].remove(key) } @@ -423,3 +438,7 @@ function getFromThis (key) { function isRangeOption (k) { return rangeOptions.has(k) } + +function has (key) { + return this.get(key) !== undefined +}