Skip to main content
Module

x/rambda/source/ifElse.spec.js

Faster and smaller alternative to Ramda
Latest
File
import { always } from './always.js'import { has } from './has.js'import { identity } from './identity.js'import { ifElse } from './ifElse.js'import { prop } from './prop.js'
const condition = has('foo')const v = function (a){ return typeof a === 'number'}const t = function (a){ return a + 1}const ifFn = x => prop('foo', x).lengthconst elseFn = () => false
test('happy', () => { const fn = ifElse(condition, ifFn)(elseFn)
expect(fn({ foo : 'bar' })).toBe(3) expect(fn({ fo : 'bar' })).toBeFalse()})
test('ramda spec', () => { const ifIsNumber = ifElse(v) expect(ifIsNumber(t, identity)(15)).toBe(16) expect(ifIsNumber(t, identity)('hello')).toBe('hello')})
test('pass all arguments', () => { const identity = function (a){ return a } const v = function (){ return true } const onTrue = function (a, b){ expect(a).toBe(123) expect(b).toBe('abc') } ifElse( v, onTrue, identity )(123, 'abc')})
test('accept constant as condition', () => { const fn = ifElse(true)(always(true))(always(false))
expect(fn()).toBeTrue()})
test('accept constant as condition - case 2', () => { const fn = ifElse( false, always(true), always(false) )
expect(fn()).toBeFalse()})
test('curry 1', () => { const fn = ifElse(condition, ifFn)(elseFn)
expect(fn({ foo : 'bar' })).toBe(3) expect(fn({ fo : 'bar' })).toBeFalse()})
test('curry 2', () => { const fn = ifElse(condition)(ifFn)(elseFn)
expect(fn({ foo : 'bar' })).toBe(3) expect(fn({ fo : 'bar' })).toBeFalse()})
test('simple arity of 1', () => { const condition = x => x > 5 const onTrue = x => x + 1 const onFalse = x => x + 10 const result = ifElse( condition, onTrue, onFalse )(1) expect(result).toBe(11)})
test('simple arity of 2', () => { const condition = (x, y) => x + y > 5 const onTrue = (x, y) => x + y + 1 const onFalse = (x, y) => x + y + 10 const result = ifElse( condition, onTrue, onFalse )(1, 10) expect(result).toBe(12)})