simple-ascii-chart
Version:
Simple ascii chart generator
292 lines (291 loc) • 9.07 kB
JavaScript
;
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
Object.defineProperty(exports, "__esModule", { value: true });
var coords_1 = require("../coords");
describe('toCoordinates', function () {
var plotWidth = 10;
var plotHeight = 10;
it('should scale point to coordinates within the plot dimensions', function () {
var rangeX = [0, 100];
var rangeY = [0, 100];
var point = [50, 50];
var _a = __read((0, coords_1.toCoordinates)(point, plotWidth, plotHeight, rangeX, rangeY), 2), x = _a[0], y = _a[1];
expect(x).toBeCloseTo(5); // midpoint of rangeX scaled to plot width
expect(y).toBeCloseTo(5); // midpoint of rangeY scaled to plot height
});
it('should handle a point at the origin', function () {
var rangeX = [0, 100];
var rangeY = [0, 100];
var point = [0, 0];
var _a = __read((0, coords_1.toCoordinates)(point, plotWidth, plotHeight, rangeX, rangeY), 2), x = _a[0], y = _a[1];
expect(x).toBeCloseTo(0);
expect(y).toBeCloseTo(0);
});
it('should handle a point at the maximum range', function () {
var rangeX = [0, 100];
var rangeY = [0, 100];
var point = [100, 100];
var _a = __read((0, coords_1.toCoordinates)(point, plotWidth, plotHeight, rangeX, rangeY), 2), x = _a[0], y = _a[1];
expect(x).toBeCloseTo(plotWidth - 1);
expect(y).toBeCloseTo(plotHeight - 1);
});
it('should correctly map a point with negative coordinates', function () {
var rangeX = [-50, 50];
var rangeY = [-50, 50];
var point = [0, 0];
var _a = __read((0, coords_1.toCoordinates)(point, plotWidth, plotHeight, rangeX, rangeY), 2), x = _a[0], y = _a[1];
expect(x).toBeCloseTo(5); // midpoint of plot width
expect(y).toBeCloseTo(5); // midpoint of plot height
});
it('should map fractional points accurately', function () {
var rangeX = [0, 1];
var rangeY = [0, 1];
var point = [0.5, 0.5];
var _a = __read((0, coords_1.toCoordinates)(point, plotWidth, plotHeight, rangeX, rangeY), 2), x = _a[0], y = _a[1];
expect(x).toBeCloseTo(5);
expect(y).toBeCloseTo(5);
});
});
describe('fromPlot', function () {
var plotWidth = 10;
var plotHeight = 10;
var reverseCoords = (0, coords_1.fromPlot)(plotWidth, plotHeight);
it('should correctly convert scaled coordinates at (0, 0)', function () {
var _a = __read(reverseCoords(0, 0), 2), x = _a[0], y = _a[1];
expect(x).toBe(0);
expect(y).toBe(9);
});
it('should correctly convert scaled coordinates at maximum width and height', function () {
var _a = __read(reverseCoords(10, 10), 2), x = _a[0], y = _a[1];
expect(x).toBe(10);
expect(y).toBe(0);
});
it('should correctly convert scaled coordinates at midpoint', function () {
var _a = __read(reverseCoords(5, 5), 2), x = _a[0], y = _a[1];
expect(x).toBe(5);
expect(y).toBe(5);
});
it('should handle non-integer scaled coordinates', function () {
var _a = __read(reverseCoords(7.5, 2.5), 2), x = _a[0], y = _a[1];
expect(x).toBe(8);
expect(y).toBe(7);
});
});
describe('getPlotCoords', function () {
describe.each([
[
'returns proper data',
[
[0, 0],
[1, 1],
],
2,
2,
[
[0, 0],
[1, 1],
],
],
[
'scales data',
[
[0, 0],
[1, 1],
],
4,
4,
[
[0, 0],
[3, 3],
],
],
[
'returns proper range',
[
[0, 0],
[1, 1],
[2, 2],
],
6,
6,
[
[0, 0],
[2.5, 2.5],
[5, 5],
],
],
])('', function (variant, coords, width, height, output) {
it(variant, function () {
expect((0, coords_1.getPlotCoords)(coords, width, height)).toStrictEqual(output);
});
});
});
describe('scaler', function () {
describe.each([
['picks right range', [0, 1], [1, 1], 0, 1],
['picks right range with negative values', [-1, 1], [0, 100], -1, 0],
['picks right domain and range', [-1, 1], [-100, 100], -1, -100],
['picks right domain and range with negatives', [0, 10], [-100, 0], 10, 0],
['picks right domain and range from zero', [-1, 0], [-100, 100], 0, 100],
['picks right values from the range', [-1, 1], [-100, 100], 0.5, 50],
['picks right negative values', [0, 1], [-100, 0], 0.5, -50],
['picks fractional range', [0, 1], [0, 3], 0.5, 1.5],
])('', function (variant, domain, range, input, output) {
it(variant, function () {
expect((0, coords_1.scaler)(domain, range)(input)).toBe(output);
});
});
});
describe('getExtrema', function () {
describe.each([
[
'gets max value',
[
[0, 1],
[1, 1],
[4, 1],
[2, 1],
],
'max',
0,
4,
],
[
'gets max value with negative values',
[
[-1, 1],
[1.3, 1],
[4, 1],
[0, 1],
],
'max',
0,
4,
],
[
'gets min value',
[
[-1, 1],
[1.3, 1],
[4, 1],
[0, 1],
],
'min',
0,
-1,
],
[
'gets min value from second row when all values are equal',
[
[-1, 1],
[1.3, 1],
[1, 1],
[0, 1],
],
'min',
1,
1,
],
[
'gets max value from second row',
[
[-1, 1],
[1.3, 10],
[1, -10],
[0, 100],
],
'max',
1,
100,
],
])('', function (variant, arr, type, position, output) {
it(variant, function () {
expect((0, coords_1.getExtrema)(arr, type, position)).toBe(output);
});
});
});
describe('toSorted', function () {
describe.each([
[
'picks keeps the same range',
[
[0, 1],
[1, 1],
],
[
[0, 1],
[1, 1],
],
],
[
'inverts range',
[
[1, 1],
[0, 1],
],
[
[0, 1],
[1, 1],
],
],
[
'keep same values in place',
[
[0, 1],
[1, 1],
[1, 2],
[2, 2],
],
[
[0, 1],
[1, 1],
[1, 2],
[2, 2],
],
],
])('', function (variant, arr, output) {
it(variant, function () {
expect((0, coords_1.toSorted)(arr)).toStrictEqual(output);
});
});
});
describe('toEmpty', function () {
it('should return an empty array of the specified size', function () {
var size = 5;
var result = (0, coords_1.toEmpty)(size, '');
expect(result).toHaveLength(size);
expect(result.every(function (item) { return item === ''; })).toBe(true);
});
it('should return an array filled with the specified empty string', function () {
var size = 3;
var emptyString = 'X';
var result = (0, coords_1.toEmpty)(size, emptyString);
expect(result).toHaveLength(size);
expect(result.every(function (item) { return item === emptyString; })).toBe(true);
});
it('should return an empty array when size is 0', function () {
var size = 0;
var result = (0, coords_1.toEmpty)(size);
expect(result).toHaveLength(size);
});
it('should return an empty array when size is negative', function () {
var size = -5;
var result = (0, coords_1.toEmpty)(size);
expect(result).toHaveLength(0);
});
});