![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Implementation of some helpers of Rust
stdlib on JavaScript/TypeScript
import { Some, None } from "krustykrab";
const someOption = Some("foo");
const noneOption = None();
import { Ok, Err } from "krustykrab";
const okResult = Ok("foo");
const errResult = Err("bar");
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
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'
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'
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'
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`
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`
FAQs
Rust utils for JavaScript projects
The npm package krustykrab receives a total of 54,106 weekly downloads. As such, krustykrab popularity was classified as popular.
We found that krustykrab demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.