Total Complexity | 7 |
Complexity/F | 1.4 |
Lines of Code | 29 |
Function Count | 5 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | /** |
||
9 | function Race (places) { |
||
10 | places = typeof places === 'number' ? places : 1 |
||
11 | var winners = 0 |
||
12 | |||
13 | /** |
||
14 | * Adds new racer by wrapping target function in a safety wrapper that will |
||
15 | * control it's non-execution in case race has been already won. Wrapped |
||
16 | * function will return original result if it has won the race or undefined |
||
17 | * if race has been lost. |
||
18 | * |
||
19 | * @param {Function} f Function to wrap |
||
20 | * @param {*} [that] Thing that will be injected as `this` during the call |
||
21 | * @return {Function} |
||
22 | */ |
||
23 | this.racer = function (f, that) { |
||
24 | return function () { |
||
25 | if (winners >= places) { return } |
||
26 | winners++ |
||
27 | return f.apply(that, arguments) |
||
28 | } |
||
29 | } |
||
30 | |||
31 | this.getWinners = function () { return winners } |
||
32 | this.getPlaces = function () { return places } |
||
33 | } |
||
34 | |||
35 | module.exports = { |
||
36 | Race: Race |
||
37 | } |
||
38 |