'use strict';

function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }

var isFun = _interopDefault(require('./isFun'));
var isStr = _interopDefault(require('./isStr'));
var isArr = _interopDefault(require('./isArr'));

/**
 * 搜索目标值在数组或字符串中的位置
 * @param {string|Array} collection
 * @param {*} predicate
 * @returns {number}
 *
 * findIndex('abcdefg', 'cd');                   // => 2
 * const users = [{name: 'Tom'}, {name: 'Bob'}];
 * findIndex(users, u => u.name === 'Bob');      // => 1
 * findIndex(users, u => u.name === 'Jerry');    // => -1
 */
function findIndex(collection, predicate) {
  var isPrdFun = isFun(predicate);

  if (isPrdFun && isStr(collection)) {
    collection = collection.split('');
  }

  if (isPrdFun && isArr(Array)) {
    var i = -1;

    while (++i < collection.length) {
      if (predicate(collection[i], i)) return i;
    }

    return -1;
  }

  return (collection || '').indexOf(predicate);
}

module.exports = findIndex;
