-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGM_PROP.ts
54 lines (47 loc) · 1.81 KB
/
GM_PROP.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
50
51
52
53
54
import { getDataGeographiesListOfCountriesEtcLookupTable } from "./gsheetsData/dataGeographies";
import { preProcessInputRangeWithHeaders } from "./lib/cleanInputRange";
/**
* Inserts a property column, including a header row, with a common Gapminder property matched against the input column/table range.
*
* @param input_column_range_with_headers A column range for a property lookup column
* @param property_id The property (eg. "UN member since") to look up
* @return A two-dimensional array containing the cell/column contents described above in the summary.
*/
export function GM_PROP(
input_column_range_with_headers: string[][],
property_id: string
): any[][] {
// Ensure expected input range contents
const inputColumn = preProcessInputRangeWithHeaders(
input_column_range_with_headers
);
// Separate the input range header row
const inputColumnWithoutHeaderRow = inputColumn.slice(1);
return dataGeographiesListOfCountriesEtcPropertyLookup(
inputColumnWithoutHeaderRow,
property_id
);
}
/**
* @hidden
*/
function dataGeographiesListOfCountriesEtcPropertyLookup(
inputColumnWithoutHeaderRow,
property
): any[][] {
const lookupTable = getDataGeographiesListOfCountriesEtcLookupTable();
// Convert the concept_id to the Gsheet-generated equivalent property (eg "UN member since" becomes "unmembersince")
const gsxProperty = property.toLowerCase().replace(/[^A-Za-z0-9.-]*/g, "");
const matchedData = inputColumnWithoutHeaderRow.map(inputRow => {
const geo = inputRow[0];
if (lookupTable[geo] === undefined) {
return [`Unknown geo: ${geo}`];
}
const result = lookupTable[geo];
if (result[gsxProperty] === undefined) {
return [`Unknown property: ${gsxProperty}`];
}
return [result[gsxProperty]];
});
return [[property]].concat(matchedData);
}