
Product
Introducing Webhook Events for Alert Changes
Add real-time Socket webhook events to your workflows to automatically receive software supply chain alert changes in real time.
declarative-pattern
Advanced tools
Pattern matching is a declarative way to check multiple conditionals. It's similar to a switch-statement but is much more powerful and at the same time simpler as it does not support fall-though which is a common source of error.
import {match, pattern, range} from 'declarative-pattern'
// Either build a pattern that can be stored and executed at a later time
const fib = pattern()
.when(0, 0)
.when(1, 1)
.default(n => fib(n-1) + fib(n-2))
// Or match a value directly
const isWeekend = match(new Date().getDay(), p => p
.when(0, false)
.when(range(1, 5), true)
.when(6, false))
Supported pattern types:
A default case can be added which will match otherwise unmatched values, if no default case exist and no pattern matches an Error will be thrown.
_ will match any value
import {pattern, _} from 'declarative-pattern'
pattern()
.when([_, 2], 'The array have a length of two or more and the second element is 2')
some will match everything but null and undefined
import {pattern, some} from 'declarative-pattern'
pattern()
.when(some, 'There is some value')
none will match null and undefined
import {pattern, none} from 'declarative-pattern'
pattern()
.when(none, 'There is no value')
either(...patterns) lets you match on two or more patterns
import {pattern, either} from 'declarative-pattern'
pattern()
.when(either(5, 10), 'The number is either a five or a six')
.when(either(Boolean, Number), 'The value is either a boolean or a number')
range(start, end) matches numbers within a range from start to end
import {pattern, range} from 'declarative-pattern'
pattern()
.when(range(1, 10), 'The number is >= 1 and <= 10')
lt(number) matches number less than number
import {pattern, lt} from 'declarative-pattern'
pattern()
.when(lt(10), 'The number < 10')
lt(number) matches number less than or equal to number
import {pattern, lte} from 'declarative-pattern'
pattern()
.when(lte(10), 'The number <= 10')
gt(number) matches number greater than number
import {pattern, gt} from 'declarative-pattern'
pattern()
.when(gt(10), 'The number > 10')
gt(number) matches number greater than or equal to number
import {pattern, gte} from 'declarative-pattern'
pattern()
.when(gte(10), 'The number >= 10')
If the value passed to a when or default is a function, it will be executed when that arm matches. The value that was matched will be passed as the only argument to the function and the match will return the return value of the function. If the value passed to a when or default is not a function, the match will return the value directly.
FAQs
Pattern matching
We found that declarative-pattern 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.

Product
Add real-time Socket webhook events to your workflows to automatically receive software supply chain alert changes in real time.

Security News
ENISA has become a CVE Program Root, giving the EU a central authority for coordinating vulnerability reporting, disclosure, and cross-border response.

Product
Socket now scans OpenVSX extensions, giving teams early detection of risky behaviors, hidden capabilities, and supply chain threats in developer tools.