
Research
/Security News
DuckDB npm Account Compromised in Continuing Supply Chain Attack
Ongoing npm supply chain attack spreads to DuckDB: multiple packages compromised with the same wallet-drainer malware.
match-operator
Advanced tools
This package is an attempt to port the match
control structure, which exists in various languages (Rust, Scala, Kotlin, PHP, ...) as a JS function, the closest possible way.
Example:
import match from 'match-operator'
const food = 'strawberry'
const description = match(food, [
['apple', 'This food is an apple'],
['strawberry', 'raspberry', 'This food is a red fruit'], // First array items are subjects, last one is the result
]) // This food is a red fruit
const food = 'unknown'
const description = match(food, [
['apple', 'This food is an apple'],
['strawberry', 'raspberry', 'This food is a red fruit'],
]) // UnhandledMatchError
const food = 'unknown'
const description = match(food, [
['apple', 'This food is an apple'],
['strawberry', 'raspberry', 'This food is a red fruit'],
[match.default, 'This food is unknown']
]) // This food is unknown
You can also use a function to evaluate the result, which will be called only if the subject matches:
const food = 'banana'
const description = match(food, [
['apple', () => 'This food is an apple'], // it won't be evaluated because it's a banana
['strawberry', () => "This function won't be evaluated"], // same here
[match.default, (subject) => `We don't know this food, but it looks like ${subject}`],
])
You can use an array of subjects if you find it more readable.
const description = match(food, [
[['strawberry', 'raspberry', 'cherry'], 'This food is a red fruit'],
[['peach', 'pineapple'], 'This food is a yellow fruit'],
])
You can also use a simple object to define your matching rules, which can be more concise for simple key-value matches:
const fallback = () => 'This food is unknown'
const description = match(food, {
'apple': 'This food is an apple',
'strawberry': 'This food is a red fruit'
}, fallback)
This is equivalent to the array syntax and supports all features including function evaluation:
const description = match(food, {
'apple': (subject) => `This food is an ${subject}`,
'strawberry': 'This food is a red fruit'
})
npm install match-operator --save # If you're using NPM
yarn add match-operator # If you're using Yarn
npm run test # If you're using NPM
yarn test # If you're using Yarn
This is my first Typescript package - please be kind!
MIT.
FAQs
Javascript port of PHP's match() control structure.
We found that match-operator demonstrated a healthy version release cadence and project activity because the last version was released less than 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.
Research
/Security News
Ongoing npm supply chain attack spreads to DuckDB: multiple packages compromised with the same wallet-drainer malware.
Security News
The MCP Steering Committee has launched the official MCP Registry in preview, a central hub for discovering and publishing MCP servers.
Product
Socket’s new Pull Request Stories give security teams clear visibility into dependency risks and outcomes across scanned pull requests.