UNPKG

mini-state-machine

Version:
31 lines (30 loc) 1.04 kB
const _getClassOf = Function.prototype.call.bind(Object.prototype.toString); export function cloneArray(arr) { return arr.map(value => Array.isArray(value) ? cloneArray(value) : isCloneable(value) ? cloneObject(value) : value); } export function cloneObject(object) { const copy = Object.assign({}, object); for (const prop in copy) { if (!copy.hasOwnProperty(prop)) continue; if (Array.isArray(copy[prop])) { copy[prop] = cloneArray(copy[prop]); } else if (isCloneable(copy[prop])) { copy[prop] = cloneObject(copy[prop]); } } return copy; } function getClassOf(object) { const internal = _getClassOf(object); if (internal !== "[object Object]") return internal; return `[object ${Object.getPrototypeOf(object).constructor.name}]`; } function isCloneable(value) { return !!value && typeof value === "object" && isPlainObject(value); } function isPlainObject(object) { return getClassOf(object) === "[object Object]"; }