Skip to content

Commit

Permalink
Merge e4c62ea into adfdd00
Browse files Browse the repository at this point in the history
  • Loading branch information
katowulf committed Aug 28, 2014
2 parents adfdd00 + e4c62ea commit 5b67be6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@
else {
dat = {};
utils.each(rec, function (v, k) {
dat[k] = v;
dat[k] = stripDollarPrefixedKeys(v);
});
}
if( angular.isDefined(rec.$value) && Object.keys(dat).length === 0 && rec.$value !== null ) {
Expand All @@ -316,4 +316,15 @@
return utils;
}
]);

function stripDollarPrefixedKeys(data) {
if( !angular.isObject(data) ) { return data; }
var out = angular.isArray(data)? [] : {};
angular.forEach(data, function(v,k) {
if(typeof k !== 'string' || k.charAt(0) !== '$') {
out[k] = stripDollarPrefixedKeys(v);
}
});
return out;
}
})();
24 changes: 24 additions & 0 deletions tests/unit/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,30 @@ describe('$firebaseUtils', function () {
expect($utils.toJSON(json)).toEqual({foo: json.foo});
});

it('should remove any deeply nested variables prefixed with $', function() {
var json = {
arr: [[
{$$hashKey: false, $key: 1, alpha: 'alpha', bravo: {$$private: '$$private', $key: '$key', bravo: 'bravo'}},
{$$hashKey: false, $key: 1, alpha: 'alpha', bravo: {$$private: '$$private', $key: '$key', bravo: 'bravo'}}

], ["a", "b", {$$key: '$$key'}]],
obj: {
nest: {$$hashKey: false, $key: 1, alpha: 'alpha', bravo: {$$private: '$$private', $key: '$key', bravo: 'bravo'} }
}
};

expect($utils.toJSON(json)).toEqual({
arr: [[
{alpha: 'alpha', bravo: {bravo: 'bravo'}},
{alpha: 'alpha', bravo: {bravo: 'bravo'}}

], ["a", "b", {}]],
obj: {
nest: {alpha: 'alpha', bravo: {bravo: 'bravo'} }
}
});
});

it('should throw error if an invalid character in key', function() {
expect(function() {
$utils.toJSON({'foo.bar': 'foo.bar'});
Expand Down

0 comments on commit 5b67be6

Please sign in to comment.