New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

ts-matching

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ts-matching

A pattern matching library for typescript with smart type inference.

latest
npmnpm
Version
0.1.2
Version published
Maintainers
1
Created
Source

ts-matching

A pattern matching library for typescript with smart type inference.

Install

yarn

yarn add ts-matching

npm

npm install --save ts-matching

Usage

matchFor

import { matchFor } from 'ts-matching'
import { pipe } from 'fp-ts/function'

type Option<T> = { _tag: 'None' } | { _tag: 'Some', value: T }
const none: Option<never> = { _tag: 'None' }
const some = <T>(value: T): Option<T> => ({ _tag: 'Some', value })

type RemoteData<E, T> = { _tag: 'Loading' } | { _tag: 'Success', value: T } | { _tag: 'Failure', error: E }

const match = matchFor('_tag')

const toMsg = (data: RemoteData<Error, Option<string>>) =>
  pipe(
    data,
    match({
      Loading: () => 'loading',
      Failure_error: (e) => `error: ${e.message}`,
      Success_value_Some_value: (value) => `found ${value}`,
      Success_value_None: () => 'not found',
    }),
  )

assert.strictEqual(toMsg({ _tag: 'Loading' }), 'loading')
assert.strictEqual(toMsg({ _tag: 'Success', value: some('a') }), 'found a')
assert.strictEqual(toMsg({ _tag: 'Success', value: none }), 'not found')

matchWithFor

import { matchWithFor } from 'ts-matching'

type Option<T> = { _tag: 'None' } | { _tag: 'Some', value: T }
const none: Option<never> = { _tag: 'None' }
const some = <T>(value: T): Option<T> => ({ _tag: 'Some', value })

type RemoteData<E, T> = { _tag: 'Loading' } | { _tag: 'Success', value: T } | { _tag: 'Failure', error: E }

const match = matchWithFor('_tag')

const toMsg = (data: RemoteData<Error, Option<string>>) => match(data).with({
  Loading: () => 'loading',
  Failure_error: (e) => `error: ${e.message}`,
  Success_value_Some_value: (value) => `found ${value}`,
  Success_value_None: () => 'not found'
})

assert.strictEqual(toMsg({ _tag: 'Loading' }), 'loading')
assert.strictEqual(toMsg({ _tag: 'Success', value: some('a') }), 'found a')
assert.strictEqual(toMsg({ _tag: 'Success', value: none }), 'not found')

License

MIT

Keywords

typescript

FAQs

Package last updated on 17 Jun 2021

Did you know?

Socket

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