nth.js 968 B

12345678910111213141516171819202122232425262728293031323334353637
  1. import _curry2 from "./internal/_curry2.js";
  2. import _isString from "./internal/_isString.js";
  3. /**
  4. * Returns the nth element of the given list or string. If n is negative the
  5. * element at index length + n is returned.
  6. *
  7. * @func
  8. * @memberOf R
  9. * @since v0.1.0
  10. * @category List
  11. * @sig Number -> [a] -> a | Undefined
  12. * @sig Number -> String -> String
  13. * @param {Number} offset
  14. * @param {*} list
  15. * @return {*}
  16. * @example
  17. *
  18. * const list = ['foo', 'bar', 'baz', 'quux'];
  19. * R.nth(1, list); //=> 'bar'
  20. * R.nth(-1, list); //=> 'quux'
  21. * R.nth(-99, list); //=> undefined
  22. *
  23. * R.nth(2, 'abc'); //=> 'c'
  24. * R.nth(3, 'abc'); //=> ''
  25. * @symb R.nth(-1, [a, b, c]) = c
  26. * @symb R.nth(0, [a, b, c]) = a
  27. * @symb R.nth(1, [a, b, c]) = b
  28. */
  29. var nth =
  30. /*#__PURE__*/
  31. _curry2(function nth(offset, list) {
  32. var idx = offset < 0 ? list.length + offset : offset;
  33. return _isString(list) ? list.charAt(idx) : list[idx];
  34. });
  35. export default nth;