syphonx-core
Version:
SyphonX is a template-driven solution for extracting data from HTML in a highly efficient way. It combines the power of jQuery, Regular Expressions, and Javascript into a declarative template-driven format that extracts and reshapes HTML data into JSON.
63 lines • 2.53 kB
JavaScript
export function findAction(actions, action_type) {
return flattenTemplateActions(actions)
.map(obj => obj.action)
.filter(action => action.hasOwnProperty(action_type));
}
export function findLastSelectGroup(actions) {
const selectActions = findAction(actions, "select").map(action => action.select);
return selectActions.at(-1);
}
export function findSelect(actions, name) {
const group = findAction(actions, "select").map(action => action.select);
const result = [];
for (const action of group)
for (const select of action)
if (select.name === name)
result.push(select);
return result;
}
export function flattenTemplateActions(actions, result = [], level = 0, n) {
for (const action of actions) {
result.push(!n ? { action, level } : { action, level, case: n });
if (action.hasOwnProperty("each"))
flattenTemplateActions(action.each.actions, result, level + 1);
else if (action.hasOwnProperty("repeat"))
flattenTemplateActions(action.repeat.actions, result, level + 1);
else if (action.hasOwnProperty("switch"))
for (const obj of action.switch)
flattenTemplateActions(obj.actions, result, level + 1, action.switch.indexOf(obj) + 1);
}
return result;
}
export function flattenTemplateSelect(actions, names) {
const group = findAction(actions, "select").map(action => action.select);
const result = [];
for (const action of group)
for (const select of action)
if (!names || (select.name && names.includes(select.name))) {
const existing_select = result.find(obj => obj.name === select.name);
if (!existing_select)
result.push(select);
else if (existing_select.query || select.query)
existing_select.query = mergeQueries(existing_select.query, select.query);
}
return result;
}
export function flattenTemplateTransforms(actions) {
return findAction(actions, "transform").map(action => action.transform).flat();
}
function mergeQueries(q1, q2) {
if (q1 && q2) {
const jsons = q1.map(obj => JSON.stringify(obj));
const a = q2.filter(obj => jsons.find(json => json === JSON.stringify(obj)));
q1.push(...a);
return q1;
}
else if (q1 && !q2)
return q1;
else if (!q1 && q2)
return q2;
else
return undefined;
}
//# sourceMappingURL=utilities.js.map