All files / module/actions/options saveOptions.js

4.17% Statements 1/24
0% Branches 0/23
0% Functions 0/6
4.17% Lines 1/24

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                                6x                                                                    
import { deepMerge, deref, hasOwn } from '../../../shared/utils'
 
export function saveOptions ({ currentItemState, changedItemState, options }) {
  const returnedItemState = deref(changedItemState)
  if (typeof options !== 'undefined' && hasOwn(returnedItemState, 'attributes')) {
    Object.keys(options).forEach(option => {
      switch (option) {
        case 'attributes':
          returnedItemState.attributes = attributeOptions({ currentItemState, changedAttributes: returnedItemState.attributes, options: options[option] })
      }
    })
  }
 
  return returnedItemState
}
 
const attributeOptions = ({ currentItemState, changedAttributes, options }) => {
  Object.keys(options).forEach(option => {
    switch (option) {
      case 'full':
        if (typeof options.full === 'string') {
          if (hasOwn(changedAttributes, options.full)) {
            changedAttributes[options.full] = deepMerge(currentItemState.attributes[options.full], changedAttributes[options.full])
          }
        } else {
          options.full.forEach(attr => {
            if (hasOwn(changedAttributes, attr)) {
              changedAttributes[options.full] = deepMerge(currentItemState.attributes[attr], changedAttributes[attr])
            }
          })
        }
        break
      case 'unchanged':
        if (typeof options.unchanged === 'string') {
          if (hasOwn(changedAttributes, options.unchanged)) {
            changedAttributes[options.unchanged] = changedAttributes[options.unchanged] ? changedAttributes[options.unchanged] : currentItemState.attributes[options.unchanged]
          }
        } else {
          options.unchanged.forEach(attr => {
            if (hasOwn(changedAttributes, attr)) {
              changedAttributes[attr] = changedAttributes[attr] ? changedAttributes[attr] : currentItemState.attributes[attr]
            }
          })
        }
        break
    }
  })
 
  return changedAttributes
}