-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.js
More file actions
24 lines (19 loc) · 723 Bytes
/
Copy pathsearch.js
File metadata and controls
24 lines (19 loc) · 723 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
const EventEmitter = require('events');
const API = require('./mock-api');
// To count the matches, call API.countMatches(term) where term is the search term
class Search extends EventEmitter {
async searchCount(searchTerm) {
if (searchTerm === undefined) {
this.emit('SEARCH_ERROR', { message: 'INVALID_TERM' });
return;
}
this.emit('SEARCH_STARTED', searchTerm);
try {
const count = await API.countMatches(searchTerm);
this.emit('SEARCH_SUCCESS', { count, term: searchTerm });
} catch (error) {
this.emit('SEARCH_ERROR', { message: error?.message, term: searchTerm });
}
}
}
module.exports = Search;