| 1 |
/** |
| 2 |
* Copyright (c) 2016 The xterm.js authors. All rights reserved. |
| 3 |
* @license MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
/* |
| 7 |
* A simple utility for cloning values |
| 8 |
*/ |
| 9 |
export function clone<T>(val: T, depth: number = 5): T { |
| 10 |
if (typeof val !== 'object') { |
| 11 |
return val; |
| 12 |
} |
| 13 |
|
| 14 |
// If we're cloning an array, use an array as the base, otherwise use an object |
| 15 |
const clonedObject: any = Array.isArray(val) ? [] : {}; |
| 16 |
|
| 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)); |
| 20 |
} |
| 21 |
|
| 22 |
return clonedObject as T; |
| 23 |
} |