functional/README.md

48 lines
1.4 KiB
Markdown
Raw Normal View History

2019-09-16 18:20:42 +00:00
tk-functional
=============
2019-09-22 21:05:33 +00:00
[![pipeline status](https://gitlab.com/thunderk/tk-functional/badges/master/pipeline.svg)](https://gitlab.com/thunderk/tk-functional/commits/master)
[![coverage report](https://gitlab.com/thunderk/tk-functional/badges/master/coverage.svg)](https://gitlab.com/thunderk/tk-functional/commits/master)
[![npm version](https://img.shields.io/npm/v/tk-functional.svg)](https://npmjs.com/tk-functional)
[![npm size](https://img.shields.io/bundlephobia/min/tk-functional.svg)](https://bundlephobia.com/result?p=tk-functional)
2019-09-16 18:20:42 +00:00
About
-----
Provides some common helpers for functional-style programming.
Typescript definitions are included.
2019-09-22 21:05:33 +00:00
Issues can be reported on [GitLab](https://gitlab.com/thunderk/tk-functional/issues).
2019-09-16 18:20:42 +00:00
Functions
---------
**nop** does nothing (useful for some callbacks):
```typescript
new ConstructorWithMandatoryCallback(nop)
```
**identity** returns its argument untouched:
```typescript
a === identity(a) // => true
```
**partial** applies a partial configuration object as first argument of compatible functions:
```typescript
const sum = (args: {a: number, b: number}) => args.a + args.b
const plus1 = partial({a: 1}, sum);
plus1({b: 8}) // => 9
```
**cmp** simplifies the use of array sorting:
```typescript
[8, 3, 5].sort(cmp()) // => [3, 5, 8]
2019-09-22 21:05:33 +00:00
[8, 3, 5].sort(cmp({ reverse: true })) // => [8, 5, 3]
2019-09-16 18:20:42 +00:00
[-2, 8, -7].sort(cmp({ key: Math.abs })) // => [-2, -7, 8]
```