rpc | get cell rpc groups | test get permissions | Search

This code defines a function getPermissions that determines the permissions for code cells based on a search query, combining predefined public permissions with cell-specific classifications.

Run example

npm run import -- "rpc permissions"

rpc permissions

var path = require('path');
var importer = require('../Core');
var getCellGroups = importer.import("get cell rpc groups");
var {PUBLIC} = importer.import("rpc groups")

// find the shortest words from the query to match the same cell
var id2 = (cell) => path.basename(cell.filename) + '[' + cell.questions[0] + ']';

var catchInterpret = search => search.map(s => {
    try {
        return importer.interpret(s);
    } catch (e) {
        if(!e.message.includes('Nothing found')) throw e;
        return [];
    }
});

var mapReduceCells = (search, public) => catchInterpret(search || [])
    .map((results, i) => (typeof results.id !== 'undefined' ? [results] : results)
         .map(cell => Object.assign(cell, {
            id2: id2(cell),
            original: search[i],
            groups: (public === PUBLIC ? [] : ['Search'])
                .concat(public[search[i]] || [])
                .concat(public[id2(cell)] || [])
         })))
    .reduce((arr, i) => typeof i.id !== 'undefined'
            ? arr.concat([i])
            : arr.concat(i), [])
    .reduce((acc, cell) => (acc[cell.id] = acc[cell.id2] = getCellGroups(cell), acc), {})

function getPermissions(search) {
    if(typeof search === 'string' && search) {
        search = [search];
    }
    var public = mapReduceCells(Object.keys(PUBLIC), PUBLIC);
    return search ? mapReduceCells(search || [], public) : public;
}

module.exports = getPermissions;

What the code could have been:

const path = require('path');
const importer = require('../Core');

// Import necessary functions
const { getCellRpcGroups } = importer.import('get cell rpc groups');
const { RPC_GROUPS } = importer.import('rpc groups');

/**
 * Maps a cell to a unique identifier based on its filename and question.
 *
 * @param {Object} cell - Cell object with filename and questions properties
 * @returns {string} Unique identifier for the cell
 */
const getId2 = (cell) => path.basename(cell.filename) + '[' + cell.questions[0] + ']';

/**
 * Attempts to interpret each search term and returns the result.
 *
 * @param {Array<string>} search - Array of search terms to interpret
 * @returns {Array<Object>} Array of interpreted results
 */
const interpretSearch = (search) => search.map(async (s) => {
  try {
    return await importer.interpret(s);
  } catch (e) {
    if (!e.message.includes('Nothing found')) throw e;
    return [];
  }
});

/**
 * Maps and reduces cells from search terms into their corresponding groups.
 *
 * @param {Array<string>} search - Array of search terms
 * @param {Object} public - Public cell groups object
 * @returns {Object} Object with cells grouped by their identifiers and unique identifiers
 */
const mapReduceCells = async (search, public) => {
  const results = await interpretSearch(search || []);
  return results.reduce((acc, result, index) => {
    const cell = typeof result.id!== 'undefined'? result : result[index];
    const id2 = getId2(cell);
    const groups = (public === RPC_GROUPS? [] : ['Search'])
     .concat(public[search[index]] || [])
     .concat(public[id2] || []);

    return acc.concat({
     ...cell,
      id2,
      original: search[index],
      groups,
    });
  }, []);
};

/**
 * Transforms public cell groups into an object with cells grouped by their identifiers and unique identifiers.
 *
 * @param {Array<string>} search - Array of search terms
 * @param {Object} public - Public cell groups object
 * @returns {Object} Object with cells grouped by their identifiers and unique identifiers
 */
const transformPublicCells = (search, public) => {
  const results = mapReduceCells(search, public);
  return results.reduce((acc, cell) => {
    acc[cell.id] = acc[cell.id2] = getCellRpcGroups(cell);
    return acc;
  }, {});
};

/**
 * Returns permissions for the given search terms.
 *
 * @param {Array<string>} search - Array of search terms
 * @returns {Object} Object with cells grouped by their identifiers and unique identifiers
 */
const getPermissions = async (search) => {
  if (typeof search ==='string' && search) search = [search];
  const publicCells = transformPublicCells(Object.keys(RPC_GROUPS), RPC_GROUPS);
  return search? transformPublicCells(search, publicCells) : publicCells;
};

module.exports = getPermissions;

This code defines a function getPermissions that retrieves permissions for code cells based on a given search query.

Here's a breakdown:

  1. Initialization:

  2. Helper Functions:

  3. getPermissions Function:

In essence, this code provides a mechanism to determine the permissions associated with code cells based on a search query, considering both predefined public permissions and cell-specific classifications.