All files / src/hocs utils.js

24.59% Statements 15/61
30.95% Branches 13/42
46.15% Functions 6/13
20.69% Lines 12/58

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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143        8x 11x 11x 2x   9x                   43x 23x 8x   27x 20x 2x   19x                                                                                                                                                                                                                                      
// @flow
import {mapValues, get, isPlainObject, isArray} from 'lodash';
 
export function getValue(value: any, idPathArr: Array<string>) {
  return idPathArr.reduce((result: any, key: string) => {
    Eif (isPlainObject(result)) {
      if ('edges' in result && 'pageInfo' in result) {
        return get(result, ['edges', key, 'node']);
      }
      return get(result, key);
    } else if (isArray(result)) {
      return get(result, key);
    } else {
      return result;
    }
  }, value);
}
 
export function parseConnectionToNormal(value: any) {
  if (isPlainObject(value)) {
    if (value.edges && value.pageInfo) {
      return value.edges.map(edge => parseConnectionToNormal(edge.node));
    }
    return mapValues(value, item => parseConnectionToNormal(item));
  } else if (isArray(value)) {
    return value.map(item => parseConnectionToNormal(item))
  } else {
    return value;
  }
}
 
export function defaultValue(type: string, relation: any, nullable?: boolean) {
  if (nullable) {
    return null;
  }
  switch (type) {
    case 'connection': {
      return {
        edges: [],
        pageInfo: {
          hasNextPage: false,
          hasPreviousPage: false
        }
      }
    }
    case 'array': {
      return [];
    }
    case 'object': {
      return {};
    }
    case 'boolean': {
      return false;
    }
    case 'number': {
      return 0;
    }
    case 'string': {
      return '';
    }
    case 'relation': {
      if (relation.type === 'toMany') {
        return {
          edges: [],
          pageInfo: {
            hasNextPage: false,
            hasPreviousPage: false
          }
        };
      } else {
        return null;
      }
    }
    case 'image':
    case 'file': {
      return {
        url: '',
        contentType: '',
        name: '',
        size: 0,
        __typename: null
      }
    }
    case 'geoPoint': {
      return {
        placeId: '',
        address: '',
        lat: 122,
        lng: 23
      };
    }
    default: {
      return null;
    }
  }
}
 
export function paginate(rootValue: Object, field: string, currentPage: number, pageSize: number = 10) {
  const copyValue = JSON.parse(JSON.stringify(rootValue));
  const index = (currentPage - 1) * pageSize;
  copyValue[field].edges = copyValue[field].edges.slice(index, index + pageSize);
  return copyValue;
}
 
export function filterByWhere(rootValue: Object, field: string, where: Object) {
  const copyValue = JSON.parse(JSON.stringify(rootValue));
  copyValue[field].edges = copyValue[field].edges.filter(edge => {
    return Object.keys(where).reduce((result, key) => {
      const condition = where[key];
      const data = edge.node[key];
      return result && passCondition(data , condition);
    }, true);
  });
  return copyValue;
}
 
export function passCondition(data: any, condition: Object) {
  return Object.keys(condition).reduce((result, conditionKey) => {
    let isPass = true;
    const conditionValue = condition[conditionKey];
    switch(conditionKey) {
      case 'contains':
        isPass = data.indexOf(conditionValue) !== -1;
        break;
      case 'eq':
        isPass = data === conditionValue;
        break;
      case 'gt':
        isPass = data > conditionValue;
        break;
      case 'gte':
        isPass = data >= conditionValue;
        break;
      case 'lt':
        isPass = data < conditionValue;
        break;
      case 'lte':
        isPass = data <= conditionValue;
        break;
    }
    return result && isPass;
  }, true);
}