Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/sleexyz/callable
- Owner: sleexyz
- Created: 2017-07-09T22:15:57.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-12-01T23:41:29.000Z (almost 7 years ago)
- Last Synced: 2024-11-02T21:03:59.721Z (5 days ago)
- Topics: callable, es6, flow, flowtype, function
- Language: JavaScript
- Homepage:
- Size: 33.2 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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
```