All files / src utils.js

88.89% Statements 24/27
75% Branches 12/16
88.89% Functions 8/9
90.48% Lines 19/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57      26x 26x 26x     26x 10x 16x     12x 38x 24x   24x             12x               6x 6x   6x 26x 17x 9x                   9x       24x        
import React from 'react';
import * as R from 'ramda';
 
const spotlightHandler = ({ term, fields }) => item =>
  R.any(field => {
    const itemValue = R.is(Function, field.accessor)
      ? field.accessor(item)
      : R.prop(field.accessor, item);
    if (R.is(Number, itemValue))
      return R.contains(R.toLower(term), R.toLower(R.toString(itemValue)));
    return R.contains(R.toLower(term), R.toLower(itemValue));
  })(fields);
 
export const getNodePath = (node, items, pathLabel = '') => {
  if (R.isNil(node.parentId)) return pathLabel;
  const nextNode = R.prop(node.parentId)(items);
 
  return getNodePath(
    R.prop(node.parentId)(items),
    items,
    R.join(' > ', [nextNode.label, pathLabel]),
  );
};
 
export const spotlightScopeListEngine = ({
  term = '',
  fields,
  items,
  itemsById,
  noPath,
  hasPath,
}) => {
  const isSelected = R.pipe(R.prop('isSelected'), R.equals(true));
  const getFields = R.pipe(R.defaultTo([]), R.filter(isSelected), R.values);
 
  return R.reduce((acc, item) => {
    if (spotlightHandler({ term, fields: getFields(fields) })(item)) {
      if (noPath) return [...acc, item];
      return [
        ...acc,
        {
          ...item,
          nodePath: hasPath
            ? R.join(' > ', R.propOr([], 'path')(item))
            : getNodePath(item, itemsById),
        },
      ];
    }
    return acc;
  }, [])(items);
};
 
export const withBlank = func => Component => props => {
  if (R.pipe(func, R.prop('isBlank'))(props)) return null;
  return <Component {...props} />;
};