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

oloader

v1.1.7

Published

Overloading functions on JS

Downloads

366

Readme

Implementing overloading function on JS

v1.1.2 important news:

  • Fixing a boolean value
  • Can be implemented on Objects
  • Can be implemented on Classes

v1.1.5 what's new:

  • Handling invalid parameter on overloading funciton
  • Class name is a valid type

v1.1.7 update:

  • fixing Object type issue
  • provided CommonJS usage

How to use

import this package into your project:

// install first

//ESM
import def from "oloader";

//CommonJS
const def = require("oloader");

define your function inside def, it takes 3 parameters:

  1. the function (with name)
  2. object counting variabels types
  3. the caller context (optional) the default is this
// def(fn, counterTypes,caller)
def(
  function example(data) {
    console.log(data);
  },
  { string: 1 }
);

def(function example() {
  console.log("nothing here!");
}, {}); // no param

def(
  function example(a, b) {
    console.log("result = " + (a + b));
  },
  { number: 2 }
); // 2 var

example("welcome!");

example();

see more example on the file: example.js

How to know the types?

just use typeof

const anyVarHere = 10;

console.log(typeof anyVarHere); //number

Function scoped

function main() {
  def(
    function dummy(...data) {
      console.log(JSON.stringify(data));
    },
    { number: 3 }
  );

  dummy(10, 24, 31); // output is [10,24,31]
}

main();

Object usage

Just need to including the Object you want to use in the 3rd parameter of def

const enemy = {
  str: 40,
  hp: 320,
  def: 400,
  spd: 10,
};

def(
  function attack(...data) {
    const [launchedDmg, receivedDmg] = data;

    console.log("hp -" + receivedDmg + " point");
    console.log("inflicted " + launchedDmg + " damage");
  },
  { number: 2 },
  enemy
); // Specify the caller as 'enemy' / the Object you want to use

def(
  function attack(...data) {
    const [launchedDmg, namedSkill] = data;

    console.log("Using a skill: " + namedSkill);
    console.log("inflicted " + launchedDmg + " damage");
  },
  { number: 1, string: 1 },
  enemy
); // remember to included the caller or function will be global function

enemy.attack(24, 30);

enemy.attack(100, "Void Strike!");

Class usage

You can call def from constructor or any method the class has so we can have this refering to the Object from the class

  • still need to explicitly the caller as this for some reasons

  • class name is valid type example:

class Heroes {}

//CountType
{"heroes":1} //valid
class Heroes {
  constructor(name) {
    console.log(`hero ${name} summoned`);

    def(
      function attack(...data) {
        console.log(`hero ${name} inflicted ${data[0]} damage to enemy`);
      },
      { number: 1 },
      this
    );

    def(
      function attack(data) {
        console.log(
          `hero ${name} ${
            data ? "dodge an enemy attack" : "the enemy dodge the attack"
          }`
        );
      },
      { boolean: 1 },
      this
    );
  }
}

const Yamahiko = new Heroes("Yamahiko");

Yamahiko.attack(400);
Yamahiko.attack(true);
Yamahiko.attack(false);

enemy.attack(Yamahiko); // parameter {"heroes":1}