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

perhaps-ts

v1.0.0

Published

This library provides a light `Maybe` type for handling optional values, inspired by functional programming languages. A `Maybe` can either be `Just` (containing a value) or `Nothing` (no value).

Downloads

17

Readme

Maybe Type Library

This library provides a light Maybe type for handling optional values, inspired by functional programming languages. A Maybe can either be Just (containing a value) or Nothing (no value).

Installation

Install via npm:

npm install perhaps-ts

Usage

Creating Maybe Instances

You can create Maybe instances using the Just and Nothing static methods or helper functions.

import { Maybe, Just, Nothing } from "perhaps-ts";

// Using static methods
const maybeValue = Maybe.Just(5);
const noValue = Maybe.Nothing<number>();

// Using helper functions
const anotherMaybeValue = Just(10);
const anotherNoValue = Nothing<string>();

Checking for Values

You can check if a Maybe instance has a value (Just) or not (Nothing).

if (maybeValue.IsJust()) console.log("This is Just:", maybeValue.FromJust());
if (noValue.IsNothing()) console.log("This is Nothing");

Retrieving Values

To retrieve the value of a Just instance, use the FromJust or ToChecked methods. Be cautious as these methods will throw an error if called on a Nothing instance.

try {
  const value = maybeValue.FromJust();
  console.log("Value:", value);
} catch (error) {
  console.error(error.message);
}

try {
  const value = noValue.ToChecked();
} catch (error) {
  console.error(error.message); // Maybe: Attempted to access value of Nothing
}

Default Values

You can provide a default value when dealing with Maybe instances, ensuring you always have a fallback.

const valueOrDefault = noValue.FromMaybe(42);
console.log("Value or Default:", valueOrDefault); // Outputs: 42

Conditional Value Extraction

You can conditionally extract the value into an output object if the instance is Just.

const output = {};
if (maybeValue.To(output)) {
  console.log("Extracted Value:", output.value); // Extracted Value: 5
} else {
  console.log("No value to extract");
}

Or you can use the To method with a custom key.

type Output = { data?: number };
type UserData = { id: number; name: string };

const output: Output = {};
if (maybeValue.To(output, "data")) {
  console.log("Extracted Data:", output.data); // Extracted Data: 5
} else {
  console.log("No data to extract");
}

const user: Maybe<UserData> = Just({ id: 1, name: "Alice" });
const userOutput: Output = {};
if (user.To(userOutput, "data")) {
  console.log("Extracted User:", userOutput.data); // Extracted User: { id: 1, name: 'Alice' }
} else {
  console.log("No user to extract");
}

Equality Comparisons

You can compare Maybe instances for equality.

const maybe1 = Just(5);
const maybe2 = Just(5);
const maybe3 = Nothing<number>();

console.log(maybe1.equals(maybe2)); // true
console.log(maybe1.equals(maybe3)); // false
console.log(maybe1.notEquals(maybe3)); // true

Sure! Here are some examples of how to write functions that return Maybe<T> instead of T | false, along with how to handle and check the returned Maybe<T> values.

Example Function Returning Maybe<T>

Let's write a function that searches for an item in an array and returns a Maybe<T> instead of the item or false.

import { Maybe, Just, Nothing } from "maybe-type";

function findItem<T>(array: T[], predicate: (item: T) => boolean): Maybe<T> {
  for (const item of array) {
    if (predicate(item)) {
      return Just(item);
    }
  }
  return Nothing<T>();
}

// Usage example
const numbers = [1, 2, 3, 4, 5];

const result = findItem(numbers, (n) => n === 3);
if (result.IsJust()) {
  console.log("Found:", result.FromJust()); // Found: 3
} else {
  console.log("Item not found");
}

Handling and Checking Maybe<T> Values

Here are several ways to handle and check Maybe<T> values returned from functions.

Using IsJust and IsNothing

const maybeNumber = findItem(numbers, (n) => n === 6);

if (maybeNumber.IsJust()) {
  console.log("Found:", maybeNumber.FromJust());
} else {
  console.log("Item not found");
}

Using FromMaybe with a Default Value

const valueOrDefault = maybeNumber.FromMaybe(0);
console.log("Value or Default:", valueOrDefault); // Value or Default: 0

Using To for Conditional Extraction

const output = {};
if (maybeNumber.To(output)) {
  console.log("Extracted Value:", output.value);
} else {
  console.log("No value to extract");
}

Another Function Example

Let's create another function that parses an integer from a string and returns Maybe<number>.

function parseIntMaybe(input: string): Maybe<number> {
  const parsed = parseInt(input, 10);
  if (isNaN(parsed)) {
    return Nothing<number>();
  }
  return Just(parsed);
}

// Usage example
const maybeParsed = parseIntMaybe("123");
if (maybeParsed.IsJust()) {
  console.log("Parsed integer:", maybeParsed.FromJust()); // Parsed integer: 123
} else {
  console.log("Failed to parse integer");
}

const maybeFailedParsed = parseIntMaybe("abc");
console.log("Parsed or Default:", maybeFailedParsed.FromMaybe(0)); // Parsed or Default: 0

Returning Maybe<T> from Asynchronous Functions

Here’s an example of how to use Maybe<T> in an asynchronous function:

async function fetchData(url: string): Promise<Maybe<string>> {
  try {
    const response = await fetch(url);
    if (!response.ok) {
      return Nothing<string>();
    }
    const data = await response.text();
    return Just(data);
  } catch (error) {
    return Nothing<string>();
  }
}

// Usage example
fetchData("https://api.example.com/data").then((maybeData) => {
  if (maybeData.IsJust()) {
    console.log("Fetched Data:", maybeData.FromJust());
  } else {
    console.log("Failed to fetch data");
  }
});

These examples demonstrate how to use the Maybe type to handle cases where values might be absent, making your code more expressive and robust.

API Reference

Maybe Class

Static Methods

  • Maybe.Just(value: T): Maybe<T>
    • Creates a Just instance containing the provided value.
  • Maybe.Nothing<T>(): Maybe<T>
    • Creates a Nothing instance with no value.

Instance Methods

  • IsJust(): boolean
    • Returns true if the instance is Just, otherwise false.
  • IsNothing(): boolean
    • Returns true if the instance is Nothing, otherwise false.
  • FromJust(): T
    • Returns the value if the instance is Just, otherwise throws an error.
  • ToChecked(): T
    • Alias for FromJust.
  • FromMaybe(defaultValue: T): T
    • Returns the value if the instance is Just, otherwise returns the provided default value.
  • To(out: { [key: string]: T }, key?: string): boolean
    • If the instance is Just, sets the value in the provided output object and returns true, otherwise returns false. Optionally, you can specify a custom key, otherwise the default key is "value".
  • equals(other: Maybe<T>): boolean
    • Returns true if the other Maybe instance is equal, otherwise false.
  • notEquals(other: Maybe<T>): boolean
    • Returns true if the other Maybe instance is not equal, otherwise false.

License

This project is licensed under the MIT License. See the LICENSE file for details.


This library simplifies handling optional values in TypeScript, providing a clear and expressive way to manage cases where a value may or may not be present.