forked from isysd/shallow-copy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
35 lines (30 loc) · 798 Bytes
/
index.js
File metadata and controls
35 lines (30 loc) · 798 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
module.exports = function (obj) {
if (!obj || typeof obj !== 'object') return obj;
var copy;
if (isArray(obj)) {
var len = obj.length;
copy = Array(len);
for (var i = 0; i < len; i++) {
copy[i] = obj[i];
}
}
else {
var keys = objectKeys(obj);
copy = {};
for (var i = 0, l = keys.length; i < l; i++) {
var key = keys[i];
copy[key] = obj[key];
}
}
return copy;
};
var objectKeys = Object.keys || function (obj) {
var keys = [];
for (var key in obj) {
if ({}.hasOwnProperty.call(obj, key)) keys.push(key);
}
return keys;
};
var isArray = Array.isArray || function (xs) {
return {}.toString.call(xs) === '[object Array]';
};