Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

krustykrab

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

krustykrab

Rust utils for JavaScript projects

  • 1.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

krustykrab

NPM dependencies status

Implementation of some helpers of Rust stdlib on JavaScript/TypeScript

Api reference

Implemented helpers

Option

import { Some, None } from "krustykrab";

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

Rust reference

krustykrab reference

Result

import { Ok, Err } from "krustykrab";

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

Rust reference

krustykrab reference

Additional helpers

unwrap

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

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

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

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

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

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

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`

Keywords

FAQs

Package last updated on 17 Sep 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc