courtbot-engine
Version:
An engine for courtbot-like functionality to be included in city/county services sites.
71 lines (61 loc) • 2.67 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.scrubArray = scrubArray;
exports.scrubObject = scrubObject;
/* Various Utility Functions */
/* This function removes functions, null and undefined from an array, mainly to avoid some super
* hacker including functions that interact in unexpected ways with code farther down the line.
*
* This function returns: false if not passed an array
* an empty array if passed an empty array
* an array scrubbed of functions, null and undefined
*/
function scrubArray(passedArray) {
if (!Array.isArray(passedArray)) return false;
if (passedArray.length === 0) return [];
var arr = [];
passedArray.forEach(function (elem) {
if (typeof elem === "string" || typeof elem === "boolean" || typeof elem === "number" || typeof elem === "symbol") {
arr.push(elem);
} else if (Array.isArray(elem)) {
arr.push(scrubArray(elem));
} else if (Object.prototype.toString.call(elem) === "[object Object]") {
arr.push(scrubObject(elem));
} // ignore everything else
});
return arr;
}
/* This function removes functions, null and undefined values from an object, mainly to
* avoid some super hacker including functions that interact in unexpected ways with code farther
* down the line. It also removes non-enumerable keys and constructor information.
*
* This function returns: false if passed ![object Object] || !null || !undefined
* an empty object if passed an empty object
* an object scrubbed of functions, null and undefined
*/
function scrubObject(passedObject) {
if (Object.prototype.toString.call(passedObject) !== "[object Object]") return false;
if (passedObject === null || passedObject === undefined) return false;
// scrubbed/flattened object
var obj = {};
// Scrub all non-enumerable properties, but keep properties farther up the prototype chain.
// Also, by assigning the properties over, this scrubs constructor information, as well as
// makes all properties writeable and configurable.
for (var key in passedObject) {
obj[key] = passedObject[key];
}
Object.keys(obj).forEach(function (key) {
if (typeof obj[key] === "string" || typeof obj[key] === "boolean" || typeof obj[key] === "number" || typeof obj[key] === "symbol") {
return;
} else if (Array.isArray(obj[key])) {
obj[key] = scrubArray(obj[key]);
} else if (Object.prototype.toString.call(obj[key]) === "[object Object]") {
obj[key] = scrubObject(obj[key]);
} else {
delete obj[key];
}
});
return obj;
}