![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
match-expression
Advanced tools
A flexible switch/match expression utility for Javascript.
import match from 'match-expression'
const x = match('bar')
.case('foo')
.then(() => 'FOO')
.case('bar')
.case('baz')
.then(() => 'BARBAZ')
.default(() => 'DEFAULT')
// x === 'BARBAZ'
Apply and get regular expression capture group matches in one go:
const inputUrl = "http://eat-frogs.io/dishes/parmigiana-di-rana"
function regexMatch(str, regex) { return regex.exec(str) }
const httpsUrl = match(inputUrl, regexMatch)
.case(/^http:\/\/(.*)/)
.then((url, _, [, noprotocol]) => `https://${noprotocol}`)
.case(/^\//)
.then(url => `https://${DOMAIN}${url}`)
.default(url => url)
// httpsUrl === "https://eat-frogs.io/dishes/parmigiana-di-rana"
Execute the function returned by then
to resolve to a value without having to use a default
clause.
match(person.type)
.case('HUMAN').then(() => greet(person))
.case('NOT_HUMAN').then(() => eat(person))()
match( value [, comparisonFunction] )
value
: any
The value to be matched against the subsequent cases.
comparisonFunction
: (value, caseValue) => any
(optional)
Defaults to strict equality (===
).
It is passed the initial value as first argument and the comparing value as second. If its result is truthy, then the initial value is interpreted as matching the comparing value.
Its result with the matching value is used a the third argument to the matching then
clause.
Once a value has matched, the comparison function is not called anymore.
Returns: { case }
An object with a .case
method.
.case( comparisonValue [, ...] )
Available after match
and case
clauses.
comparisonValue
: any
The value(s) to compare with the initial value provided to match
. The actual matched value will be passed as second argument to then
handler.
Returns: { then, case }
An object with .then
and .case
methods.
.then( callback )
Available after case
clauses.
callback
: (value, matchedValue, comparisonFunctionResult) => any
Executed only if a previous case
clause matched, in which case its return value will be used as the return value of the match expression.
The first argument is the initial value, the second is the matching case value, the third one is the result of the call to the comparison function with the two previous arguments (defaults to true
if no custom comparisonFunction
was provided).
Returns: [Callable: () => result]{ case, default }
A callable object with .case
and .default
methods.
The function can be called to resolve the match directly, without a "default
" clause.
.default( callback )
Available after then
clauses.
callback
: (value) => any
Executed only if no previous case
clause matched, in which case its return value will be used as the return value of the match expression.
The first argument is the initial value.
Returns: any
Returns resolved value from then
clause callback corresponding to the matched case
clause, or from its own callback if no case
matched.
./test.js
for an exhaustive spec.match
:match: (value, [ ...cases, default ])
cases: [ [ ...caseValues, callback ] ]
and default: [ callback ]
.FAQs
Switch/match expression in method chaining style
The npm package match-expression receives a total of 2 weekly downloads. As such, match-expression popularity was classified as not popular.
We found that match-expression demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.