Skip to content

Instantly share code, notes, and snippets.

@gitdagray
Created August 11, 2021 19:30
Show Gist options
  • Save gitdagray/229fc6e00e998b7f6c76f49d9b6252ca to your computer and use it in GitHub Desktop.
Save gitdagray/229fc6e00e998b7f6c76f49d9b6252ca to your computer and use it in GitHub Desktop.
Using multiple decorators
// Our basic function
let rectangleArea = (length, width) => {
return length * width;
}
// A decorator that counts the parameters
const countParams = (fn) => {
return (...params) => {
console.log('countParams')
if (params.length !== fn.length) {
throw new Error(`Incorrect number of parameters for ${fn.name}`);
}
return fn(...params);
}
}
// A decorator that requires integers
const requireIntegers = (fn) => {
return (...params) => {
console.log('requireIntegers')
params.forEach(param => {
if (!Number.isInteger(param)) {
throw new TypeError(`Params must be integers`);
}
});
return fn(...params);
}
}
// applied first makes it runs last
rectangleArea = countParams(rectangleArea);
// applied last makes it runs first
rectangleArea = requireIntegers(rectangleArea);
//console.log(rectangleArea(20, 30, "Dave")) //caught 1st by integers error
//console.log(rectangleArea(20, 30, 40)); //number of parameters error
console.log(rectangleArea(20, 30)); //no error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment