Last active
June 11, 2025 15:55
-
-
Save optinsoft/02b9dcce3afe6419e72cae9ed9fc975d to your computer and use it in GitHub Desktop.
Convert javascript class to object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function classToObject(clss) { | |
var clss_list = [] | |
var promises = [] | |
function c2o(clss) { | |
const isObject = v => typeof v === "object" && v !== null | |
const isFunction = v => typeof v === "function" | |
const isRecursion = v => clss_list.find(item => item.clss === v) | |
const keys = x => Object.getOwnPropertyNames(x).concat(Object.getOwnPropertyNames(x?.__proto__||{})).filter(k => k !== 'enabledPlugin') | |
var object = {} | |
clss_list.push({clss, object}) | |
return keys(clss ?? {}).reduce((object, key) => { | |
const [val, arr, obj, func, rec] = [clss[key], Array.isArray(clss[key]), isObject(clss[key]), isFunction(clss[key]), isRecursion(clss[key])] | |
if (!func) { | |
object[key] = arr ? val.map(c2o) : rec !== undefined ? rec.object : obj ? c2o(val) : val | |
} | |
else if (key === 'getHighEntropyValues') { | |
promises.push(clss.getHighEntropyValues(["architecture", "bitness", "brands", "mobile", "model", "platform", "platformVersion", "uaFullVersion"]).then(h => {object['highEntropyValues'] = h})) | |
} | |
return object | |
}, object) | |
} | |
object = c2o(clss) | |
const x = await Promise.all(promises) | |
return object | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment