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