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

@ayte/optional

v0.1.3

Published

[![npm](https://img.shields.io/npm/v/@ayte/optional.svg?style=flat-square)](https://www.npmjs.com/package/@ayte/optional) [![CircleCI/Master](https://img.shields.io/circleci/project/github/ayte-io/ts-optional/master.svg?style=flat-square)](https://circlec

Downloads

11

Readme

@ayte/optional

npm CircleCI/Master Coveralls/Master

Installation

yarn add @ayte/optional
# - or -
npm install -S @ayte/optional

Usage

This package is Java Optional port with some additional features. Optional is a functor / monad that allows you to perform operations over identity without explicit null / undefined checking every time you need to perform an operation.

To create an Optional, you need to call one of static methods:

import {Optional} from '@ayte/optional';

// will eat anything you provide
const unknown = Optional.ofNullable(dunnoIfPresent);
// will throw if value is null or undefined
const present = Optional.of(value);
// just an empty optional
const empty = Optional.empty();

To operate over identity, you have .map() and .flatMap() methods, each returning another Optional:

const sum = Optional.of(2).map(value => value + value);
const product = Optional.of(2).flatMap(value => Optional.of(value).map(value * value));

This allows easy chained operations when identity may or may not be present, and when result of operations may be either value or null / undefined. Optional is immutable, so .map() and .flatMap() return new optionals:

const body = Optional.ofNullable(request.body)
    .map(JSON.parse);

// this will be empty optional both if body was absent and if token
// was absent in body 
const token = body.map(identity => identity.token)
const product = body
  .map(identity => identity.value)
  .flatMap(identity => Optional.ofNullable(multiplier).map(value => value * identity))

Filter allows you to pass predicate and turn optional to empty if identity doesn't satisfy condition:

const length = Optional.ofNullable(collection)
    .map(identity => identity.length)
    // if identity is less than zero, returned optional would be empty
    .filter(identity => identity >= 0);

Also you have two methods to rescue probably empty optional:

const optional = Optional.empty();
const fallback = optional.fallback(Optional.of(2));
// same as .fallback(), but takes in producer rather than prepared value
const rescued = optional.or(() => Optional.of(2));

You also have several methods to unwrap the value:

const optional = Optional.empty();
// will throw since identity is absent
const value = optional.get();
const either = optional.orElse(2);
const nullable = optional.orNull();
const produced = optional.orElseGet(() => 2);
const ensured = optional.orElseThrow(() => new Error('Assertion failed'));

To inspect current state, you can use .isPresent() / .isAbsent() methods. Also there are conditional methods for producing side effects:

const optional = Optional.empty();
Optional.empty()
    .ifPresent(value => console.log(value))
    .ifAbsent(() => console.log('nothing here'))
    .peek(value => console.log(value), () => console.log('nothing here'));

All those methods return current optional, which allows chaining as above.

That's pretty much it.

TypeScript Typings

Baked in, as well as source map.

Contribution

Send your PRs to dev branch.

Dev branch state

CircleCI/Dev Coveralls/Dev

Licensing

MIT / Ayte Labs, 2018

Promise to have fun using it.