2 * Copyright (c) 2016 The xterm.js authors. All rights reserved.
7 * A simple utility for cloning values
9 export function clone<T>(val: T, depth: number = 5): T {
10 if (typeof val !== 'object') {
14 // If we're cloning an array, use an array as the base, otherwise use an object
15 const clonedObject: any = Array.isArray(val) ? [] : {};
17 for (const key in val) {
18 // Recursively clone eack item unless we're at the maximum depth
19 clonedObject[key] = depth <= 1 ? val[key] : (val[key] ? clone(val[key], depth - 1) : val[key]);
22 return clonedObject as T;