New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

option-t

Package Overview
Dependencies
Maintainers
1
Versions
333
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

option-t

Option type implementation whose APIs are inspired by Rust's `Option<T>`.

  • 0.3.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
86K
decreased by-14.87%
Maintainers
1
Weekly downloads
 
Created
Source

option-t

npm version Build Status

Installation

npm install --save option-t

Usage

var OptionT = require('option-t');

// `Some<T>`
var some = new OptionT.Some(1);
console.log(some.isSome); // true
console.log(some.unwrap()); // 1

// `None`
var none = new OptionT.None();
console.log(none.isSome); // false
console.log(none.unwrap()); // this will throw `Error`.

JSON Representation

Some<T>

new Some(1) will be:

{
    "is_some": true,
    "value": 1
}
None

new None() will be:

{
    "is_some": false
}

API

Semantics

This library represents Option type in ECMAScript. So this object will be the one of following states:

  • Some<T>
    • option instanceof OptionT.Some
    • option.isSome === true.
  • None
    • option instanceof OptionT.None
    • option.isSome === false.

Option<T>

This type is a interface to represent Option<T>. Some<T> and None must implement this Option<T> interface.

This is just interface. This is not exported to an environment which has no interface feature as a part of its type system like TypeScript.

If you'd like to check whether the object option is Option<T> or not in such an environment, you can use option instanceof OptionT.OptionT to check it.

But this way is not a tier-1 approach. We recommend to use a interface and type system strongly. Thus we don't export OptionT object to the type definition for TypeScript.

Some<T>

This type represents that there are some values T. If this value wraps null, it just means that there is a null value.

None (None<T>)

This type represents that there is no value explicitly. It is just None !== null.

License

MIT License

FAQs

Package last updated on 20 Mar 2015

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc