all files / src/ util.js

81.08% Statements 30/37
71.43% Branches 20/28
80% Functions 8/10
100% Lines 29/29
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            14×           14× 14× 10×           12×       12× 24×   12×     14×                                        
'use strict';
 
Object.defineProperty(exports, "__esModule", {
  value: true
});
 
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
 
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
 
/**
 * transfer { a: 'A' } to [{value: 'a', text: 'A'}]
 * transfer [{key: 'a', label: 'A'}] to [{value: 'a', text: 'A'}]
 */
var processData = function processData(data) {
  var values = [];
  if ((typeof data === 'undefined' ? 'undefined' : _typeof(data)) === 'object' && !(data instanceof Array)) {
    var keys = Object.keys(data);
    values = keys.map(function (key) {
      return {
        value: key,
        text: data[key]
      };
    });
  } else {
    values = data.map(function (item) {
      var newItem = _extends({}, item, {
        value: item.key || item.value,
        text: item.label || item.text
      });
      ['key', 'label'].forEach(function (key) {
        delete newItem[key];
      });
      return newItem;
    });
  }
  return values;
};
 
/**
 * transfer [{value: 'a', text: 'A'}] to { a: 'A' }
 */
var transferDataToObj = function transferDataToObj(data) {
  var obj = {};
  data.forEach(function (item) {
    var key = item.value === '' ? '__all__' : item.value;
    obj[key] = item.text;
  });
  return obj;
};
 
/**
 * valueProp maybe props.value or key
 */
var getValuePropValue = function getValuePropValue(child) {
  var key = '';
  if ('value' in child.props) {
    key = child.props.value;
  } else {
    key = child.key;
  }
  return key;
};
 
exports['default'] = {
  processData: processData,
  transferDataToObj: transferDataToObj,
  getValuePropValue: getValuePropValue
};
module.exports = exports['default'];