-
Notifications
You must be signed in to change notification settings - Fork 21
/
format.js
106 lines (94 loc) · 2.68 KB
/
format.js
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
* @module
*
* Formatting helpers for spreadsheet output. Note that these helpers are
* designed to NOT apply any validation of their own. Output generation should
* reliably succeed if uploads have all passed validation.
*/
const round = require('lodash/round');
const { ecCodes } = require('./arpa-ec-codes');
const EXPENDITURE_CATEGORIES = {
ec1: '1-Public Health',
ec2: '2-Negative Economic Impacts',
ec3: '3-Public Health-Negative Economic Impact: Public Sector Capacity',
ec4: '4-Premium Pay',
ec5: '5-Infrastructure',
ec7: '7-Administrative',
};
/**
* Normalize casing of single word values.
* This is especially useful for "Yes"/"No" responses.
*
* @param {string} value
* @returns {string}
*/
function capitalizeFirstLetter(value) {
if (typeof value !== 'string' || value === '') return value;
return `${value[0].toUpperCase()}${value.slice(1).toLowerCase()}`;
}
function currency(value) {
if (typeof value !== 'number') return value;
return round(value, 2).toString();
}
/*
* Round to two decimal places, but keep the number type
*/
function currencyNumeric(value) {
if (typeof value !== 'number') return null;
return round(value, 2);
}
function ec(value) {
return EXPENDITURE_CATEGORIES[value];
}
/**
* Normalize delimeters in a multiselect value.
*
* @param {string} value
* @returns {string} normalized value
*/
function multiselect(value) {
if (typeof value !== 'string') return value;
return value
.trim()
.replace(/^-/, '') // remove preceding hyphen
.replace(/,/g, '') // remove all commas
.split(/;[- ]*/) // match any delimiter format
.filter((v) => v !== '') // remove empty values (e.g. trailing or double delimiter)
.join(';');
}
/**
* Transform a subcategory code to its canonical name.
*
* @param {string} value
* The expecditure subcategory code as supplied in the input sheet
*/
function subcategory(value) {
if (value == null) return value;
if (!ecCodes[value]) return undefined;
return `${value}-${ecCodes[value]}`;
}
function tin(value) {
if (value == null) return value;
return value.toString().replace('-', '');
}
function zip(value) {
if (value == null) return value;
return value.toString().padStart(5, '0');
}
function zip4(value) {
if (value == null) return value;
return value.toString().padStart(4, '0');
}
module.exports = {
capitalizeFirstLetter,
currency,
currencyNumeric,
ec,
multiselect,
subcategory,
tin,
zip,
zip4,
EXPENDITURE_CATEGORIES,
};
// NOTE: This file was copied from src/server/lib/format.js (git @ ada8bfdc98) in the arpa-reporter repo on 2022-09-23T20:05:47.735Z