Skip to content

Commit

Permalink
datastore: deep clone user objects (#1552)
Browse files Browse the repository at this point in the history
* datastore: deep clone user objects

* add notStrictEqual test
  • Loading branch information
stephenplusplus authored and callmehiphop committed Aug 31, 2016
1 parent 413dd46 commit 2948c14
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/datastore/src/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

var arrify = require('arrify');
var createErrorClass = require('create-error-class');
var extend = require('extend');
var is = require('is');

var entity = module.exports;
Expand Down Expand Up @@ -285,6 +286,8 @@ function encodeValue(value) {

if (is.object(value)) {
if (!is.empty(value)) {
value = extend(true, {}, value);

for (var prop in value) {
if (value.hasOwnProperty(prop)) {
value[prop] = entity.encodeValue(value[prop]);
Expand Down
18 changes: 18 additions & 0 deletions packages/datastore/test/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var deepStrictEqual = require('deep-strict-equal');
assert.deepStrictEqual = assert.deepStrictEqual || function() {
return assert(deepStrictEqual.apply(this, arguments));
};
var extend = require('extend');

var Datastore = require('../');

Expand Down Expand Up @@ -430,6 +431,23 @@ describe('entity', function() {
assert.deepEqual(entity.encodeValue(value), expectedValueProto);
});

it('should clone an object', function() {
var value = {
a: {
b: {
obj: true
}
}
};

var originalValue = extend(true, {}, value);

var encodedValue = entity.encodeValue(value);

assert.deepEqual(value, originalValue);
assert.notStrictEqual(value, encodedValue);
});

it('should encode an empty object', function() {
var value = {};

Expand Down

0 comments on commit 2948c14

Please sign in to comment.