'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.isJSON = exports.isString = exports.isEmpty = undefined;

var _typeof2 = require('babel-runtime/helpers/typeof');

var _typeof3 = _interopRequireDefault(_typeof2);

exports.debounce = debounce;
exports.transformArrToMap = transformArrToMap;
exports.isFunction = isFunction;
exports.isObject = isObject;
exports.isPromise = isPromise;

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }

var isEmpty = exports.isEmpty = function isEmpty(v) {
  return v === '' || v === void 0 || v === null;
};

function debounce(delay, fn) {
  var movement = null;
  return function () {
    var args = arguments;
    clearTimeout(movement);
    movement = setTimeout(function () {
      fn.apply(this, args);
    }, delay);
  };
}

function transformArrToMap(arr, key) {
  return arr.reduce(function (res, data) {
    if (key) {
      res[data[key]] = true;
    } else {
      res[data] = true;
    }
    return res;
  }, {});
}

function isFunction(target) {
  return Object.prototype.toString.call(target) === '[object Function]';
}

function isObject(target) {
  return Object.prototype.toString.call(target) === '[object Object]';
}

var isString = exports.isString = function isString(target) {
  return Object.prototype.toString.call(target) === '[object String]';
};

var isJSON = exports.isJSON = function isJSON(target) {
  if (isString(target)) {
    try {
      var obj = JSON.parse(target);
      if ((isObject(obj) || Array.isArray(obj)) && obj) {
        return true;
      } else {
        return false;
      }
    } catch (e) {
      return false;
    }
  }
};

function isPromise(obj) {
  return !!obj && ((typeof obj === 'undefined' ? 'undefined' : (0, _typeof3['default'])(obj)) === 'object' || typeof obj === 'function') && typeof obj.then === 'function';
}