|
|
|
|
|
|
|
|
|
(function (root, factory) {
|
|
if (typeof define === 'function' && define.amd) {
|
|
|
|
define([], function () {
|
|
return factory();
|
|
});
|
|
} else if (typeof module === 'object' && module.exports) {
|
|
|
|
|
|
|
|
module.exports = factory();
|
|
} else {
|
|
|
|
root.jsonSchemaLinks = factory();
|
|
}
|
|
}(this, function () {
|
|
var exports = {};
|
|
exports.cacheLinks = true;
|
|
exports.getLink = function(relation, instance, schema){
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var links = schema.__linkTemplates;
|
|
if(!links){
|
|
links = {};
|
|
var schemaLinks = schema.links;
|
|
if(schemaLinks && schemaLinks instanceof Array){
|
|
schemaLinks.forEach(function(link){
|
|
|
|
|
|
|
|
|
|
|
|
|
|
links[link.rel] = link.href;
|
|
});
|
|
}
|
|
if(exports.cacheLinks){
|
|
schema.__linkTemplates = links;
|
|
}
|
|
}
|
|
var linkTemplate = links[relation];
|
|
return linkTemplate && exports.substitute(linkTemplate, instance);
|
|
};
|
|
|
|
exports.substitute = function(linkTemplate, instance){
|
|
return linkTemplate.replace(/\{([^\}]*)\}/g, function(t, property){
|
|
var value = instance[decodeURIComponent(property)];
|
|
if(value instanceof Array){
|
|
|
|
return '(' + value.join(',') + ')';
|
|
}
|
|
return value;
|
|
});
|
|
};
|
|
return exports;
|
|
})); |