I need to validate, update, escape every value string in object or array in the data object, the data is always changing this the base scheme.
I have this code is working, can be done better or do less depth of recursion ?
function _has_array_to_validate_get_string(array) {
return Array.isArray(array) && array.length === 2 && typeof array[0] === "string" && array[0].includes('valid_type');
}
function validate_escape(...args) {return args}
function funct(object) {
for ( var key in object ) {
if (typeof object[key] === "string") {
object[key] = validate_escape(object[key]);
}
else if ( _has_array_to_validate_get_string(object[key]) ) { // ["valid_type.pathdir", "/pathdir/desdss"],
object[key][1] = validate_escape(object[key][0], object[key][1]);
}
else if ( Array.isArray(object[key]) && object[key].every(item => typeof item === "string") ) {
object[key].forEach((value, index) => {
object[key][index] = validate_escape(value);
});
}
else if ( Array.isArray(object[key]) && object[key].every( item => typeof item === "object" && !Array.isArray(item) ) ) {
object[key].forEach((value) => {
funct(value);
});
}
else if (typeof object[key] === 'object' && !Array.isArray(object[key]) ) {
funct(object[key])
}
else if ( Array.isArray(object[key] ) ) {
funct(object[key])
}
}
return object;
}