All files / src extractSet.js

87.5% Statements 7/8
75% Branches 6/8
100% Functions 2/2
87.5% Lines 7/8
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                          19x       3x 3x   3x     1x     1x                 1x                    
/**
 * @copyright   2017, Miles Johnson
 * @license     https://opensource.org/licenses/MIT
 * @flow
 */
 
import pick from 'lodash/pick';
import pickBy from 'lodash/pickBy';
import { EXPANDED, STANDARD, COMPACT, CUSTOM } from './constants';
 
import type { Format } from './types';
 
function isDefined(value: *): boolean {
  return (value !== null && typeof value !== 'undefined');
}
 
export default function extractSet(data: Object, format: Format, fields: string[] = []): Object {
  const { name, unicode, hexcode, codepoint, shortnames } = data;
  const [shortname] = shortnames;
 
  switch (format) {
    default:
    case EXPANDED:
      return pickBy(data, isDefined);
 
    case STANDARD:
      return pickBy({
        name,
        unicode,
        hexcode,
        codepoint,
        shortname,
      }, isDefined);
 
    case COMPACT:
      return pickBy({
        unicode,
        hexcode,
        shortname,
      }, isDefined);
 
    case CUSTOM:
      return pick(data, fields);
  }
}