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 / last.ts

import { AnyArrayOrObj, ObjValue, ReverseObjKeyValue } from '../_typings'
import { isIndexable } from '../is'
import lastIndex from './lastIndex'

/**
 * Returns the last element of the given list or string.
 *
 * @func
 * @memberOf fp
 * @since 5.0.0-beta.2
 *
 * @param {*} x list to get last index of
 * @return {*}
 *
 * @tests fp/last
 *
 * @ramda v0.1.4
 * @category List
 * @sig [a] -> a | Undefined
 * @sig String -> String
 *
 * @see R.init, R.head, R.tail
 * @extends deps/fp/lastIndex
 *
 * {@link https://github.com/jashkenas/underscore/blob/master/underscore.js#L507 underscore-last}
 * @see {@link underscore-last}
 *
 * @types fp
 * @tests fp/*
 *
 * @example
 *
 *      last(['fi', 'fo', 'fum']); //=> 'fum'
 *      last([]); //=> undefined
 *
 *      last('abc'); //=> 'c'
 *      last(''); //=> ''
 *
 */
function last<Type>(x: Type): undefined | keyof ReverseObjKeyValue<Type> {
  return isIndexable(x) ? x[lastIndex(x)] : undefined
}

export default last