summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--index.js26
-rw-r--r--test/collection_json.test.js55
2 files changed, 49 insertions, 32 deletions
diff --git a/index.js b/index.js
index f467018..8e2709a 100644
--- a/index.js
+++ b/index.js
@@ -12,6 +12,12 @@ function fromObject(root) {
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;
+ }, {});
+ }
});
c.links = _.isArray(c.links) ? c.links : [];
@@ -21,19 +27,13 @@ function fromObject(root) {
query.data = _.isArray(query.data) ? query.data : [];
});
- c.templates = _.isArray(c.templates) ? c.templates : [];
+ c.template = _.isObject(c.template) ? c.template : undefined;
+ // c.templates = _.isArray(c.templates) ? c.templates : [];
// TODO: make un-enumerable
- root.mapItemData = function(f) {
+ root.mapItems = function(f) {
return _.map(this.collection.items, function(item) {
-// console.log("item", item);
- var map = _.reduce(item.data, function(map, field) {
-// console.log("field", field);
- map[field.name] = field.value;
- return map;
- }, {});
-// console.log("map", map);
- return f(map);
+ return f(item.toObject());
});
};
root.findLinkByRel = function(rel) {
@@ -41,6 +41,12 @@ function fromObject(root) {
return rel === link.rel;
});
}
+ root.isCollection = function() {
+ return typeof this.collection == 'object';
+ }
+ root.isTemplate = function() {
+ return typeof this.template == 'object';
+ }
return root;
}
diff --git a/test/collection_json.test.js b/test/collection_json.test.js
index 24f69e4..0cd003e 100644
--- a/test/collection_json.test.js
+++ b/test/collection_json.test.js
@@ -3,37 +3,48 @@ var assert = require('assert')
function identity(x) { return x; }
-var collection1 = {collection: {
- items: [
- { href: "http://example.com/1", links: [],
- data: [
- { prompt: "prompt1", name: "field-1", value: "1", baz: "woot" },
- { prompt: "prompt2", name: "field-2", value: "2", foz: "asdf" },
- ]
- },
- { href: "http://example.com/2", links: [],
- data: [
- { prompt: "prompt1", name: "field-1", value: "3" },
- { prompt: "prompt2", name: "field-2", value: "4" },
- ]
- }
- ]
-}};
+function collection1() {
+ return collection_json.fromObject({collection: {
+ items: [
+ { href: "http://example.com/1", links: [],
+ data: [
+ { prompt: "prompt1", name: "field-1", value: "1", baz: "woot" },
+ { prompt: "prompt2", name: "field-2", value: "2", foz: "asdf" },
+ ]
+ },
+ { href: "http://example.com/2", links: [],
+ data: [
+ { prompt: "prompt1", name: "field-1", value: "3" },
+ { prompt: "prompt2", name: "field-2", value: "4" },
+ ]
+ }
+ ]
+ }});
+}
describe('fromObject', function() {
it('Has basic fields', function() {
var collection = collection_json.fromObject({}).collection;
- assert.deepEqual([], collection.items);
- assert.deepEqual([], collection.links);
- assert.deepEqual([], collection.queries);
- assert.deepEqual([], collection.templates);
+ assert.deepEqual(collection.items, []);
+ assert.deepEqual(collection.links, []);
+ assert.deepEqual(collection.queries, []);
+ assert.equal(collection.template, undefined);
});
it('Can map data field in items', function() {
- assert.deepEqual(collection_json.fromObject(collection1).mapItemData(identity),
+ assert.deepEqual(collection1().mapItems(identity),
[{"field-1": "1", "field-2": "2"}
,{"field-1": "3", "field-2": "4"}]);
function f(map) { return parseInt(map["field-1"]) + parseInt(map["field-2"]); }
- assert.deepEqual(collection_json.fromObject(collection1).mapItemData(f), [3, 7]);
+ assert.deepEqual(collection1().mapItems(f), [3, 7]);
+ });
+});
+
+describe('collection object', function() {
+ it('Can create objects from items', function() {
+ assert.deepEqual(collection1().collection.items[0].toObject(), {
+ "field-1": "1",
+ "field-2": "2"
+ });
});
});