# krustykrab

> Rust utils for JavaScript projects

Latest version **1.1.0** (published 2024-09-17) · MIT license · 0 weekly downloads

## Install

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

## Health

**Score 50/100 (C)** — status: stable.

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

Warnings: low downloads.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2024-09-17 |
| First published | 2024-01-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 76.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Vadim Taits |
| Maintainers | vtaits |
| Keywords | rust, std, stdlib, utils, helpers |

## Links

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

## Alternatives

- [lodash.assign](https://npm.io/package/lodash.assign.md) — 2.3M weekly downloads
- [lodash.chunk](https://npm.io/package/lodash.chunk.md) — 1.8M weekly downloads
- [react-native-ios-utilities](https://npm.io/package/react-native-ios-utilities.md) — 138.5K weekly downloads
- [@technically/lodash](https://npm.io/package/@technically/lodash.md) — 50.9K weekly downloads
- [@fluid-topics/ft-icon](https://npm.io/package/@fluid-topics/ft-icon.md) — 20.6K weekly downloads

## Recent versions

- 1.1.0 (latest) — 2024-09-17
- 1.0.1 — 2024-08-27
- 1.0.0 — 2024-01-15

## README

# krustykrab

[![NPM](https://img.shields.io/npm/v/krustykrab.svg)](https://www.npmjs.com/package/krustykrab)
![dependencies status](https://img.shields.io/librariesio/release/npm/krustykrab)

Implementation of some helpers of `Rust` stdlib on `JavaScript/TypeScript`

[Api reference](https://vtaits.github.io/krustykrab/)

## Implemented helpers

### Option

```ts
import { Some, None } from "krustykrab";

const someOption = Some("foo");
const noneOption = None();
```

[Rust reference](https://doc.rust-lang.org/std/option/enum.Option.html)

[krustykrab reference](https://vtaits.github.io/krustykrab/types/Option.html)

### Result

```ts
import { Ok, Err } from "krustykrab";

const okResult = Ok("foo");
const errResult = Err("bar");
```

[Rust reference](https://doc.rust-lang.org/std/result/enum.Result.html)

[krustykrab reference](https://vtaits.github.io/krustykrab/types/Result.html)

## Additional helpers

### unwrap

Panics if the value is `null` or `undefined` or returns it otherwise

```ts
import { unwrap } from "krustykrab";

const fooOrUndefined = document.getElementById('foo'); // html element or `undefined`
const foo = unwrap(fooOrUndefined); // exactly html element
```

### unwrapOr

Returns the value if it's not `null` or `undefined`, or returns the default value

```ts
import { unwrapOr } from "krustykrab";

const foo: Partial<Record<string, string>> = { bar: 'baz' };

unwrapOr(foo.bar, 'qux'); // returns 'baz'
unwrapOr(foo.bat, 'qux'); // returns 'qux'
```

### unwrapOrElse

Returns the value if it's not `null` or `undefined`, or computes it

```ts
import { unwrapOrElse } from "krustykrab";

const foo: Partial<Record<string, string>> = { bar: 'baz' };

unwrapOrElse(foo.bar, () => 'qux'); // returns 'baz'
unwrapOrElse(foo.bat, () => 'qux'); // returns 'qux'
```

### getResult

Converts `Promise` to `Result`

```ts
import { getResult } from "krustykrab";

const successResult = await getResult(Promise.resolve('foo'));
successResult.unwrap(); // returns 'foo'

const errorResult = await getResult(Promise.reject('bar'));
errorResult.unwrapErr(); // returns 'bar'
```

### toOption

Convert a nullable variable to `Option`


```ts
import { toOption } from "krustykrab";

const option = toOption('foo');
option.isNone(); // returns `false`;
option.unwrap(); // returns `'foo'`;

toOption(null).isNone(); // returns `true`
toOption(undefined).isNone(); // returns `true`
```

### tryCatch

Wrap the result of a function call with `Result`

```ts
import { tryCatch } from "krustykrab";

const successResult = tryCatch(() => JSON.parse('{"foo": "bar"}'));
successResult.unwrap(); // returns `{ foo: "bar" }`

const errorResult = tryCatch(() => JSON.parse("{invalid json}"));
errorResult.isErr(); // returns `true`
```

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