-
Notifications
You must be signed in to change notification settings - Fork 8
/
querymatcher.js
67 lines (64 loc) · 2.09 KB
/
querymatcher.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
67
var T = require('transducers.js')
var onlySimilar = function(item, index) {
var record = item[0]
return record.toLowerCase().indexOf(this.query.toLowerCase()) >= 0
}
var onlyRecordType = function(item, index) {
return item[1][this.recordType] != undefined
}
var intoResponses = function(item, index) {
var record = item[0]
var data = item[1]
var records = data[this.recordType].map(function(store_data) {
return {
name : record.toLowerCase(),
type : this.recordType,
store_data : store_data,
ttl : data.ttl,
}
}.bind(this))
return [ record, records ]
}
var intoGroups = function(item, index) {
var record_name = item[0]
var records = item[1]
if (!this.wildcard && record_name != this.query) records.forEach(function(record) {
record.name = this.query.toLowerCase()
}.bind(this))
return item
}
var transformer = {
init : function() {
return []
},
step : function(result, x) {
x[1].forEach(function(record) {
var store_data = typeof record.store_data == 'object' ? record.store_data : {}
Object.keys(store_data).forEach(function(key) {
record[key] = record.store_data[key]
})
delete record.store_data
result.push(record)
})
return result
},
result : function(result) {
return result
}
}
/** This is where the magic happens **/
var matcher = function(records, query, type, wildcard) {
var filterAndMap = T.compose(
T.filter(onlySimilar.bind({ query : query })),
T.filter(onlyRecordType.bind({ recordType : type })),
T.map(intoResponses.bind({ recordType : type })),
T.map(intoGroups.bind({ query : query, wildcard : wildcard }))
)
return T.transduce(records, filterAndMap, transformer)
}
module.exports = function(records, query, type) {
var wildcard = false
if (query[0] == '*') { query = query.split('*.')[1]; wildcard = true }
if (!query) return []
return matcher(records, query, type, wildcard)
}