@xcrap/factory
Version:
Xcrap Factory is a set of utilities for dynamically creating instances of clients, extractors, and parsing models, making it easier to configure and extend scraping and parsing pipelines.
33 lines (32 loc) • 1.33 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.extractorArgumentSeparator = void 0;
exports.getExtractor = getExtractor;
exports.extractorArgumentSeparator = ":";
function getExtractor({ allowedExtractors, extractorArguments, extractorKey }) {
if (!(extractorKey in allowedExtractors)) {
throw new Error(`'${extractorKey}' is not a allowed extractor!`);
}
if (extractorArguments && extractorArguments.length > 0) {
const extractorGenerator = allowedExtractors[extractorKey];
const extractor = extractorGenerator(...extractorArguments);
return extractor;
}
const extractor = allowedExtractors[extractorKey];
return extractor;
}
function createExtractor({ extractorText, config: { allowedExtractors, argumentSeparator } }) {
if (argumentSeparator && extractorText.includes(argumentSeparator)) {
const [extractorKey, ...extractorArguments] = extractorText.split(argumentSeparator);
return getExtractor({
extractorKey: extractorKey,
extractorArguments: extractorArguments,
allowedExtractors: allowedExtractors
});
}
return getExtractor({
extractorKey: extractorText,
allowedExtractors: allowedExtractors
});
}
exports.default = createExtractor;