all files / src/utils/ index.js

73.33% Statements 11/15
60% Branches 6/10
85.71% Functions 6/7
78.57% Lines 11/14
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 52 53 54 55 56 57 58                                                                              13×                
import cloneDeep from 'lodash/cloneDeep';
import defaultsDeep from 'lodash/defaultsDeep';
import set from 'lodash/set';
import isPlainObject from 'lodash/isPlainObject';
 
 
export function flatten(obj, parentPath) {
  return Object.keys(obj || {}).reduce((result, k) => {
    Iif ( ! obj.hasOwnProperty(k)) return result;
    const currentPath = parentPath ? parentPath + '.' + k : k;
    const currentProp = obj[k];
 
    //if (Array.isArray(currentProp)) {
      //const arrayResult = currentProp.map((value, i) => {
        //const arrayPath = `${currentPath}[${i}]`;
        //if (isPlainObject(value)) return flatten(value, arrayPath);
        //return { [arrayPath] : value };
      //});
      //return Object.assign({}, result, ...arrayResult);
    //}
    Iif (isPlainObject(currentProp)) {
      return {
        ...result,
        ...flatten(currentProp, currentPath),
      };
    }
 
    return {
      ...result,
      [currentPath]: currentProp,
    };
  }, {});
}
 
 
export function defaultTo(obj, defaults) {
  return defaultsDeep(cloneDeep(obj), defaults);
}
 
 
export function update(obj, newData) {
  const flattenedData = flatten(newData);
  return Object.keys(flattenedData).reduce((result, k) => {
    return set(result, k, flattenedData[k]);
  }, cloneDeep(obj));
}
 
 
export function arrayFrom(value) {
  return Array.isArray(value) ? value : [value];
}
 
 
export function log(value) {
  console.log(value);
  return value;
}