-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
28 lines (23 loc) · 858 Bytes
/
main.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
const couch_ = module.exports = {}
couch_.mapResult = (res, type) => res.rows.map(row => row[type])
couch_.mapDoc = res => couch_.mapResult(res, 'doc')
couch_.mapId = res => res.rows.map(row => row.id)
couch_.mapValue = res => res.rows.map(row => row.value)
couch_.mapValueId = res => res.rows.map(row => row.value._id)
couch_.firstDoc = docs => docs != null ? docs[0] : null
couch_.joinOrderedIds = function (idA, idB) {
if (idA < idB) return `${idA}:${idB}`
else return `${idB}:${idA}`
}
couch_.ignoreNotFound = err => {
if (!(err && err.error === 'not_found')) throw err
}
couch_.getObjIfSuccess = (db, body) => {
if ((db.get != null) && body.ok) {
return db.get(body.id)
} else if (db.get != null) {
throw new Error(`${body.error}: ${body.reason}`)
} else {
throw new Error('bad db object passed to _.getObjIfSuccess')
}
}