Skip to main content
Module

x/rambda/source/lastIndexOf.spec.js

Faster and smaller alternative to Ramda
Latest
File
import { lastIndexOf as lastIndexOfRamda } from 'ramda'
import { compareCombinations } from './_internals/testUtils.js'import { possibleIterables, possibleTargets } from './indexOf.spec.js'import { lastIndexOf } from './lastIndexOf.js'
test('with NaN', () => { expect(lastIndexOf(NaN, [ NaN ])).toBe(0)})
test('will throw with bad input', () => { expect(lastIndexOfRamda([], true)).toBe(-1) expect(() => indexOf([], true)).toThrowErrorMatchingInlineSnapshot('"indexOf is not defined"')})
test('without list of objects - no R.equals', () => { expect(lastIndexOf(3, [ 1, 2, 3, 4 ])).toBe(2) expect(lastIndexOf(10)([ 1, 2, 3, 4 ])).toBe(-1)})
test('list of objects uses R.equals', () => { const listOfObjects = [ { a : 1 }, { b : 2 }, { c : 3 } ] expect(lastIndexOf({ c : 4 }, listOfObjects)).toBe(-1) expect(lastIndexOf({ c : 3 }, listOfObjects)).toBe(2)})
test('list of arrays uses R.equals', () => { const listOfLists = [ [ 1 ], [ 2, 3 ], [ 2, 3, 4 ], [ 2, 3 ], [ 1 ], [] ] expect(lastIndexOf([], listOfLists)).toBe(5) expect(lastIndexOf([ 1 ], listOfLists)).toBe(4) expect(lastIndexOf([ 2, 3, 4 ], listOfLists)).toBe(2) expect(lastIndexOf([ 2, 3, 5 ], listOfLists)).toBe(-1)})
test('with string as iterable', () => { expect(() => lastIndexOf('a', 'abc')).toThrowErrorMatchingInlineSnapshot('"Cannot read property \'indexOf\' of abc"') expect(lastIndexOfRamda('a', 'abc')).toBe(0)})
describe('brute force', () => { compareCombinations({ fn : lastIndexOf, fnRamda : lastIndexOfRamda, firstInput : possibleTargets, secondInput : possibleIterables, callback : errorsCounters => { expect(errorsCounters).toMatchInlineSnapshot(` { "ERRORS_MESSAGE_MISMATCH": 0, "ERRORS_TYPE_MISMATCH": 34, "RESULTS_MISMATCH": 0, "SHOULD_NOT_THROW": 51, "SHOULD_THROW": 0, "TOTAL_TESTS": 170, } `) }, })})