All files cleanRecord.js

91.3% Statements 21/23
84.21% Branches 16/19
85.71% Functions 6/7
95% Lines 19/20

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              7x   1x 7x 2x 5x     5x 5x 10x   6x 2x 5x 2x   3x     5x                       3x 3x 3x 3x 3x    
import includes from 'lodash/includes'
import isArray from 'lodash/isArray'
import isBoolean from 'lodash/isBoolean'
import isNumber from 'lodash/isNumber'
import isString from 'lodash/isString'
import uniq from 'lodash/uniq'
 
const isPrimitive = (val) => isNumber(val) || isString(val) || isBoolean(val)
 
const cleanRecord = ({store, record = {}, omitKeys = []}) => {
  if (isPrimitive(record)) {
    return record
  } else Iif (isArray(record)) {
    return record.map((item) => cleanRecord({store, record: item, omitKeys}))
  } else {
    let o = {}
    Object.entries(record)
      .filter(([key, value]) => !includes(omitKeys, key) && value)
      .forEach(([key, value]) => {
        if (isArray(value)) {
          o[key] = value.map((item) => cleanRecord({store, record: item, omitKeys}))
        } else if (typeof value === 'object') {
          o[key] = cleanRecord({store, record: value, omitKeys})
        } else {
          o[key] = value
        }
      })
    return o
  }
}
 
/**
 * @param {object} options
 * @param {vdata.Store} options.store
 * @param {object} options.record
 * @param {string[]} options.omitKeys
 * @param {string} options.collectionName
 */
export default (options) => {
  const record = options.record
  const store = options.store
  const omitKeys = uniq([...options.omitKeys || [], '_id'])
  const cleanedRecord = cleanRecord({store, record, omitKeys})
  return store.createRecord(record._collection || options.collectionName, cleanedRecord)
}