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 🙏

© 2025 – Pkg Stats / Ryan Hefner

ovl

v1.0.2

Published

create overload function

Readme

Usage

const ovl = require("ovl").ovl
const func = ovl(overload_definition, default_function, option)
func(...any_arguments)

For the arguments, see the Example section

Examples

1

function isInt32(n){ return n === ~~n }
const obj = {a: 1}

const f1 = ovl([
	{
		type: ["number", "number", "number", "number"],
		this: obj,
		func(a, b, c, d){
			console.log(1, this, a, b, c, d)
		}
	},{
		type: ["string", "Date", "Array"],
		func: (a, b, c) => {
			console.log(2, a, b, c)
		}
	},{
		type: [isInt32, e => e > 100],
		func: (a, b) => {
			console.log(3, a, b)
		}
	},{
		type: (a, b, c) => a + b + c < 1,
		func: (a, b, c) => {
			console.log(4, a, b, c)
		}
	}
], (...args) => console.error("No matched function", args))


f1(1 ,2, 3, 4)
// 1 {a: 1} 1 2 3 4

f1("abc", new Date(), [100, 200])
// 2 "abc" 2017-03-25T06:48:20.127Z [ 100, 200 ]

f1(1, 2)
// No matched function [1, 2]

f1(1, 200)
// 3 1 200

f1(-10, -20, 30)
// 4 -10 -20 30

f1(10, 20, 30)
// No matched function [10, 20, 30]

2

const f2 = ovl({
	overloads: [
		{
			type: ["Array", "number"],
			func_name: "splitAt",
		},
		{
			type: ["Array"],
			func_name: "splitHalf",
		}
	],
	funcpool: {
		splitAt(arr, n){
			return [arr.slice(0, n), arr.slice(n)]
		},
		splitHalf(arr){
			return this.splitAt(arr, Math.ceil(arr.length / 2))
		},
	}
}, (...args) => console.error("No matched function", args))


console.log(f2([1, 2, 3, 4, 5, 6], 2))
// [ [ 1, 2 ], [ 3, 4, 5, 6 ] ]

console.log(f2([1, 2, 3, 4, 5]))
// [ [ 1, 2, 3 ], [ 4, 5 ] ]

3

const f3 = ovl([
	{
		type: ["number", "number", "number", "number"],
		this: obj,
		func(a, b, c, d){
			console.log(1, this, a, b, c, d)
			return "NNNN"
		}
	},{
		type: ["string", "Date", "Array"],
		func: (a, b, c) => {
			console.log(2, a, b, c)
			return "SDA"
		}
	},{
		type: [isInt32, e => e > 100],
		func: (a, b) => {
			console.log(3, a, b)
			return "IF"
		}
	},{
		type: (a, b, c) => a + b + c < 1,
		func: (a, b, c) => {
			console.log(4, a, b, c)
			return "XXX<1"
		}
	}
], (...args) => console.error("No matched function", args), {
	before: args => {console.log(args);return args},
	after: args => {console.log(args);return args},
	mode: "multi",
})


f3(-100, 200, -100, 200)
// [ -100, 200, -100, 200]          <-- before callback
// 1 {a: 1} -100 200 -100 200       <-- matched function
// 3 -100 200                       <-- matched function
// 4 -100 200 -100                  <-- matched function
// [ "NNNN", null, "IF", "XXX<1" ]  <-- after callback
// [ "NNNN", null, "IF", "XXX<1" ]  <-- return value

f3(100, 200)
// [ 100, 200]                 <-- before callback
// 3 100 200                   <-- matched function
// [ null, null, "IF", null ]  <-- after callback
// [ null, null, "IF", null ]  <-- return value

4

const f4 = ovl({
	overloads: [
		{
			type: ["Array", "number", "string"],
			func_name: "fn",
		},
		{
			type: {num: "number", arr: "Array", str: "string"},
			func_name: "fn2",
		}
	],
	funcpool: {
		fn(arr, num, str){
			return arr[num] === str
		},
		fn2({num, arr, str}){
			return this.fn(arr, num, str)
		},
	}
}, (...args) => console.error("No matched function", args))


console.log(f4(["a", "b", "c", "d"], 1, "b"))
// true

console.log(f4(["a", "b", "c", "d"], 3, "d"))
// true

console.log(f4(["a", "b", "c", "d"], 3, "b"))
// false

console.log(f4({num: 1, str: "def", arr: ["abc", "def", "ghi"]}))
// true

console.log(f4({num: 1, str: "xyz", arr: ["abc", "def", "ghi"]}))
// false

5

const f5 = ovl([
	{
		type: ["number?", "boolean"],
		func(a, b){
			console.log(1, a, b)
		}
	},{
		type: ["string|Date"],
		func: (d) => {
			console.log(2, new Date(d))
		}
	}
], (...args) => console.error("No matched function", args))


f5(1, true)
// 1 1 true

f5(null, true)
// 1 null true

f5(undefined, true)
// 1 undefined true

f5("2020-12-31")
// 2 2020-12-30T15:00:00.000Z

f5(new Date(2020, 11, 31))
// 2 2020-12-30T15:00:00.000Z

Matching

type: ["number", "number"]
arguments: [1, 2]
result: yes

type: ["number", "string"]
arguments: [1, 2]
result: no

type: ["number"]
arguments: [1, 2]
result: yes

type: ["number", function(x){ return x === 2 }]
arguments: [1, 2]
result: yes

type: ["number", function(x){ return x === 1 }]
arguments: [1, 2]
result: no

type: ["number", "number?"]
arguments: [1, null]
result: yes

type: ["number", "number"]
arguments: [1, null]
result: no

type: ["number", "number|string"]
arguments: [1, 1]
result: yes

type: ["number", "number|string"]
arguments: [1, "a"]
result: yes

type: function(a, b){return a === 1 && b === 2}
arguments: [1, 2]
result: yes

type: {a: "number", b: "string"}
arguments: [{a: 1, b: "a"}]
result: yes

type: {a: "number", b: "string"}
arguments: [{a: 1, b: 2}]
result: no

Change log

1.0.2 fix readme

1.0.1 add readme and fix meta data