@kyleshevlin/kase
A simple, type-safe pattern-ish matching function.
const result = kase(person.age)
.when(1, () => "You're just a baby!")
.when(
x => x < 18,
() => "You've gotten so big!"
)
.when(
x => x < 65,
() => "So how's work?"
)
.when(
x => x < 90,
age => `Holy smokes, You don't look a day over ${age - 10}!`
)
.otherwise(() => 'Uhh, do you even have a pulse?')
API
Note: It might be to your benefit to take a look at the types in kase.ts
.
kase
function kase(input) {}
kase
takes an input
which is any value, and stores it to be compared to the pattern
s passed to the when
and otherwise
methods.
when
function when(pattern, callback) {}
when
receives a pattern
to compare to the input
. A pattern
can either be a value of the same type as input
, or a predicate function that receives the input
as an argument. If the pattern
argument is a value, it is matched strictly against the input
with ===
.
when
also receives a callback
function which should return the desired result should a match be found. The first when
to match is the result of the kase
chain.
otherwise
function otherwise(callback) {}
otherwise
is how we provide a default result. It receives a callback
exactly like when
. otherwise
will return the result of the callback
as the result of the entire chain.
end
function end() {}
If no otherwise
is provided, end
is necessary to complete the chain and return the result. Like so:
const result = kase(Math.random())
.when(
x => x <= 0.33,
() => 'low'
)
.when(
x => x <= 0.67,
() => 'mid'
)
.when(
x => x <= 0.99,
() => 'high'
)
.end()