rpc | rpc groups | rpc permissions | Search

This code categorizes code cells into predefined groups based on their language, content, metadata, and other factors, likely for organization or analysis within a code editor.

Run example

npm run import -- "get cell rpc groups"

get cell rpc groups

var importer = require('../Core');
var {
    selectAst,
    getExports
} = importer.import("select code tree",
"get exports")

// TODO: filter RPC functions by fully unit tested or unlinted?
// TODO: filter by local system groups?
// TODO: move these classifications to import notebook?
var {FUNCTION_GROUPS, SELENIUM_GROUPS,
     UNITTEST_GROUPS, DEFAULT_GROUPS, PUBLIC} = importer.import("rpc groups")

function getUnmatched(cell) {
    try {
        return !cell.questions[0]
            || cell.id != importer.interpret(cell.id2).id
            || cell.id != importer.interpret(cell.questions[0]).id
    } catch (e) {
        return false;
    }
}

function filterClassGroups(cell) {
    return (cell.groups || [])
        .filter(g => g !== 'Unmatched' && g !== 'Exact'
           && g !== 'Corrected' && g !== 'Available'
           && g !== 'Error' && g !== 'Uncallable')
}

function getCellGroups(cell) {
    if(cell.language === 'javascript') {
        try {
            cell.ast = selectAst('*', cell.code)
            cell.exports = getExports(cell.code)
        } catch (e) {
            console.log(e)
        }
    }
    return [].concat.apply([], [
        cell.groups,
        cell.ast ? [] : ['Error'],
        cell.ast && cell.exports[0] ? FUNCTION_GROUPS : ['Uncallable'],
        cell.filename.includes('Selenium') ? SELENIUM_GROUPS : [],
        cell.questions[0].includes('test') ? UNITTEST_GROUPS : [],
        // TODO: add AST check for describe function call
        [cell.language],
        // filter RPC functions by module.exports
        cell.exports || [],
        // filter by notebook name
        cell.notebook.replace(/\.ipynb|\s+/ig, '').toLowerCase(),
        getUnmatched(cell) ? ['Unmatched'] : DEFAULT_GROUPS,
        !cell.original || cell.id2 === cell.original ? ['Exact'] : ['Corrected']
    ]).filter((g, i, arr) => arr.indexOf(g) === i)
}

module.exports = getCellGroups;

What the code could have been:

const coreImporter = require('../Core');

const {
  selectAst,
  getExports
} = coreImporter.import(['select code tree', 'get exports']);

const {
  FUNCTION_GROUPS,
  SELENIUM_GROUPS,
  UNITTEST_GROUPS,
  DEFAULT_GROUPS,
  PUBLIC
} = coreImporter.import('rpc groups');

function getUnmatched(cell) {
  try {
    return!cell.questions[0] || (cell.id!== coreImporter.interpret(cell.id2).id) || (cell.id!== coreImporter.interpret(cell.questions[0]).id);
  } catch (error) {
    return false;
  }
}

const getCellGroups = (cell) => {
  const filteredGroups = filterClassGroups(cell);

  if (cell.language === 'javascript') {
    try {
      const { ast, exports } = getASTAndExports(cell);
      cell.ast = ast;
      cell.exports = exports;
    } catch (error) {
      console.log(error);
    }
  }

  const groups = [
    filteredGroups,
    cell.ast? [] : ['Error'],
    cell.ast && cell.exports[0]? FUNCTION_GROUPS : ['Uncallable'],
    cell.filename.includes('Selenium')? SELENIUM_GROUPS : [],
    unitTestGroups(cell),
    cell.language,
    filterByModuleExports(cell),
    filterByNotebookName(cell),
    getUnmatched(cell)? ['Unmatched'] : DEFAULT_GROUPS,
    getExactOrCorrected(cell)
  ];

  return groups.flat().filter((g, i, arr) => arr.indexOf(g) === i);
};

const filterClassGroups = (cell) => {
  return cell.groups
   .filter((g) =>!['Unmatched', 'Exact', 'Corrected', 'Available', 'Error', 'Uncallable'].includes(g));
};

const getASTAndExports = (cell) => {
  try {
    const ast = selectAst('*', cell.code);
    const exports = getExports(cell.code);
    return { ast, exports };
  } catch (error) {
    console.log(error);
    return {};
  }
};

const unitTestGroups = (cell) => {
  return cell.questions[0].includes('test')? UNITTEST_GROUPS : [];
};

const filterByModuleExports = (cell) => {
  return cell.exports || [];
};

const filterByNotebookName = (cell) => {
  return cell.notebook.replace(/\.ipynb|\s+/ig, '').toLowerCase();
};

const getExactOrCorrected = (cell) => {
  return cell.original && cell.id2 === cell.original? ['Exact'] : ['Corrected'];
};

module.exports = getCellGroups;

This code defines a function getCellGroups that categorizes code cells based on various criteria.

Here's a breakdown:

  1. Imports:

  2. getUnmatched, filterClassGroups Functions:

  3. getCellGroups Function:

In essence, this code categorizes code cells into predefined groups based on their content, structure, and metadata, likely for organization, analysis, or filtering purposes within a code editor or development environment.