Skip to main content
Module

x/rambda/findLast.spec.js

Faster and smaller alternative to Ramda
Go to Latest
File
import { findLast } from './findLast'
test('happy', () => { const result = findLast(x => x > 1, [ 1, 1, 1, 2, 3, 4, 1 ]) expect(result).toEqual(4)
expect(findLast(x => x === 0, [ 0, 1, 1, 2, 3, 4, 1 ])).toEqual(0)})
test('with curry', () => { expect(findLast(x => x > 1)([ 1, 1, 1, 2, 3, 4, 1 ])).toEqual(4)})
const obj1 = { x : 100 }const obj2 = { x : 200 }const a = [ 11, 10, 9, 'cow', obj1, 8, 7, 100, 200, 300, obj2, 4, 3, 2, 1, 0 ]const even = function (x){ return x % 2 === 0}const gt100 = function (x){ return x > 100}const isStr = function (x){ return typeof x === 'string'}const xGt100 = function (o){ return o && o.x > 100}
test('ramda 1', () => { expect(findLast(even, a)).toEqual(0) expect(findLast(gt100, a)).toEqual(300) expect(findLast(isStr, a)).toEqual('cow') expect(findLast(xGt100, a)).toEqual(obj2)})
test('ramda 2', () => { expect(findLast(even, [ 'zing' ])).toEqual(undefined)})
test('ramda 3', () => { expect(findLast(even, [ 2, 3, 5 ])).toEqual(2)})
test('ramda 4', () => { expect(findLast(even, [])).toEqual(undefined)})