-
Notifications
You must be signed in to change notification settings - Fork 4
/
sharedFunctions.ts
96 lines (89 loc) · 2.93 KB
/
sharedFunctions.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
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
// Identify which HTML option elements are supposed to be selected by default,
// so that they stay selected when the form is cleared. Used by LibraryEditForm
// and ServiceEditForm.
import numeral = require("numeral");
export function findDefault(setting) {
const defaultOptions = [];
setting.options &&
setting.default &&
setting.options.map((option) => {
if (
setting.default.indexOf(option.key) >= 0 ||
setting.default === option.key
) {
defaultOptions.push(option);
}
});
if (defaultOptions.length > 0) {
return defaultOptions;
}
}
// Blank out the create form on successful submission, so that the user can go
// ahead and create another new thing. Used by IndividualAdminEditForm,
// LibraryEditForm, and ServiceEditForm.
export function clearForm(refs, useCurrent = false) {
if (refs) {
const keys = Object.keys(refs);
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
if (useCurrent) {
refs[key].current?.clear && refs[key].current.clear();
if (refs[key].current?.props && refs[key].current.props.onRemove) {
refs[key].current.props.onRemove();
}
} else {
refs[key]?.clear && refs[key].clear();
if (refs[key]?.props && refs[key].props.onRemove) {
refs[key].props.onRemove();
}
}
}
}
}
export function formatString(
word: string,
replacement?: string[],
capitalize = true
) {
if (capitalize) {
word = word.substr(0, 1).toUpperCase() + word.substr(1);
}
if (replacement) {
// If the replacement array contains more than one element, the last element will replace
// all of the previous ones. Otherwise, the one element will be replaced with a space.
const replaceWith = replacement.length > 1 ? replacement.pop() : " ";
replacement.forEach((char) => {
word = word.replace(new RegExp(char, "g"), replaceWith);
});
}
return word;
}
/** Compares two arrays to see whether they contain the same items. (The order of the items doesn't matter.) */
export function isEqual(array1: Array<any>, array2: Array<any>): boolean {
return (
array1.length === array2.length &&
array1.every((x) => array2.indexOf(x) >= 0)
);
}
/**
* Format number using US conventions, if value provided is a number.
*
* If the value is a non-number string, return that value.
* Otherwise, return the empty string.
*
* @param n - the (possibly numeric) value to format
*/
export const formatNumber = (n: number | string | null): string => {
return !isNaN(Number(n))
? Intl.NumberFormat("en-US").format(Number(n))
: n === String(n)
? n
: "";
};
/**
* Make a number rounded to it's nearest units (ones, thousands, millions, ...).
* @param n - the number to round (e.g., 1,215)
* @return - the rounded number with its unit (e.g., "1.2k") as string.
*/
export const roundedNumber = (n: number): string =>
n ? numeral(n).format("0.[0]a") : "0";