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

funcify

v1.0.1

Published

Super funky function overloading

Downloads

13

Readme

funcify.js: Super funky function overloading

Funcify is a straight forward, easy to use function overloading library written in JavaScript that works in browsers and JS backends.

It allows you to easily create overloaded functions that perform different actions based on the number and types of arguments.

Installing

To install funcify, either:

Install via NPM

npm install funcify

Install via Bower

bower install funcify

Download a release

https://github.com/garydouble/funcify/releases

Getting started

Each overload is defined as an Array of parameter types (filters) and a callback.

The general signature looks like this

funcify(function|array, ...): function

The funcify function returns a new function that handles the overloading.

Here are some examples:

// fn will be an overloaded function.
var fn = funcify(
  
  // fn()
  function() {
    // This is a "catch-all" function and
    // will be called if no specific override exists.
  },

  // fn(string)
  ['string', function(someString) {
    doSomethingWith(someString);
  }],
  
  // fn(*, int)
  ['any', 'int', function(o, i) { /* ... */ }]
);

Built-in type checking filters

Wildcard / Any type filter

'*' or 'any'

Allows anything and everything, except undefined.

String filter

's' or 'string' or /regexp/

Validates the parameter is a String.

var fn = funcify(['string', function(str) { /* ... */ }]);

// yea
fn('hello');

// nay
fn(123);

When a /regexp/ is provided an additional check will be made to ensure the parameter tests positive against the pattern.

var fn = funcify([/^[abc]$/, function(str) { /* ... */ }]);

// yea
fn('a');
fn('b');
fn('c');

// nay
fn('xyz');
fn('abc');

Function filter

'f' or 'function'

Validates the parameter is a Function.

var fn = funcify(['function', function(func) { /* ... */ }]);

// yea
fn(function() {});
fn(myPreDefinedFunction);

// nay
fn('hello');

Object filter

'o' or 'object'

Validates the parameter is an Object literal, explicitly.

var fn = funcify(['object', function(obj) { /* ... */ }]);

// yea
fn({});

// nay
fn('hello');
fn([]);
fn(new Date());

Array filter

'a' or 'array'

Validates the parameter is an Array or "Array-like".

var fn = funcify(['array', function(arr) { /* ... */ }]);

// yea
fn([1, 2, 3]);
fn(new Float32Array())
fn(arguments)

// nay
fn('hello');
fn({});

RegExp filter

'r' or 'regexp'

Validates the parameter is a RegExp.

Not to be confused with pattern matching, for that see the String filter.

var fn = funcify(['regexp', function(rx) { /* ... */ }]);

// yea
fn(/abc/);
fn(new RegExp());

// nay
fn('hello');
fn({});

Boolean filter

'b' or 'boolean'

Validates the parameter is either true or false.

var fn = funcify(['boolean', function(b) { /* ... */ }]);

// yea
fn(true);
fn(false);

// nay
fn([]);
fn('true');
fn(0);

Date filter

'd' or 'date'

Validates the parameter is a Date.

var fn = funcify(['date', function(date) { /* ... */ }]);

// yea
fn(new Date());

// nay (sorry not yet)
fn(moment()); // 
fn('2017-12-17');
fn(5873242342);

Number filter

'n' or 'number'

Validates the parameter is a Number.

var fn = funcify(['number', function(n) { /* ... */ }]);

// yea
fn(123);
fn(4.56);
fn(NaN);
fn(Infinity);

Int filter

'int'

Validates the parameter does not equal NaN when passed through parseInt.

var fn = funcify(['int', function(i) { /* ... */ }]);

// yea
fn(123);
fn(4.56);

// nay
fn(NaN);
fn(Infinity);

Float filter

'float'

Validates the parameter does not equal NaN when passed through parseFloat.

var fn = funcify(['float', function(f) { /* ... */ }]);

// yea
fn(123);
fn(4.56);

// nay
fn(NaN);
fn(Infinity);

Class filter

Class

Validates the parameter is an instance of a particular class.

var fn = funcify([MyClass, function(inst) { /* ... */ }]);

var inst = new MyClass();

// yea
fn(inst);

Defining your own filters

Filters are defined as factory functions that return new functions that act upon a single argument parameter and return true or false depending on whether said parameter meets the specific filter requirements.

To define a custom filter simply attach a factory function to a property on the funcify.filters object.

// Register your shiney new filter.
funcify.filters['binary'] = function() {
  return function(arg) {
    return /^[01 ]+$/.test(arg);
  };
};

// Configure an overload that uses it
var fn = funcify(['binary', function(bitString) {
  return convertToAscii(bitString); // or whatever!
}]);

Once a new filter has been defined you can then refer to it in your signature specifications.

// Call your function with the new override
var howCoolIsThis = fn('01000101 01010000 01001001 01000011');

Issues and Contributions

https://github.com/garydouble/funcify/issues

Issues, feedback and pull requests all welcome.
Happy Coding!