-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.mjs
More file actions
61 lines (51 loc) · 1.54 KB
/
Copy pathindex.mjs
File metadata and controls
61 lines (51 loc) · 1.54 KB
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
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env node
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
import Fuse from 'fuse.js'
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const [, , ...args] = process.argv;
const flag = args[1];
const query = args[0] ?? '';
let fuzzySearch = () => {}
const dataPath = path.join(__dirname, 'data', 'data.json');
const countryCodeData = JSON.parse(
fs.readFileSync(dataPath, 'utf-8')
)
if (query === '') {
console.log('No query detected')
console.log('Please provide a query to search for. Eg: \'cntry zim\'')
} else {
const fuse = new Fuse(countryCodeData, {
keys: ['name', 'alpha2', 'alpha3'], // Fields to search
threshold: 0.3, // Adjust this for more/less fuzzy matches
});
fuzzySearch = (query) => {
const results = fuse.search(query);
if (results.length === 0) {
console.log(`No matches found for "${query}"`);
return;
}
results.forEach(({ item }) => {
console.log(JSON.stringify(item, null, 2));
});
}
}
if (flag === '-r') {
fuzzySearch = (query) => {
const normalizedQuery = query.toUpperCase().trim()
const found = countryCodeData.find(eachCountry => {
const { alpha2, alpha3 } = eachCountry;
if (alpha2 === normalizedQuery || alpha3 === normalizedQuery) {
return true
}
return false
})
if (found === undefined) {
console.log(`No matches found for "${query}"`);
}
console.log(JSON.stringify(found, null, 2));
}
}
fuzzySearch(query)