Skip to content
This repository was archived by the owner on Mar 17, 2025. It is now read-only.

Commit a9ea2d1

Browse files
mcleanraJacob Wenger
authored andcommitted
Updated $firebaseUtils.deepCopy() to properly support Date object
1 parent 7e5a7e8 commit a9ea2d1

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

src/utils/utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@
183183
},
184184

185185
deepCopy: function(obj) {
186-
if( !angular.isObject(obj) ) { return obj; }
186+
if( !angular.isObject(obj) || angular.isDate(obj) ) { return obj; }
187187
var newCopy = angular.isArray(obj) ? obj.slice() : angular.extend({}, obj);
188188
for (var key in newCopy) {
189189
if (newCopy.hasOwnProperty(key)) {
@@ -395,7 +395,7 @@
395395
]);
396396

397397
function stripDollarPrefixedKeys(data) {
398-
if( !angular.isObject(data) ) { return data; }
398+
if( !angular.isObject(data) || angular.isDate(data)) { return data; }
399399
var out = angular.isArray(data)? [] : {};
400400
angular.forEach(data, function(v,k) {
401401
if(typeof k !== 'string' || k.charAt(0) !== '$') {

tests/unit/utils.spec.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,33 @@ describe('$firebaseUtils', function () {
136136
expect(spy).toHaveBeenCalledWith('baz', 'biz');
137137
});
138138
});
139+
140+
describe('#deepCopy', function() {
141+
it('should work for empty objects', function() {
142+
var obj = {};
143+
expect($utils.deepCopy(obj)).toEqual(obj);
144+
});
145+
it('should work for primitives', function() {
146+
var obj = 'foo';
147+
expect($utils.deepCopy(obj)).toEqual(obj);
148+
});
149+
it('should work for dates', function() {
150+
var obj = new Date();
151+
expect($utils.deepCopy(obj)).toEqual(obj);
152+
});
153+
it('should work for nested objects', function() {
154+
var d = new Date();
155+
var obj = { date: {date: [{date: d}, {int: 1}, {str: "foo"}, {}]}};
156+
expect($utils.deepCopy(obj)).toEqual(obj);
157+
});
158+
it('should work for functions', function() {
159+
var f = function(){
160+
var s = 'foo';
161+
};
162+
var obj = f;
163+
expect($utils.deepCopy(obj)).toEqual(obj);
164+
});
165+
});
139166

140167
describe('#updateRec', function() {
141168
it('should return true if changes applied', function() {
@@ -229,6 +256,13 @@ describe('$firebaseUtils', function () {
229256
var json = {foo: 'bar', $foo: '$bar'};
230257
expect($utils.toJSON(json)).toEqual({foo: json.foo});
231258
});
259+
260+
it('should be able to handle date objects', function(){
261+
var d = new Date();
262+
var json = {date: d};
263+
264+
expect($utils.toJSON(json)).toEqual({date: d});
265+
});
232266

233267
it('should remove any deeply nested variables prefixed with $', function() {
234268
var json = {

0 commit comments

Comments
 (0)