Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d1e360f

Browse files
authoredMay 5, 2019
Bugfix/fix return type of latlng (#12)
* bump a version to test npm build * update the output for accuracy test * remove peer dependencies and update readme * change returning lat/lng to number instead of string
1 parent 5cc104d commit d1e360f

File tree

7 files changed

+88
-207
lines changed

7 files changed

+88
-207
lines changed
 

‎README.md

+3-6
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@
22

33
## Installation
44

5-
You are required to install these two dependencies yourself if they are not already in your project:
6-
```bash
7-
npm i @turf/turf proj4
8-
```
9-
Then install the address parser:
5+
Install library from npm directly
6+
107
```bash
11-
npm i hk-address-parser
8+
npm i hk-address-parser-lib
129
```
1310

1411
## Usage

‎accuracy_test/full_test.js

+34-15
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ function toRad(Value) {
4747

4848
/**
4949
* Load the test cases from file
50-
* @param {string} filePath
50+
* @param {string} filePath
5151
*/
5252
async function readTestCases(filePath) {
5353
return new Promise((resolve, reject) => {
@@ -141,12 +141,27 @@ function loadFromCache(url) {
141141

142142
// Replace the fetch function
143143
const nodeFetch = global.fetch;
144+
const metrics = {
145+
loadedFromCache: 0,
146+
totalRequest: 0,
147+
repeatedRequest: 0,
148+
uriHash: {},
149+
}
150+
144151
global.fetch = function (...args) {
145152
const url = args[0];
146153

154+
metrics.totalRequest += 1;
155+
if (metrics.uriHash[url] === undefined) {
156+
metrics.uriHash[url] = 1;
157+
} else {
158+
metrics.uriHash[url] += 1;
159+
}
160+
147161
// try to load the cached files
148162
const cache = loadFromCache(url);
149163
if (cache) {
164+
metrics.loadedFromCache += 1;
150165
return new Promise((resolve) => {
151166
resolve({
152167
json: () => JSON.parse(cache)
@@ -169,11 +184,11 @@ global.fetch = function (...args) {
169184
}
170185
}
171186

172-
async function main({ limit = Infinity, outputFile }) {
187+
async function main({ limit = Infinity, outputFile, verbose = false }) {
173188
return new Promise(async (resolve, reject) => {
174189
const startTime = moment();
175190
const allTestData = await readTestCases(__dirname + '/test_cases/testcases_ogcio_searchable.csv');
176-
const result ={
191+
const result ={
177192
total: 0
178193
}
179194

@@ -182,15 +197,15 @@ async function main({ limit = Infinity, outputFile }) {
182197
if (typeof(tag) === 'string' && tag.length > 0) {
183198
result.tag = tag;
184199
}
185-
200+
186201
result.success = 0;
187202
result.failed = [];
188203

189204
async.eachOfLimit(allTestData.slice(0, limit), 2000, async (testData) => {
190205
result.total += 1;
191206
try {
192207
const [address, lat, lng] = testData;
193-
const jsResult = await runTest(address);
208+
const jsResult = await runTest(address);
194209
if (checkResult(jsResult, lat, lng)) {
195210
result.success += 1;
196211
} else {
@@ -202,20 +217,25 @@ async function main({ limit = Infinity, outputFile }) {
202217
}
203218
}, // callback
204219
() => {
205-
log(`Finished! Total ${result.total} tests executed .`);
220+
// output the result
206221
const timeElapsed = moment().diff(startTime, 'ms');
222+
log(`Finished! Total ${result.total} tests executed .`);
207223
log(`Time elapsed: ${timeElapsed}ms`);
224+
log(`========================================`);
225+
log(`Total Request fired: ${metrics.totalRequest}`);
226+
log(`Request cached: ${metrics.loadedFromCache}`);
227+
log(`Repeated request: ${metrics.repeatedRequest}`);
228+
log(`Average request per query: ${Math.round(metrics.totalRequest * 100/result.total) / 100}`);
208229
// Write to file
209-
230+
210231
result.success_rate = `${result.success / result.total}`;
211232

212233
if (outputFile) {
213234
outputResultTofile(result, outputFile);
214-
} else {
235+
}
236+
if (verbose) {
215237
log(result);
216238
}
217-
218-
219239
resolve();
220240
})
221241
});
@@ -244,19 +264,18 @@ program
244264
program
245265
.description('Run the test cases')
246266
.option('-o, --output [file]', 'Output the test result to the file, default output to console')
247-
.option('-l, --limit [n]', 'Limit the number of test cases to run')
267+
.option('-l, --limit [n]', 'Limit the number of test cases to run')
268+
.option('-v, --verbose', 'Show verbose log including the failed cases')
248269
.parse(process.argv);
249270

250271

251272
const outputFile = program.output;
252-
const tag = program.tag;
253-
273+
const verbose = program.verbose;
254274
// bitwise flag: | python | node |
255275
const limit = program.limit || Infinity;
256276

257-
main({ limit, outputFile })
277+
main({ limit, outputFile, verbose })
258278
.then((end) => {
259-
log('Done');
260279
})
261280
.catch((err) => {
262281
error(err);

‎package-lock.json

+28-172
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+3-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
"module": "dist/hk-address-parser.esm.js",
1616
"browser": "dist/hk-address-parser.min.js",
1717
"dependencies": {
18-
"cross-fetch": "^3.0.1"
18+
"cross-fetch": "^3.0.1",
19+
"@turf/turf": "^5.1.6",
20+
"proj4": "^2.5.0"
1921
},
2022
"devDependencies": {
2123
"@babel/core": "^7.3.4",
@@ -42,11 +44,6 @@
4244
"semver": ">=4.3.2",
4345
"sinon": "^7.2.7"
4446
},
45-
"peerDependencies": {
46-
"@turf/turf": "^5.1.6",
47-
"bluebird": "^3.5.3",
48-
"proj4": "^2.5.0"
49-
},
5047
"scripts": {
5148
"build": "rollup -c",
5249
"dev": "rollup -c -w",

‎src/models/land-address.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Address from './address';
2+
import { toFloat } from './../utils/util';
23

34
export default class LandAddress extends Address{
45
constructor(landRecord) {
@@ -41,15 +42,15 @@ export default class LandAddress extends Address{
4142

4243
coordinate() {
4344
return {
44-
lat: this.record.lat,
45-
lng: this.record.lng,
45+
lat: toFloat(this.record.lat),
46+
lng: toFloat(this.record.lng),
4647
}
4748
}
4849

4950
coordinates() {
5051
return [{
51-
lat: this.record.lat,
52-
lng: this.record.lng,
52+
lat: toFloat(this.record.lat),
53+
lng: toFloat(this.record.lng),
5354
}];
5455
}
5556

‎src/models/ogcio-address.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Address from './address';
22
import ogcioHelper from "../utils/ogcio-helper.js";
3+
import { toFloat } from './../utils/util';
34

45
export default class OGCIOAddress extends Address {
56
constructor(ogcioRecord) {
@@ -52,17 +53,17 @@ export default class OGCIOAddress extends Address {
5253
lng: 0,
5354
};
5455
if (this.record.geo !== undefined && this.record.geo.length > 0) {
55-
geo.lat = this.record.geo[0].Latitude;
56-
geo.lng = this.record.geo[0].Longitude;
56+
geo.lat = toFloat(this.record.geo[0].Latitude);
57+
geo.lng = toFloat(this.record.geo[0].Longitude);
5758
}
5859
return geo;
5960
}
6061

6162
coordinates() {
6263
if (this.record.geo !== undefined && this.record.geo.length > 0) {
6364
return this.record.geo.map(geo => ({
64-
lat: geo.Latitude,
65-
lng: geo.Longitude
65+
lat: toFloat(geo.Latitude),
66+
lng: toFloat(geo.Longitude)
6667
}));
6768
}
6869
return [];

‎src/utils/util.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export const toFloat = (value) => {
2+
if (typeof value === 'number') {
3+
return value;
4+
}
5+
let val = parseFloat(value);
6+
if (isNaN(val)) {
7+
return 0;
8+
}
9+
return val;
10+
}

0 commit comments

Comments
 (0)
Please sign in to comment.