UNPKG

tetris-fumen

Version:
64 lines (63 loc) 2.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Buffer = void 0; var ENCODE_TABLE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; var Buffer = /** @class */ (function () { function Buffer(data) { if (data === void 0) { data = ''; } this.values = data.split('').map(decodeToValue); } Buffer.prototype.poll = function (max) { var value = 0; for (var count = 0; count < max; count += 1) { var v = this.values.shift(); if (v === undefined) { throw new Error('Unexpected fumen'); } value += v * Math.pow(Buffer.tableLength, count); } return value; }; Buffer.prototype.push = function (value, splitCount) { if (splitCount === void 0) { splitCount = 1; } var current = value; for (var count = 0; count < splitCount; count += 1) { this.values.push(current % Buffer.tableLength); current = Math.floor(current / Buffer.tableLength); } }; Buffer.prototype.merge = function (postBuffer) { for (var _i = 0, _a = postBuffer.values; _i < _a.length; _i++) { var value = _a[_i]; this.values.push(value); } }; Buffer.prototype.isEmpty = function () { return this.values.length === 0; }; Object.defineProperty(Buffer.prototype, "length", { get: function () { return this.values.length; }, enumerable: false, configurable: true }); Buffer.prototype.get = function (index) { return this.values[index]; }; Buffer.prototype.set = function (index, value) { this.values[index] = value; }; Buffer.prototype.toString = function () { return this.values.map(encodeFromValue).join(''); }; Buffer.tableLength = ENCODE_TABLE.length; return Buffer; }()); exports.Buffer = Buffer; function decodeToValue(v) { return ENCODE_TABLE.indexOf(v); } function encodeFromValue(index) { return ENCODE_TABLE[index]; }