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

typedinterface

v0.1.0

Published

TypedInterface allows you to define the parameter types and return type that a function or method must accept.

Downloads

5

Readme

TypedInterface

TypedInterface allows you to define the parameter types and return type that a function or method must accept.

Usage

defineFunction

Used for wrapping functions within a given context. You can pass in the local context this or even window for globals.

Arguments

  • ctx the context (or environment, or namespace) where the function is defined.
  • name the name of the function
  • argTypes ordered array of types that the function must accept
  • rType The return value type

Example

Interface.defineFunction(this, 'renderTasks', [Array], String);
function renderTasks(tasks) {
  return "Tasks: " + tasks.join(", ");
}

try {
  renderTasks(1, 2);
} catch (e) {
  console.error(e.toString());
  // TypeError: Mismatched argument lengths: got 2, wanted 1 
}

defineMethod

Used for wrapping methods of a prototype. This allows you to define the types for each method of the prototype, and then use the extend method to adopt the same definitions for each instance of that prototype. Check out the example for a better explanation.

Arguments

  • cls the class/prototype reference
  • name the name of the method
  • argTypes ordered array of types that the function must accept
  • rType The return value type

Example

// Define our Interface prototype that various other 'classes' will implement
var ServiceClass = function(){};
ServiceClass.prototype.login = function() {}; // no need to actually write logic here
ServiceClass.prototype.getInputFields = function() {};

Interface.defineMethod(ServiceClass, 
  "login", [optional(Object), optional(Function)], undefined);

Interface.defineMethod(ServiceClass, 
  "getInputFields", [Object, String, Function], undefined);

// Example of a class that implements the above defined methods
var SomeService = function(){};
SomeService.prototype.login = function(data, cb) {
  [...]
};
SomeService.prototype.getInputFields = function(data, site, cb) {
  [...]
};

// Extend it! This will adopt the same type checks as the ServiceClass. 
// Any method that SomeService does not implement will fallback to ServiceClass.
// This does not create new wrappers, rather relies on the CACHED wrappers 
// for ServiceClass.
SomeService = Interface.extend(ServiceClass, SomeService);

// And instantiate as you normally would:
var SomeServiceInstance = new SomeService();

Exception Examples


var myTasks = [
  'Goto park',
  'Bake bread',
  'Win lottery'
];

var renders = {
  tasks: function(tasks) {
    return "Tasks: " + tasks.join(", ");
  },
  badTasks: function(tasks) {
    return tasks;
  }
};
Interface.defineFunction(renders, 'tasks', [Array], String);
Interface.defineFunction(renders, 'badTasks', [Array], String);


try {
  renders.tasks(true);
} catch (e) {
  console.error(e.toString());
  // TypeError: Mismatched argument 1: got boolean, wanted function Array() { [native code] } for undefined() 
}

try {
  renders.tasks(myTasks, true);
} catch (e) {
  console.error(e.toString());
  // TypeError: Mismatched argument lengths: got 2, wanted 1 
}

try {
  renders.tasks();
} catch (e) {
  console.error(e.toString());
  // TypeError: Mismatched argument lengths: got 0, wanted 1 
}

try {
  renders.badTasks(myTasks);
} catch (e) {
  console.error(e.toString());
  // Mismatched return value (got: object, wanted: function String() { [native code] }) 
}

// yay!
var out = renders.tasks(myTasks);
console.debug(out);

Credits

The original idea and proof was the work of the rather illustrious Lewis Zimmerman.