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 🙏

© 2026 – Pkg Stats / Ryan Hefner

typeproto

v0.1.0

Published

A prototype library for JavaScript types

Readme

typeproto

A prototype handling javascript library. Features: classes, enums and traits.

How to use

In browser:

Copy the file lib/typeproto.js to your web server. Then, use the global tp object.

In node.js:

Type into the terminal: npm install typeproto. Then, use the typeproto module.

Reference

tp.Class

Type

function/pseudo-constructor

Usage

// In node.js, uncomment the line below
// var tp = require("typeproto");
var MyClass = tp.Class({
  //options
});

Options reference

parent (optional)

type: function/constructor (optional)

The class's parent

constructor (optional)

type: function (optional)

The class's constructor function

prototype (optional)

type: object (optional)

The prototype's properties. The constructor of the class can be passed as a property of this object.

statics (optional)

type: object (optional)

The static members of the class.

objects especial members

__super__

A function that calls the parent's constructor on the current object. Only exists if the class has a parent. Example:

// In node.js, uncomment the line below
// var tp = require("typeproto");
var Parent = tp.Class({
  constructor: function (name) {
    this.name = name;
  },
});
var Child = tp.Class({
  constructor: function () {
    this.__super__("Joe");
  },
});

tp.Trait

Type

function/constructor

Usage

// In node.js, uncomment the line below
// var tp = require("typeproto");
var MyTrait = new tp.Trait("MyTrait", {
  //methods
});

MyTrait.implementFor(someObj, {
  //methods
});

console.log(Stack.validate(someObj));

Class Reference

tp.Trait.prototype.constructor

Arguments: (name: string, methods: object)

Example Usage
// In node.js, uncomment the line below
// var tp = require("typeproto");
var Stack = new tp.Trait("Stack", {
  push: null, //abstract method
  pop: null, //abstract method
  truncate: function () {
    while (this.pop() !== undefined);
  }, // default method
});

tp.Trait.prototype.methods

type: map-like object

tp.Trait.prototype.name

type: string

tp.Trait.prototype.fromParent

Arguments: (parent: tp.Trait)

Return value: tp.Trait (this)

Inherits from a parent trait

tp.Trait.prototype.validate

Arguments: (tested: object)

Return value: boolean

Tests if an object implements the trait

tp.Trait.prototype.implementFor

Arguments: (implemented: object, methods: object)

Return value: object (implemented)

Implements the default methods and the passed methods via argument. Doesn't override the object's methods.

tp.Enum

Type

function/constructor

Usage

// In node.js, uncomment the line below
// var tp = require("typeproto");
var MyEnum = new tp.Enum({
  //values
});

console.log(MyEnum.validate(MyEnum.SOME_VALUE));

Class Reference

tp.Enum.prototype.constructor

Arguments: (values: object)

Example Usage
// In node.js, uncomment the line below
// var tp = require("typeproto");
var Direction = new tp.Enum({
  LEFT: {},
  CENTER: {},
  RIGHT: {},
});

console.log(Direction.LEFT);

tp.Enum.prototype.keyOf

Arguments: (value: object)

Return value: string

tp.Enum.prototype.validate

Arguments: (value: object)

Return value: boolean (whether value is part of the enum or not)

tp.Enum.prototype.eq

Arguments: (x: object, y: object)

Return value: boolean (whether the objects are the same for the enum)

tp.Enum.prototype.keys

Arguments: ()

Return value: array (of string)

Serialization-proof

// In node.js, uncomment the line below
// var tp = require("typeproto");
var Direction = new tp.Enum({
  LEFT: {},
  CENTER: {},
  RIGHT: {},
});

Direction.eq(JSON.parse(JSON.stringify(Direction.LEFT)), Direction.LEFT);

Full example:


var assert = require("assert");
var tp = require("./");

var GrandParent = new tp.Class({
  prototype: {
    field: 5,
  },
});



var Parent = new tp.Class({
  parent: GrandParent,
  constructor: function (field) {
    this.field = field;
  },
});


var Child = new tp.Class({
  parent: Parent,
  prototype: {
    constructor: function () {
      this.__super__(3);
    },
    sayHello: function () {
      return Child.MESSAGE + this.field;
    },
  },
  statics: {
    MESSAGE: "My field is ",
  },
});

assert(!(new Parent instanceof Child));
assert(new Child instanceof Child);
assert(new Child instanceof Parent);
assert(new Child instanceof GrandParent);
assert(new Child().sayHello() == "My field is 3");

var MyEnum = new tp.Enum({
  FOO: {
    toString: function () {
      return "Foo";
    },
  },
  BAR: {
    toString: function () {
      return "Bar";
    },
  },
});

assert(MyEnum.validate(MyEnum.FOO));
assert(!MyEnum.validate({}));
assert(MyEnum.eq(MyEnum.FOO, MyEnum.FOO));
assert(!MyEnum.eq(MyEnum.BAR, MyEnum.FOO));
assert.deepEqual(MyEnum.keys(), ["FOO", "BAR"]);


var ToString = new tp.Trait("ToString", {
  toString: null,
});

var Encodable = new tp.Trait("Encodable", {
  encode: null,
  asEncodedArray: function () {
    return this.encode().split().map(function (val) {
      return val.charCodeAt(0);
    });
  },
}).fromParent(ToString);


Encodable.implementFor(Child.prototype, {
  encode: function () {
    return String.fromCharCode(this.field);
  },
});


assert(Encodable.validate(new Child));
assert(!Encodable.validate(new Parent));

console.log("Success")