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

@gregchlosta/option-ts

v0.1.2

Published

Simple implementation of Option type with Pattern Matching for TypesScript

Downloads

12

Readme

@gregchlosta/option-ts

Simple implementation of Option type with Pattern Matching for TypesScript

The Option<T> and Its Advantages Over Null Values

The problem with null values is that if you try to use a null value as a not-null value, you’ll get an error of some kind. Because this null or not-null property is pervasive, it’s extremely easy to make this kind of error.

However, the concept that null is trying to express is still a useful one: a null is a value that is currently invalid or absent for some reason.

The problem isn’t really with the concept but with the particular implementation. Type Option<T> with Pattern Matching functionality provided by this library can help you with this.

How to Install:

npm install @gregchlosta/option-ts

How to Use

Option<T> is a discriminated union of Some<T> and None. When we have a Some value, we know that a value is present and the value is held within the Some. When we have a None value, in some sense, it means the same thing as null. We don’t have a valid value.

Initialize the Option<T> value

import { Option, newOption } from '@gregchlosta/option-ts'

// Type will automatically get inferred when value is provided as parameter.
const some = newOption('Some Value') // -> Option<string> -> (Some<string>)

// When creating a None Option, manual typing possible value type is required since automatic type inferrance is not possible
const none: Option<string> = newOption() // -> Option<string> -> (None)

Patern matching with match()

Match function will take Option<T>, and matcher object as parameters. Functions declared in the matcher object always have to return the same value type.

Example below come from React, but option-ts can be used with any other front-end framework or regular TypeScript.

import React from 'react'
import { Option, match } from '@gregchlosta/option-ts'

export type ButtonProps = {
  title: Option<string>
}

export const Button: React.FC<ButtonProps> = ({ title }) => {
  return match(title, {
    Some: (v) => <button>{v}</button>,
    None: () => <button>Default</button>,
  })
}

Unpacking Option<T> with withDefault()

To get direct access to the value we can simple unpack option using withDefault() which takes two parameters: option, and default value.

import { Option, newOption, withDefault } from '@gregchlosta/option-ts'

const someOption = newOption('Some!')
const noneOption: Option<string> = newOption()

const valueSome = withDefault(someOption, 'default') // 'Some!'
const valueDefault = withDefault(noneOption, 'default') // 'default'

Type Guards

import { newOption, isSome, isNone } from '@gregchlosta/option-ts'

const someOption = newOption('Some!')
const noneOption: Option<string> = newOption()

isSome(someOption) // true
isNone(noneOption) // false

License

The MIT License