CartoDB-SQL-API/lib/utils/logs.js
Daniel García Aubert 014158c968 Eslint errors
2019-12-26 17:46:27 +01:00

46 lines
1.2 KiB
JavaScript

'use strict';
module.exports = {
/**
* Remove problematic nested characters
* from object for logs RegEx
*
* @param {Object} object
* @param {Number} maxStringLength
*/
stringifyForLogs (object, maxStringLength = 1024) {
return JSON.stringify(cloneAndFilter(object, maxStringLength));
}
};
function cloneAndFilter (object, maxStringLength) {
if (!object || !(object instanceof Object)) {
return null;
}
const newObject = {};
Object.keys(object).map(key => {
if (typeof object[key] === 'string') {
newObject[key] = filterString(object[key], maxStringLength);
} else if (typeof object[key] === 'object') {
newObject[key] = cloneAndFilter(object[key], maxStringLength);
} else if (object[key] instanceof Array) {
newObject[key] = [];
for (const element of object[key]) {
newObject[key].push(cloneAndFilter(element, maxStringLength));
}
} else {
newObject[key] = object[key];
}
});
return newObject;
}
function filterString (s, maxStringLength) {
return s
.substring(0, maxStringLength)
.replace(/[^a-zA-Z0-9]/g, ' ');
}