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,10 +204,7 @@ function clientErrorHandler(err, socket) {
|
|||
}
|
||||
});
|
||||
} else {
|
||||
fs.access(
|
||||
"." + errorCode.toString(),
|
||||
fs.constants.F_OK,
|
||||
(err) => {
|
||||
fs.access("." + errorCode.toString(), fs.constants.F_OK, (err) => {
|
||||
try {
|
||||
if (err) {
|
||||
callback(errorCode.toString() + ".html");
|
||||
|
@ -217,8 +214,7 @@ function clientErrorHandler(err, socket) {
|
|||
} 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,10 +369,7 @@ function requestHandler(req, res) {
|
|||
}
|
||||
});
|
||||
} else {
|
||||
fs.access(
|
||||
"." + errorCode.toString(),
|
||||
fs.constants.F_OK,
|
||||
(err) => {
|
||||
fs.access("." + errorCode.toString(), fs.constants.F_OK, (err) => {
|
||||
try {
|
||||
if (err) {
|
||||
callback(errorCode.toString() + ".html");
|
||||
|
@ -382,11 +379,10 @@ function requestHandler(req, res) {
|
|||
} 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,11 +1,13 @@
|
|||
// 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;
|
||||
}
|
||||
|
||||
const recurse = (obj, _objectsArray, _clonesArray) => {
|
||||
if (!_objectsArray) _objectsArray = [];
|
||||
if (!_clonesArray) _clonesArray = [];
|
||||
|
||||
let objectsArrayIndex = _objectsArray.indexOf(obj);
|
||||
if (objectsArrayIndex != -1) {
|
||||
return _clonesArray[objectsArrayIndex];
|
||||
|
@ -18,7 +20,7 @@ function deepClone(obj, _objectsArray, _clonesArray) {
|
|||
_objectsArray.push(obj);
|
||||
_clonesArray.push(clone);
|
||||
obj.forEach((item, index) => {
|
||||
clone[index] = deepClone(item, _objectsArray, _clonesArray);
|
||||
clone[index] = recurse(item, _objectsArray, _clonesArray);
|
||||
});
|
||||
} else {
|
||||
clone = {};
|
||||
|
@ -26,12 +28,16 @@ function deepClone(obj, _objectsArray, _clonesArray) {
|
|||
_clonesArray.push(clone);
|
||||
Object.keys(obj).forEach((key) => {
|
||||
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
||||
clone[key] = deepClone(obj[key], _objectsArray, _clonesArray);
|
||||
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