1.1.7 • Published 7 months ago

oloader v1.1.7

Weekly downloads
-
License
ISC
Repository
-
Last release
7 months ago

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}
1.1.7

7 months ago

1.1.5

7 months ago

1.1.2

7 months ago

1.0.1

7 months ago

1.0.0

7 months ago