Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

typescript-optional

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

typescript-optional

Optional (like Java) implementation in TypeScript

  • 1.2.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
8.2K
increased by5.51%
Maintainers
1
Weekly downloads
 
Created
Source

TypeScript Optional

npm License Build Status Coverage Status

Optional (like Java) implementation in TypeScript

Overview

Optional<T> is a type which may contain a payload of type T or may not contain. It provides the common interface regardless of an instance is present or is empty.

This module reffers to Optional class in Java 8.

Install

npm install --save typescript-optional

Usage

import

// import Optional type from this module
import Optional from 'typescript-optional';

creating Optional<T> objects

let nullableString: string | null = /* some nullable value */;

// all the following variables will be parameterized as Optional<string>.
let optional = Optional.ofNullable(nullableString);
let optionalPresent1 = Optional.ofNullable("foo");
let optionalPresent2 = Optional.ofNonNull("foo"); // accepts non-null value (or else throws TypeError)
let optionalEmpty1: Optional<string> = Optional.empty(); // type hinting required
let optionalEmpty2 = Optional.empty<string>(); // or parameterize explicitly

operations

let optional: Optional<string> = Optional.ofNullable( /* some optional value */ );

// force to retrieve the payload. (or else throws TypeError.)
// this method is not used match.
optional.get();

// be whether a payload is present or not.
optional.isPresent

// be whether this is empty or not. (negation of `isPresent` property)
optional.isEmpty

// if a payload is present, execute the given procedure.
optional.ifPresent(value => console.log(value));

// filter a payload with additional predicate.
optional.filter(value => value.length > 0);

// map a payload with the given mapper.
optional.map(value => value.length);

// map a payload with the given mapper which returns value wrapped with Optional type.
let powerIfPositive: (x: Number) => Optional<Number>
    = x => (x > 0) ? Optional.ofNonNull(x * x) : Optional.empty();

optional.flatMap(value => powerIfPositive(value));

// retrieve a payload if this is present, return the given value or else.
optional.orElse("bar");

// retrieve a payload if this is present, return a value supplied by the given function or else.
optional.orElseGet(() => "bar");

// retrieve a payload if this is present, throws an exception supplied by the given function or else.
optional.orElseThrow(() => new Error());

License

MIT License - LICENSE.md

FAQs

Package last updated on 26 Oct 2017

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