All Projects → kube → when-switch

kube / when-switch

Licence: MIT license
JavaScript functional implementation of switch/case

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to when-switch

Pampy.js
Pampy.js: Pattern Matching for JavaScript
Stars: ✭ 544 (+2620%)
Mutual labels:  functional, pattern-matching
Pampy
Pampy: The Pattern Matching for Python you always dreamed of.
Stars: ✭ 3,419 (+16995%)
Mutual labels:  functional, pattern-matching
pipe
Functional Pipeline in Go
Stars: ✭ 30 (+50%)
Mutual labels:  functional
fast-cartesian
Fast cartesian product
Stars: ✭ 51 (+155%)
Mutual labels:  functional
Amazon-Price-Alert
Price tracker of Amazon
Stars: ✭ 83 (+315%)
Mutual labels:  switch
ComicNX
NSFW comic browser for the Nintendo Switch
Stars: ✭ 15 (-25%)
Mutual labels:  switch
lambda-zero
A minimalist pure lazy functional programming language
Stars: ✭ 65 (+225%)
Mutual labels:  functional
transmute
kind of like lodash but works with Immutable
Stars: ✭ 35 (+75%)
Mutual labels:  functional
extractacy
Spacy pipeline object for extracting values that correspond to a named entity (e.g., birth dates, account numbers, laboratory results)
Stars: ✭ 47 (+135%)
Mutual labels:  pattern-matching
BuBBLE
A DSL/LISP dialect written in Haskell
Stars: ✭ 20 (+0%)
Mutual labels:  functional
match
Pattern-Matching written by Dan Friedman, Erik Hilsdale and Kent Dybvig
Stars: ✭ 20 (+0%)
Mutual labels:  pattern-matching
grand central
State-management and action-dispatching for Ruby apps
Stars: ✭ 20 (+0%)
Mutual labels:  functional
csharp-workshop
NDC London 2019, Workshop: Become a better C# programmer: more Value, more Expressions, no Waiting
Stars: ✭ 21 (+5%)
Mutual labels:  pattern-matching
Fae
A functional module for Deno inspired from Ramda.
Stars: ✭ 44 (+120%)
Mutual labels:  functional
SeLite
Automated database-enabled navigation ✔️ of web applications
Stars: ✭ 34 (+70%)
Mutual labels:  functional
pythonic
Python like utility functions for JavaScript: range, enumerate, zip and items.
Stars: ✭ 28 (+40%)
Mutual labels:  functional
functional-structures-refactoring-kata
Starting code and proposed solution for Functional Structures Refactoring Kata
Stars: ✭ 31 (+55%)
Mutual labels:  functional
ControlBlockService2
This is the driver for the ControlBlock re.v 2.X, a power switch and input/output/gameapd gadget for the Raspberry Pi
Stars: ✭ 18 (-10%)
Mutual labels:  switch
java-modern-tech-practice
😎 Java™ modern tech practice sandbox ⏳
Stars: ✭ 43 (+115%)
Mutual labels:  functional
swift-declarative-configuration
Declarative configuration for your objects
Stars: ✭ 46 (+130%)
Mutual labels:  functional

CircleCI

when-switch

JavaScript functional implementation of switch/case, inspired by Ruby case/when.

Usage

You can convert a switch-case use in a functional way, using a single expression:

Strict Equality

import when from 'when-switch'

const getDrinkPrice = drink =>
  when(drink)
    .is('Coke', 1.5)
    .is('Pepsi', 1.8)
    .else(2.0)

Structural Matching

You can use match method with any object exposing a test method.

Regular Expressions

const getCaseStyle = text =>
  when(text)
    .match(/^([A-Z][a-z]*)+$/, 'UpperCamelCase')
    .match(/^([a-z]+[A-Z][a-z]*)+$/, 'LowerCamelCase')
    .match(/^([a-z]+_[a-z]+)+$/, 'SnakeCase')
    .else('Unknown')

Custom Type Guard Matcher

type SpaceObject = { x: number; y: number; z: number }
type Cube = SpaceObject & { width: number }
type Sphere = SpaceObject & { radius: number }

const SpaceObjectSchema = {
  test: (_: any): _ is SpaceObject =>
    typeof _.x === 'number' &&
    typeof _.y === 'number' &&
    typeof _.z === 'number'
}

const CubeSchema = {
  test: (_: any): _ is Cube =>
    typeof _.width === 'number' && SpaceObjectSchema.test(_)
}

const SphereSchema = {
  test: (_: any): _ is Sphere =>
    typeof _.radius === 'number' && SpaceObjectSchema.test(_)
}

const getObjectVolume = (object: SpaceObject) =>
  // Each match handler will receive correct static type
  when(object)
    .match(CubeSchema, cube => cube.width ** 3)
    .match(SphereSchema, sphere => Math.PI * 3 / 4 * sphere.radius ** 3)
    .else(_ => null)

match and is can both be used in the same when expression.

TypeScript

when is fully compatible with TypeScript, and will check the types you return in each is expression:

const getDrinkPrice = (drink: 'Pepsi' | 'Coke' | 'Orangina'): number =>
  when(drink)
    .is('Coke', 1.5)
    .is('Pepsi', 1.8)
    .else(2.0)

Here the return type of the when expression will be number

Union types

For each is or else expression added to the current when expression, the type is added as an union to the previous type.

const getDrinkPrice = (drink: 'Pepsi' | 'Coke' | 'Orangina') =>
  when(drink)
    .is('Coke', 1.5)
    .is('Pepsi', true)
    .else('Free')

Here the return type of getDrinkPrice expression will be number | string | boolean

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].