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

Commit 99c36b1

Browse files
committed
Fixes #400 - strip deeply nested $ prefixes when saving json
1 parent bedb45b commit 99c36b1

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/utils.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@
277277
else {
278278
dat = {};
279279
utils.each(rec, function (v, k) {
280-
dat[k] = v;
280+
dat[k] = stripDollarPrefixedKeys(v);
281281
});
282282
}
283283
if( angular.isDefined(rec.$value) && Object.keys(dat).length === 0 && rec.$value !== null ) {
@@ -303,4 +303,15 @@
303303
return utils;
304304
}
305305
]);
306+
307+
function stripDollarPrefixedKeys(data) {
308+
if( !angular.isObject(data) ) { return data; }
309+
var out = angular.isArray(data)? [] : {};
310+
angular.forEach(data, function(v,k) {
311+
if(typeof k !== 'string' || k.charAt(0) !== '$') {
312+
out[k] = stripDollarPrefixedKeys(v);
313+
}
314+
});
315+
return out;
316+
}
306317
})();

tests/unit/utils.spec.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,30 @@ describe('$firebaseUtils', function () {
108108
expect($utils.toJSON(json)).toEqual({foo: json.foo});
109109
});
110110

111+
it('should remove any deeply nested variables prefixed with $', function() {
112+
var json = {
113+
arr: [[
114+
{$$hashKey: false, $key: 1, alpha: 'alpha', bravo: {$$private: '$$private', $key: '$key', bravo: 'bravo'}},
115+
{$$hashKey: false, $key: 1, alpha: 'alpha', bravo: {$$private: '$$private', $key: '$key', bravo: 'bravo'}}
116+
117+
], ["a", "b", {$$key: '$$key'}]],
118+
obj: {
119+
nest: {$$hashKey: false, $key: 1, alpha: 'alpha', bravo: {$$private: '$$private', $key: '$key', bravo: 'bravo'} }
120+
}
121+
};
122+
123+
expect($utils.toJSON(json)).toEqual({
124+
arr: [[
125+
{alpha: 'alpha', bravo: {bravo: 'bravo'}},
126+
{alpha: 'alpha', bravo: {bravo: 'bravo'}}
127+
128+
], ["a", "b", {}]],
129+
obj: {
130+
nest: {alpha: 'alpha', bravo: {bravo: 'bravo'} }
131+
}
132+
});
133+
});
134+
111135
it('should throw error if an invalid character in key', function() {
112136
expect(function() {
113137
$utils.toJSON({'foo.bar': 'foo.bar'});

0 commit comments

Comments
 (0)