All files / src/fromArgs index.js

100% Statements 22/22
100% Branches 22/22
100% Functions 4/4
100% Lines 20/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 4499x         5x         99x   99x 171x   171x 233x 106x 127x 97x 30x 28x         99x       59x 85x 31x 54x 46x       59x       115x  
const fromArgs = ({errs = [], args = [{_: []}]} = {errs: [], args: [{_: []}]}) => ({
  errs,
  args: merge(args)
})
 
module.exports = {
  fromArgs
}
 
function merge (args) {
  let args2 = {}
 
  for (let i = 0; i < args.length; i++) {
    const arg = args[i]
 
    for (let [key, value] of Object.entries(arg)) {
      if (key === '_' && Array.isArray(value)) {
        args2[key] = [...(args2[key] || []), ...value]
      } else if (typeof args2[key] === 'undefined') {
        args2[key] = value
      } else if (isObject(value)) {
        args2[key] = mergeDeep(args2[key], value)
      }
    }
  }
 
  return args2
}
 
function mergeDeep (obj1, obj2) {
  for (let [key, value] of Object.entries(obj2)) {
    if (isObject(value)) {
      obj1[key] = mergeDeep(obj1[key] || {}, value)
    } else if (typeof obj1[key] === 'undefined') {
      obj1[key] = value
    }
  }
 
  return obj1
}
 
function isObject (a) {
  return !Array.isArray(a) && a !== null && typeof a === 'object'
}