typescript-libraries
Version:
To install this library, run:
206 lines (205 loc) • 8.33 kB
JavaScript
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TSLotto = void 0;
var TSNumber_1 = require("../utils/TSNumber");
var TSLotto = /** @class */ (function () {
function TSLotto(options) {
if (options === void 0) { options = { max: 36, pick: 3, extra: 2, tickets: 5 }; }
var _this = this;
this.numbers = [];
this.selections = [];
this.bonus = [];
this.options = options;
this.numbers = Array.from({ length: options.max }, function (v, i) { return i + 1; });
this.selections = Array.from({ length: options.tickets }, function () { return _this.emptyLine(options.pick); });
if (options.extra) {
this.bonus = Array.from({ length: options.tickets }, function () { return _this.emptyLine(options.extra); });
}
if (this.options.randomize) {
this.selections.forEach(function (l, i) {
_this.fillLine(l);
if (options.extra) {
_this.fillLine(_this.bonus[i], l);
}
});
}
}
Object.defineProperty(TSLotto.prototype, "render", {
get: function () {
var _this = this;
var sortNumber = function (a, b) { return a - b; }, formatLines = function () {
var lines = [];
_this.selections.forEach(function (l, i) {
if (_this.options.sort) {
lines[i] = _this.selections[i] = _this.selections[i].sort(sortNumber);
if (_this.options.extra) {
_this.bonus[i] = _this.bonus[i].sort(sortNumber);
lines[i] = lines[i].concat(_this.bonus[i]);
}
}
else {
lines[i] = _this.selections[i];
if (_this.options.extra) {
lines[i] = lines[i].concat(_this.bonus[i]);
}
}
});
return { selections: _this.selections, bonus: _this.bonus, lines: lines };
};
return __assign(__assign({ options: this.options }, formatLines()), { numbers: this.numbers });
},
enumerable: false,
configurable: true
});
Object.defineProperty(TSLotto.prototype, "lotto", {
set: function (data) {
this.options = data.options;
this.selections = data.selections;
this.bonus = data.bonus;
this.numbers = data.numbers;
},
enumerable: false,
configurable: true
});
TSLotto.prototype.fillLine = function (line, existingLine) {
if (existingLine === void 0) { existingLine = []; }
var index = line.findIndex(Number.isNaN);
while (index > -1) {
var random = TSNumber_1.TSNumber.random(1, this.options.max);
if (line.indexOf(random) === -1 && existingLine.indexOf(random) === -1) {
line[index] = random;
}
index = line.findIndex(Number.isNaN);
}
};
TSLotto.prototype.randomLine = function (index) {
this.clearLine(index);
this.fillLine(this.selections[index], this.bonus[index]);
if (this.options.extra) {
this.fillLine(this.bonus[index], this.selections[index]);
}
};
TSLotto.prototype.addLine = function (line) {
var _this = this;
if (line) {
var lineLength = this.options.pick + this.options.extra;
this.selections.unshift([]);
this.selections[0] = line.slice(0, this.options.pick);
if (this.options.extra) {
this.bonus.unshift([]);
this.bonus[0] = line.slice(this.options.pick, lineLength);
}
if (this.selections[0].length + (this.bonus[0] ? this.bonus[0].length : 0) !== lineLength) {
this.deleteLine(0);
return -2;
}
else if (this.validateLine(0)) {
this.deleteLine(0);
return -1;
}
return 0;
}
else {
this.selections.push(this.emptyLine(this.options.pick));
this.bonus.push(this.emptyLine(this.options.extra));
var lastIndex_1 = this.selections.length - 1;
if (this.options.randomize) {
this.selections.forEach(function () {
_this.fillLine(_this.selections[lastIndex_1]);
if (_this.options.extra) {
_this.fillLine(_this.bonus[lastIndex_1], _this.selections[lastIndex_1]);
}
});
}
return lastIndex_1;
}
};
TSLotto.prototype.shufleLines = function () {
var _this = this;
this.selections.forEach(function (s, i) { return _this.randomLine(i); });
};
TSLotto.prototype.adjustLines = function (tickets) {
this.options.tickets = tickets;
var count = this.selections.length;
for (var j = 0; j < Math.abs(tickets - count); j++) {
if (tickets - count > 0) {
this.addLine();
}
else {
this.deleteLine(this.render.lines.length - 1);
}
}
};
TSLotto.prototype.clearLine = function (index) {
this.selections[index] = this.emptyLine(this.options.pick);
this.bonus[index] = this.emptyLine(this.options.extra);
};
TSLotto.prototype.deleteLine = function (index) {
this.bonus.splice(index, 1);
this.selections.splice(index, 1);
};
TSLotto.prototype.emptyLine = function (length) {
return Array.from({ length: length }, function () { return NaN; });
};
TSLotto.prototype.validateLine = function (index) {
var _this = this;
var l = this.selections[index].concat(this.bonus[index] || []);
return l.some(function (v, i) { return l.indexOf(v) !== i || (v > _this.options.max || v < 1); });
};
TSLotto.prototype.clear = function () {
var _this = this;
this.selections.forEach(function (v, i) { return _this.clearLine(i); });
};
TSLotto.prototype.toggle = function (line, index) {
var sli = this.selections[line].indexOf(this.numbers[index]), bli = this.bonus[line] && this.bonus[line].length ? this.bonus[line].indexOf(this.numbers[index]) : -1, isFull = sli === -1 && bli === -1 && this.isComplete(line);
if (isFull) {
return;
}
else if (sli > -1) {
this.selections[line][sli] = NaN;
return;
}
else if (bli > -1) {
this.bonus[line][bli] = NaN;
return;
}
var s = this.selections[line].findIndex(Number.isNaN), b = this.bonus[line] && this.bonus[line].length ? this.bonus[line].findIndex(Number.isNaN) : -1;
if (s > -1 && bli === -1 && sli === -1) {
this.selections[line][s] = this.numbers[index];
}
else if (b > -1 && sli === -1) {
this.bonus[line][b] = this.numbers[index];
}
};
TSLotto.prototype.isComplete = function (index) {
var _this = this;
var isLineComplete = function (i) {
var hasNaN = _this.selections[i].findIndex(Number.isNaN) === -1;
if (_this.options.extra) {
hasNaN = hasNaN && _this.bonus[i].findIndex(Number.isNaN) === -1;
}
return hasNaN;
};
if (typeof index !== 'undefined') {
return isLineComplete(index);
}
else {
var isFull_1 = true;
this.selections.forEach(function (l, i) { return isFull_1 = isFull_1 && isLineComplete(i); });
return isFull_1;
}
};
return TSLotto;
}());
exports.TSLotto = TSLotto;