semantic-network
Version:
A utility library for manipulating a list of links that form a semantic interface to a network of resources.
66 lines • 2.57 kB
JavaScript
;
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.UriFieldResolver = void 0;
var anylogger_1 = __importDefault(require("anylogger"));
var log = (0, anylogger_1.default)('UriFieldResolver');
var UriFieldResolver = exports.UriFieldResolver = /** @class */ (function () {
function 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
*/
UriFieldResolver.resolve = function (value, resolver) {
var e_1, _a;
if (typeof value === 'string') {
var str = value;
var found = str.match(UriFieldResolver.regExp);
if (found) {
try {
for (var found_1 = __values(found), found_1_1 = found_1.next(); !found_1_1.done; found_1_1 = found_1.next()) {
var uri = found_1_1.value;
str = str.replace(new RegExp(uri, 'g'), resolver.resolve(uri));
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (found_1_1 && !found_1_1.done && (_a = found_1.return)) _a.call(found_1);
}
finally { if (e_1) throw e_1.error; }
}
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;
return UriFieldResolver;
}());
//# sourceMappingURL=uriFieldResolver.js.map