Created
December 2, 2011 19:17
-
-
Save codeboost/1424465 to your computer and use it in GitHub Desktop.
Convert Mongoose obj to JS obj and all ObjectIds to String
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
toObject = (item) -> | |
return item unless item.toObject | |
json = item.toObject() | |
for key, val of json | |
if _.isArray val | |
json[key] = _.map val, (element) -> toObject element | |
else | |
if val.toHexString then json[key] = val.toHexString() | |
return json | |
jsonify = (item) -> | |
return item unless item | |
if _.isArray item | |
_.map item, (element) -> toObject element | |
else | |
toObject(item) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This sample use toHexString to detect if an object is a ObjectID.