I have a function :
function __throwError(func) {
if (func.length === 1) {
function passNumber() {
func(0);
}
function passString() {
func("item");
}
function passEmptyArray() {
func([]);
}
function passUndefinedOrNull() {
func(undefined || null);
}
expect(passNumber).toThrowError("The parameter should be an array");
expect(passString).toThrowError("The parameter should be an array");
expect(passEmptyArray).toThrowError("The array is empty");
expect(passUndefinedOrNull).toThrowError("The parameter is null or undefined");
}
if (func.length === 2) {
function passNumber() {
func(0, 1);
}
function passString() {
func("item", 1);
}
function passEmptyArray() {
func([], 1);
}
function passUndefinedOrNull() {
func(undefined || null, 1);
}
expect(passNumber).toThrowError("The parameter should be an array");
expect(passString).toThrowError("The parameter should be an array");
expect(passEmptyArray).toThrowError("The array is empty");
expect(passUndefinedOrNull).toThrowError("The parameter is null or undefined");
}
}
You may notice that there are duplicated passNumber,passString,passEmpty inside different if statements and each function call different callback function func.
How do I remove duplicated functions: passNumber,passString,passEmpty and just have once call with different parameters ?
(func.length === 3)can you provide it? \$\endgroup\$expect(passString).toThrowError("The parameter should be an array")beexpect(passString).toThrowError("The parameter should be a sting")("a string" instead of "an array")? \$\endgroup\$