utils/array-utils/src/head.js

  1. /**
  2. * Return the first element of the given array.
  3. * @param {*} array - anything
  4. * @returns {*} first element of the array, or undefined
  5. * @alias module:array-utils.head
  6. * @example
  7. * let element = head([1, 2])
  8. */
  9. const head = (array) => {
  10. if (!Array.isArray(array) || array.length === 0) {
  11. return undefined
  12. }
  13. return array[0]
  14. }
  15. module.exports = head