@stencila/jesta
Version:
Stencila plugin for executable documents using JavaScript
40 lines (39 loc) • 1.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.select = exports.schema = void 0;
exports.schema = {
title: 'select',
description: 'Select child nodes from a node.',
required: ['node', 'query'],
properties: {
node: {
description: 'The node to select from.',
},
query: {
description: 'The query to run against the node.',
type: 'string',
},
lang: {
description: 'The language that the query is written in.',
enum: ['simplepath'],
},
},
};
/* eslint-disable @typescript-eslint/require-await */
async function select(node, query, lang = 'simplepath') {
if (lang === 'simplepath') {
const items = query.replace(/\[(\d+)\]/, '.$1').split('.');
let value = node;
for (const property of items) {
// eslint-disable-next-line
// @ts-ignore
// eslint-disable-next-line
if (value !== undefined)
value = value[property];
}
return value;
}
throw Error(`Incapable of selecting with language "${lang}"`);
}
exports.select = select;
select.schema = exports.schema;