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

ts-scala-utils

v1.0.2

Published

Scala Option Utils in TypeScript

Downloads

5

Readme

ts-scala-utils NPM version NPM monthly downloads NPM total downloads

A TypeScript library that provides Scala-like Option, Some, and None utilities, helping you manage optional values safely and efficiently.

Features

Option Type: Provides a type-safe way to handle optional values. Some and None: Distinguishes between the presence (Some) and absence (None) of a value.

Utility Methods: Includes a variety of methods to manipulate and query Option instances, such as map, getOrElse, isDefined, fold, forEach, and more. Functional Programming: Encourages a functional programming style in TypeScript.

Installation

To use this library in your project, install it via npm:

npm i ts-scala-utils

Usage

Here’s how you can use the library in your TypeScript projects:

Importing the Library

import { Option } from "ts-scala-utils";

Basic Example

const someValue = Option.some(42);
const noneValue = Option.none<number>();

console.log(someValue.isDefined()); // true
console.log(noneValue.isDefined()); // false

console.log(someValue.get()); // 42
console.log(someValue.getOrElse(0)); // 42

console.log(noneValue.getOrElse(0)); // 0

Advanced Usage

Map Function

const mappedOption = someValue.map(x => x \* 2);
console.log(mappedOption.get()); // 84

Fold Function

const result = noneValue.fold(
  () => "Empty",
  (value) => `Value: ${value}`
);
console.log(result); // "Empty"

Chain Options

const option1 = Option.some(10);
const option2 = Option.none<number>();

const combined = option1.orElse(option2);
console.log(combined.get()); // 10

API Reference

Option Class

static some<T>(value: T): Option<T>: Creates an instance of Some containing the provided value.

static none<T>(): Option<T>: Creates an instance of None indicating the absence of a value.

get(): T: Returns the value if present, otherwise throws an error.

getOrElse(defaultValue: T): T: Returns the value if present, otherwise returns the provided default value.

isDefined(): boolean: Returns true if the option contains a value, otherwise false.

map<U>(f: (value: T) => U): Option<U>: Transforms the value if present, otherwise returns None.

fold<U>(ifEmpty: () => U, f: (value: T) => U): U: Applies a function to the value if present, otherwise applies the ifEmpty function.

forEach(f: (value: T) => void): void: Applies a side-effecting function to the value if present.

orElse(alternative: Option<T>): Option<T>: Returns the current Option if it contains a value, otherwise returns the provided alternative Option.

Contributing

Contributions are welcome! Please feel free to submit a pull request or open an issue if you find any bugs or have feature requests.

Steps to Contribute: Fork the repository. Create a new branch for your feature or bugfix. Implement your changes. Ensure all tests pass (npm test). Submit a pull request.

License

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

Acknowledgments

Inspired by Scala’s Option type.

Thanks to all contributors and users of this library.