Skip to content

Commit

Permalink
🐛 Fix ifElseFn function logic
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed May 12, 2021
1 parent 936fbc4 commit 9cc8ee6
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions src/ifElseFn.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Copyright 2021-present the Fonction authors. All rights reserved. MIT license.
import { ifElse } from './ifElse.ts'
import { isFunction } from './isFunction.ts'
import { FalsyLike } from './types/index.ts'
/**
Expand All @@ -22,22 +21,24 @@ import { FalsyLike } from './types/index.ts'
*
* @see Related to {@link ifElse}
*
* @public
* @beta
*/
const ifElseFn = <V, R, T, F>(
condition: (val: V) => R,
onTrue: T | ((val: V) => T),
onFalse: F | ((val: V) => F)
) => (val: V): R extends true ? T : R extends FalsyLike ? F : T | F => {
return ifElse(
condition(val),
isFunction(onTrue)
? (onTrue as (val: V) => T)(val)
: ((onTrue as T) as any),
isFunction(onFalse)
? (onFalse as (val: V) => F)(val)
: ((onFalse as F) as any)
)
}
const ifElseFn =
<V, R, T, F>(
condition: (val: V) => R,
onTrue: T | ((val: V) => T),
onFalse: F | ((val: V) => F)
) =>
(val: V): R extends true ? T : R extends FalsyLike ? F : T | F => {
if (condition(val)) {
return isFunction(onTrue)
? (onTrue as (val: V) => T)(val)
: (onTrue as T as any)
} else {
return isFunction(onFalse)
? (onFalse as (val: V) => F)(val)
: (onFalse as F as any)
}
}

export { ifElseFn }

0 comments on commit 9cc8ee6

Please sign in to comment.