src/decorator.js   A
last analyzed

Complexity

Total Complexity 4
Complexity/F 1.33

Size

Lines of Code 9
Function Count 3

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 7
mnd 1
bc 1
fnc 3
dl 0
loc 9
rs 10
bpm 0.3333
cpm 1.3333
noi 1
c 0
b 0
f 0
1
/**
2
 * @callback Validator {(...args: any) => boolean}
3
 */
4
5
/**
6
 * A decorator configurable to validate the param type of a function
7
 *
8
 * @param {object} config - config options
9
 * @param {Validator} config.validator - The validation method
10
 * @returns {Function} - The decorated
11
 */
12
const inputValidation =
13
  ({ validator }) =>
14
  (action) =>
15
  (...args) => {
16
    if (validator(...args)) return action(...args);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
17
    throw Error('function validation failed');
18
  };
19
20
export default inputValidation;
21