-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
51 lines (45 loc) · 1.42 KB
/
index.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
41
42
43
44
45
46
47
48
49
50
51
const countries = require('./data/countries.json')
const soa = require('sort-objects-array')
const filter = require('lodash.filter')
const {
lower,
toInt,
getFilterField,
getCurrencyField,
pickReturnFields,
checkDupsAltNames
} = require('./utils')
function queryCurrency (q, f) {
const lq = lower(q)
const filtered = filter(countries, (x => lower(x[getCurrencyField(q)]) === lq))
return pickReturnFields(filtered, f)
}
function queryPhone (q, f) {
const intQuery = toInt(q)
const filtered = filter(countries, x => toInt(x.phone) === intQuery)
return pickReturnFields(filtered, f)
}
function queryCitizenship (q, f) {
const lq = lower(q)
const filtered = filter(countries, (x => x.nationality.map(nat => lower(nat)).includes(lq)))
const sorted = filtered.length > 0 ? soa(filtered, 'population', 'desc') : filtered
return pickReturnFields(sorted, f)
}
function query (q, f) {
const lq = lower(q)
const filtered = filter(countries, (x => lower(x[getFilterField(lq)]) === lq))
return filtered.length > 0 ? pickReturnFields(filtered, f) : checkDupsAltNames(lq, f)
}
function queryfuzzy (q, f) {
const lq = lower(q)
const filtered = filter(countries, (x => lq.includes(lower(x.name))))
const sorted = filtered.length > 0 ? soa(filtered, 'population', 'desc') : filtered
return pickReturnFields(sorted, f)
}
module.exports = {
queryCurrency,
queryPhone,
queryCitizenship,
query,
queryfuzzy
}