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 / __tests__ / first.ts

import first from '../../fp/first'
import map from '../../loop/map/mapObjOrArray'
import toArguments from '../../cast/toArguments'

// https://github.com/jashkenas/underscore/blob/master/test/arrays.js#L6
const eq = (x, y, msg) => expect(x).toEqual(y)
test('first', () => {
  eq(first([1, 2, 3]), 1, 'can pull out the first element of an array')

  // @TODO `first` in underscore means first X, not first value...
  // eq(first([1, 2, 3], 0), [], 'returns an empty array when n <= 0 (0 case)')
  // eq(first([1, 2, 3], -1), [], 'returns an empty array when n <= 0 (negative case)')
  // eq(first([1, 2, 3], 2), [1, 2], 'can fetch the first n elements')
  // eq(first([1, 2, 3], 5), [1, 2, 3], 'returns the whole array if n > length')

  let result = toArguments(4, 3, 2, 1)
  eq(first(result), 4, 'works on an arguments object')

  result = map([[1, 2, 3], [1, 2, 3]], (value, key) => {
    console.log({ value, key })
    return first(value)
  })

  eq(result, [1, 1], 'works well with map')
  eq(first(null), void 0, 'returns undefined when called on null')

  // Array.prototype[0] = 'boo'
  Array.prototype[-1] = 'boo'
  eq(first([]), void 0, 'return undefined when called on a empty array')
  delete Array.prototype[0]
})