UNPKG

@bokeh/bokehjs

Version:

Interactive, novel data visualization

47 lines 1.51 kB
import { entries } from "./object"; import { isPrimitive, isPlainObject, isObject, isArray, isFunction } from "./types"; export const clone = Symbol("clone"); export function is_Cloneable(obj) { return isObject(obj) && clone in obj; } export class CloningError extends Error { static __name__ = "CloningError"; } export class Cloner { static __name__ = "Cloner"; constructor() { } clone(obj) { if (is_Cloneable(obj)) { return obj[clone](this); } else if (isPrimitive(obj) || isFunction(obj)) { return obj; } else if (isArray(obj)) { const n = obj.length; const result = new Array(n); for (let i = 0; i < n; i++) { const value = obj[i]; result[i] = this.clone(value); } return result; } else if (isPlainObject(obj)) { const result = {}; for (const [key, value] of entries(obj)) { result[key] = this.clone(value); } return result; } else if (obj instanceof Map) { return new Map([...obj].map(([k, v]) => [this.clone(k), this.clone(v)])); } else if (obj instanceof Set) { return new Set([...obj].map((v) => this.clone(v))); } else { throw new CloningError(`${Object.prototype.toString.call(obj)} is not cloneable`); } } } //# sourceMappingURL=cloneable.js.map