get-lat-long 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/data/data.json +1 -1
- package/package.json +1 -1
- package/src/index.js +34 -7
package/package.json
CHANGED
package/src/index.js
CHANGED
@@ -1,12 +1,39 @@
|
|
1
1
|
#!/usr/bin/env -S node --experimental-vm-modules
|
2
2
|
"use strict";
|
3
3
|
|
4
|
-
const
|
5
|
-
|
6
|
-
|
7
|
-
|
4
|
+
const data = require("../data/data.json");
|
5
|
+
|
6
|
+
function binarySearchCity(city_name) {
|
7
|
+
let l = 0,
|
8
|
+
r = data.length - 1;
|
9
|
+
|
10
|
+
while (l <= r) {
|
11
|
+
const mid = l + parseInt((r - l) / 2);
|
12
|
+
if (data[mid][1].startsWith(city_name)) return data[mid];
|
13
|
+
if (city_name < data[mid][1]) r = mid - 1;
|
14
|
+
else l = mid + 1;
|
15
|
+
}
|
16
|
+
|
17
|
+
return;
|
8
18
|
}
|
9
|
-
const [city] = args;
|
10
19
|
|
11
|
-
|
12
|
-
console.
|
20
|
+
function findByCity(query_city) {
|
21
|
+
console.time("Searching");
|
22
|
+
// TC - 1.692ms, 1.56ms, 1.556ms, 1.7ms, 1.568ms, 1.56ms
|
23
|
+
// const found = data.find((element) => element[1].startsWith(query_city));
|
24
|
+
// TC - 0.084ms, 0.078ms, 0.08ms, 0.081ms, 0.088ms, 0.076ms
|
25
|
+
const found = binarySearchCity(query_city);
|
26
|
+
console.timeEnd("Searching");
|
27
|
+
if (!found) return console.log("No matches found");
|
28
|
+
|
29
|
+
const [city, city_ls, lat, lng, country, population] = found;
|
30
|
+
console.log({ city, city_ls, lat, lng, country, population });
|
31
|
+
}
|
32
|
+
|
33
|
+
const args = process.argv.slice(2);
|
34
|
+
if (args.length < 1 || args.length > 1) {
|
35
|
+
console.error("Invalid params provided");
|
36
|
+
process.exit(1);
|
37
|
+
}
|
38
|
+
const [query_city] = args;
|
39
|
+
findByCity(query_city);
|