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

sanctuary-type-identifiers

v4.0.0

Published

Specification for type identifiers

Downloads

294,690

Readme

sanctuary-type-identifiers

A type is a set of values. Boolean, for example, is the type comprising true and false. A value may be a member of multiple types (42 is a member of Number, PositiveNumber, Integer, and many other types).

In certain situations it is useful to divide JavaScript values into non-overlapping types. The language provides two constructs for this purpose: the typeof operator and Object.prototype.toString. Each has pros and cons, but neither supports user-defined types.

sanctuary-type-identifiers comprises:

  • an npm and browser -compatible package for deriving the type identifier of a JavaScript value; and
  • a specification which authors may follow to specify type identifiers for their types.

Specification

For a type to be compatible with the algorithm:

  • every member of the type MUST have a @@type property (the type identifier); and

  • the type identifier MUST be a string primitive and SHOULD have format '<namespace>/<name>[@<version>]', where:

    • <namespace> MUST consist of one or more characters, and SHOULD equal the name of the npm package which defines the type (including scope where appropriate);

    • <name> MUST consist of one or more characters, and SHOULD be the unique name of the type; and

    • <version> MUST consist of one or more digits, and SHOULD represent the version of the type.

If the type identifier does not conform to the format specified above, it is assumed that the entire string represents the name of the type; namespace will be null and version will be 0.

If the version is not given, it is assumed to be 0.

Usage

const type = require ('sanctuary-type-identifiers');
> const Identity$prototype = {
.   '@@type': 'my-package/Identity@1',
.   '@@show': function() {
.     return 'Identity (' + show (this.value) + ')';
.   },
. }

> const Identity = value => (
.   Object.assign (Object.create (Identity$prototype), {value})
. )

> type (Identity (0))
'my-package/Identity@1'

> type.parse (type (Identity (0)))
{namespace: 'my-package', name: 'Identity', version: 1}

API

type :: Any -⁠> String

Takes any value and returns a string which identifies its type. If the value conforms to the specification, the custom type identifier is returned.

> type (null)
'Null'

> type (true)
'Boolean'

> type (Identity (0))
'my-package/Identity@1'

type.parse :: String -⁠> { namespace :: Nullable String, name :: String, version :: Number }

Takes any string and parses it according to the specification, returning an object with namespace, name, and version fields.

> type.parse ('my-package/List@2')
{namespace: 'my-package', name: 'List', version: 2}

> type.parse ('nonsense!')
{namespace: null, name: 'nonsense!', version: 0}

> type.parse (type (Identity (0)))
{namespace: 'my-package', name: 'Identity', version: 1}