semantic-network
Version:
A utility library for manipulating a list of links that form a semantic interface to a network of resources.
35 lines • 1.13 kB
JavaScript
import anylogger from 'anylogger';
const log = anylogger('UriFieldResolver');
export class UriFieldResolver {
/**
* Look through a string and tokenise for all urls and replace using a resolver
* @param value text string containing an urls
* @param resolver has a map of key/values to make the replacements in the string
*/
static resolve(value, resolver) {
if (typeof value === 'string') {
let str = value;
const found = str.match(UriFieldResolver.regExp);
if (found) {
for (const uri of found) {
str = str.replace(new RegExp(uri, 'g'), resolver.resolve(uri));
}
return str;
}
}
else {
log.warn('Uri field resolver cannot process field of type %s', typeof value);
}
return value;
}
}
/**
* Match urls within a text field
*
* @example
* http://example.com/2
* https://example.com/2
* https://example.com/resource/2
*/
UriFieldResolver.regExp = /(https?:\/\/)([^ '"]*)/gi;
//# sourceMappingURL=uriFieldResolver.js.map