Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
cswbrian committed Jan 17, 2019
2 parents 232b3f5 + d0e99b9 commit 31ff871
Showing 1 changed file with 87 additions and 0 deletions.
87 changes: 87 additions & 0 deletions web/src/lib/models/ogcio-address.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import Address from './address';
import ogcioHelper from "../../utils/ogcio-helper.js";

export default class OGCIOAddress extends Address {
constructor(ogcioRecord) {
super();
this.record = ogcioRecord;
this.flattenedComponents = null;
}

components(lang) {
if (this.flattenedComponents === null) {
this.flattenedComponents = this.flattenComponents();
}
if (lang === Address.LANG_EN) {
return this.flattenedComponents['eng'];
} else {
return this.flattenedComponents['chi'];
}
}

flattenComponents() {
const flattenedComponents = {
[Address.LANG_EN]: [],
[Address.LANG_ZH]: [],
};
const langs = [Address.LANG_ZH, Address.LANG_EN]
for (const lang of langs) {
for (const key of Object.keys(this.record[lang])) {
flattenedComponents[lang].push({
key,
translatedLabel: ogcioHelper.textForKey(key, lang),
translatedValue: ogcioHelper.textForValue(this.record, key, lang),
});
}
}

return flattenedComponents;
}

fullAddress(lang) {
if (lang === Address.LANG_EN) {
return ogcioHelper.fullEnglishAddressFromResult(this.record['eng']);
} else {
return ogcioHelper.fullChineseAddressFromResult(this.record['chi']);
}
}

coordinate() {
const geo = {
lat: 0,
lng: 0,
};
if (this.record.geo !== undefined && this.record.geo.length > 0) {
geo.lat = this.record.geo[0].Latitude;
geo.lng = this.record.geo[0].Longitude;
}
return geo;
}

coordinates() {
if (this.record.geo !== undefined && this.record.geo.length > 0) {
return this.record.geo.map(geo => ({
lat: geo.Latitude,
lng: geo.Longitude
}));
}
return [];
}

// In the future it can be multiple source
dataSource() {
return '資科辦';
}

confidence() {
return Math.min(
4,
(this.record.matches
.filter(match => match.matchedKey === key)
.map(match => match.confident)
.reduce((p, c) => c, 0) *
5) |
0
);
}
}

0 comments on commit 31ff871

Please sign in to comment.