Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add #inspect() helper for clearer console.logging

  • Loading branch information...
commit 5897af6e073bed50ed4af346de169b00963803bf 1 parent 17c93e0
@aheckmann aheckmann authored
View
13 lib/mongoose/document.js
@@ -9,6 +9,7 @@ var EventEmitter = require('events').EventEmitter
, Schema = require('./schema')
, utils = require('./utils')
, clone = utils.clone
+ , inspect = require('util').inspect
, ActiveRoster = utils.StateMachine.ctor('require', 'modify', 'init')
, deepEqual = utils.deepEqual;
@@ -674,7 +675,6 @@ Document.prototype.doQueue = function () {
* @api public
*/
-
Document.prototype.toObject = function () {
return clone(this.doc, true);
};
@@ -691,6 +691,17 @@ Document.prototype.toJSON = function () {
};
/**
+ * Helper for console.log
+ *
+ * @api public
+ */
+
+Document.prototype.inspect = function () {
+ return inspect(this.doc);
+};
+
+
+/**
* Returns true if the Document stores the same data as doc.
* @param {Document} doc to compare to
* @return {Boolean}
View
14 lib/mongoose/types/array.js
@@ -283,6 +283,20 @@ MongooseArray.prototype.toObject = function () {
};
/**
+ * Helper for console.log
+ *
+ * @api public
+ */
+
+MongooseArray.prototype.inspect = function () {
+ return '[' + this.map(function (doc) {
+ return ' ' + doc;
+ }) + ' ]';
+};
+
+
+
+/**
* Return the index of `obj` or `-1.`
*
* @param {Object} obj
View
13 lib/mongoose/types/document.js
@@ -3,7 +3,8 @@
* Module dependencies.
*/
-var Document = require('../document');
+var Document = require('../document')
+ , inspect = require('util').inspect;
/**
* EmbeddedDocument constructor.
@@ -92,6 +93,16 @@ EmbeddedDocument.prototype.remove = function (fn) {
};
/**
+ * Helper for console.log
+ *
+ * @api public
+ */
+
+EmbeddedDocument.prototype.inspect = function () {
+ return inspect(this.toObject());
+};
+
+/**
* Register hooks for some methods
*/
View
12 lib/mongoose/types/documentarray.js
@@ -88,6 +88,18 @@ MongooseDocumentArray.prototype.toObject = function () {
};
/**
+ * Helper for console.log
+ *
+ * @api public
+ */
+
+MongooseDocumentArray.prototype.inspect = function () {
+ return '[' + this.map(function (doc) {
+ return doc.inspect();
+ }).join('\n') + ']';
+};
+
+/**
* Module exports.
*/
View
2  lib/mongoose/types/number.js
@@ -69,10 +69,12 @@ MongooseNumber.prototype.decrement = function(){
* @api public
*/
+MongooseNumber.prototype.inspect =
MongooseNumber.prototype.toString = function () {
return String(this.valueOf());
};
+
/**
* Module exports
*/
View
29 test/model.test.js
@@ -3324,6 +3324,35 @@ module.exports = {
should.strictEqual(err, null);
save.should.be.true;
});
+ },
+
+ 'console.log shows helpful values': function () {
+ var db = start()
+ , BlogPost = db.model('BlogPost', collection);
+
+ var date = new Date(1305730951086);
+ var id0 = new DocumentObjectId('4dd3e169dbfb13b4570000b9');
+ var id1 = new DocumentObjectId('4dd3e169dbfb13b4570000b6');
+ var id2 = new DocumentObjectId('4dd3e169dbfb13b4570000b7');
+ var id3 = new DocumentObjectId('4dd3e169dbfb13b4570000b8');
+
+ var post = new BlogPost({
+ title: 'Test'
+ , _id: id0
+ , date: date
+ , numbers: [5,6,7]
+ , owners: [id1]
+ , meta: { visitors: 45 }
+ , comments: [
+ { _id: id2, title: 'my comment', date: date, body: 'this is a comment' },
+ { _id: id3, title: 'the next thang', date: date, body: 'this is a comment too!' }]
+ });
+
+ db.close();
+
+ var a = '{ meta: { visitors: 45 },\n numbers: [ 5, 6, 7 ],\n owners: [ 4dd3e169dbfb13b4570000b6 ],\n comments: \n [{ _id: 4dd3e169dbfb13b4570000b7,\n comments: [],\n body: \'this is a comment\',\n date: Wed, 18 May 2011 15:02:31 GMT,\n title: \'my comment\' }\n { _id: 4dd3e169dbfb13b4570000b8,\n comments: [],\n body: \'this is a comment too!\',\n date: Wed, 18 May 2011 15:02:31 GMT,\n title: \'the next thang\' }],\n _id: 4dd3e169dbfb13b4570000b9,\n date: Wed, 18 May 2011 15:02:31 GMT,\n title: \'Test\' }'
+
+ post.inspect().should.eql(a);
}
};
Please sign in to comment.
Something went wrong with that request. Please try again.