@easyscrape/core
Version:
EasyScrape is a NodeJS module designed to be integrated into your web scraping project. With it, you can more easily get information from the web from a JSON object to organized data, as a REST API could give you!
126 lines • 5.38 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const IESQuery_1 = tslib_1.__importDefault(require("../Interfaces/ITypes/IESQuery.type"));
const ESMiddlewareErrorsManager_1 = tslib_1.__importDefault(require("./ESMiddlewareErrorsManager.abstract"));
const IESQueriesManager_1 = tslib_1.__importDefault(require("../Interfaces/IESQueriesManager.interface"));
class AbstractESMiddlewareUtilities extends ESMiddlewareErrorsManager_1.default {
isValidQuery(obj) {
let isQuery = false;
if (typeof obj === 'object') {
const keys = Object.keys(obj);
isQuery = keys.length > 0;
for (let i = 0; i < keys.length; i++) {
if (!this.getHandleKeyFor(keys[i])) {
isQuery = false;
break;
}
}
}
return isQuery;
}
getAftersQueries(query, afterTo) {
let SubQuery;
const keys = Object.keys(query);
const afterToIndex = keys.indexOf(afterTo);
let key;
if (afterToIndex >= 0) {
SubQuery = {};
for (let i = afterToIndex + 1; i < keys.length; i++) {
key = keys[i];
SubQuery[key] = query[key];
}
}
return SubQuery;
}
getSupportedQueryKeys(usePrefix = true) {
if (!this.SupportedQueryKeys) {
let QueriesManagerPrototype = Object.getPrototypeOf(this.QueriesManager);
this.SupportedQueryKeys = [];
do {
this.SupportedQueryKeys = Object.getOwnPropertyNames(QueriesManagerPrototype)
.filter(key => key !== 'constructor' && !this.SupportedQueryKeys.includes(key))
.concat(this.SupportedQueryKeys);
QueriesManagerPrototype = Object.getPrototypeOf(QueriesManagerPrototype);
} while (QueriesManagerPrototype !== Object.getPrototypeOf({}));
}
return usePrefix ? this.SupportedQueryKeys : this.SupportedQueryKeys.map(key => key.replace(/^_/, ''));
}
getSupportedSpecialQueryKeys(usePrefix = true) {
if (!this.SpecialSupportedQueryKeys) {
const SupportedQueryKeys = this.getSupportedQueryKeys(true);
this.SpecialSupportedQueryKeys = SupportedQueryKeys.filter(key => /\$/.test(key));
this.SpecialSupportedQueryKeysUnprefixed = this.SpecialSupportedQueryKeys.map((key) => key.replace(/^_/, ''));
}
return (usePrefix ? this.SpecialSupportedQueryKeys : this.SpecialSupportedQueryKeysUnprefixed);
}
getHandleKeyFor(queryKey) {
const isSupportedByQueriesManager = this.getSupportedQueryKeys().some(keyName => keyName === queryKey);
return isSupportedByQueriesManager ? queryKey : this.isSpecialQuery(queryKey);
}
getHandleFor(key) {
const QueryKey = this.getHandleKeyFor(key);
const handle = this.QueriesManager[QueryKey];
return handle ? handle.bind(this.QueriesManager) : handle;
}
exec(key, ...args) {
let response;
const QueryKey = this.getHandleKeyFor(key);
const handle = QueryKey ? this.getHandleFor(QueryKey) : undefined;
if (handle) {
try {
response = handle(...args);
}
catch (e) {
if (args.length > 0) {
const newArgs = [...args];
newArgs[0] = this.QueriesManager._select$(newArgs[0]);
response = handle(...newArgs);
}
}
}
return response;
}
haveSupportFor(queryKey) {
return this.getSupportedQueryKeys().indexOf(queryKey) >= 0;
}
isSpecialQuery(queryKey) {
if (this.isSpecialQueryRegExp !== undefined) {
if (this.getSupportedSpecialQueryKeys().includes(queryKey)) {
return queryKey;
}
else {
const match = queryKey.match(this.isSpecialQueryRegExp);
let SpecialQuery = '_';
if (match !== null) {
SpecialQuery += match
.filter((val, i) => val !== undefined && i > 1)
.map((part, i) => (i + 1) % 2 === 0 ? '$' : part)
.join('');
return SpecialQuery;
}
else {
return false;
}
}
}
else {
const SpecialQueries = this.getSupportedSpecialQueryKeys(false);
const regex = `^_(${SpecialQueries.join('|')
.replace(/\$+/g, '$')
.replace(/\|\$/g, '|()$')
.replace(/([^$|()]+|\$)/g, '($1)')
.replace(/\$/g, '[A-Za-z0-9_]*')})$`;
this.isSpecialQueryRegExp = new RegExp(regex);
return this.isSpecialQuery(queryKey);
}
}
isIterable(obj) {
if (obj == null || typeof obj !== 'object') {
return false;
}
return typeof obj[Symbol.iterator] === 'function';
}
}
exports.default = AbstractESMiddlewareUtilities;
//# sourceMappingURL=ESMiddlewareUtilities.abstract.js.map