-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (51 loc) · 1.63 KB
/
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
57
58
59
60
61
62
63
64
65
66
'use strict'
function reachdown (db, visit, strict) {
return walk(db, visitor(visit), !!visit && strict !== false)
}
function walk (db, visit, strict) {
if (visit(db, type(db))) return db
if (isAbstract(db.db)) return walk(db.db, visit, strict)
if (isAbstract(db._db)) return walk(db._db, visit, strict)
if (isLevelup(db.db)) return walk(db.db, visit, strict)
if (strict) return null
return db
}
function isAbstract (db) {
// Loose by design, for when node_modules contains multiple versions of abstract-leveldown.
return isObject(db) && typeof db._batch === 'function' && typeof db._iterator === 'function'
}
function visitor (v) {
return typeof v === 'function' ? v : v ? typeVisitor(v) : none
}
function typeVisitor (wanted) {
return function (db, type) {
return type ? type === wanted : false
}
}
function none () {
return false
}
function type (db) {
if (db.type) return db.type
// Feature-detect older versions (that don't have a type property)
if (isLevelup(db)) return 'levelup' // For levelup < 4.3.0
if (isEncdown(db)) return 'encoding-down' // For < 6.3.0
if (isDeferred(db)) return 'deferred-leveldown' // For < 5.2.1
}
function isLevelup (db) {
return isObject(db) && /^levelup$/i.test(db)
}
function isEncdown (db) {
return isObject(db) && isObject(db.codec) && isObject(db.codec.encodings)
}
function isDeferred (db) {
return isObject(db) && Array.isArray(db._operations) && Array.isArray(db._iterators)
}
function isObject (o) {
return typeof o === 'object' && o !== null
}
function is (db, visit) {
return !!visitor(visit)(db, type(db))
}
reachdown.is = is
module.exports = reachdown