Learn more  » Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Bower components Debian packages RPM packages NuGet packages

skava / chain-able-deps   js

Repository URL to install this package:

Version: 6.0.4 

/ src / fp / firstIndex.ts

import { AnyArrayOrObj } from '../_typings'
import ObjectKeys from '../util/keys'
import { isArray, isString, isObj } from '../is'

/**
 * @desc get first index in a list
 * @since 5.0.0-beta.2
 * @version 5.0.0-beta.7 <- fixed silly position[0] giving wrong index in arr
 * @memberOf fp
 *
 * @param  x item to find the first index of
 * @return first index, usually number/string
 *
 * @NOTE works for strings too eh
 * @extends deps/util/keysObjOrArray
 * @see deps/fp/first
 *
 * @example
 *
 *   firstIndex([0, 'one']) //=> 0
 *   firstIndex({one: 1, two: 2}) //=> 'one'
 *
 */
function firstIndex(x: string | AnyArrayOrObj) {
  // any string or array starts @ 0
  if (isString(x) || isArray(x)) return 0
  // otherwise, object is good, if no keys, use 0, not sure how best to-do
  // probably better if this always returned a number, firstKey, firstIndex...
  else if (isObj(x)) return ObjectKeys(x)[0] || '0'
  // any other value, 0
  else return 0
}

export default firstIndex