Comparing version 1.0.5 to 1.0.6
14
index.js
@@ -1,5 +0,11 @@ | ||
var parse = require('./parse'); | ||
(function(exports, win) { | ||
if (exports) { | ||
var parse = require('./parse'); | ||
exports.parse = parse.parse; | ||
exports.ParsedResource = parse.ParsedResource; | ||
exports.Resource = require('./resource'); | ||
exports.parse = parse.parse; | ||
exports.ParsedResource = parse.ParsedResource; | ||
exports.Resource = require('./resource'); | ||
} else if (win) { | ||
win.hal = {}; | ||
} | ||
})(typeof(exports) == "undefined" ? null : exports, typeof(window) == "undefined" ? null : window); |
{ | ||
"name": "halson", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"description": "Fault-tolerant HAL+JSON parser and writer", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
144
parse.js
@@ -0,87 +1,93 @@ | ||
(function(exports, win) { | ||
function ParsedResource(data) { | ||
this.data = data; | ||
this._links = this._parse("_links"); | ||
this._embedded = this._parse("_embedded"); | ||
}; | ||
function ParsedResource(data) { | ||
this.data = data; | ||
this._links = this._parse("_links"); | ||
this._embedded = this._parse("_embedded"); | ||
}; | ||
ParsedResource.prototype.className = "HALSONParsedResource"; | ||
ParsedResource.prototype.className = "HALSONParsedResource"; | ||
ParsedResource.prototype._parse = function(list) { | ||
var data = this.data[list] || {}; | ||
var ret = {}; | ||
ParsedResource.prototype._parse = function(list) { | ||
var data = this.data[list] || {}; | ||
var ret = {}; | ||
for (var rel in data) { | ||
var items = [].concat(data[rel]); | ||
if (list == "_embedded") { | ||
items = items.map(function(item) { | ||
return new ParsedResource(item); | ||
}); | ||
for (var rel in data) { | ||
var items = [].concat(data[rel]); | ||
if (list == "_embedded") { | ||
items = items.map(function(item) { | ||
return new ParsedResource(item); | ||
}); | ||
} | ||
ret[rel] = items; | ||
} | ||
ret[rel] = items; | ||
} | ||
return ret; | ||
}; | ||
return ret; | ||
}; | ||
ParsedResource.prototype.get = function(key) { | ||
return this.data[key]; | ||
}; | ||
ParsedResource.prototype.get = function(key) { | ||
return this.data[key]; | ||
}; | ||
ParsedResource.prototype.self = function(attribute) { | ||
var self = this.link('self') || {}; | ||
if (!attribute) { | ||
return self; | ||
} | ||
return self[attribute]; | ||
}; | ||
ParsedResource.prototype.self = function(attribute) { | ||
var self = this.link('self') || {}; | ||
if (!attribute) { | ||
return self; | ||
} | ||
return self[attribute]; | ||
}; | ||
ParsedResource.prototype.links = function(rel) { | ||
return this._links[rel] || []; | ||
}; | ||
ParsedResource.prototype.links = function(rel) { | ||
return this._links[rel] || []; | ||
}; | ||
ParsedResource.prototype.link = function(rel) { | ||
var links = this.links(rel); | ||
return links[0]; | ||
}; | ||
ParsedResource.prototype.link = function(rel) { | ||
var links = this.links(rel); | ||
return links[0]; | ||
}; | ||
ParsedResource.prototype.linkByName = function(rel, name) { | ||
var links = this.links(rel); | ||
for (var i in links) { | ||
var link = links[i]; | ||
if (name === link.name) { | ||
return link; | ||
ParsedResource.prototype.linkByName = function(rel, name) { | ||
var links = this.links(rel); | ||
for (var i in links) { | ||
var link = links[i]; | ||
if (name === link.name) { | ||
return link; | ||
} | ||
} | ||
} | ||
}; | ||
}; | ||
ParsedResource.prototype.embeds = function(rel) { | ||
return this._embedded[rel] || []; | ||
}; | ||
ParsedResource.prototype.embeds = function(rel) { | ||
return this._embedded[rel] || []; | ||
}; | ||
ParsedResource.prototype.embed = function(rel) { | ||
var embeds = this.embeds(rel); | ||
return embeds[0]; | ||
}; | ||
ParsedResource.prototype.embed = function(rel) { | ||
var embeds = this.embeds(rel); | ||
return embeds[0]; | ||
}; | ||
ParsedResource.prototype.embedByURI = function(rel, uri) { | ||
var embeds = this.embeds(rel); | ||
for (var i in embeds) { | ||
var embed = embeds[i]; | ||
var self = embed.link("self"); | ||
if (self && (uri == self.href)) { | ||
return embed; | ||
ParsedResource.prototype.embedByURI = function(rel, uri) { | ||
var embeds = this.embeds(rel); | ||
for (var i in embeds) { | ||
var embed = embeds[i]; | ||
var self = embed.link("self"); | ||
if (self && (uri == self.href)) { | ||
return embed; | ||
} | ||
} | ||
} | ||
}; | ||
}; | ||
function parse(data) { | ||
if (typeof data == "string") { | ||
data = JSON.parse(data); | ||
} | ||
function parse(data) { | ||
if (typeof data == "string") { | ||
data = JSON.parse(data); | ||
} | ||
return new ParsedResource(data); | ||
}; | ||
return new ParsedResource(data); | ||
}; | ||
exports.parse = parse; | ||
exports.ParsedResource = ParsedResource; | ||
if (exports) { | ||
exports.parse = parse; | ||
exports.ParsedResource = ParsedResource; | ||
} else if (win) { | ||
win.hal.parse = parse; | ||
win.hal.ParsedResource = ParsedResource; | ||
} | ||
})(typeof(exports) == "undefined" ? null : exports, typeof(window) == "undefined" ? null : window); |
156
resource.js
@@ -0,96 +1,102 @@ | ||
(function(module, win) { | ||
function Resource(data, self) { | ||
this.data = data || {}; | ||
this.links = {}; | ||
this.embedded = {}; | ||
if (self) { | ||
this.link("self", self); | ||
} | ||
return this; | ||
}; | ||
function Resource(data, self) { | ||
this.data = data || {}; | ||
this.links = {}; | ||
this.embedded = {}; | ||
if (self) { | ||
this.link("self", self); | ||
} | ||
return this; | ||
}; | ||
Resource.prototype.className = "HALSONResource"; | ||
Resource.prototype.className = "HALSONResource"; | ||
Resource.prototype._append = function(list, key, value) { | ||
if (key in list) { | ||
list[key].push(value); | ||
} else { | ||
list[key] = [value]; | ||
} | ||
}; | ||
Resource.prototype._append = function(list, key, value) { | ||
if (key in list) { | ||
list[key].push(value); | ||
} else { | ||
list[key] = [value]; | ||
} | ||
}; | ||
Resource.prototype._expand = function(key, stack) { | ||
if (!Object.keys(stack).length) { | ||
return; | ||
} | ||
Resource.prototype._expand = function(key, stack) { | ||
if (!Object.keys(stack).length) { | ||
return; | ||
} | ||
this.data[key] = {}; | ||
this.data[key] = {}; | ||
for (var rel in stack) { | ||
var items = stack[rel]; | ||
if ((items.length == 1) && (rel !== "curies")) { | ||
this.data[key][rel] = items[0]; | ||
} else { | ||
this.data[key][rel] = items; | ||
} | ||
} | ||
}; | ||
for (var rel in stack) { | ||
var items = stack[rel]; | ||
if ((items.length == 1) && (rel !== "curies")) { | ||
this.data[key][rel] = items[0]; | ||
} else { | ||
this.data[key][rel] = items; | ||
Resource.prototype.link = function(rel, link) { | ||
if (typeof link == "string") { | ||
link = {href: link}; | ||
} | ||
} | ||
}; | ||
Resource.prototype.link = function(rel, link) { | ||
if (typeof link == "string") { | ||
link = {href: link}; | ||
} | ||
if (!link.href) { | ||
throw new Error('Link MUST contain a href attribute'); | ||
if (!link.href) { | ||
throw new Error('Link MUST contain a href attribute'); | ||
} | ||
} | ||
if ('curies' == rel) { | ||
if (!link.name) { | ||
throw new Error('Curie must be named'); | ||
} | ||
if ('curies' == rel) { | ||
if (!link.name) { | ||
throw new Error('Curie must be named'); | ||
} | ||
if (!link.templated) { | ||
link.templated = true; | ||
} | ||
if (!link.templated) { | ||
link.templated = true; | ||
if (link.href.indexOf('{rel}') == -1) { | ||
throw new Error('Curie must contain {rel} placeholder'); | ||
} | ||
} | ||
if (link.href.indexOf('{rel}') == -1) { | ||
throw new Error('Curie must contain {rel} placeholder'); | ||
} | ||
} | ||
this._append(this.links, rel, link); | ||
return this; | ||
}; | ||
this._append(this.links, rel, link); | ||
return this; | ||
}; | ||
Resource.prototype.curie = function(name, template) { | ||
this.link("curies", {name: name, href: template, templated: true}); | ||
return this; | ||
}; | ||
Resource.prototype.curie = function(name, template) { | ||
this.link("curies", {name: name, href: template, templated: true}); | ||
return this; | ||
}; | ||
Resource.prototype.embed = function(rel, embed) { | ||
var data = (embed instanceof Resource) ? embed.toObject() : embed; | ||
this._append(this.embedded, rel, data); | ||
return this; | ||
}; | ||
Resource.prototype.embed = function(rel, embed) { | ||
var data = (embed instanceof Resource) ? embed.toObject() : embed; | ||
this._append(this.embedded, rel, data); | ||
return this; | ||
}; | ||
Resource.prototype.set = function(key, value) { | ||
if ((key == '_links') || (key == '_embedded')) { | ||
throw new Error('Reserved attribute ' + key); | ||
} | ||
this.data[key] = value; | ||
return this; | ||
}; | ||
Resource.prototype.set = function(key, value) { | ||
if ((key == '_links') || (key == '_embedded')) { | ||
throw new Error('Reserved attribute ' + key); | ||
} | ||
this.data[key] = value; | ||
return this; | ||
}; | ||
Resource.prototype.toObject = function() { | ||
this._expand("_links", this.links); | ||
this._expand("_embedded", this.embedded); | ||
return this.data; | ||
}; | ||
Resource.prototype.toObject = function() { | ||
this._expand("_links", this.links); | ||
this._expand("_embedded", this.embedded); | ||
return this.data; | ||
}; | ||
Resource.prototype.toJSON = function(spacer) { | ||
return JSON.stringify(this.toObject(), null, spacer); | ||
}; | ||
Resource.prototype.toJSON = function(spacer) { | ||
return JSON.stringify(this.toObject(), null, spacer); | ||
}; | ||
if (module) { | ||
module.exports = Resource; | ||
} else if (win) { | ||
win.hal.Resource = Resource; | ||
} | ||
module.exports = Resource; | ||
})(typeof(module) == "undefined" ? null : module, typeof(window) == "undefined" ? null : window); |
21336
492