项目作者: higherkinded

项目描述 :
Simple stub factories slightly mimicking undefined from Haskell
高级语言: TypeScript
项目地址: git://github.com/higherkinded/bottom.git
创建时间: 2019-08-20T23:59:54Z
项目社区:https://github.com/higherkinded/bottom

开源协议:BSD 3-Clause "New" or "Revised" License

下载


Bottom

@higherkinded/bottom"">Check out on NPM
@higherkinded/bottom"">Check out on
NPM
License
Typedefs
JS Support

A set of easily pluggable and extremely touchy empty definitions.

Naming

The name is ripped off the Haskell community’s term for undefined. The idea
partially comes from there as well — a polymorphic value that can be put
anyplace to signify unimplemented functionality.

Motivation

I’ve had a lot of situations where I had to implement some large module and had
to write the bottom function myself just to plug something in place of
implementation for some time. What was also quite important to it is to fend off
any accidental uses by causing the bottom to throw exceptions. Being tired of
rewriting this multiple time and, surprisingly, seeing no alternative
implementations (at all), I’ve written this library. It covers use cases of both
TypeScript bottom-related needs and JavaScript bottom-related needs.

Getting a copy

The first couple of badges under the header both lead to the npm, essentially
giving that aspect away. But in case you want to copy-paste, here you go!

NPM:

  1. npm i @higherkinded/bottom --save

Yarn:

  1. yarn add @higherkinded/bottom

The type definitions are bundled with the library, so if you want some for
your TypeScript project, there’s no need to reach for the @types, got you
covered. The whole package (with definition file and comparedly thick source
map) is around 13 Kb, not to mention that the library itself targets development
versions only, so I suppose that there’s no problem in serving the whole bunch
at once.

How do I?

So how to use it? In case of TypeScript functions, it will be…

  1. import { bottom, bottomFn } from 'bottom';
  2. // ...like this:
  3. const product: (...args: number[]) => number = bottom;
  4. // Or like this:
  5. const someComplexProcedure = <A, B, C, R>(a: A, b: B, c: C): R => {
  6. /*
  7. ...
  8. Some partial implementation here
  9. ...
  10. */
  11. return bottom();
  12. };
  13. // Or perhaps you want to carry the name?
  14. const namedFunction: <A, R>(a: A): R = bottomFn('namedFunction');
  15. // Also OK in return statement:
  16. const someNamedProcedure = <A, B, C, R>(a: A, b: B, c: C): R => {
  17. /*
  18. ...
  19. Some partial implementation here
  20. ...
  21. */
  22. return bottomFn('someNamedProcedure')();
  23. };

For classes, you have these options:

  1. import { Bottom, bottomMethod } from 'bottom';
  2. // Declared but unimplemented and uninstantiable class
  3. class Foo extends Bottom {
  4. /* Bottom-ed method of a class, carrying the info about who they are and who
  5. owns them. */
  6. static someMethod = bottomMethod('someMethod', 'Foo');
  7. }