syntax | Cell 27 | Cell 29 | Search

The code is a Node.js module that exports a function testExpressions, which generates an XPath expression from the findImport function and searches for cells in the cellCache array that match the expression. If the $ variable is defined, the function is called and its result is sent to the $ object using the sendResult method.

Run example

npm run import -- "test expression on notebook code"

test expression on notebook code

var importer = require('../Core');
var {exprToXpath, selectAst} = importer.import("select expression",
"select code tree");
var {cellCache} = importer.import("cell cache")

function matchCell(xpath, cell) {
    try {
        var match = selectAst([`//${xpath}`], cell.code);
        if(match.length > 0) {
            console.log(`match ${cell.id}`)
            return cell;
        }
        return false;
    } catch (e) {
        console.log(e.message);
        return false;
    }
}

function findImport("importer") {
    var importer = require('../Core');
}

function testExpressions() {
    var xpath = exprToXpath(findImport);
    console.log(`matching ${xpath}`);
    var allCellIds = cellCache.map(c => c.id)
    // get only first occurrence
    var allCells = cellCache
        .filter((c, i) => allCellIds.indexOf(c.id) == i)
        .filter(c => c.code.length < 10000
                && c.code.trim().length > 10)
    //    .slice(0, 10)
    return Promise
        .all(allCells.map(cell => new Promise(resolve => {
            return setTimeout(() => resolve(matchCell(xpath, cell)), 100);
        })))
        .then(matches => matches.filter(cell => cell)
              .map(cell => cell.id))
}

module.exports = testExpressions;

if(typeof $ !== 'undefined') {
    testExpressions()
        .then(matches => $.sendResult(matches))
}

What the code could have been:

const { exprToXpath, selectAst, CellCache } = require('../Core');

/**
 * Match a cell based on an XPath expression
 * @param {string} xpath - XPath expression
 * @param {object} cell - Cell object
 * @returns {Promise} - Promised cell object if match found, false otherwise
 */
async function matchCell(xpath, cell) {
    try {
        const match = selectAst([`//${xpath}`], cell.code);
        if (match.length > 0) {
            console.log(`Matched cell with id: ${cell.id}`);
            return cell;
        }
        return false;
    } catch (e) {
        console.error(e.message);
        return false;
    }
}

/**
 * Find the importer function
 * @returns {importer} - The importer function
 */
function findImport() {
    // TODO: Refactor this to avoid circular dependency
    return require('../Core');
}

/**
 * Test XPath expressions on cached cells
 * @returns {Promise} - Promised array of matched cell ids
 */
async function testExpressions() {
    const importer = findImport();
    const xpath = exprToXpath(importer);
    console.log(`Matching against XPath: ${xpath}`);

    const allCellIds = CellCache.map(c => c.id);
    const filteredCells = CellCache
       .filter((c, i) => allCellIds.indexOf(c.id) === i)
       .filter(c => c.code.length < 10000 && c.code.trim().length > 10);

    const matches = await Promise.all(filteredCells.map(async cell => {
        await new Promise(resolve => setTimeout(resolve, 100));
        return matchCell(xpath, cell);
    }));

    const matchedCellIds = matches.filter(cell => cell).map(cell => cell.id);
    return matchedCellIds;
}

module.exports = testExpressions;

if (typeof $!== 'undefined') {
    testExpressions()
       .then(matches => $.sendResult(matches))
       .catch(err => console.error(err));
}

Code Breakdown

Overview

The code is a Node.js module that exports a function testExpressions. It appears to be part of a larger project, likely a browser extension or a web application, that interacts with a Core module.

Modules and Imports

Functions

Export and Usage