-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
156 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import test, { ExecutionContext, Macro } from "ava"; | ||
import { GM_GEO_LOOKUP_TABLE } from "./GM_GEO_LOOKUP_TABLE"; | ||
import { MinimalUrlFetchApp } from "./lib/MinimalUrlFetchApp"; | ||
import { MinimalUtilities } from "./lib/MinimalUtilities"; | ||
(global as any).UrlFetchApp = MinimalUrlFetchApp; | ||
(global as any).Utilities = MinimalUtilities; | ||
|
||
/** | ||
* @hidden | ||
*/ | ||
const testGmGeoLookupTable: Macro<any> = ( | ||
t: ExecutionContext, | ||
{ geography, expectedTopFiveRowsOfOutput } | ||
) => { | ||
const output = GM_GEO_LOOKUP_TABLE(geography); | ||
const topFiveRowsOfOutput = output.slice(0, 5); | ||
// t.log({ topFiveRowsOfOutput, expectedTopFiveRowsOfOutput }); | ||
t.deepEqual(topFiveRowsOfOutput, expectedTopFiveRowsOfOutput); | ||
}; | ||
|
||
[ | ||
/* tslint:disable:object-literal-sort-keys */ | ||
{ | ||
geography: "world_4region", | ||
expectedTopFiveRowsOfOutput: [ | ||
["alias", "geo", "name"], | ||
["Africa", "africa", "Africa"], | ||
["africa", "africa", "Africa"], | ||
["africa (total)", "americas", "The Americas"], | ||
["Africa (total)", "americas", "The Americas"] | ||
] | ||
} | ||
/* tslint:enable:object-literal-sort-keys */ | ||
].forEach((testData, index) => { | ||
test("testGmGeoLookupTable - " + index, testGmGeoLookupTable, testData); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { | ||
GeoAliasesAndSynonymsDataRow, | ||
GeoAliasesAndSynonymsLookupTable, | ||
GeoAliasesAndSynonymsWorksheetData, | ||
geoAliasesAndSynonymsWorksheetDataToGeoLookupTable, | ||
getGeoAliasesAndSynonymsWorksheetData | ||
} from "./gsheetsData/geoAliasesAndSynonyms"; | ||
|
||
/** | ||
* Inserts a table with Gapminder’s geo ids together with their aliases (all spellings we have seen before), including lower cased | ||
* variants without diacritics and special characters to allow for somewhat fuzzy matching. | ||
* | ||
* To be used as the source range for VLOOKUP where the dataset is too large for GM_ID or GM_NAME to be used directly. | ||
* | ||
* @param geography Should be one of the sets listed in the gapminder geo ontology such as "countries_etc" | ||
* @return A two-dimensional array containing the cell/column contents described above in the summary. | ||
*/ | ||
export function GM_GEO_LOOKUP_TABLE(geography: string): string[][] { | ||
if (!geography) { | ||
geography = "countries_etc"; | ||
} | ||
const geoAliasesAndSynonymsWorksheetData: GeoAliasesAndSynonymsWorksheetData = getGeoAliasesAndSynonymsWorksheetData( | ||
geography | ||
); | ||
const fuzzyMatchLookupTable: GeoAliasesAndSynonymsLookupTable = geoAliasesAndSynonymsWorksheetDataToGeoLookupTable( | ||
geoAliasesAndSynonymsWorksheetData, | ||
null | ||
); | ||
const exactMatchLookupTable: GeoAliasesAndSynonymsLookupTable = geoAliasesAndSynonymsWorksheetDataToGeoLookupTable( | ||
geoAliasesAndSynonymsWorksheetData, | ||
lookupKey => lookupKey | ||
); | ||
const lookupTable = { ...fuzzyMatchLookupTable, ...exactMatchLookupTable }; | ||
const lookupKeys = Object.keys(lookupTable); | ||
const lookupTableOutput = lookupKeys.map(lookupKey => { | ||
const geoAliasesAndSynonymsDataRow: GeoAliasesAndSynonymsDataRow = | ||
lookupTable[lookupKey]; | ||
return [ | ||
lookupKey, | ||
geoAliasesAndSynonymsDataRow.geo, | ||
geoAliasesAndSynonymsDataRow.name | ||
]; | ||
}); | ||
const sortedLookupTableOutput = lookupTableOutput.sort((a, b) => { | ||
const geoIdComparison = a[1].localeCompare(b[1], "en", { | ||
sensitivity: "accent" | ||
}); | ||
const aliasComparison = a[0].localeCompare(b[0], "en", { | ||
sensitivity: "accent" | ||
}); | ||
if (geoIdComparison === 0) { | ||
return aliasComparison; | ||
} | ||
return geoIdComparison; | ||
}); | ||
return [["alias", "geo", "name"]].concat(sortedLookupTableOutput); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters