Options
All
  • Public
  • Public/Protected
  • All
Menu

Class PatternMatching<EV, PV, R>

You can use PatternMatching class as a evoluated Pattern Strategy

Compile strategy you need, and execute the adapted startegy matched by executed value.

example
import http from 'http';
import https from 'https';
import http2 from 'http2';

import { PatternMatching } from '@tpoisseau/pattern-matching';
import { throwError } from '@tpoisseau/pattern-matching/evaluators';

const serverStrategies = new PatternMatching()
 .match('http', http)
 .match('https', https)
 .match('http2', http2)
 .default(throwError('Unsupported protocol'))

const createStrategies = new PatternMatching()
 .match(protocol => /^https?$/.test(protocol), 'createServer')
 .match('http2', 'createSecureServer')
 .default(throwError('Unsupported protocol'))

serverStrategies.exec('http')[createStrategies.exec('http')]()
serverStrategies.exec('https')[createStrategies.exec('https')]()
serverStrategies.exec('http2')[createStrategies.exec('http2')]()
see

Predicate

see

Evaluator

Type parameters

  • EV

  • PV

  • R

Hierarchy

Index

Properties

Private _defaultEvaluator

_defaultEvaluator: Evaluator<PV, R>

Private _stack

_stack: Map<Predicate<PV>, Evaluator<PV, R>>

Methods

default

  • default(evaluator?: Evaluator<PV, R> | EV): this

exec

  • exec(value: PV): R
  • Iterate on stack entries If value match predicate, return R value from Evaluator

    Parameters

    • value: PV

    Returns R

match

Generated using TypeDoc