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

@jemmyphan/optional-chain

v0.1.2

Published

Optional chaining implementation in TypeScript. Uses [`option type`](https://en.wikipedia.org/wiki/Option_type)

Downloads

2

Readme

optional-chain travis-ci

Optional chaining implementation in TypeScript.
Uses option type

Requirement

This library requires TS 2.8+ version to use conditional type

Install

npm install optional-chain

Usage

import { optional } from "optional-chain";

type User = {
  name?: {
    first: string
  }
}
const user: User = getUser(); // { name: null }
const optionalUser = optional(user);
optionalUser.k("name").k("first").get(); // undefined, does not throw an exception.

API

optional-chain library exports below APIs.

optional

optional is a fuctory function to creates a Option instance.

Option<T>

An instance of Option can be constructed with a value for the souce of T. Option class can hold a type of Some or None based on given source.

.k(name: string)

type User = {
  name: string;
  sns: SNS;
  followers: User[];
};

type SNS = {
  twitter?: {
    username: string;
  };
  facebook?: {
    url: string;
  };
};

const user: User = {
  name: "yayoc",
  sns: {
    twitter: {
      username: "@yayoc_"
    }
  },
  followers: []
};
const optionalUser = optional(user);
optionalUser.k("name"); // Option<string>
optionalUser.k("sns"); // Option<{twitter: { username: string }}>
optionalUser.k("sns").k("facebook"); // None
optionalUser.k("foo"); // compile error

Returns a Option<T> narrowed by specified property of Object.

.i(index: number)

Returns a Option<T> narrowed by specified index of Array. If index is not in array, this returns Option<undefined>.

optionalUser
  .k("followers")
  .i(0)
  .k("name"); // None

.get()

Returns a value of Option.

optionalUser.k("name").get(); // yayoc

.match({ some: T => any, none: T => any })

A public method of Option to do pattern matching. If target Option is Some type, this funciton returns a result of given some function. Otherwise, this function returns a result of given none function.

optionalUser
  .k("sns")
  .k("twitter")
  .match({
    some: v => v,
    none: v => `there is no account: ${v}`
  }); // @yayoc_

.getOrElse(value: any)

A public method to return T value when the instance contains some value. Otherwise, this function will return given value.

optionalUser
  .k("sns")
  .k("facebook")
  .k("url")
  .getOrElse("https://facebook.com"); // https://facebook.com

Credits

This library is highly inspired by lens.ts @utatti created.

License

MIT