File size: 833 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import isObject from './isObject';
import isPlainObject from './isPlainObject';
import isWeb from './isWeb';

export default function cloneObject<T>(data: T): T {
  let copy: any;
  const isArray = Array.isArray(data);
  const isFileListInstance =
    typeof FileList !== 'undefined' ? data instanceof FileList : false;

  if (data instanceof Date) {
    copy = new Date(data);
  } else if (
    !(isWeb && (data instanceof Blob || isFileListInstance)) &&
    (isArray || isObject(data))
  ) {
    copy = isArray ? [] : Object.create(Object.getPrototypeOf(data));

    if (!isArray && !isPlainObject(data)) {
      copy = data;
    } else {
      for (const key in data) {
        if (data.hasOwnProperty(key)) {
          copy[key] = cloneObject(data[key]);
        }
      }
    }
  } else {
    return data;
  }

  return copy;
}