
Research
Supply Chain Attack on Axios Pulls Malicious Dependency from npm
A supply chain attack on Axios introduced a malicious dependency, plain-crypto-js@4.2.1, published minutes earlier and absent from the project’s GitHub releases.
overload-js
Advanced tools
Function overloading in JavaScript for 3.9KB minified, 1.4KB gzipped.
Provides tools to mimic function overloading that is present in most strictly-types languages. Prevents messy, long, if-statement, type-checking functions that are hard to read and maintain. Style and API inspired by Moreiki and Mongoose.
npm install overload-js
var hello = (function() {
var secret = '!';
return overload()
.args().use(function() {
return secret;
})
.args(String).use(function(val) {
secret = val;
});
}());
hello('world'); // calls setter
hello(); // returns 'world'
hello(0); // throws a Type Error
null
undefined
Infinity
Date
NaN
Number
String
Object
Array
RegExp
Boolean
Function
Element // browser only
A map can be defined as an overload as well:
var hello = overload.map({
what: String
}).use(function(obj) {
return 'hello ' + obj.what;
});
hello({ what: 'world!' }); // returns 'hello world!'
hello('world'); // throws a Type Error
or a map can be used as a custom type (see below):
var hello = overload.args(String, o.map({
what: String
})).use(function(str, obj) {
return str + obj.what;
});
hello('hello', { what: 'world!' }); // returns 'hello world!'
hello('hello', 'world'); // throws a Type Error
A custom type can be defined by passing a string and validation function to
defineType. The validation function will be passed the value to validate
and expects a boolean return.
overload.defineType('$', function(val) {
return val instanceof jQuery;
});
Custom types are available under o.
var overload = require('overload-js'),
o = overload.o;
var method = overload().args(o.$).use(function($elem) {
console.log($elem);
});
method(); // fails
method(''); // fails
method($('body')); // succeeds
o.any() accepts multiple types that a parameter will match against.
var method = overload().args(o.any(String, Number)).use(function() {
console.log('passed!');
});
method(); // fails
method([]); // fails
method(''); // passed!
method(0); // passed!
The inverse of o.any is o.except.
var method = overload().args(o.except(Object)).use(function() {
console.log('passed!');
});
method(); // passed!
method([]); // passed!
method({}); // fails
Also available are o.truthy and o.falsy.
var method = overload()
.args(o.truthy).use(function() {
console.log('truthy');
})
.args(o.falsy).use(function() {
console.log('falsy');
});
method(); // fails
method(0); // falsy
method(1); // truthy
In addition to overloading by type, argument length can be used.
If a number is not passed, the function.length will be used.
var method = overload()
.len(0).use(function() {
console.log('0 args');
})
.len(1).use(function(a) {
console.log('1 arg');
})
.len().use(function(a, b, c) {
console.log('3 args');
});
method(); // '0 args'
method({}); // '1 arg'
method(null, [], {}); // '3 args'
alias: count, size
If args and length are used in the overload, args will be matched
first, followed by length.
A fallback function can be defined via the fallback method.
var method = overload().args(String).use(function(a) {
console.log(a);
})
.fallback(function() {
console.log('handled!');
});
method('hello'); // 'hello'
method(); // 'handled'
If a fallback is not defined and the exposed method is called without a matching function, an error will be thrown...
A clean function can be exposed so that overload properties and methods are hidden from the outside world.
var method = overload()
.args().use(function() {
return 'hi';
})
.expose();
// method.args === undefined
// method.use === undefined
// etc...
The error from unmatched calls can be handled by defining your own err method on overload or
by passing a function to handle the error.
overload.err = function() {
console.log('there was an error');
};
overload()
.error(function() { console.trace(); })
Node.js, modern browsers and IE8+
To run the tests, simply open test/index.html in your favorite browser or run npm test.
#License
The MIT License (MIT)
Copyright (c) 2014 Joseph Clay
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
Method overloading in JavaScript
The npm package overload-js receives a total of 1,422 weekly downloads. As such, overload-js popularity was classified as popular.
We found that overload-js demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Research
A supply chain attack on Axios introduced a malicious dependency, plain-crypto-js@4.2.1, published minutes earlier and absent from the project’s GitHub releases.

Research
Malicious versions of the Telnyx Python SDK on PyPI delivered credential-stealing malware via a multi-stage supply chain attack.

Security News
TeamPCP is partnering with ransomware group Vect to turn open source supply chain attacks on tools like Trivy and LiteLLM into large-scale ransomware operations.