Skip to content

Commit

Permalink
Fix several nits in GeoIP API region labels
Browse files Browse the repository at this point in the history
  • Loading branch information
Gowee committed Mar 13, 2021
1 parent 5e98274 commit 13588b0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
7 changes: 4 additions & 3 deletions src/geoip/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ export class IP2Geo {
}
const { country, city, region, loc, org } = data;
// const region_city = city && `${city.indexOf(region) === -1 ? `${city}, ${region}` : city}, ${country}`;
const regionFull = regionJoin(country, region, city);
const regionFull = regionJoin(city, region, country);
const [lat, lon] = loc ? loc.split(',').map(parseFloat) : [undefined, undefined];
const geo = { region: regionFull, label: org, lat, lon };
return geo;
Expand All @@ -199,8 +199,9 @@ export class IP2Geo {
static async IPSB(ip: string): Promise<IPGeo> {
const r = await fetch(`https://api.ip.sb/geoip/${ip}`, { headers: { Accept: 'application/json' } });
const data = await r.json();
const { country, latitude, longitude, isp } = data;
const geo = { region: country, label: isp, lon: longitude, lat: latitude };
const { country, region, city, latitude, longitude, isp } = data;
const regionFull = regionJoin(city, region, country);
const geo = { region: regionFull, label: isp, lon: longitude, lat: latitude };
return geo;
}

Expand Down
6 changes: 4 additions & 2 deletions src/utils/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ export function orgJoin(org1: string | undefined, org2: string | undefined): str
}

export function eliminatePrefixOrSuffix(label1?: string, label2?: string): string[] {
if (label1 && label2?.startsWith(label1)) {
if (label1 && (label2?.startsWith(label1) || label2?.endsWith(label1))) {
label1 = undefined;
} else if (label2 && label1?.startsWith(label2)) {
} else if (label2 && (label1?.startsWith(label2) || label1?.endsWith(label2))) {
label2 = undefined;
}
return [label1, label2].filter((value) => value) as string[];
}

// TODO: over cutting?

0 comments on commit 13588b0

Please sign in to comment.