summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2012-06-28 12:27:39 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2012-06-28 12:27:39 +0200
commitb883e87b1e5222388e6562296009a49126d3af3c (patch)
tree90cb37dcc307f442aab63e52d1ecd0a13adedd14
downloadcollection_json.js-b883e87b1e5222388e6562296009a49126d3af3c.tar.gz
collection_json.js-b883e87b1e5222388e6562296009a49126d3af3c.tar.bz2
collection_json.js-b883e87b1e5222388e6562296009a49126d3af3c.tar.xz
collection_json.js-b883e87b1e5222388e6562296009a49126d3af3c.zip
o Initial import of application/vnd.collection+json parser code.
-rw-r--r--index.js40
-rw-r--r--package.json12
-rw-r--r--test/collection_json.test.js39
3 files changed, 91 insertions, 0 deletions
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..5705fec
--- /dev/null
+++ b/index.js
@@ -0,0 +1,40 @@
+var _ = require('underscore');
+
+/**
+ * Takes any object and make it look like a collection+json object with utility methods.
+ */
+function fromObject(root) {
+ root = root || {};
+ var collection = root.collection || {};
+ collection.items = collection.items || {};
+ collection.links = collection.links || {};
+ collection.queries = collection.queries || {};
+ collection.templates = collection.templates || {};
+
+ collection.mapItemData = function(f) {
+ return _.map(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 collection;
+}
+module.exports.fromObject = fromObject;
+
+function fromString(s) {
+ var object = {};
+ try {
+ object = JSON.parse(s);
+ }
+ catch(ex) {
+ }
+ return fromObject(object);
+}
+module.exports.fromString = fromString;
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..f14b77c
--- /dev/null
+++ b/package.json
@@ -0,0 +1,12 @@
+{
+ "name": "collection_json",
+ "version": "0.0.1",
+ "description": "application/vnd.collection+json Utility library",
+ "author": "Trygve Laugstøl <trygvis@inamo.no>",
+ "dependencies": {
+ "underscore": "latest"
+ },
+ "devDependencies": {
+ "mocha": "1.0.3"
+ }
+}
diff --git a/test/collection_json.test.js b/test/collection_json.test.js
new file mode 100644
index 0000000..b74d99b
--- /dev/null
+++ b/test/collection_json.test.js
@@ -0,0 +1,39 @@
+var assert = require('assert')
+ , collection_json = require('../');
+
+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" },
+ ]
+ }
+ ]
+}};
+
+describe('fromObject', function() {
+ it('Has basic fields', function() {
+ var collection = collection_json.fromObject({});
+ assert.deepEqual([], collection.items);
+ assert.deepEqual([], collection.links);
+ assert.deepEqual([], collection.queries);
+ assert.deepEqual([], collection.templates);
+ });
+
+ it('Can map data field in items', function() {
+ assert.deepEqual(collection_json.fromObject(collection1).mapItemData(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]);
+ });
+});