# manatki

> Function programming in Typescript/Javascript

Latest version **0.2.0** (published 2024-12-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install manatki
pnpm add manatki
yarn add manatki
bun add manatki
```

## Health

**Score 35/100 (D)** — status: maintenance-mode.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2024-12-18 |
| First published | 2018-06-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 31.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | George Carpow |
| Maintainers | eakarpov |
| Keywords | typescript, javascript, fp, monads, functional programming |

## Links

- npm: https://www.npmjs.com/package/manatki
- Repository: https://github.com/eakarpov/manatki
- Homepage: https://github.com/eakarpov/manatki#readme
- Issues: https://github.com/eakarpov/manatki/issues
- npm.io page: https://npm.io/package/manatki

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 0.2.0 (latest) — 2024-12-18
- 0.0.0 — 2018-06-15

## README

# manatki

[![Build status](https://img.shields.io/github/actions/workflow/status/eakarpov/manatki/ci.yml?branch=v1.x&label=CI&logo=github&style=flat-square)](https://github.com/eakarpov/manatki/actions/workflows/ci.yml)

Dependency-free library for functional programming in Typescript/Javascript

In functional programming, a monad is a structure that combines program fragments (functions) and wraps their return values in a type with additional computation. In addition to defining a wrapping monadic type, monads define two operators: one to wrap a value in the monad type, and another to compose together functions that output values of the monad type.

The library implements Scala approach of monad classes: `Option[T], Some[T], None`

## Get started

To install the library you should use npm:

```
npm i --save manatki
```

Then you are able to import it in your project:

```typescript
import {Option, Some, None} from 'manatki';
```

Optionally turn on implicit constructors of Option and Either

String and Number classes are Optionative and Validative, so they can be converted to Option<string>, Either<void, string> etc (see further).

To add this possibility you should import implicits from manatki:

```typescript
import 'manatki/implicits'; // All implicits
import 'manatki/implicits/String'; // implicits for strings
import 'manatki/implicits/Number'; // implicits for number
```

## Features

1. Option[T]

```typescript
const name: Option[String] = Some("name") // here we can get some optional value from outside
const upper = name
  .map(String.prototype.trim)
  .filter(e => e.length !== 0)
  .map(String.prototype.toUpperCase);
console.log(upper.getOrElse(""))
```

2. Either[K, T]

```typescript

```

3. Try[T]

```typescript
const a = (n: number) => (d: number) => {
  if (d === 0) throw new Error("division by zero");
  return n / d;
};

const status1 = Try<number>(() => a(5)(1)).fold<string>((n: number) => "Success", (e: Error) => e.message);
console.log(status1); // Success

const status2 = Try<number>(() => a(5)(0)).fold<string>((n: number) => "Success", (e: Error) => e.message);
console.log(status2); // division by zero
```

You can make your class Optionative by inheriting from Optionative<T>:

```typescript
class MyClass extends Optionative<MyClass> {
  // ...
}
const a = new MyClass();
a.some(); // returns Option<MyClass>
```

You can make your class Validative by inheriting from Validative<T>:

```typescript
class MyClass extends Validative<MyClass> {
  // ...
}
const a = new MyClass();
a.asLeft(); // returns Either<MyClass, void>
a.asRight(); // returns Either<void, MyClass>
```

5. Algebra

Monoid, Semigroup interfaces. Implicitly, String and Number are Monoidal in manatki.

## Functional programming

1. lFold

```typescript
lFold ((f: string) => (g: string) => f + g) ("") (["2","3","4","5","6"]); // 23456

lFold ((e: number) => (f: number) => e + f) (0) ([1,2,3]); // 6
```

2. rFold

```typescript
rFold ((f: string) => (g: string) => f + g)("") (["2","3","4","5","6"]); // "65432"
```

## License

MIT

---
_Source: https://npm.io/package/manatki · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
