Skip to content

Commit

Permalink
Merge pull request Automattic#849 from aheckmann/configurableMinimize
Browse files Browse the repository at this point in the history
configurable minimize #to{Object,JSON}(option)
  • Loading branch information
rauchg committed Apr 20, 2012
2 parents 36646ed + d72d189 commit cf130ac
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/document.js
Expand Up @@ -1156,7 +1156,7 @@ Document.prototype.toObject = function (options) {
: {};
}

options.minimize = true;
;('minimize' in options) || (options.minimize = true);

var ret = clone(this._doc, options);

Expand Down Expand Up @@ -1237,7 +1237,10 @@ Document.prototype.toJSON = function (options) {

Document.prototype.toString =
Document.prototype.inspect = function (options) {
return inspect(this.toObject(options));
var opts = options && 'Object' == options.constructor.name
? options
: undefined
return inspect(this.toObject(opts));
};

/**
Expand Down
18 changes: 18 additions & 0 deletions test/document.test.js
Expand Up @@ -222,6 +222,7 @@ module.exports = {
, cool : DocumentObjectId.fromString('4c6c2d6240ced95d0e00003c')
, path : 'my path'
}
, nested2: {}
});

var clone = doc.toObject({ getters: true, virtuals: false });
Expand Down Expand Up @@ -262,6 +263,17 @@ module.exports = {
clone.nested.agePlus2.should.eql(7);
clone.em[0].title.should.equal('asdf');
delete doc.schema.options.toObject;

// minimize
clone = doc.toObject({ minimize: true });
should.equal(undefined, clone.nested2);
clone = doc.toObject({ minimize: false });
should.eql({}, clone.nested2);

doc.schema.options.toObject = { minimize: false };
clone = doc.toObject();
should.eql({}, clone.nested2);
delete doc.schema.options.toObject;
},

'toJSON options': function () {
Expand All @@ -276,6 +288,7 @@ module.exports = {
, cool : DocumentObjectId.fromString('4c6c2d6240ced95d0e00003c')
, path : 'my path'
}
, nested2: {}
});

// override to check if toJSON gets fired
Expand All @@ -295,6 +308,11 @@ module.exports = {
clone.em[0].should.eql({});
delete doc.schema.options.toJSON;
delete path.casterConstructor.prototype.toJSON;

doc.schema.options.toObject = { minimize: false };
clone = doc.toObject();
should.eql({}, clone.nested2);
delete doc.schema.options.toObject;
},

'test hooks system': function(beforeExit){
Expand Down

0 comments on commit cf130ac

Please sign in to comment.