import | search notebook questions | convert regexp matches to an array | Search

This code provides functions to filter and search notebook files based on various queries and criteria. Functions support searching local files, filtering filenames, and querying a database or cache for matching notebook files.

Cell 9

var fs = require('fs')
var path = require('path')

// read notebook from files and make a module
function filterLocal(query) {
    var {getCells} = require('../Core')
    var filename = query.replace(/\.ipynb\[[0-9]+\].*/ig, '.ipynb')
    var query = path.basename(query).split(/[\[\]]/ig);
    if(fs.existsSync(filename)) {
        var local = getCells(filename)
        if(query.length < 3) {
            return local
        } else {
            return [local[parseInt(query[1])]]
        }
    } else {
        return []
    }
}

// filter query results by filename
function filterFilename(arr, fname) {
    return arr
        .filter(id => path.basename(id)
                .substr(0, fname.length + 1) === fname + '[');
}

function searchFiles(query, cellCache) {
    return filterFilename(cellCache.map(c => c[1]), path.basename(query))
        .sort((a, b) => 
              parseInt(a.split(/[\[\]]/ig)[1])
              - parseInt(b.split(/[\[\]]/ig)[1]))
}

function searchQueryFiles(query, cellCache) {
    var {queryDatabase} = require('../Core')
    var isNumeric = parseInt(query[1]) + '' === query[1]
    return isNumeric
        ? [(cellCache.filter(cell => path.basename(cell[1]) === `${query[0]}[${query[1]}]`)[0] || [])[1]]
        : filterFilename(queryDatabase(query[1]), query[0])
}

module.exports.filterLocal = filterLocal
module.exports.filterFilename = filterFilename
module.exports.searchFiles = searchFiles
module.exports.searchQueryFiles = searchQueryFiles

What the code could have been:

```javascript
const fs = require('fs');
const path = require('path');

// Import necessary functions from the Core module
const { getCells, queryDatabase } = require('../Core');

/**
 * Filters local notebook cells based on the query parameters.
 * 
 * @param {string} query - The file path of the notebook to filter.
 * @returns {array|object} The filtered cells if the file exists, otherwise an empty array.
 */
function filterLocal(query) {
    const filename = getFilenameFromQuery(query);
    if (fs.existsSync(filename)) {
        const local = getCells(filename);
        if (query.includes('[') && query.includes(']')) {
            return [local[parseInt(getQueryNumberFromFilename(query))]];
        }
        return local;
    }
    return [];
}

/**
 * Extracts the filename from the query string, removing any path or index suffix.
 * 
 * @param {string} query - The query string to extract the filename from.
 * @returns {string} The filename.
 */
function getFilenameFromQuery(query) {
    return query.replace(/\.ipynb\[[0-9]+\].*/ig, '.ipynb');
}

/**
 * Extracts the number from the query string, assuming it's in the format '[number]'.
 * 
 * @param {string} query - The query string to extract the number from.
 * @returns {number|string} The extracted number or an empty string if not found.
 */
function getQueryNumberFromFilename(query) {
    const queryParts = query.split(/[\[\]]/ig);
    return queryParts.length > 1? queryParts[1] : '';
}

/**
 * Filters an array of file names based on the query filename.
 * 
 * @param {array} arr - The array of file names to filter.
 * @param {string} fname - The query filename to filter by.
 * @returns {array} The filtered array of file names.
 */
function filterFilename(arr, fname) {
    return arr.filter(id => path.basename(id).startsWith(`${fname}[`));
}

/**
 * Searches for files in the cell cache based on the query filename.
 * 
 * @param {string} query - The query filename to search for.
 * @param {array} cellCache - The array of cell cache entries.
 * @returns {array} The sorted array of matching files.
 */
function searchFiles(query, cellCache) {
    const fname = path.basename(query);
    return cellCache
       .filter(cell => path.basename(cell[1]).startsWith(`${fname}[`)))
       .sort((a, b) => parseInt(a[1].split(/[\[\]]/ig)[1]) - parseInt(b[1].split(/[\[\]]/ig)[1]));
}

/**
 * Searches for files in the query database based on the query.
 * 
 * @param {string} query - The query to search for.
 * @param {array} cellCache - The array of cell cache entries.
 * @returns {array} The matching files or an empty array if not found.
 */
function searchQueryFiles(query, cellCache) {
    const isNumericQuery = getQueryNumberFromFilename(query) + '' === getQueryNumberFromFilename(query);
    if (isNumericQuery) {
        return cellCache
           .filter(cell => path.basename(cell[1]) === `${query[0]}[${getQueryNumberFromFilename(query)}]`)
           .map(cell => cell[1]);
    }
    return filterFilename(queryDatabase(query[1]), query[0]);
}

module.exports = {
    filterLocal,
    filterFilename,
    searchFiles,
    searchQueryFiles
};
```

Code Breakdown

Required Modules

Functions

filterLocal(query)

filterFilename(arr, fname)

searchFiles(query, cellCache)

searchQueryFiles(query, cellCache)

Exports

Notes