Socket
Socket
Sign inDemoInstall

match-operator

Package Overview
Dependencies
0
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    match-operator

Javascript port of PHP's match() control structure.


Version published
Weekly downloads
19
Maintainers
1
Install size
6.07 kB
Created
Weekly downloads
 

Readme

Source

CI Workflow

Match Operator

PHP has a control structure named match, which has been introduced as of PHP8.0.

It's like a simplification of the switch control structure.

Example:

$food = 'strawberry';
$description = match ($food) {
  'apple' => 'This food is an apple',
  'strawberry', 'raspberry' => 'This food is a red fruit',
}; // This food is a red fruit

$food = 'unknown';
$description = match ($food) {
  'apple' => 'This food is an apple',
  'strawberry', 'raspberry' => 'This food is a red fruit',
}; // UnhandledMatchError

$food = 'unknown';
$description = match ($food) {
  'apple' => 'This food is an apple',
  'strawberry', 'raspberry' => 'This food is a red fruit',
  default => 'This food is unknown',
}; // This food is unknown

Usage

This package is an attempt to port that control structure to 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'],
]) // 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

Alternate syntax

You can use an array of subjects if you find it more readable.

const description = match(food, [
  [['strawberry', 'raspberry'], 'This food is a red fruit'],
]) 

Installation

npm install match-operator --save # If you're using NPM
yarn add match-operator # If you're using Yarn

Tests

npm run test # If you're using NPM
yarn test # If you're using Yarn

Disclaimer

This is my first Typescript package - please be kind!

License

MIT.

Keywords

FAQs

Last updated on 28 Nov 2023

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc