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