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.
npm run import -- "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))
}
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 {PromiseCode Breakdown
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.
../Core module and imports several functions from it:
exprToXpath and selectAst are imported together with the label select expression and select code tree, respectively.cellCache is imported separately.findImport function is supposed to import the ../Core module, but it ends up re-declaring the importer variable and doing nothing else. This function seems to be unnecessary and can be removed.matchCell(xpath, cell): This function takes an XPath expression and a code cell as input. It uses the selectAst function to search for a match in the code cell and returns the cell if a match is found, or false otherwise.testExpressions(): This is the main exported function. It:
exprToXpath to convert the findImport function into an XPath expression.cellCache array to get only cells with short code (less than 10,000 characters and more than 10 characters).matchCell with the generated XPath expression and the cell.testExpressions function is exported as a module.$ variable is defined, the function is called and its result is sent to the $ object using the sendResult method.