Drops undefined arguments from the end of a function call.
Useful if you’re forwarding arguments from one function to another, but the second function has behavior which is affected by the presence of an explicit undefined
argument.
Accepts an arguments list, just like Function.prototype.call()
. If you want to provide an argument array instead, use the trim-apply
module.
Requires Node.js 6.0.0 or above.
npm i trim-call
The module exports a function (trimCall()
) that has one other function attached to it as a method (trimCall.new()
).
fn
(function): The function to call.thisArg
(any): The value ofthis
while the function is being called.- Variadic:
...args
(one or more of: any): The arguments for the function call. Anyundefined
arguments at the end will be dropped.
The return value of fn
when called with thisArg
and args
.
Cls
(class): The class whose constructor you want to call.- Variadic:
...args
(one or more of: any): The arguments for the constructor call. Anyundefined
arguments at the end will be dropped.
A new instance of Cls
constructed with args
.
const trimCall = require('trim-call')
f1('test')
function f1 (a, b) {
trimCall(f2, this, a, b)
}
function f2 () {
arguments.length // 1
}
Because of trimCall()
, the f2()
function only receives one argument.
Here is the above example repeated without trimCall()
:
f1('test')
function f1 (a, b) {
f2.call(this, a, b)
}
function f2 () {
arguments.length // 2
}
Without trimCall()
, the undefined b
argument of f1()
becomes an explicit second argument for f2()
.