New:Microsoft Teams Notifications Are Now Available in Socket.Learn more
Get Started

matchacho

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

matchacho

Pattern matching for JavaScript

Source
npmnpm
Version
0.3.5
Version published
Weekly downloads
503
29.64%
Maintainers
1
Weekly downloads
 
Created
Source

Pattern matching for JavaScript

TL;DR

npm i matchacho
return match(value,
  // equals
  1, () => 'One!',

  // instanceof
  Readable, (stream) => stream.read(),
  Error, 'oopsie!',

  // test function
  positive, (number) => Math.sqrt(number),

  // regular expression
  /(?<left>\d+) \+ (?<right>\d+)/, (value, groups) => groups.left + groups.right,

  // object matching
  { statusCode: 200 }, (response) => response.data,

  // array matching
  [0, 1], (numbers) => `${numbers[0]} + ${numbers[1]}`,

  // nested test function
  { statusCode: (x) => x >= 200 && x < 300 }, (response) => response.data,

  // default
  (value) => value
)

function positive (value) {
  return value > 0
}

Keywords

javascript

FAQs

Package last updated on 04 Nov 2023

Related posts