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

single-key

v1.0.0

Published

Utilities for key-tagged values (tagged unions).

Downloads

18

Readme

single-key

Build Status

Utilities for key-tagged values (tagged unions).

API

NOTE: All functions (except isKeyTaggedValue) throw NonConformingError—a subtype of TypeError—when the argument is not a key-tagged value.

isKeyTaggedValue

isKeyTaggedValue(val: ?): Boolean

Returns true if the value conforms to the key-tagged value protocol. That is, it returns true if val:

  • is an object (note, an array counts as an object)
  • contains exactly one enumerable property name according to Object.keys() (note: symbols don't count as names, even if they are enumerable)

Otherwise, returns false.

let obj = { foo: 'bar' };
assert(isKeyTaggedValue(obj));

let obj = { foo: 'bar', 'herp': 'derp'};
assert(isKeyTaggedValue(obj) === false);

let obj = { [Symbol.iterator]: function*() { yield 'foo'; } };
assert(isKeyTaggedValue(obj) === false);

match

match(obj: Object, cases: Object, otherwise: function|undefined): ?

Similar to a switch statement on the key. Given a key-tagged value and an object of function, calls the function whose name matches the key. match() returns the result of the called function. An optional third parameter is called when no match can be made.

The provided functions (including the third parameter) are called with the value of the key-tagged value, and the key, respectively.

Throws MatchError when no callback is matched and no third parameter is given.

/* Standard match. */
let obj = { sypha: 'belnades' };
let value = match(obj, {
  trevor: () => 'vampire killer',
  sypha: () => 'magic',
  grant: () => 'daggers'
});

assert(value === 'magic');

/* No match found for the object. */
try {
  match(obj, {
    alucard: (val, key) => 'turning into a flippin bat'
  });
} catch (err) {
  assert(err instanceof MatchError);
}


/* Default provided to suppress MatchError. */
let value = match(obj,
  {
    alucard: (val, key) => 'turning into a flippin bat'
  },
  (val, key) => {
    return "No one knows.";
  }
);

assert(value === "No one knows.");

unpack

unpack(obj: Object): [key: String, value: ?]

Returns an array of two elements: the key and the value from the key-tagged value.

let [key, value] = unpack({ content: 42 });

assert(key === 'content');
assert(value === 42);

unpackObject

unpackObject(obj: Object): {key: String, value: ?}

Same as unpack, but returns an object with keys, key and value:

let {key, value} = unpackObject({ content: 42 });

assert(key === 'content');
assert(value === 42);