From 3f310615b388d428c68bbf3b773505c868e90db6 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sat, 14 Jul 2012 00:41:25 +0200 Subject: o More tests. --- index.js | 76 ++++++++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 50 insertions(+), 26 deletions(-) (limited to 'index.js') diff --git a/index.js b/index.js index 8e2709a..07078cc 100644 --- a/index.js +++ b/index.js @@ -1,33 +1,62 @@ var _ = require('underscore'); +function toObject() { + return _.reduce(this.data, function(map, field) { + map[field.name] = field.value; + return map; + }, {}); +}; + +function fixTemplate(t) { + t.toObject = toObject; + t.data = _.isArray(t.data) ? t.data : []; +} + /** - * Takes any object and make it look like a collection+json object with utility methods. + * Takes any object and make it look like a collection+json collection object. */ function fromObject(root) { - root = _.isObject(root) ? root : {}; - root.collection = _.isObject(root.collection) ? root.collection : {}; - var c = root.collection; - c.version = _.isString(c.version) ? c.version : "1.0"; +// console.log('in ', root); + + root.isCollection = function() { + return typeof this.collection == 'object'; + } + root.isTemplate = function() { + return typeof this.template == 'object'; + } - c.items = _.isArray(c.items) ? c.items : []; - _.each(c.items, function(item) { - item.links = _.isArray(item.links) ? item.links : []; - item.toObject = function() { - return _.reduce(item.data, function(map, field) { - map[field.name] = field.value; - return map; - }, {}); + if(root.isTemplate()) { + fixTemplate(root.template); + } + else { + root = _.isObject(root) ? root : {collection: {}}; + if(!_.isObject(root.collection)) { + root.collection = {}; } - }); + var c = root.collection; + + c.version = _.isString(c.version) ? c.version : "1.0"; + c.items = _.isArray(c.items) ? c.items : []; + _.each(c.items, function(item) { + item.links = _.isArray(item.links) ? item.links : []; + item.toObject = toObject; + }); + + c.links = _.isArray(c.links) ? c.links : []; - c.links = _.isArray(c.links) ? c.links : []; + c.queries = _.isArray(c.queries) ? c.queries : []; + _.each(c.queries, function(query) { + query.data = _.isArray(query.data) ? query.data : []; + }); - c.queries = _.isArray(c.queries) ? c.queries : []; - _.each(c.queries, function(query) { - query.data = _.isArray(query.data) ? query.data : []; - }); + if(_.isObject(c.template)) { + fixTemplate(c.template); + } + else { + delete c.template; + } + } - c.template = _.isObject(c.template) ? c.template : undefined; // c.templates = _.isArray(c.templates) ? c.templates : []; // TODO: make un-enumerable @@ -41,13 +70,8 @@ function fromObject(root) { return rel === link.rel; }); } - root.isCollection = function() { - return typeof this.collection == 'object'; - } - root.isTemplate = function() { - return typeof this.template == 'object'; - } +// console.log('out', root); return root; } module.exports.fromObject = fromObject; -- cgit v1.2.3