UNPKG

1.56 kBJavaScriptView Raw
1'use strict'
2
3const argx = require('argx')
4const co = require('co')
5const SrchLogger = require('./logging/srch_logger')
6
7const types = require('./types.json')
8const matchAnyProp = require('./matching/match_any_prop')
9const searchType = require('./searching/search_type')
10
11/** @lends apemanSrch */
12function apemanSrch (terms, options) {
13 let args = argx(arguments)
14 options = args.pop('object') || {}
15 terms = args.remain()
16
17 let typeName = options.type
18 let logger = new SrchLogger({})
19
20 return co(function * () {
21 let invalid = typeName && !types[ typeName ]
22 if (invalid) {
23 throw new Error('Unknown type: ' + typeName)
24 }
25 logger.logSearchStart()
26 let found = []
27 if (typeName) {
28 found.push(
29 yield searchType(types[ typeName ])
30 )
31 } else {
32 for (let typeName of Object.keys(types)) {
33 found.push(
34 yield searchType(types[ typeName ])
35 )
36 }
37 }
38 logger.logSearchDone()
39 found = [].concat(found || [])
40 .filter((found) => !!found)
41 .filter((found) => {
42 if (terms.length === 0) {
43 found.hit = true
44 return found
45 }
46 for (let i = 0, len = terms.length; i < len; i++) {
47 let term = terms[ i ]
48 let hit = matchAnyProp(found, term)
49 if (hit) {
50 found.hit = true
51 found.hitTerm = term
52 return found
53 }
54 }
55 found.hit = false
56 return found
57 })
58 logger.logSearched(found, terms)
59 return found
60 })
61}
62
63module.exports = apemanSrch