https://github.com/josestg/maybe
Maybe is a utility for handling optional values safely and cleanly in TypeScript. Inspired by Haskell and OCaml, it provides a clear and functional approach to dealing with values that may or may not exist.
https://github.com/josestg/maybe
deno maybe optional-type typescript
Last synced: about 2 months ago
JSON representation
Maybe is a utility for handling optional values safely and cleanly in TypeScript. Inspired by Haskell and OCaml, it provides a clear and functional approach to dealing with values that may or may not exist.
- Host: GitHub
- URL: https://github.com/josestg/maybe
- Owner: josestg
- License: mit
- Created: 2024-11-19T13:35:45.000Z (6 months ago)
- Default Branch: main
- Last Pushed: 2024-11-19T14:02:21.000Z (6 months ago)
- Last Synced: 2025-01-22T15:43:39.300Z (3 months ago)
- Topics: deno, maybe, optional-type, typescript
- Language: TypeScript
- Homepage: https://jsr.io/@josestg/maybe
- Size: 6.84 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Maybe - Safe Optional Values for TypeScript
`Maybe` is a utility for handling optional values safely and cleanly in
TypeScript. Inspired by Haskell and OCaml, it provides a clear and functional
approach to dealing with values that may or may not exist.## Features
- **Explicit Optional Values**: Use `Some` and `None` for clarity.
- **Functional API**: Transform and handle values with `map`, `flatMap`, and
`match`.
- **Type-Safe**: Eliminate `null` and `undefined` errors.## Example
```ts
import { map, match, none, some, unwrapOr } from "./mod.ts";const value = some(42);
const empty = none();console.log(unwrapOr(value, 24)); // 42
console.log(unwrapOr(empty, 24)); // 24const squared = map(value, (v) => v ** 2); // some(1764)
console.log(squared);const message = match(value, {
some: (v) => `Value is ${v}`,
none: () => "No value",
});console.log(message); // "Value is 42"
```