@miyauci/option
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

option

deno land deno doc GitHub release (latest by date) codecov License

test NPM standard-readme compliant semantic-release: angular

Minimum option type port of Rust.

Table of Contents

Background

This project provides minimum option features. They are designed to be optimized in tree-shaking.

One of the existing challenges in the JavaScript/TypeScript community is tree-shaking. Due to the dynamic nature of the language, tree-shaking can only be used in limited situations. That is, tree-shaking cannot do anything about unused class methods or properties.

The only solution to this is separation into top-level functions. It is very unfortunate but true that no other method exists.

Having said that, a future proposal could solve this.

Install

deno.land:

import * as mod from "https://deno.land/x/optio/mod.ts";

npm:

npm i @miyauci/option

Usage

Type Option represents an optional value.

Every Option is either Some and contains a value, or None, and does not.

import {
  expect,
  None,
  type Option,
  Some,
} from "https://deno.land/x/optio/mod.ts";

function divide(numerator: number, denominator: number): Option<number> {
  if (!denominator) return None;

  return Some(numerator / denominator);
}

const opt = divide(100, 0);
expect(opt, "divide by 0");

All operators for Option are separated from prototype.

Feature

The pipeline operator will linearize the nesting.

import { map, type Option, match } from "https://deno.land/x/optio/mod.ts";

declare const option: Option<unknown>;
declare const mapper: (value: unknown) => unknown

const result = map(option, mapper)
  |> map(%, mapper)
  |> match(%, {
    Some: mapper,
    None:mapper
  })

proposal extensions allows for successive function adaptations, as in the Fluent API.

import { map, type Option, match } from "https://deno.land/x/optio/mod.ts";

declare const option: Option<unknown>;
declare const mapper: (value: unknown) => unknown

const result = option
  ::map(mapper)
  ::map(mapper)
  ::match({
    Some: mapper,
    None: mapper
  })

API

See deno doc for all APIs.

Acknowledgements

Contributing

See contribution.

License

MIT © 2023 Tomoki Miyauchi

Package Sidebar

Install

npm i @miyauci/option

Weekly Downloads

20

Version

1.0.0

License

MIT

Unpacked Size

80.4 kB

Total Files

29

Last publish

Collaborators

  • miyauci