-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGM_NAME.ts
49 lines (43 loc) · 1.98 KB
/
GM_NAME.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { matchColumnValuesUsingGeoAliasesAndSynonyms } from "./gsheetsData/geoAliasesAndSynonyms";
import { preProcessInputRangeWithHeaders } from "./lib/cleanInputRange";
import { validateAndAliasTheGeoSetArgument } from "./lib/validateAndAliasTheGeoSetArgument";
/**
* Inserts a matching column, including a header row, with Gapminder’s common name for the geo matched against the input column range, based on all spellings we have seen before. (Like GM_ID but inserts Gapminder’s common name for the geo instead of its id.)
* Note: Automatically adds geo ids as aliases in geo lookup tables, so that "USA" matches "usa" even though no specific alias "usa" is mapped to "usa".
*
* @param column_range_with_headers
* @param geo_set (Optional with default "countries_etc") Should be one of the geo set names listed in the "geo aliases and synonyms" spreadsheet
* @param verbose (Optional with default "FALSE") Explains how a certain row is invalid instead of simply returning "[Invalid]" for the row
* @return A two-dimensional array containing the cell/column contents described above in the summary.
*/
export function GM_NAME(
column_range_with_headers: string[][],
geo_set: string,
verbose: boolean
) {
// Ensure expected input range contents
const inputColumn = preProcessInputRangeWithHeaders(
column_range_with_headers
);
// Validate and accept alternate geo set references (countries-etc, regions, world) for the geo_set argument
const validatedGeoSetArgument = validateAndAliasTheGeoSetArgument(geo_set);
// Default argument value
if (verbose === undefined) {
verbose = false;
}
// Drop the input range header row
inputColumn.shift();
const matchedGeos = matchColumnValuesUsingGeoAliasesAndSynonyms(
inputColumn,
validatedGeoSetArgument
);
return [["name"]].concat(
matchedGeos.map(matchedGeo => [
matchedGeo.name
? matchedGeo.name
: verbose
? `Unknown alias: ${matchedGeo.alias}`
: "[Invalid]"
])
);
}