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.4.0
Version published
Weekly downloads
381
-27.7%
Maintainers
1
Weekly downloads
 
Created
Source

Pattern matching for JavaScript

npm i matchacho
import { match } from 'matchacho'

Examples

return match(input,
  String, (item: string) => [item],
  Array, input,
  null, [])
for (const directive of directives)
  mask |= match<number>(directive,
    'private', PRIVATE,
    'public', PUBLIC,
    'no-cache', NO_CACHE,
    0)
return match(Class,
  Role, () => new Role(value, this.discovery.roles),
  Rule, () => new Rule(value, this.create.bind(this)),
  Incept, () => new Incept(value, this.discovery),
  () => new Class(value))
throw match(error.code,
  'NOT_ACCEPTABLE', () => new UnsupportedMediaType(),
  'TYPE_MISMATCH', () => new BadRequest(),
  error)

Examples approved by Ed.

Reference

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

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

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

  // regular expression
  /(\d+) \+ (\d+)/, (groups) => groups[0] + groups[1],
  /(?<left>\d+) \+ (?<right>\d+)/, (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
}

See tests.

Keywords

javascript

FAQs

Package last updated on 05 Nov 2023

Related posts