es/ratelimit.js   A
last analyzed

Complexity

Total Complexity 18
Complexity/F 1.2

Size

Lines of Code 46
Function Count 15

Duplication

Duplicated Lines 46
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
c 1
b 0
f 0
nc 1
dl 46
loc 46
rs 10
wmc 18
mnd 1
bc 18
fnc 15
bpm 1.2
cpm 1.2
noi 2

13 Functions

Rating   Name   Duplication   Size   Complexity  
A RateLimit.getOriginalCalls 1 1 1
A RateLimit.breakTimer 5 5 1
A RateLimit.getCallsLeft 1 1 1
A RateLimit.decrementCall 1 1 1
A RateLimit.decrementTime 1 1 1
A RateLimit.restartTimer 4 4 1
A RateLimit.getOriginalTime 1 1 1
A RateLimit.isActive 1 1 1
A RateLimit.setActive 3 3 1
A RateLimit.getTimeLeft 1 1 1
A ratelimit.js ➔ RateLimit 9 9 3
A RateLimit.reset 4 4 1
A RateLimit.startTimer 10 10 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1 View Code Duplication
var RateLimit = /** @class */ (function () {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
2
    function RateLimit(calls, time, callsLeft, timeLeft) {
3
        if (callsLeft === void 0) { callsLeft = calls; }
0 ignored issues
show
Coding Style introduced by
Consider using undefined instead of void(0). It is equivalent and more straightforward to read.
Loading history...
4
        if (timeLeft === void 0) { timeLeft = time; }
0 ignored issues
show
Coding Style introduced by
Consider using undefined instead of void(0). It is equivalent and more straightforward to read.
Loading history...
5
        this.originCalls = calls;
6
        this.originTime = time;
7
        this.currentCalls = callsLeft;
8
        this.currentTime = timeLeft;
9
        this.active = false;
10
    }
11
    RateLimit.prototype.reset = function () {
12
        this.currentCalls = this.originCalls;
13
        this.currentTime = this.originTime;
14
    };
15
    RateLimit.prototype.decrementCall = function () { this.currentCalls--; };
16
    RateLimit.prototype.decrementTime = function () { this.currentTime--; };
17
    RateLimit.prototype.getOriginalCalls = function () { return this.originCalls; };
18
    RateLimit.prototype.getOriginalTime = function () { return this.originTime; };
19
    RateLimit.prototype.getCallsLeft = function () { return this.currentCalls; };
20
    RateLimit.prototype.getTimeLeft = function () { return this.currentTime; };
21
    RateLimit.prototype.isActive = function () { return this.active; };
22
    RateLimit.prototype.setActive = function (active) {
23
        this.active = active;
24
    };
25
    RateLimit.prototype.startTimer = function () {
26
        var _this = this;
27
        this.setActive(true);
28
        this.timer = setInterval(function () {
29
            _this.decrementTime();
30
            if (_this.getTimeLeft() <= 0) {
31
                _this.breakTimer();
32
            }
33
        }, 1000);
34
    };
35
    RateLimit.prototype.breakTimer = function () {
36
        this.reset();
37
        this.setActive(false);
38
        clearInterval(this.timer);
39
    };
40
    RateLimit.prototype.restartTimer = function () {
41
        this.breakTimer();
42
        this.startTimer();
43
    };
44
    return RateLimit;
45
}());
46
export { RateLimit };
47
//# sourceMappingURL=ratelimit.js.map