forked from svrjs/svrjs
Optimize deepClone function and lint out the codebase
This commit is contained in:
parent
47ad7006e9
commit
c73ce5d9f7
3 changed files with 55 additions and 57 deletions
|
@ -204,21 +204,17 @@ function clientErrorHandler(err, socket) {
|
|||
}
|
||||
});
|
||||
} else {
|
||||
fs.access(
|
||||
"." + errorCode.toString(),
|
||||
fs.constants.F_OK,
|
||||
(err) => {
|
||||
try {
|
||||
if (err) {
|
||||
callback(errorCode.toString() + ".html");
|
||||
} else {
|
||||
callback("." + errorCode.toString());
|
||||
}
|
||||
} catch (err2) {
|
||||
callServerError(500, err2);
|
||||
fs.access("." + errorCode.toString(), fs.constants.F_OK, (err) => {
|
||||
try {
|
||||
if (err) {
|
||||
callback(errorCode.toString() + ".html");
|
||||
} else {
|
||||
callback("." + errorCode.toString());
|
||||
}
|
||||
},
|
||||
);
|
||||
} catch (err2) {
|
||||
callServerError(500, err2);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
@ -54,7 +54,7 @@ function requestHandler(req, res) {
|
|||
return true;
|
||||
}
|
||||
});
|
||||
if (vhostP && vhostP.headers) ph = {...ph, ...vhostP.headers};
|
||||
if (vhostP && vhostP.headers) ph = { ...ph, ...vhostP.headers };
|
||||
}
|
||||
Object.keys(ph).forEach((phk) => {
|
||||
if (typeof ph[phk] == "string")
|
||||
|
@ -369,24 +369,20 @@ function requestHandler(req, res) {
|
|||
}
|
||||
});
|
||||
} else {
|
||||
fs.access(
|
||||
"." + errorCode.toString(),
|
||||
fs.constants.F_OK,
|
||||
(err) => {
|
||||
try {
|
||||
if (err) {
|
||||
callback(errorCode.toString() + ".html");
|
||||
} else {
|
||||
callback("." + errorCode.toString());
|
||||
}
|
||||
} catch (err2) {
|
||||
res.error(500, err2);
|
||||
fs.access("." + errorCode.toString(), fs.constants.F_OK, (err) => {
|
||||
try {
|
||||
if (err) {
|
||||
callback(errorCode.toString() + ".html");
|
||||
} else {
|
||||
callback("." + errorCode.toString());
|
||||
}
|
||||
},
|
||||
);
|
||||
} catch (err2) {
|
||||
res.error(500, err2);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if (!_i) _i = 0;
|
||||
if (_i >= list.length) {
|
||||
|
@ -412,7 +408,7 @@ function requestHandler(req, res) {
|
|||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
getErrorFileName(config.errorPages, function (errorFile) {
|
||||
// Generate error stack if not provided
|
||||
|
|
|
@ -1,37 +1,43 @@
|
|||
// Function to deep clone an object or array
|
||||
function deepClone(obj, _objectsArray, _clonesArray) {
|
||||
if (!_objectsArray) _objectsArray = [];
|
||||
if (!_clonesArray) _clonesArray = [];
|
||||
function deepClone(obj) {
|
||||
if (typeof obj !== "object" || obj === null) {
|
||||
return obj;
|
||||
}
|
||||
|
||||
let objectsArrayIndex = _objectsArray.indexOf(obj);
|
||||
if (objectsArrayIndex != -1) {
|
||||
return _clonesArray[objectsArrayIndex];
|
||||
}
|
||||
const recurse = (obj, _objectsArray, _clonesArray) => {
|
||||
if (!_objectsArray) _objectsArray = [];
|
||||
if (!_clonesArray) _clonesArray = [];
|
||||
|
||||
let clone;
|
||||
let objectsArrayIndex = _objectsArray.indexOf(obj);
|
||||
if (objectsArrayIndex != -1) {
|
||||
return _clonesArray[objectsArrayIndex];
|
||||
}
|
||||
|
||||
if (Array.isArray(obj)) {
|
||||
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);
|
||||
}
|
||||
});
|
||||
}
|
||||
let clone;
|
||||
|
||||
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;
|
||||
|
|
Reference in a new issue