All files / packages/design-system/shared/utils annotations.js

0% Statements 0/25
0% Branches 0/3
0% Functions 0/10
0% Lines 0/22

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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93                                                                                                                                                                                         
import I from 'immutable';
import uiJson from '../../.generated/ui.json';
import { toList } from './tree';
 
const ui = I.fromJS(uiJson);
 
/**
 * Returns alphabetized array of component names
 */
export const getComponents = () =>
  ui
    .get('components')
    .keySeq()
    .sort()
    .toArray();
 
/**
 * Gets the annotations object for a particular component
 * @param {string} name - component name to get the data for
 */
export const getComponent = name => ui.getIn(['components', name]);
 
/**
 * Gets the annotations object for a particular component
 * @param {string} name - component name to get the data for
 */
export const getComponentVariants = name =>
  toList(getComponent(name))
    .filter(item => item.getIn(['annotations', 'variant']))
    .map(item => item.getIn(['annotations', 'name']))
    .toSet() // this removes duplicate entries - [TODO] but why do we have duplicates?
    .toJS();
 
/**
 * Returns alphabetized array of utilities
 */
export const getUtils = () =>
  ui
    .get('utilities')
    .keySeq()
    .sort()
    .toArray();
 
/**
 * Gets the annotations object for a particular utility
 * @param {string} name - utility name to get the data for
 */
export const getUtil = name => ui.getIn(['utilities', name]);
 
/**
 * Returns array of components filtered by the criteria passed in
 * @param {array} criteria - array of criteria objects used to filter component results,
 *    ex. [ { path: ['annotations', 'layout'], type: <'exists','match','contains'>, value: <value> }, {...} ]
 */
export const getComponentsByCriteria = criteria => {
  const components = ui.get('components');
  let componentsFiltered = I.Map();
 
  // iterate over all criteria
  criteria.forEach(criterion => {
    let filterRule;
 
    switch (criterion.type) {
      // exact match
      case 'match':
        filterRule = item => {
          return item.getIn(criterion.path) === criterion.value;
        };
        break;
 
      // contains matching
      case 'contains':
        filterRule = item =>
          item.getIn(criterion.path).includes(criterion.value);
        break;
 
      // exists is default behavior
      default:
        filterRule = item => item.getIn(criterion.path);
    }
 
    // concatenate new results for each criterion
    componentsFiltered = componentsFiltered.mergeDeep(
      components.filter(filterRule)
    );
  });
 
  return componentsFiltered
    .keySeq()
    .sort()
    .toArray();
};