Skip to content

Commit 6fffac0

Browse files
committed
release for version 0.5.0
2 parents 04eb779 + 4da06b6 commit 6fffac0

File tree

10 files changed

+128
-23
lines changed

10 files changed

+128
-23
lines changed

.closurelinter

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
--strict
22
--nojsdoc
33
-e node_modules,bower_components
4-
-x deepcopy.min.js
4+
-x deepcopy.min.js,test.pwr.js

.travis.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
language: node_js
22
node_js:
33
- "0.10"
4+
- "0.11"
5+
- "0.12"
6+
- "iojs"
7+
sudo: false
8+
cache:
9+
directories:
10+
- node_modules
11+
- bower_components
412
notifications:
513
email: false
614
before_script:

HISTORY.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 0.5.0 / 2015-04-11
2+
3+
- supported for Symbol
4+
15
# 0.4.0 / 2015-01-10
26

37
- refactored

README.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
[![Build Status](https://travis-ci.org/sasaplus1/deepcopy.js.svg)](https://travis-ci.org/sasaplus1/deepcopy.js)
44
[![Dependency Status](https://gemnasium.com/sasaplus1/deepcopy.js.svg)](https://gemnasium.com/sasaplus1/deepcopy.js)
5-
[![NPM version](https://badge.fury.io/js/deepcopy.js.svg)](http://badge.fury.io/js/deepcopy.js)
6-
[![Bower version](https://badge.fury.io/bo/deepcopy.js.svg)](http://badge.fury.io/bo/deepcopy.js)
5+
[![NPM version](https://badge.fury.io/js/deepcopy.svg)](http://badge.fury.io/js/deepcopy)
6+
[![Bower version](https://badge.fury.io/bo/deepcopy.svg)](http://badge.fury.io/bo/deepcopy)
77

88
deep copy for any data
99

@@ -84,6 +84,22 @@ console.log(require('util').inspect(deep, { depth: null }));
8484
// to: [Circular] } }
8585
```
8686

87+
```js
88+
var data, deep;
89+
90+
data = { object: {} };
91+
data.object[Symbol.for('sym')] = 123;
92+
93+
deep = deepcopy(data);
94+
95+
delete data.object;
96+
97+
console.log(data.object);
98+
// undefined
99+
console.log(deep.object[Symbol.for('sym')]);
100+
// 123
101+
```
102+
87103
## Functions
88104

89105
### deepcopy(value)
@@ -112,6 +128,7 @@ supported types are below:
112128
* recursive copy
113129
* also can copy if it has circular reference
114130
* Buffer (node.js only)
131+
* Symbol
115132

116133
## Test
117134

bower.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"bower_components"
88
],
99
"devDependencies": {
10+
"es5-shim": "~4.0",
1011
"mocha": "~2.1",
1112
"power-assert": "~0.10"
1213
}

deepcopy.js

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@
2424
}(this, function() {
2525
'use strict';
2626

27-
var util, isBuffer, getKeys, indexOfArray;
27+
var isNode, util, isBuffer, getKeys, getSymbols, indexOfArray;
28+
29+
// is node.js/io.js?
30+
isNode = (typeof process !== 'undefined' && typeof require !== 'undefined');
2831

2932
// fallback util module for browser.
30-
util = (typeof exports === 'object') ? require('util') : (function() {
33+
util = (isNode) ? require('util') : (function() {
3134
function isArray(value) {
3235
return (typeof value === 'object' &&
3336
Object.prototype.toString.call(value) === '[object Array]');
@@ -43,18 +46,28 @@
4346
Object.prototype.toString.call(value) === '[object RegExp]');
4447
}
4548

49+
function isSymbol(value) {
50+
return (typeof value === 'symbol');
51+
}
52+
4653
return {
4754
isArray: (typeof Array.isArray === 'function') ?
4855
function(obj) {
4956
return Array.isArray(obj);
5057
} : isArray,
5158
isDate: isDate,
52-
isRegExp: isRegExp
59+
isRegExp: isRegExp,
60+
isSymbol: (typeof Symbol === 'function') ?
61+
isSymbol :
62+
function() {
63+
// always return false when Symbol is not supported.
64+
return false;
65+
}
5366
};
5467
}());
5568

5669
// fallback Buffer.isBuffer
57-
isBuffer = (typeof exports === 'object' && typeof Buffer === 'function') ?
70+
isBuffer = (isNode) ?
5871
function(obj) {
5972
return Buffer.isBuffer(obj);
6073
} :
@@ -83,6 +96,16 @@
8396
return keys;
8497
};
8598

99+
// get symbols in object.
100+
getSymbols = (typeof Symbol === 'function') ?
101+
function(obj) {
102+
return Object.getOwnPropertySymbols(obj);
103+
} :
104+
function() {
105+
// always return empty array when Symbol is not supported.
106+
return [];
107+
};
108+
86109
// fallback Array#indexOf for old browsers.
87110
indexOfArray = (typeof Array.prototype.indexOf === 'function') ?
88111
function(array, searchElement) {
@@ -117,14 +140,25 @@
117140
function copyValue_(value, clone, visited, reference) {
118141
var str, pos, buf, keys, i, len, key, val, idx, obj, ref;
119142

120-
// number, string, boolean, null, undefined and function.
143+
// number, string, boolean, null, undefined, function and symbol.
121144
if (value === null || typeof value !== 'object') {
122145
return value;
123146
}
124147

125148
// Date.
126149
if (util.isDate(value)) {
127150
// Firefox need to convert to Number
151+
//
152+
// Firefox:
153+
// var date = new Date;
154+
// +date; // 1420909365967
155+
// +new Date(date); // 1420909365000
156+
// +new Date(+date); // 1420909365967
157+
// Chrome:
158+
// var date = new Date;
159+
// +date; // 1420909757913
160+
// +new Date(date); // 1420909757913
161+
// +new Date(+date); // 1420909757913
128162
return new Date(+value);
129163
}
130164

@@ -151,7 +185,7 @@
151185
}
152186

153187
// Object or Array.
154-
keys = getKeys(value);
188+
keys = getKeys(value).concat(getSymbols(value));
155189

156190
for (i = 0, len = keys.length; i < len; ++i) {
157191
key = keys[i];

deepcopy.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "deepcopy",
3-
"version": "0.4.0",
3+
"version": "0.5.0",
44
"author": "sasa+1 <[email protected]>",
55
"contributors": [
66
"kjirou <[email protected]>"
@@ -20,19 +20,19 @@
2020
"bower": "bower install",
2121
"fix": "fixjsstyle --flagfile .closurelinter -r .",
2222
"lint": "gjslint --flagfile .closurelinter -r .",
23-
"minify": "uglifyjs ./deepcopy.js --comments -cm -r deepcopy -o ./deepcopy.min.js",
23+
"minify": "uglifyjs ./deepcopy.js --comments '@license' -cm -r deepcopy -o ./deepcopy.min.js",
2424
"power": "espower ./test/test.js > ./test/test.pwr.js",
25-
"test": "mocha",
26-
"testem": "testem ci",
25+
"test": "mocha ./test/test.js",
26+
"testem": "testem",
2727
"travis": "testem ci --launch Firefox,PhantomJS"
2828
},
2929
"devDependencies": {
30-
"bower": "~1.3",
31-
"espower-cli": "^0.2",
32-
"intelli-espower-loader": "~0.5",
33-
"mocha": "~2.1",
30+
"bower": "~1.4",
31+
"espower-cli": "~0.2",
32+
"intelli-espower-loader": "~0.6",
33+
"mocha": "~2.2",
3434
"power-assert": "~0.10",
35-
"testem": "~0.6",
35+
"testem": "~0.7",
3636
"uglify-js": "~2.4"
3737
}
3838
}

test/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
</head>
99
<body>
1010
<div id="mocha"></div>
11+
<!--[if lte IE 8.0]>
12+
<script src="../bower_components/es5-shim/es5-shim.min.js"></script>
13+
<![endif]-->
1114
<script src="../bower_components/power-assert/build/power-assert.js"></script>
1215
<script src="../bower_components/mocha/mocha.js"></script>
1316
<script src="/testem.js"></script>

test/test.js

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@
6363
assert.deepEqual(deepcopy(buffer), buffer);
6464
} : undefined);
6565

66+
it('should return Symbol', (typeof Symbol === 'function') ? function() {
67+
var symbol = Symbol();
68+
69+
assert(deepcopy(symbol) === symbol);
70+
} : undefined);
71+
6672
});
6773

6874
describe('check for recursive copy', function() {
@@ -97,14 +103,28 @@
97103
});
98104

99105
assert(copy === copy.to);
100-
assert(copy.to === copy);
101106
});
102107

108+
it('should return object, it has symbol', (typeof Symbol === 'function') ?
109+
function() {
110+
var object = {},
111+
a = Symbol.for('a'),
112+
b = Symbol.for('b'),
113+
c = Symbol.for('c');
114+
115+
object[a] = 1;
116+
object[b] = 2;
117+
object[c] = 3;
118+
119+
assert.deepEqual(deepcopy(object), object);
120+
} : undefined
121+
);
122+
103123
});
104124

105-
describe('check for duplicate item', function() {
125+
describe('check for duplicate function', function() {
106126

107-
it('should return array, it has duplicate item', function() {
127+
it('should return array, it has duplicate function', function() {
108128
var array = [fn, fn],
109129
copy = deepcopy(array);
110130

@@ -113,7 +133,7 @@
113133
assert(copy[0] === copy[1]);
114134
});
115135

116-
it('should return object, it has duplicate item', function() {
136+
it('should return object, it has duplicate function', function() {
117137
var object = { a: fn, b: fn },
118138
copy = deepcopy(object);
119139

@@ -122,6 +142,24 @@
122142
assert(copy.a === copy.b);
123143
});
124144

145+
it('should return object, it has symbol', (typeof Symbol === 'function') ?
146+
function() {
147+
var object = {},
148+
a = Symbol.for('a'),
149+
b = Symbol.for('b'),
150+
copy;
151+
152+
function fn() {}
153+
154+
object[a] = fn;
155+
object[b] = fn;
156+
157+
copy = deepcopy(object);
158+
159+
assert(copy[a] === copy[b]);
160+
} : undefined
161+
);
162+
125163
});
126164

127165
});

0 commit comments

Comments
 (0)