All files / modules fields.js

100% Statements 12/12
90% Branches 9/10
100% Functions 4/4
100% Lines 10/10

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      1x         1x                 3x 4x   4x                                 66x 66x 2x     64x                   3x        
/**
 * Fields changed action.
 */
export const FIELDS_CHANGED = 'validation/fields/FIELDS_CHANGED';
 
/**
 * The initial state.
 */
export const INITIAL_STATE = [];
 
/**
 * Process the fields into an autocomplete friendly format.
 *
 * @param {Object} fields - The fields.
 *
 * @returns {Array} The processed fields.
 */
const process = (fields) => Object.keys(fields).map((key) => {
  const field = (key.indexOf('.') > -1 || key.indexOf(' ') > -1) ? `"${key}"` : key;
 
  return {
    name: key,
    value: field,
    score: 1,
    meta: 'field',
    version: '0.0.0'
  };
});
 
/**
 * Reducer function for handle state changes to fields.
 *
 * @param {Array} state - The fields state.
 * @param {Object} action - The action.
 *
 * @returns {Array} The new state.
 */
export default function reducer(state = INITIAL_STATE, action) {
  if (action.type === FIELDS_CHANGED) {
    return process(action.fields);
  }
 
  return state;
}
 
/**
 * Action creator for fields changed events.
 *
 * @param {Object} fields - The fields value.
 *
 * @returns {Object} The fields changed action.
 */
export const fieldsChanged = (fields) => ({
  type: FIELDS_CHANGED,
  fields
});