npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@lassepitkanen/optionalts

v1.0.4

Published

TypeScript library that provides an Optional<T> type similar to Java's Optional class.

Downloads

9

Readme

OptionalTS

OptionalTS is an npm package for TypeScript that provides an Optional class similar to Java's Optional class. This class allows you to work with values that may be null or undefined in a more concise and readable way.

Installation

You can install the package using npm:

npm install @lassepitkanen/optionalts

Usage

You can import the Optional class in your TypeScript code:

import { Optional } from '@lassepitkanen/optionalts';

Creating an Optional

You can create an Optional object using the of static method. This method takes a value as a parameter and returns an Optional object containing the value:

const value = 'hello';
const optional = Optional.of(value);

If the value is null or undefined, the of method returns an empty Optional:

const value = null;
const optional = Optional.of(value);
console.log(optional.isPresent()); // false

You can also create an empty Optional using the empty static method:

const optional = Optional.empty();
console.log(optional.isPresent()); // false

Mapping an Optional

You can apply a mapping function to the content of an Optional using the map method. This method takes a mapping function as a parameter and returns a new Optional object containing the result of the mapping function:

const value = 'hello';
const optional = Optional.of(value);
const mappedOptional = optional.map((str: string) => str.length);
console.log(mappedOptional.get()); // 5

If the Optional is empty, the map method returns an empty Optional:

const optional = Optional.empty();
const mappedOptional = optional.map((str: string) => str.length);
console.log(mappedOptional.isPresent()); // false

Checking for Presence

You can check whether an Optional object contains a value using isPresent and isEmpty methods:

const value = 'hello';
const optional = Optional.of(value);
console.log(optional.isPresent()); // true
console.log(optional.isEmpty()); // false

Getting the Value

You can get the value of an Optional object using the get method. If the Optional is empty, the get method throws an error:

const value = 'hello';
const optional = Optional.of(value);
console.log(optional.get()); // hello
const optional = Optional.empty();
console.log(optional.get()); // throws an error

Consuming the Value

You can consume the value of an Optional object using the ifPresent method. This method takes a consumer function as a parameter and applies it to the value if the Optional is not empty:

const value = 'hello';
const optional = Optional.of(value);
optional.ifPresent((str: string) => console.log(str)); // logs 'hello'

If the Optional is empty, the ifPresent method does nothing:

const optional = Optional.empty();
optional.ifPresent((str: string) => console.log(str)); // does nothing

Providing a Default Value

You can provide a default value for an Optional object using the orElse method. This method takes a default value as a parameter and returns either the value of the Optional if it is not empty, or the default value if it is empty:

const optional = Optional.of('hello');
const result = optional.orElse('world');
console.log(result); // hello
const optional = Optional.empty();
const result = optional.orElse('world');
console.log(result); // world

You can provide a default value for an Optional object using the orElseGet method. This method takes a function that returns a default value as a parameter and returns either the value of the Optional if it is not empty, or the result of the function if it is empty:

const optional = Optional.of('hello');
const result = optional.orElseGet(() => 'world');
console.log(result); // hello
const optional = Optional.empty();
const result = optional.orElseGet(() => 'world');
console.log(result); // world

Comparing Optionals

You can compare two Optional objects for equality using the equals method. This method takes another Optional as a parameter and returns true if the two objects are equal, or false otherwise. Two Optional objects are considered equal if they are of the same type and their content is equal:

const optional1 = Optional.of('hello');
const optional2 = Optional.of('hello');
console.log(optional1.equals(optional2)); // true
const optional1 = Optional.of('hello');
const optional2 = Optional.of('world');
console.log(optional1.equals(optional2)); // false
const optional1 = Optional.of('hello');
const optional2 = Optional.empty();
console.log(optional1.equals(optional2)); // false

API

Optional.of(value: T): Optional

Creates a new Optional instance containing the specified value. If the value is null or undefined, an empty Optional is returned instead.

Optional.empty(): Optional

Returns an empty Optional instance.

map<K extends (param: NonNullable) => any>(mapper: K): Optional<ReturnType> | Optional

Applies the given mapping function to the content of this Optional and returns a new Optional that wraps the result.

isPresent(): boolean

Returns true if this Optional contains a value, false otherwise.

isEmpty(): boolean

Returns true if this Optional does not contain a value, false otherwise.

get(): NonNullable | never

Returns the content of this Optional, throwing an error if it is empty.

ifPresent<K extends (param: NonNullable) => any>(consumer: K): Optional

Applies the given consumer function to the content of this Optional, if it is not empty.

orElse(value: K): K | NonNullable

Returns the content of this Optional if it is not empty, or the specified value otherwise.

orElseGet<K extends () => any>(func: K): ReturnType | NonNullable

Returns the content of this Optional if it is not empty, or the result of the given function otherwise.

orElseThrow(error: K): never | NonNullable

Returns the content of this Optional if it is not empty, or throws the specified error otherwise.

equals(optional: Optional): boolean

Compares this Optional with another to determine equality. Returns true if the two Optional objects are equal, false otherwise.