This repository was archived by the owner on Jul 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathclean.js
More file actions
67 lines (59 loc) · 1.36 KB
/
clean.js
File metadata and controls
67 lines (59 loc) · 1.36 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
'use strict';
/* eslint-disable no-control-regex */
module.exports.data = function (obj) {
delete obj._id;
if (!obj.hasOwnProperty('_key')) {
return null;
}
var key = obj._key;
// clean up importer bugs
delete obj.undefined;
if ((key.startsWith('chat:room:') && key.endsWith('uids') && !key.endsWith(':uids')) || (key.startsWith('uid:') && key.endsWith('sessionUUID:sessionId') && !key.endsWith(':sessionUUID:sessionId'))) {
return null;
}
// remove importer cache on live objects
if (!key.startsWith('_imported')) {
for (var k of Object.keys(obj)) {
if (k.startsWith('_imported')) {
delete obj[k];
}
}
}
return module.exports.value(obj);
}
module.exports.value = function (obj) {
for (var k in obj) {
if (!Object.prototype.hasOwnProperty.call(obj, k)) {
continue;
}
var v = obj[k];
if (!v || v === true) {
continue;
}
if (v instanceof Date) {
obj[k] = v.getTime();
continue;
}
if (typeof v === 'number') {
if (Number.isNaN(v)) {
obj[k] = 'NaN';
}
continue;
}
if (typeof v === 'string') {
if (v.indexOf('\x00') !== -1) {
obj[k] = v.replace(/\x00/g, 'x00');
}
continue;
}
if (Array.isArray(v)) {
obj[k] = v.map(function(a) {
return String(a || '').replace(/\x00/g, 'x00');
});
continue;
}
// Object, possibly from a plugin
obj[k] = module.exports.value(v);
}
return obj;
}