Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit e296d7c

Browse files
committed
fix(copy): add support for String/Boolean/Number object types
1 parent e4e5677 commit e296d7c

File tree

2 files changed

+60
-12
lines changed

2 files changed

+60
-12
lines changed

src/Angular.js

+33-12
Original file line numberDiff line numberDiff line change
@@ -668,8 +668,8 @@ function isBlob(obj) {
668668
}
669669

670670

671-
function isBoolean(value) {
672-
return typeof value === 'boolean';
671+
function isBoolean(obj) {
672+
return typeof obj === 'boolean';
673673
}
674674

675675

@@ -886,16 +886,7 @@ function copy(source, destination) {
886886
if (isArray(source)) {
887887
destination = [];
888888
needsRecurse = true;
889-
} else if (isTypedArray(source)) {
890-
destination = new source.constructor(source);
891-
} else if (isDate(source)) {
892-
destination = new Date(source.getTime());
893-
} else if (isRegExp(source)) {
894-
destination = new RegExp(source.source, source.toString().match(/[^\/]*$/)[0]);
895-
destination.lastIndex = source.lastIndex;
896-
} else if (isFunction(source.cloneNode)) {
897-
destination = source.cloneNode(true);
898-
} else {
889+
} else if (!(destination = copyType(source))) {
899890
destination = Object.create(getPrototypeOf(source));
900891
needsRecurse = true;
901892
}
@@ -907,6 +898,36 @@ function copy(source, destination) {
907898
? copyRecurse(source, destination)
908899
: destination;
909900
}
901+
902+
function copyType(source) {
903+
switch (toString.call(source)) {
904+
case '[object Int8Array]':
905+
case '[object Int16Array]':
906+
case '[object Int32Array]':
907+
case '[object Float32Array]':
908+
case '[object Float64Array]':
909+
case '[object Uint8Array]':
910+
case '[object Uint8ClampedArray]':
911+
case '[object Uint16Array]':
912+
case '[object Uint32Array]':
913+
case '[object Boolean]':
914+
case '[object Number]':
915+
case '[object String]':
916+
return new source.constructor(source);
917+
918+
case '[object Date]':
919+
return new Date(source.getTime());
920+
921+
case '[object RegExp]':
922+
var re = new RegExp(source.source, source.toString().match(/[^\/]*$/)[0]);
923+
re.lastIndex = source.lastIndex;
924+
return re;
925+
}
926+
927+
if (isFunction(source.cloneNode)) {
928+
return source.cloneNode(true);
929+
}
930+
}
910931
}
911932

912933
/**

test/AngularSpec.js

+27
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,33 @@ describe('angular', function() {
471471
expect(dest.c).toBe(3);
472472
expect(Object.keys(dest)).toEqual(['a', 'b', 'c']);
473473
});
474+
475+
it('should copy String() objects', function() {
476+
/*jshint -W053 */
477+
var obj = new String('foo');
478+
/*jshint +W053 */
479+
var dest = copy(obj);
480+
expect(obj).not.toBe(dest);
481+
expect(obj + "").toBe(dest + "");
482+
});
483+
484+
it('should copy Boolean() objects', function() {
485+
/*jshint -W053 */
486+
var obj = new Boolean(true);
487+
/*jshint +W053 */
488+
var dest = copy(obj);
489+
expect(obj).not.toBe(dest);
490+
expect(+obj).toBe(+dest);
491+
});
492+
493+
it('should copy Number() objects', function() {
494+
/*jshint -W053 */
495+
var obj = new Number(42);
496+
/*jshint +W053 */
497+
var dest = copy(obj);
498+
expect(obj).not.toBe(dest);
499+
expect(+obj).toBe(+dest);
500+
});
474501
});
475502

476503
describe("extend", function() {

0 commit comments

Comments
 (0)