-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcleanObject.js
More file actions
72 lines (68 loc) · 1.69 KB
/
cleanObject.js
File metadata and controls
72 lines (68 loc) · 1.69 KB
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
const whiteListedArrays = [
'packages',
'cakes_found_by_name',
'unlockedColors',
'unlockedParts',
'custom_titles',
'bridgeMapWins',
'tutorial',
'visited_zones',
'crafted_minions',
'achievement_spawned_island_types',
'unlocked_coll_tiers'
];
const blacklistedValues = ['contract_choices', 'currently_upgrading', 'bats_spawned', 'oldItem'];
function containsBadKey(key) {
return (
blacklistedValues.includes(key) ||
key.startsWith('fetchur-') ||
key.startsWith('spooky_festival_') ||
key.startsWith('layout_items_') ||
key.startsWith('claimed_solo_bank_') ||
key.startsWith('claimed_coop_bank_') ||
key.startsWith('given_cookies_') ||
/\d+:\d+_\d+:/.test(key) ||
/^[0-9a-fA-F]{32}$/.test(key)
);
}
function normalizeObject(object) {
const o = { ...object };
const keys = Object.keys(o).filter((key) => {
if (containsBadKey(key)) {
delete o[key];
return false;
}
return true;
});
keys.forEach((key) => {
let entry = o[key];
switch (typeof entry) {
case 'number':
entry = 0;
break;
case 'string':
entry = '';
break;
case 'boolean':
entry = true;
break;
default:
if (Array.isArray(entry)) {
if (!whiteListedArrays.includes(key)) {
if (typeof entry[0] === 'object') {
entry = [{ ...normalizeObject(entry[0]) }];
break;
}
entry = [];
}
} else if (entry === null) {
entry = '';
} else {
entry = normalizeObject(entry);
}
}
o[key] = entry;
});
return o;
}
module.exports = { containsBadKey, normalizeObject };