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

tagval

v1.0.0

Published

A simple JavaScript library includes Option class, Status class and their basic class of Tagged Value.

Downloads

34

Readme

TagVal (tagval.js) Build Status

Tagged union, discriminated union, variant or sum type.. for JavaScript.

Example

JavaScript

var T = require('tagval');

// with underscore.js
var nums = [3, 5, 6];
function is_even(x){ return x%2 === 0; }
function is_gt_10(x){ return (x > 10); }

var optional_x = T.Option.fromValue(_.find(nums, is_even));
var optional_y = T.Option.fromValue(_.find(nums, is_gt_10));

optional_x.match({
  Some: function(x){ console.log("I got "+x); },
  None: function(){ console.log("nothing"); }
});  // output: I got 6

var y = optional_y.getOrElse(10);
console.log(y); // 10

Coffee Script

T = require 'tagval'

# with underscore.js
nums = [3, 5, 6]
is_even = (x)-> x%2 is 0
is_gt_10 = (x)-> (x > 10)

optional_x = T.Option.fromValue _.find(nums, is_even)
optional_y = T.Option.fromValue _.find(nums, is_gt_10)

optional_x.match
  Some: (x)-> console.log "I got #{x}"
  None:    -> console.log "nothing"
# output: I got 6

y = optional_y.getOrElse 10
console.log y # 10

TagVal.Option class

Option is a class which expresses "something x"(Some) or "nothing"(None).

Example

// Finding a number from collection.
var optionalX = find_num(); // assume it returns `Option`
var DEFAULT_X = 30;

// Try to set the found number or 30 by default.
var x = optionalX.getOrElse(DEFAULT_X);

API

| method | detail | |----|----| | TagVal.Some(v) | creates a new Option object tagged Some with v in the value. | | TagVal.None() | creates a new Option object tagged None. | | TagVal.Option.fromValue(v) | creates Some(v) if v is neither undefined nor null. | | TagVal.optionFrom(v) | alias of TagVal.Option.fromValue(v) | | TagVal.fromBool(v) | If v is truthy, it returns Some(true) | | opt.map(f) | Some(x) to Some(f(x)) / nothing to None() | | opt.getOrElse(x) | Some(y) to y / None() to x | | opt.getOrElseF(f) | Some(y) to y / None() to f() | | opt.toArray() | Some(y) to [y] / None() to [] | | opt.toStatus(msg) | Some(v) to Success(v) / None() to Failure(msg) | | opt.toValue() | Some(v) to v / None() to undefined | | opt.equal(y) | If opt=Some(x) and y=Some(y) then x === y, and true if both are None(), otherwise false. | | opt.mapEqual(y, f) | If opt=Some(x) and y=Some(y) then f(x, y), and true if both are None(), otherwise false. | | opt.match | see Matchable | | opt.when | see Matchable | | opt.toString | see Matchable |

TagVal.Status class

Status object is a class that express "success with result"(Success) or "failure with message"(Failure).

API

| method | detail | |----|----| | TagVal.Status.trying(f) | Success(f()) or Failure(e) if caught an exception e. | | TagVal.withTry | alias of TagVal.Status.trying| | stat.getOrThrow() | Success(v) to v / throw msg if Failure(msg)| | stat.toOption() | Success(v) to Some(v) / Failure(msg) to None() | | stat.match | see Matchable | | stat.when | see Matchable | | stat.toString | see Matchable | | stat.equal | see Matchable |

TagVal.Matchable class

TagVal's basic concept is regarding {tag: String, val: Value} as minimum tagged-value interface. Matchable has actually only this two fields, and they are initialized simply:

var value = "some value"
var tv = new TagVal.Matchable("Tag", value); // I often express as "Tag(value)"

console.log(tv.tag); // Tag
console.log(tv.val); // value

In addition, Matchable has a few, generic methods below. You can inherit it. Option and Status are subclasses of Matchable.

API

| method | detail | |----|----| | tv.match(table[, default_fun]) | table[T](v) if table[T] exists. Next, default_fun(v) if default_fun exists. Otherwise undefined. | | tv.when(table) | table[T](v) if table[T] exists, otherwise returns object itself. | | tv.toString() | Tag( value.toString() ) | | tv.equal(y) | tv.valEqual(y) if same tag, otherwise false | | tv.valEqual(y) | tv.val === y.val | | TagVal.match(tv)(table) | match method for arbitrary { tag: Tag, value: Value } |

Matchable#when example:

In JavaScript:

x_stat = try_something1()
  .when({ Failure: function(){ return try_something2(); } })
  .when({ Failure: function(){ return try_something3(); } })
  .when({
    Failure: function(){ returns TagVal.Failure("All tries failed."); }
  });

In CoffeeScript:

x_stat = try_something1()
  .when Failure: -> try_something2()
  .when Failure: -> try_something3()
  .when Failure: -> TagVal.Failure "All tries failed."