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

Commit b5baf52

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

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

src/Angular.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ function isBlankObject(value) {
554554
* @param {*} value Reference to check.
555555
* @returns {boolean} True if `value` is a `String`.
556556
*/
557-
function isString(value) {return typeof value === 'string';}
557+
function isString(value) {return toString.call(value) === '[object String]';}
558558

559559

560560
/**
@@ -575,7 +575,7 @@ function isString(value) {return typeof value === 'string';}
575575
* @param {*} value Reference to check.
576576
* @returns {boolean} True if `value` is a `Number`.
577577
*/
578-
function isNumber(value) {return typeof value === 'number';}
578+
function isNumber(value) {return toString.call(value) === '[object Number]';}
579579

580580

581581
/**
@@ -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 toString.call(obj) === '[object Boolean]';
673673
}
674674

675675

@@ -888,6 +888,8 @@ function copy(source, destination) {
888888
needsRecurse = true;
889889
} else if (isTypedArray(source)) {
890890
destination = new source.constructor(source);
891+
} else if (isBoolean(source) || isString(source) || isNumber(source)) {
892+
destination = new source.constructor(source);
891893
} else if (isDate(source)) {
892894
destination = new Date(source.getTime());
893895
} else if (isRegExp(source)) {

test/AngularSpec.js

+24
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,30 @@ 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+
var obj = new String('foo');
477+
var dest = copy(obj);
478+
expect(obj).not.toBe(dest);
479+
expect(isString(dest)).toBe(true);
480+
expect(obj + "").toBe(dest + "");
481+
});
482+
483+
it('should copy Boolean() objects', function() {
484+
var obj = new Boolean(true);
485+
var dest = copy(obj);
486+
expect(obj).not.toBe(dest);
487+
expect(isBoolean(dest)).toBe(true);
488+
expect(+obj).toBe(+dest);
489+
});
490+
491+
it('should copy Number() objects', function() {
492+
var obj = new Number(42);
493+
var dest = copy(obj);
494+
expect(obj).not.toBe(dest);
495+
expect(isNumber(dest)).toBe(true);
496+
expect(+obj).toBe(+dest);
497+
});
474498
});
475499

476500
describe("extend", function() {

0 commit comments

Comments
 (0)