@csvw-rdf-convertor/core
Version:
This library was generated with [Nx](https://nx.dev).
30 lines (29 loc) • 1.46 kB
JavaScript
export function trimUrl(value, templateUrl, columnName, issueTracker) {
var _match_groups;
const matches = [
...templateUrl.matchAll(/{(.*?)}/g)
].map((match)=>match[1]);
const foundIndex = matches.indexOf(columnName);
if (foundIndex == -1) {
issueTracker.addWarning(`The column "${columnName}" is not found in the template URL "${templateUrl}".`);
return value;
}
const foundTemplateValue = '{' + matches[foundIndex] + '}';
value = value.split('<').join('');
value = value.split('>').join('');
const templateName = foundTemplateValue.slice(1, -1);
// Convert template into a regex with capture groups
// Replace {param} with (.+?), but mark the one we want with a named capture
const regexPattern = templateUrl.replace(/\{([^}]+)\}/g, (_, name)=>name === templateName ? `(?<${name}>[^/]+)` : `[^/]+`);
const regex = new RegExp(`^${regexPattern}$`);
const match = value.match(regex);
var _match_groups_templateName;
const result = (_match_groups_templateName = match == null ? void 0 : (_match_groups = match.groups) == null ? void 0 : _match_groups[templateName]) != null ? _match_groups_templateName : null;
if (result) {
return result;
} else {
issueTracker.addWarning(`The value "${value}" does not match the template URL "${templateUrl}" for column "${columnName}".`);
return value;
}
}
//# sourceMappingURL=url-trimming.js.map