1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- (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;
- }));
|