-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
57 lines (46 loc) · 934 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
* Module dependencies.
*/
var once = require('once');
/**
* Expose `exists` and `install`.
*/
module.exports = exists;
exists.install = install;
/**
* Check if there is a value stored under `key`.
*
* @param {LevelUp} db
* @param {Object} key
* @param {Function} cb
*/
function exists(db, key, cb) {
if (typeof db == 'undefined') throw new Error('database required');
if (typeof key == 'undefined') throw new Error('key required');
if (typeof cb == 'undefined') throw new Error('callback required');
cb = once(cb);
db.createKeyStream({
gte: key,
lte: key
})
.on('data', function() {
cb(null, true);
})
.on('error', function(err) {
cb(err);
})
.on('end', function() {
cb(null, false);
});
}
/**
* Install `exists` on a `db`.
*
* @param {LevelUp} db
* @return {LevelUp}
*/
function install(db) {
db.exists = function(key, cb) {
exists(db, key, cb);
};
}