-
Notifications
You must be signed in to change notification settings - Fork 39
/
parsing.js
51 lines (46 loc) · 1.42 KB
/
parsing.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
import lodashKebabCase from "lodash.kebabcase";
import lodashStartCase from "lodash.startcase";
export const zeroAddress = "0x0000000000000000000000000000000000000000";
export const ethereumAddressRegExp = /^0x[\dA-Fa-f]{40}$/;
export const createEnum = (keys, parse) => {
const _enum = keys.reduce(
(acc, key, index) => {
let extra;
if (Array.isArray(key)) [key, extra] = key;
const value = {
key,
index,
camelCase: key[0].toLowerCase() + key.slice(1),
kebabCase: lodashKebabCase(key),
startCase: lodashStartCase(key),
...extra,
};
value.color = value.camelCase;
value.toString = () => value.startCase;
acc[key] = value;
acc[index] = value;
acc[value.camelCase] = value;
acc[value.kebabCase] = value;
acc[value.startCase] = value;
return acc;
},
{
parse:
parse ||
((arrayOrKey) =>
Array.isArray(arrayOrKey)
? arrayOrKey.reduce((acc, key) => {
const value = _enum[key];
acc[key] = value;
acc[value.index] = value;
acc[value.camelCase] = value;
acc[value.kebabCase] = value;
acc[value.startCase] = value;
return acc;
}, {})
: _enum[arrayOrKey]),
}
);
_enum.array = keys.map((_, index) => _enum[index]);
return _enum;
};