-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclone.js
More file actions
48 lines (40 loc) · 951 Bytes
/
clone.js
File metadata and controls
48 lines (40 loc) · 951 Bytes
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
35
36
37
38
39
40
41
42
43
44
45
46
47
// https://github.com/component/clone
/**
* Module dependencies.
*/
import type from './type'
/**
* Clones objects.
*
* @param {Mixed} any object
* @api public
*/
export default function clone(obj) {
switch (type(obj)) {
case 'object':
let copy = {}
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
copy[key] = clone(obj[key])
}
}
return copy
case 'array':
let arr = new Array(obj.length)
for (let i = 0, l = obj.length; i < l; i++) {
arr[i] = clone(obj[i])
}
return arr
case 'regexp':
// from millermedeiros/amd-utils - MIT
let flags = ''
flags += obj.multiline ? 'm' : ''
flags += obj.global ? 'g' : ''
flags += obj.ignoreCase ? 'i' : ''
return new RegExp(obj.source, flags)
case 'date':
return new Date(obj.getTime())
default: // string, number, boolean, …
return obj
}
}