{"id":12742,"library":"krustykrab","title":"KrustyKrab: Rust-Inspired Utilities for JavaScript/TypeScript","description":"KrustyKrab provides idiomatic implementations of Rust's `Option` and `Result` types, along with several utility functions inspired by Rust's standard library, for JavaScript and TypeScript projects. It aims to enhance error handling and null safety by introducing concepts like explicit handling of present/absent values and success/failure states. The current stable version is 1.1.0. As a relatively new library focused on bringing Rust paradigms to TypeScript, its release cadence is likely driven by feature additions and improvements rather than a fixed schedule. Key differentiators include its faithful adaptation of `Option` and `Result` with methods like `unwrap`, `unwrapOr`, `map`, and `andThen`, as well as practical utilities such as `getResult` for converting Promises, `toOption` for nullable conversions, and `tryCatch` for wrapping synchronous operations in `Result` types. This approach encourages more robust, explicit, and functional error management compared to traditional try-catch blocks and null checks.","status":"active","version":"1.1.0","language":"javascript","source_language":"en","source_url":"ssh://git@github.com/vtaits/krustykrab","tags":["javascript","rust","std","stdlib","utils","helpers","typescript"],"install":[{"cmd":"npm install krustykrab","lang":"bash","label":"npm"},{"cmd":"yarn add krustykrab","lang":"bash","label":"yarn"},{"cmd":"pnpm add krustykrab","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"KrustyKrab is primarily designed for ESM consumption and TypeScript; CommonJS `require` is not the idiomatic way to import these utilities, though it might work with transpilation.","wrong":"const { Some, None } = require('krustykrab');","symbol":"Option and Result types","correct":"import { Some, None, Ok, Err } from 'krustykrab';"},{"note":"All utility functions are named exports from the main package entry point, not default exports from subpaths.","wrong":"import unwrap from 'krustykrab/unwrap';","symbol":"unwrap, unwrapOr, unwrapOrElse","correct":"import { unwrap, unwrapOr, unwrapOrElse } from 'krustykrab';"},{"note":"While `import * as KrustyKrab` works, importing specific named exports is generally preferred for tree-shaking and clarity.","wrong":"import * as KrustyKrab from 'krustykrab';","symbol":"getResult, toOption, tryCatch","correct":"import { getResult, toOption, tryCatch } from 'krustykrab';"}],"quickstart":{"code":"import { Some, None, Ok, Err, unwrap, getResult, toOption, tryCatch } from 'krustykrab';\n\n// Demonstrating Option type for null safety\nconst maybeValue = Some(\"Hello, KrustyKrab!\");\nconsole.log(maybeValue.isSome()); // true\nconsole.log(maybeValue.unwrapOr(\"Default\")); // \"Hello, KrustyKrab!\"\n\nconst noValue = None<string>();\nconsole.log(noValue.isNone()); // true\nconsole.log(noValue.unwrapOr(\"Default Value\")); // \"Default Value\"\n\n// Demonstrating Result type for error handling\nconst successfulResult = Ok(42);\nconsole.log(successfulResult.isOk()); // true\nconsole.log(successfulResult.unwrap()); // 42\n\nconst failedResult = Err(\"Something went wrong!\");\nconsole.log(failedResult.isErr()); // true\nconsole.log(failedResult.unwrapErr()); // \"Something went wrong!\"\n\n// Utility: unwrap - panics on null/undefined\nconst fooOrNull: string | null = 'foo';\nconst guaranteedFoo = unwrap(fooOrNull); // 'foo'\n// unwrap(null); // This would throw an error at runtime\n\n// Utility: getResult - converting Promises\nasync function fetchData() {\n  const dataPromise = Promise.resolve({ id: 1, name: 'Spongebob' });\n  const result = await getResult(dataPromise);\n  if (result.isOk()) {\n    console.log('Fetched data:', result.unwrap());\n  } else {\n    console.error('Failed to fetch:', result.unwrapErr());\n  }\n}\nfetchData();\n\n// Utility: tryCatch - wrapping synchronous operations\nconst jsonString = '{\"item\":\"crabby patty\"}';\nconst parsedResult = tryCatch(() => JSON.parse(jsonString));\nif (parsedResult.isOk()) {\n  console.log('Parsed JSON:', parsedResult.unwrap());\n} else {\n  console.error('JSON parse error:', parsedResult.unwrapErr());\n}\n\nconst invalidJsonString = '{invalid json}';\nconst invalidParsedResult = tryCatch(() => JSON.parse(invalidJsonString));\nconsole.log('Invalid JSON parse was an error:', invalidParsedResult.isErr()); // true\n","lang":"typescript","description":"This quickstart demonstrates the core `Option` and `Result` types, along with key utilities like `unwrap`, `getResult`, and `tryCatch`, showcasing their usage for null safety and robust error handling in TypeScript."},"warnings":[{"fix":"Prefer `unwrapOr(defaultValue)` or conditional checks like `if (option.isSome()) { option.unwrap(); }` to safely access values, or use `map()` and `andThen()` for chained operations.","message":"The `unwrap()` method on `Option` and `Result` types will throw a runtime error if called on a `None` or `Err` instance respectively. This behavior mimics Rust's `panic!` and is intended for situations where the value is *guaranteed* to be present. Use `unwrapOr()`, `map()`, `andThen()`, or `match` (if implemented) for safer access.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Integrate KrustyKrab into a TypeScript project and ensure your `tsconfig.json` enables strict null checks (`strictNullChecks: true`) for optimal type safety.","message":"KrustyKrab is designed with TypeScript in mind, and its full benefits, particularly type inference and strict null checks, are best utilized in a TypeScript project. While it can be used in plain JavaScript, you'll lose compile-time type safety for `Option` and `Result` operations, potentially leading to more runtime errors that TypeScript would catch.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Ensure the `Option` is `Some` before calling `unwrap()` using `isSome()`, or use safer alternatives like `unwrapOr(defaultValue)`, `map()`, or `andThen()`.","cause":"Attempted to call `.unwrap()` on an `Option` instance that was actually `None`.","error":"Error: Called unwrap on a None value"},{"fix":"Check if the `Result` is `Ok` using `isOk()` before calling `unwrap()`, or handle the error gracefully using `unwrapOr(defaultValue)`, `mapErr()`, or `andThen()`.","cause":"Attempted to call `.unwrap()` on a `Result` instance that was actually `Err`.","error":"Error: Called unwrap on an Err value"}],"ecosystem":"npm"}