-
Notifications
You must be signed in to change notification settings - Fork 2
/
stations.js
40 lines (33 loc) · 825 Bytes
/
stations.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
"use strict";
require("array.prototype.find");
var stations = require("./stations.json");
var stationTerms = stations.map(function(station) {
return {
terms: [station.id, station.name].concat(station.aliases).map(simplify),
station: station,
};
});
function simplify(query) {
return query.toLowerCase()
.replace(/\W/g, "");
}
function isMatch(terms, query) {
query = simplify(query);
return terms.some(function(term) {
return term === query;
});
}
module.exports = stations;
module.exports.byId = function(id) {
return stations.find(function(station) {
return station.id === id;
});
};
module.exports.search = function(query) {
var station = stationTerms.find(function(station) {
return isMatch(station.terms, query);
});
if (station) {
return station.station;
}
};