/
findLast.spec.js
47 lines (39 loc) · 1.06 KB
/
findLast.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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)
})