Giter VIP home page Giter VIP logo

option's Introduction

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

option's People

Contributors

semantic-release-bot avatar tomokimiyauci avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.