Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@kyleshevlin/kase

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@kyleshevlin/kase

A simple type-safe pattern matcher

  • 0.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

@kyleshevlin/kase

A simple, type-safe pattern-ish matching function.

const result = kase(person.age)
  .when(1, () => "You're just a baby!")
  .when(
    x => x < 18,
    () => "You've gotten so big!"
  )
  .when(
    x => x < 65,
    () => "So how's work?"
  )
  .when(
    x => x < 90,
    age => `Holy smokes, You don't look a day over ${age - 10}!`
  )
  .otherwise(() => 'Uhh, do you even have a pulse?')

API

Note: It might be to your benefit to take a look at the types in kase.ts.

kase

function kase(input) {}

kase takes an input which is any value, and stores it to be compared to the patterns passed to the when and otherwise methods.

when

function when(pattern, callback) {}

when receives a pattern to compare to the input. A pattern can either be a value of the same type as input, or a predicate function that receives the input as an argument. If the pattern argument is a value, it is matched strictly against the input with ===.

when also receives a callback function which should return the desired result should a match be found. The first when to match is the result of the kase chain.

otherwise

function otherwise(callback) {}

otherwise is how we provide a default result. It receives a callback exactly like when. otherwise will return the result of the callback as the result of the entire chain.

end

function end() {}

If no otherwise is provided, end is necessary to complete the chain and return the result. Like so:

const result = kase(Math.random())
  .when(
    x => x <= 0.33,
    () => 'low'
  )
  .when(
    x => x <= 0.67,
    () => 'mid'
  )
  .when(
    x => x <= 0.99,
    () => 'high'
  )
  .end() // would return `undefined` in the cases where x is > .99

FAQs

Package last updated on 12 Sep 2023

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

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc