all files / src/ getValuesFromState.js

95.65% Statements 22/23
95% Branches 19/20
100% Functions 3/3
95.24% Lines 20/21
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         105×     105× 105×   104× 286× 286× 164× 90× 80×   74× 84×   41× 38×   38× 30×       286×          
import {isFieldValue} from './fieldValue';
 
/**
 * A different version of getValues() that does not need the fields array
 */
const getValuesFromState = state => {
  Iif (!state) {
    return state;
  }
  const keys = Object.keys(state);
  if (!keys.length) {
    return undefined;
  }
  return keys.reduce((accumulator, key) => {
    const field = state[key];
    if (field) {
      if (isFieldValue(field)) {
        if (field.value !== undefined) {
          accumulator[key] = field.value;
        }
      } else if (Array.isArray(field)) {
        accumulator[key] = field.map(arrayField =>
          isFieldValue(arrayField) ? arrayField.value : getValuesFromState(arrayField));
      } else if (typeof field === 'object') {
        const result = getValuesFromState(field);
 
        if (result && Object.keys(result).length > 0) {
          accumulator[key] = result;
        }
      }
    }
    return accumulator;
  }, {});
};
 
export default getValuesFromState;