CartoDB-SQL-API/lib/utils/logs.js

46 lines
1.2 KiB
JavaScript
Raw Normal View History

2019-02-27 17:40:39 +08:00
'use strict';
module.exports = {
/**
* Remove problematic nested characters
* from object for logs RegEx
*
* @param {Object} object
2019-12-27 00:46:27 +08:00
* @param {Number} maxStringLength
2019-02-27 17:40:39 +08:00
*/
2019-12-27 00:46:27 +08:00
stringifyForLogs (object, maxStringLength = 1024) {
return JSON.stringify(cloneAndFilter(object, maxStringLength));
2019-02-27 17:40:39 +08:00
}
2019-02-27 18:59:04 +08:00
};
2019-02-27 17:40:39 +08:00
2019-12-27 00:46:27 +08:00
function cloneAndFilter (object, maxStringLength) {
if (!object || !(object instanceof Object)) {
return null;
}
const newObject = {};
2019-02-27 17:40:39 +08:00
Object.keys(object).map(key => {
if (typeof object[key] === 'string') {
2019-12-27 00:46:27 +08:00
newObject[key] = filterString(object[key], maxStringLength);
2019-02-27 17:40:39 +08:00
} else if (typeof object[key] === 'object') {
2019-12-27 00:46:27 +08:00
newObject[key] = cloneAndFilter(object[key], maxStringLength);
2019-02-27 17:40:39 +08:00
} else if (object[key] instanceof Array) {
newObject[key] = [];
2019-12-24 01:19:08 +08:00
for (const element of object[key]) {
2019-12-27 00:46:27 +08:00
newObject[key].push(cloneAndFilter(element, maxStringLength));
2019-02-27 17:40:39 +08:00
}
} else {
newObject[key] = object[key];
2019-02-27 17:40:39 +08:00
}
});
return newObject;
2019-02-27 17:40:39 +08:00
}
2019-12-27 00:46:27 +08:00
function filterString (s, maxStringLength) {
2019-02-27 17:40:39 +08:00
return s
2019-12-27 00:46:27 +08:00
.substring(0, maxStringLength)
2019-02-27 17:40:39 +08:00
.replace(/[^a-zA-Z0-9]/g, ' ');
}