@xapp/arachne-utils
Version:
87 lines • 2.87 kB
JavaScript
;
/*! Copyright (c) 2020, XAPP AI */
Object.defineProperty(exports, "__esModule", { value: true });
exports.ArachneURLPattern = void 0;
/**
* Parses PURL into Regex string.
*
* From apify-js SDK
*
* {@link https://github.com/apify/apify-js/blob/64a8c1036adc26d7b901187c9b24667e333096d1/src/pseudo_url.js#L10}
* @ignore
*/
function parsePurl(purl) {
const trimmedPurl = purl.trim();
if (trimmedPurl.length === 0)
throw new Error(`Cannot parse PURL '${trimmedPurl}': it must be a non-empty string`);
let regex = "^";
let openBrackets = 0;
let insideNegation = false;
let negationPattern = "";
try {
for (let i = 0; i < trimmedPurl.length; i++) {
const ch = trimmedPurl.charAt(i);
// console.log(`Inside loop char ${ch} insideNegation?${insideNegation}`);
if (ch === "[" && ++openBrackets === 1) {
regex += "("; // Start regex capture group
}
else if (ch === "]" && openBrackets > 0 && --openBrackets === 0) {
if (insideNegation) {
regex += `(?!${negationPattern})`; // Correct placement of negative lookahead
insideNegation = false;
negationPattern = "";
}
regex += ")";
}
else if (openBrackets > 0) {
if (ch === "!" && !insideNegation) {
insideNegation = true;
}
else if (insideNegation) {
negationPattern += ch;
}
else {
regex += ch;
}
}
else {
const code = ch.charCodeAt(0);
if ((code >= 48 && code <= 57) || (code >= 65 && code <= 90) || (code >= 97 && code <= 122)) {
regex += ch;
}
else {
regex += `\\x${code.toString(16).padStart(2, "0")}`;
}
}
}
regex += "$";
}
catch (err) {
throw new Error(`Cannot parse PURL '${purl}': ${err}`);
}
return regex;
}
/**
* A URL pattern leveraging RegEx.
*
* {@link https://sdk.apify.com/docs/api/pseudo-url}
*/
class ArachneURLPattern {
constructor(url) {
if (url instanceof RegExp) {
this.regExp = url;
}
else if (typeof url === "string") {
this.regExp = new RegExp(parsePurl(url), "i");
// console.log(this.regExp);
}
else {
throw new Error("A URL is required to instantiate the URL pattern.");
}
}
matches(url) {
return typeof url === "string" && url.match(this.regExp) !== null;
}
}
exports.ArachneURLPattern = ArachneURLPattern;
//# sourceMappingURL=ArachneURLPattern.js.map