Skip to main content
Module

x/ramda/nth.js

:ram: Practical functional Javascript
Go to Latest
File
import _curry2 from './internal/_curry2';import _isString from './internal/_isString';

/** * Returns the nth element of the given list or string. If n is negative the * element at index length + n is returned. * * @func * @memberOf R * @since v0.1.0 * @category List * @sig Number -> [a] -> a | Undefined * @sig Number -> String -> String * @param {Number} offset * @param {*} list * @return {*} * @example * * const list = ['foo', 'bar', 'baz', 'quux']; * R.nth(1, list); //=> 'bar' * R.nth(-1, list); //=> 'quux' * R.nth(-99, list); //=> undefined * * R.nth(2, 'abc'); //=> 'c' * R.nth(3, 'abc'); //=> '' * @symb R.nth(-1, [a, b, c]) = c * @symb R.nth(0, [a, b, c]) = a * @symb R.nth(1, [a, b, c]) = b */var nth = _curry2(function nth(offset, list) { var idx = offset < 0 ? list.length + offset : offset; return _isString(list) ? list.charAt(idx) : list[idx];});export default nth;