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

@onegen/optional

v1.1.0

Published

C++-based optional utility for TypeScript

Downloads

1

Readme

@onegen/optional

Optional utility class in TypeScript, based on C++ std::optional.

Made as minimally as I could, without any dependencies.
I just like how std::optional works and wanted it in JS/TS.
Guess it’s mostly done unless I find some mistake or something. 🤷🏼‍♂️

import { Optional } from "@onegen/optional";

function safeDiv (x: number, y: number): Optional<number> {
     if (y === 0) return Optional.nullopt;
     return Optional.some(x / y);
}

var result = safeDiv(2, 0);
if (!result.hasValue())
     console.log('Dividing be zero again, you dummy?');
else
     console.log('Result:', result.value());

Optional is a wrapper helper class around a value that may or may not be defined. Instead of returning some arbitrary value (like -1), null or throwing an error, Optional can be returned, prompting you – the dev – to first check if there is any value before actually using it in a fun and type safe manner! Look, it’s not for everyone, but I like it, OK?

Working with Optionals

Declaring

import { Optional, some } from '@onegen/optional';

var opt1 = new Optional<number>(2024);
var opt2 = Optional.some(2024);
var opt3 = some(2024)

All of these lines are equivalent. They create an Optional holding a number value 2024. First line specifies the type explicitly, second implies it from its value, third line is just a shorthand for the second.

As you – a smart TypeScript developer – might’ve guessed, you can’t change the Optional type. Once you make an instance of Optional<T>, the variable will only ever have Optional<T>.

var opt = Optional.some(2024);
opt = Optional.some('2024'); // TS won’t let you do this!

You can change the value, of course:

var opt = Optional.some(2024);
opt = Optional.some(2025);

// ..or as of 1.1.0 with a method:
opt.assign(2026);

…and even remove it:

var opt = Optional.some(2024);
opt = Optional.none();
opt = Optional.nullopt; // equivalent to Optional.none()

Do mind that this doesn’t clear the variable type. The variable is still Optional<number>, even if nullopt. It will ever contain only a number or nothing.

Typeless declaration

Be mindful when creating empty Optionals:

var opt = Optional.none()

The type of opt is Optional<unknown> and that is not something you really want now. I wish I could somehow forbid this, but oh well. Seriously, don’t do that. When making an empty Optional, specify its type explicitly, for your own sanity:

var opt: Optional<number> = Optional.none();
var opt = new Optional<number>();

Both these options work perfectly fine.

Base usage

The main advantage of Optional is that you don’t need an arbitrary "did not work" value (-1) or use nulls that you may forgot to check for. Optional makes it natural (at least for me) to check if it has a value before actually using it:

const result: Optional<number> = saferDivide(10, 0);
if (!result.hasValue())
     return "Whoops, something went wrong!"

const value = result.value();

Calling result.value() while there is no value will lead to an error.

If you want a more comprehensive Rust-like Result utility for error handling, you might want to take a look at neverthrow. It has a lot more functionality for error handling, Optional is just a simple "has value or has no value" wrapper. With a few more functions copied from C++:

Functions

Observers

| Function | Return Type | Description | | :-----------------------------------: | :-------: | :------------------------------------------------------------------------------: | | Optional.hasValue() | boolean | Checks whether the object contains a value | | Optional.value() | T | Returns the included value (throws error if there is none) | | Optional.valueOr(defaultValue: T) | T | Returns either the included value OR provided default value |

Modifiers

| Function | Return Type | Description | | :-----------------------------------: | :-------: | :------------------------------------------------------------------------------: | | Optional.reset() | this | Removes the contained value | | Optional.assign() | this | Assigns a new contained value | | Optional.swap(other: Optional<T>) | this | Swaps values of same-type Optionals |

C++ also has emplace(), but TS types cannot be used to make new instances, as far as I know.

assign() is also deviance from std::optional, but I chose to add it, as TS/JS does not allow operator overloading.

Also, all modifier methods return themselves (this) to allow chaining.

var opt = Optional.some(2024);
opt.reset().assign(2025).reset();

Monadic Operations

| Function | Return Type | Description | | :-----------------------------------: | :-------: | :------------------------------------------------------------------------------: | | Optional.andThen<U> (fn: (value: T) => Optional<U>) | Optional<U> | – | | Optional.transform<U> (fn: (value: T) => U) | Optional<U> | – | | Optional.orElse (fn: () => Optional<T>) | Optional<U> | – |

These are harder to explain in short Markdown table, there are simple usage examples in optional-mon.test.ts. Who am I even writing this for nobody will use this except me lmao.

Licence

@onegen/optional is available as an open-source utility library licenced under the MIT Licence.

  • TL;DR;NAL: Do absolutely whatever you want with the code, just include the LICENCE file if you re-distribute it.
  • See LICENCE file or tl;drLegal for more details.