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

import last from '../../fp/last'
import lastIndex from '../../fp/lastIndex'
import arrayToObj from '../../src/cast/arrayToObj'

// https://github.com/jashkenas/underscore/blob/master/test/arrays.js#L61
const eq = (x, y, msg) => expect(x).toEqual(y)
test('last', () => {
  const obj = arrayToObj([1, 2, 3])

  eq(last(obj), 3)
  eq(last([1, 2, 3]), 3, 'can pull out the last element of an array')

  eq(last(null), undefined, 'returns undefined when called on null')

  const arr = []
  arr[-1] = 'boo'
  eq(last(arr), 'boo', 'return undefined when called on a empty array')
})

test.skip('last with index @TODO', () => {
  // let result = (function() { return _(arguments).last() })(1, 2, 3, 4)
  // eq(result, 4, 'works on an arguments object')
  // result = map([[1, 2, 3], [1, 2, 3]], last)
  // eq(result, [3, 3], 'works well with map')

  eq(last([1, 2, 3], 0), [], 'returns an empty array when n <= 0 (0 case)')
  eq(
    last([1, 2, 3], -1),
    [],
    'returns an empty array when n <= 0 (negative case)'
  )
  eq(last([1, 2, 3], 2), [2, 3], 'can fetch the last n elements')
  eq(last([1, 2, 3], 5), [1, 2, 3], 'returns the whole array if n > length')

  // changed to -1
  eq(last(null), void 0, 'returns undefined when called on null')
  const arr = []
  arr[-1] = 'boo'
  eq(last(arr), void 0, 'return undefined when called on a empty array')
})