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

operator-overloadjs

v1.0.1

Published

Operator overload for Javascript

Readme

operator-overload.js

operator-overload.js adds overloading to javascript. It can be written in a simple notation and can be converted by the built-in transpiler to work in vanilla javascript.

Note: This text was translated using the DeepL translation. The translation may not be perfect.

Installation

shell :

$ npm install --save operator-overloadjs

package.json :

"scripts": {
    "build": "overload -d ./src -o ./dist", // To transpile the entire directory
    "transpile": "overload" // To transpile a single file
},
"type": "module"

How to Use

In this example, the '+' operator of the customArray class is overloaded to allow easier string concatenation than using the concat method.

src/customArray.js :

import _overload from "operator-overloadjs";
class CustomArray extends Array {
    [_overload.plus](a) {
        return this.concat(a);
    }
}
const array_a = new CustomArray();
const array_b = new CustomArray();
array_b.push(1, 1, 3);
array_a.push('a', 'b', 'c');
console.log(array_a + array_b);

Transpile the source code.

$ npm run build

Alternatively, you can specify the file name directly to transpile it.

$ npm run transpile -- ./src/customArray.js -o ./dist/customArray.js

The transpiled file will be output.

dist/customArray.js :

import _overload from "operator-overloadjs";
class CustomArray extends Array {
    [_overload.plus](a) {
        return this.concat(a);
    }
}
const array_a = new CustomArray();
const array_b = new CustomArray();
array_b.push(1, 1, 3);
array_a.push('a', 'b', 'c');
- console.log(array_a + array_b);
+ console.log((array_a)[_overload.plus](array_b));

Let's run it.

$ node ./dist/customArray.js
CustomArray(6) [ 'a', 'b', 'c', 1, 1, 3 ]

It worked.

Note: In this example, we have overloaded the customArray class we created, not the Array class. Therefore, if you want to overload the Array class, write the following

Object.defineProperty(Array.prototype, _overload.plus, {
   value: function(a) {
       return this.concat(a);
   }
});

The following is a list of operators that can be overloaded as in the previous example. |Operator|Property identifier|Number of arguments (not including 'this')| |:-:|:-:|:-:| |a + b|_overload.plus|1| |a - b|_overload.minus|1| |a * b|_overload.multiply|1| |a / b|_overload.divide|1| |a % b|_overload.modulus|1| |+ a|_overload.unaryplus|0| |- a|_overload.unaryminus|0| |~ a|_overload.bitnot|0| |a & b|_overload.bitand|1| |a \| b|_overload.bitor|1| |a ^ b|_overload.bitxor|1| |! a|_overload.not|0| |a && b|_overload.and|1| |a \|\| b|_overload.or|1| |a == b|_overload.equal|1| |a != b|_overload.notequal|1| |a === b|_overload.identityequal|1| |a !== b|_overload.identitynotequal|1| |a[b]|_overload.index|1|

Note: 'this' refers to 'a' and the first argument refers to 'b'.

Please help us find the bugs

operator-overload.js may contain a number of bugs. If you find a bug, please contact us in Issues.