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

irrelon-overload

v1.0.3

Published

Simple JavaScript type-based method overloading.

Downloads

58

Readme

Overload

Simple JavaScript Method Overloading

Usage

You can create a new function that takes overloaded calls by definining what those calls should accept by type.

Here is an example of usage:

var user = new Overload({
	// Get user name and data
	"": function () {
		return {
			"name": this._name,
			"data": this._data
		};
	},

	// Set user name
	"string": function (name) {
		this._name = name;
	},

	// Set user data
	"object": function (data) {
		this._data = data;
	},

	// Set user name and data
	"string, object": function (name, data) {
		this._name = name;
		this._data = data;
	},

	// Take arbitrary number of exta arguments
	"string, object, ...": function () {
		// Can access arguments through the "arguments" object
		console.log(arguments);
	},

	// Take any type as an argument
	"string, *, object": function () {
		console.log(arguments);
	}
});

Shared Main Method

Your overloaded functions support calling a primary function that will handle all variations of your funtion signatures. This allows you to write a single piece of logic that all overloaded functions can call to reduce code replication.

Here is an example from ForerunnerDB's Collection class:

Core.prototype.collection = new Overload({
	/**
	 * Get a collection by name. If the collection does not already exist
	 * then one is created for that name automatically.
	 * @param {Object} options An options object.
	 * @returns {Collection}
	 */
	'object': function (options) {
		return this.$main.call(this, options);
	},

	/**
	 * Get a collection by name. If the collection does not already exist
	 * then one is created for that name automatically.
	 * @param {String} collectionName The name of the collection.
	 * @returns {Collection}
	 */
	'string': function (collectionName) {
		return this.$main.call(this, {
			name: collectionName
		});
	},

	/**
	 * Get a collection by name. If the collection does not already exist
	 * then one is created for that name automatically.
	 * @param {String} collectionName The name of the collection.
	 * @param {String} primaryKey Optional primary key to specify the primary key field on the collection
	 * objects. Defaults to "_id".
	 * @returns {Collection}
	 */
	'string, string': function (collectionName, primaryKey) {
		return this.$main.call(this, {
			name: collectionName,
			primaryKey: primaryKey
		});
	},

	/**
	 * Get a collection by name. If the collection does not already exist
	 * then one is created for that name automatically.
	 * @param {String} collectionName The name of the collection.
	 * @param {Object} options An options object.
	 * @returns {Collection}
	 */
	'string, object': function (collectionName, options) {
		options.name = collectionName;

		return this.$main.call(this, options);
	},

	/**
	 * Get a collection by name. If the collection does not already exist
	 * then one is created for that name automatically.
	 * @param {String} collectionName The name of the collection.
	 * @param {String} primaryKey Optional primary key to specify the primary key field on the collection
	 * objects. Defaults to "_id".
	 * @param {Object} options An options object.
	 * @returns {Collection}
	 */
	'string, string, object': function (collectionName, primaryKey, options) {
		options.name = collectionName;
		options.primaryKey = primaryKey;

		return this.$main.call(this, options);
	},

	/**
	 * The main handler method. This get's called by all the other variants and
	 * handles the actual logic of the overloaded method.
	 * @param {Object} options An options object.
	 * @returns {*}
	 */
	'$main': function (options) {
		var name = options.name;

		if (name) {
			if (!this._collection[name]) {
				if (options && options.autoCreate === false) {
					throw('ForerunnerDB.Core "' + this.name() + '": Cannot get collection ' + name + ' because it does not exist and auto-create has been disabled!');
				}

				if (this.debug()) {
					console.log('Creating collection ' + name);
				}
			}

			this._collection[name] = this._collection[name] || new Collection(name).db(this);

			if (options.primaryKey !== undefined) {
				this._collection[name].primaryKey(options.primaryKey);
			}

			return this._collection[name];
		} else {
			throw('ForerunnerDB.Core "' + this.name() + '": Cannot get collection with undefined name!');
		}
	}
});

Notice that the defined $main function can be referenced from each of the overloaded functions via this.$main().