1
0
Fork 0
forked from svrjs/svrjs

Optimize deepClone function and lint out the codebase

This commit is contained in:
Dorian Niemiec 2024-08-27 22:49:05 +02:00
parent 47ad7006e9
commit c73ce5d9f7
3 changed files with 55 additions and 57 deletions

View file

@ -204,21 +204,17 @@ function clientErrorHandler(err, socket) {
} }
}); });
} else { } else {
fs.access( fs.access("." + errorCode.toString(), fs.constants.F_OK, (err) => {
"." + errorCode.toString(), try {
fs.constants.F_OK, if (err) {
(err) => { callback(errorCode.toString() + ".html");
try { } else {
if (err) { callback("." + errorCode.toString());
callback(errorCode.toString() + ".html");
} else {
callback("." + errorCode.toString());
}
} catch (err2) {
callServerError(500, err2);
} }
}, } catch (err2) {
); callServerError(500, err2);
}
});
} }
} }
}; };

View file

@ -54,7 +54,7 @@ function requestHandler(req, res) {
return true; return true;
} }
}); });
if (vhostP && vhostP.headers) ph = {...ph, ...vhostP.headers}; if (vhostP && vhostP.headers) ph = { ...ph, ...vhostP.headers };
} }
Object.keys(ph).forEach((phk) => { Object.keys(ph).forEach((phk) => {
if (typeof ph[phk] == "string") if (typeof ph[phk] == "string")
@ -369,24 +369,20 @@ function requestHandler(req, res) {
} }
}); });
} else { } else {
fs.access( fs.access("." + errorCode.toString(), fs.constants.F_OK, (err) => {
"." + errorCode.toString(), try {
fs.constants.F_OK, if (err) {
(err) => { callback(errorCode.toString() + ".html");
try { } else {
if (err) { callback("." + errorCode.toString());
callback(errorCode.toString() + ".html");
} else {
callback("." + errorCode.toString());
}
} catch (err2) {
res.error(500, err2);
} }
}, } catch (err2) {
); res.error(500, err2);
}
});
} }
} }
} };
if (!_i) _i = 0; if (!_i) _i = 0;
if (_i >= list.length) { if (_i >= list.length) {
@ -412,7 +408,7 @@ function requestHandler(req, res) {
} }
}); });
} }
} };
getErrorFileName(config.errorPages, function (errorFile) { getErrorFileName(config.errorPages, function (errorFile) {
// Generate error stack if not provided // Generate error stack if not provided

View file

@ -1,37 +1,43 @@
// Function to deep clone an object or array // Function to deep clone an object or array
function deepClone(obj, _objectsArray, _clonesArray) { function deepClone(obj) {
if (!_objectsArray) _objectsArray = [];
if (!_clonesArray) _clonesArray = [];
if (typeof obj !== "object" || obj === null) { if (typeof obj !== "object" || obj === null) {
return obj; return obj;
} }
let objectsArrayIndex = _objectsArray.indexOf(obj); const recurse = (obj, _objectsArray, _clonesArray) => {
if (objectsArrayIndex != -1) { if (!_objectsArray) _objectsArray = [];
return _clonesArray[objectsArrayIndex]; if (!_clonesArray) _clonesArray = [];
}
let clone; let objectsArrayIndex = _objectsArray.indexOf(obj);
if (objectsArrayIndex != -1) {
return _clonesArray[objectsArrayIndex];
}
if (Array.isArray(obj)) { let clone;
clone = [];
_objectsArray.push(obj);
_clonesArray.push(clone);
obj.forEach((item, index) => {
clone[index] = deepClone(item, _objectsArray, _clonesArray);
});
} else {
clone = {};
_objectsArray.push(obj);
_clonesArray.push(clone);
Object.keys(obj).forEach((key) => {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
clone[key] = deepClone(obj[key], _objectsArray, _clonesArray);
}
});
}
return clone; if (Array.isArray(obj)) {
clone = [];
_objectsArray.push(obj);
_clonesArray.push(clone);
obj.forEach((item, index) => {
clone[index] = recurse(item, _objectsArray, _clonesArray);
});
} else {
clone = {};
_objectsArray.push(obj);
_clonesArray.push(clone);
Object.keys(obj).forEach((key) => {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
clone[key] =
typeof obj[key] !== "object" || obj[key] === null
? obj[key]
: recurse(obj[key], _objectsArray, _clonesArray);
}
});
}
return clone;
};
return recurse(obj);
} }
module.exports = deepClone; module.exports = deepClone;