Match
Simple Matching for Mixed-Up Situations
This module was made with affection for but in no way trying to be a pattern matcher. We've already got that on the way. Instead, it is made to replace ugly ternary. It matches a test value to a simple key value. Simple in that the key must be a valid key in an object, so a primitive.
Motivating Examples
const traficLightDisplay = intersection === 'stop' ? 'red' : intersection === 'yeild' ? 'yellow' : intersection === 'go' ? 'green' : 'yellow flashing';
becomes
const lightOptions = stop: 'red' yeild: 'yellow' go: 'green' _: 'yellow flashing'; const traficLightDisplay = ;
Though that situation could be cleared up with a mapper function. What a mapper couldn't cleanly handle for you would be something like
const ViewType = userdeveloper ? <Debug ... /> : useradmin ? userbeta ? <Admin /> : <Admin /> : userbeta ? <Customer /> : <Customer />
With match
it becomes
const userTypeViewMatching = ; const ViewType = userCase ;
There is an additional option matchAll
that will let you turn
const WeatherCard = sunny cloudy windy rain snow <Card> sunny && <SunIcon /> cloudy && <CloudIcon /> windy && <WindIcon /> rain && <RainIcon /> snow && <SnowIcon /> </Card>;
into
const weatherCardMatcher = ; const WeatherCard = <Card></Card>;
Usage
;// => 1
Will return undefined if no match is found
;// => undefined
Will return a default value when no match is found if a default key/value is in the matchClauses
;// => 'default'
Match can be partially applied with just the matchClauses ahead of time.
const requestStatus = ; const res = await
If the matching value is a function, it will be called with the testExpression
const getVectorLength = ;// => 3.74165
<Fetch ="https://api.github.com/users/easilyBaffled"> // Fetch is a render prop that passes the fetch status (`{loading, data, error}`) to its child </Fetch>
API
match(matchClauses, testExpression, [options={}])
Match a value against a set of keys and produce the associated value.
The value may be a primitive, in which case it is matched directly against the keys of the matchClauses
object.
The value may also be an object in which case, the keys in the object, whose values are not empty are used to match against the matchClauses
.
Arguments
matchClauses
(Object): an object whose keys will be matched against. A default value, whose key is indicated by defaultKey, can be provided in the case that there is no matchtestExpression
(Object|boolean|number|string): a value that will be matched against the keys of matchClauses[options={}]
(Object): allow the user to set the defaultKey and matchAll case;[options.defaultKey=_]
(string): The key used for the default value in the matchClauses.[options.matchAll=false]
(boolean): If truematch
will return an array of all matches otherwise it will return just the first match
Returns
(undefined|*): Returns undefined if there is no match, otherwise returns the matching value(s)