# All files / serialization/utils convert-values.js

0% Statements 0/10
0% Branches 0/10
0% Functions 0/4
0% Lines 0/8

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19

     // Convert object or array values
    export default function convertValues (obj, detect, convert) {
      if (!isObjectOrArray(obj)) return obj

      if (Array.isArray(obj)) {
        return obj.map((value, i) => detect(value) ? convert(value) : convertValues(value, detect, convert))
      }

      return Object.keys(obj).reduce((clone, key) => {
        clone[key] = detect(obj[key]) ? convert(obj[key]) : convertValues(obj[key], detect, convert)
        return clone
      }, {})
    }

    function isObjectOrArray (obj) {
      const type = Object.prototype.toString.call(obj)
      return type === '[object Object]' || type === '[object Array]'
    }