Socket
Socket
Sign inDemoInstall

kung-fu

Package Overview
Dependencies
0
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    kung-fu

Functional Standard Library


Version published
Weekly downloads
938
increased by33.05%
Maintainers
1
Install size
44.3 kB
Created
Weekly downloads
 

Readme

Source

kung-fu

Functional Standard Library with TypeScript support

Kung-fu aims to provide a collection of immutable datastructures and functional algorithms that can be used to create robust JavaScript and TypeScript programs.

It is written in TypeScript to ensure a type-safe and idiomatic implementation.

Installation

Kung-fu is available on npm:

npm install kung-fu

Features

Option<T>

Option can be used to represent an optional value, without having to resort to null and undefined. It can be empty, or contain a value of type T.

function zeroAsDefault(maybeNumber: Option<number>) {
  return maybeNumber.caseOf({
    none: () => 0,
    some: (value) => value
  });
}

zeroAsDefault(Option.some(42)) // == 42
zeroAsDefault(Option.none<number>()) // == 0

Either<L, R>

Either can be used to represent a result with two different outcomes, e.g. error or success.

function doSomethingDangerous(lucky: boolean): Either<Error, String> {
  if (!lucky) {
    return Either.left<Error, String>(new Error('uh oh!'));
  } else {
    return Either.right<Error, String>('wow, it worked. lucky you!');
  }
}

doSomethingDangerous(true) // == Either.right('wow, it worked. lucky you!');
doSomethingDangerous(true).flatMap(msg => doSomethingDangerous(false)) // == Either.left(new Error('uh oh!'))

Pair<A, B>

Pair can be used to represent a heterogenous tuple of two values.

const twoThings = new Pair('one thing', 42);

twoThings.mapFirst(str => str + '!') // == new Pair('one thing!', 42);

PartialFunction<From, To>

A partial function from From to To.

const partial = PartialFunction.asInstanceOf(Number).map(n => n + 1);

partial.call(42).getOr(0);

FAQs

Last updated on 21 Jul 2016

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc