idea-toolbox
Version:
IDEA's utility functions
68 lines (67 loc) • 1.74 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.LimitCounter = void 0;
const resource_model_1 = require("./resource.model");
/**
* A limit counter.
*/
class LimitCounter extends resource_model_1.Resource {
load(x) {
super.load(x);
this.counter = this.clean(x.counter, Number, 0);
this.limit = this.clean(x.limit, Number, 0);
}
/**
* Add a number to the counter; default: 1.
*/
add(number, forceLimit) {
if (forceLimit || this.canAdd())
this.counter += number || 1;
}
/**
* Substract a number to the counter; default: 1.
*/
subtract(number, forceLimit) {
if (forceLimit || this.canSubtract())
this.counter -= number || 1;
}
/**
* Reset the counter.
*/
reset() {
this.counter = 0;
}
/**
* Whether the counter can increase.
*/
canAdd() {
return this.counter < this.limit;
}
/**
* Whether the counter can decrease.
*/
canSubtract() {
return this.counter > 0;
}
/**
* Whether the counter is closed to the limit, based on a percentage; default: 80.
*/
isCloseToLimit(maxPercentage) {
maxPercentage = maxPercentage || 80;
const percentage = (this.counter * 100) / this.limit;
return percentage >= maxPercentage;
}
/**
* Whether the limit has been reached by the counter.
*/
limitReached() {
return this.counter >= this.limit;
}
/**
* Whether the counter exceeds the limit or goes negative.
*/
exceeds() {
return this.counter > this.limit || this.counter < 0;
}
}
exports.LimitCounter = LimitCounter;