Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/sleexyz/callable

Type-safe extendable function class
https://github.com/sleexyz/callable

callable es6 flow flowtype function

Last synced: 1 day ago
JSON representation

Type-safe extendable function class

Awesome Lists containing this project

README

        

# Callable

[![NPM](https://nodei.co/npm/callable-class.png)](https://npmjs.org/package/callable-class)

[![CircleCI](https://circleci.com/gh/sleexyz/callable.svg?style=svg)](https://circleci.com/gh/sleexyz/callable)

An extendable base class for callable classes, designed to work nicely with Flow.

## Type definition:

```js
// @flow
declare class Callable extends Function {
// See https://github.com/facebook/flow/issues/3084
// (A): B
$call: A => B;
constructor(A => B): this;
}
```

## Examples:

#### Extend `Callable` with custom methods
```js
// @flow
import Callable from "callable-class";

class Composable extends Callable {
andThen(next: B => C): Composable
{
return new Composable(x => next(this(x)));
}
}

const foo = new Composable(x => [...x, 1])
.andThen(x => [...x, 2])
.andThen(x => [...x, 3]);

console.log(foo([])); // [1, 2, 3]
```

#### Extend `Callable` with custom fields and constructor
```js
// @flow
import Callable from "callable-class";

class ActionCreator extends Callable {
type: string;
constructor(type: Type, fn: A => B) {
super(fn);
this.type = type;
}
}

const foo = new ActionCreator("foo", x => x + 1);

console.log(foo.type); // "foo"
console.log(foo(0)); // 1
```