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

ooptional

v2.0.2

Published

TypeScript optional types with seamless async support and magical type inference

Downloads

211

Readme

License: MIT npm version

OOptional - TypeScript optional types with seamless async support and 🪄 magical 🪄 type inference

OOPtional empowers you to write robust and expressive TypeScript code by elegantly handling optional values within functional or object-oriented design patterns. This library eliminates the need for cumbersome null and undefined checks, leading to cleaner, more maintainable code. Experience the magic of its seamless type inference with zero dependencies!

Key Features:

  • Optional Type: Introduces a powerful Optional type, which encapsulates the presence or absence of a value.
  • Object-Oriented Integration: Designed to work effortlessly within your object-oriented code.
  • Null/Undefined Elimination: Eradicates the need to perform constant null and undefined checks.
  • Chaining and Composition: Supports elegant method chaining for seamless manipulation of optional values.
  • Sync/Async Support: Works seamlessly with synchronous and asynchronous operations through Promise-based methods.
  • JSON.stringify Safe: Designed to handle serialization via JSON.stringify without causing errors.
  • Magical Type Inference: OOptional infers types intelligently, minimizing the need for explicit type annotations.

Installation

npm install ooptional

Basic Usage

import { Optional, Option } from "ooptional";

function divide(numerator: number, denominator: number): Optional<number> {
    if (denominator === 0) {
        return Option.none();
    } else {
        return Option.of(numerator / denominator);
    }
}

const result = divide(10, 2)
    .map(value => value * 2) // Only executes if result has a value
    .unwrapOr(0); // Provides a default value of 0 if result is empty

console.log(result); // Output: 10

Advanced Functionality

OOptional offers a rich set of methods for working with optional values:

  • and(other: Optional<B>): Combines two optionals.
  • andThen(mapper: (value: A) => Optional<B>): Applies a function to the value if it exists.
  • filter(predicate: (value: A) => boolean): Filters the optional based on a condition.
  • flatMap(mapper: (value: A) => Optional<B>): Similar to map, but allows mapping to another optional.
  • map(mapper: (value: A) => B): Transforms the value if it exists.
  • or(other: Optional<B>): Provides a fallback optional if the current one is empty.
  • unwrapOr(other: A): Returns the contained value or a default value.
  • ...and many more!

Why OOptional?

  • Improved Code Readability: Makes your code more explicit and easier to understand.
  • Reduced Errors: Prevents potential runtime errors caused by null or undefined.
  • Elegant Expressiveness: Provides a fluent API for handling optional values in a functional style.

Contributions

OOPtional welcomes contributions! Feel free to open issues, submit pull requests, or provide feedback to help shape the development of this library.

License

This project is licensed under the MIT License