simple-ascii-chart
Version:
Simple ascii chart generator
242 lines (241 loc) • 10.5 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;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAxisCenter = exports.getPlotCoords = exports.toCoordinates = exports.scaler = exports.getMin = exports.getMax = exports.getExtrema = exports.fromPlot = exports.toPlot = exports.toSorted = exports.toArrays = exports.toFlat = exports.distance = exports.toUnique = exports.toArray = exports.toEmpty = void 0;
var index_1 = require("../constants/index");
/**
* Creates an array filled with a specified string.
* @param {number} size - The size of the array.
* @param {string} empty - The value to fill the array with (default: EMPTY).
* @returns {string[]} - An array filled with the specified string.
*/
var toEmpty = function (size, empty) {
if (empty === void 0) { empty = index_1.EMPTY; }
return Array(size >= 0 ? size : 0).fill(empty);
};
exports.toEmpty = toEmpty;
/**
* Converts a number or string to an array of its characters.
* @param {number | string} input - The input to convert.
* @returns {string[]} - An array of characters.
*/
var toArray = function (input) { return input.toString().split(''); };
exports.toArray = toArray;
/**
* Removes duplicate values from an array.
* @param {number[]} array - The array of numbers.
* @returns {number[]} - An array containing only unique values.
*/
var toUnique = function (array) { return __spreadArray([], __read(new Set(array)), false); };
exports.toUnique = toUnique;
/**
* Calculates the distance between two integer coordinates by rounding to the nearest integers.
* @param {number} x - The x-coordinate of the first point.
* @param {number} y - The y-coordinate of the second point.
* @returns {number} - The absolute distance between the rounded points.
*/
var distance = function (x, y) { return Math.abs(Math.round(x) - Math.round(y)); };
exports.distance = distance;
/**
* Flattens a multi-line array of points into a single array of points.
* @param {MultiLine} array - The multi-line array.
* @returns {Point[]} - A flat array of points.
*/
var toFlat = function (array) {
var _a;
return (_a = []).concat.apply(_a, __spreadArray([], __read(array), false));
};
exports.toFlat = toFlat;
/**
* Converts a multi-line array into arrays of unique x and y values.
* @param {MultiLine} array - The multi-line array.
* @returns {[number[], number[]]} - Arrays of unique x and y values.
*/
var toArrays = function (array) {
var rangeX = [];
var rangeY = [];
(0, exports.toFlat)(array).forEach(function (_a) {
var _b = __read(_a, 2), x = _b[0], y = _b[1];
rangeX.push(x);
rangeY.push(y);
});
return [(0, exports.toUnique)(rangeX), (0, exports.toUnique)(rangeY)];
};
exports.toArrays = toArrays;
/**
* Sorts a single-line array of points in ascending order based on the x-coordinate.
* @param {SingleLine} array - The single-line array to sort.
* @returns {SingleLine} - The sorted array.
*/
var toSorted = function (array) {
return array.sort(function (_a, _b) {
var _c = __read(_a, 1), x1 = _c[0];
var _d = __read(_b, 1), x2 = _d[0];
if (x1 < x2)
return -1;
if (x1 > x2)
return 1;
return 0;
});
};
exports.toSorted = toSorted;
/**
* Returns a function that converts a coordinate (x, y) to scaled plot coordinates.
* @param {number} plotWidth - The width of the plot.
* @param {number} plotHeight - The height of the plot.
* @returns {function} - A function that takes (x, y) and returns scaled plot coordinates [scaledX, scaledY].
*/
var toPlot = function (plotWidth, plotHeight) {
return function (x, y) { return [
Math.round((x / plotWidth) * plotWidth),
plotHeight - 1 - Math.round((y / plotHeight) * plotHeight),
]; };
};
exports.toPlot = toPlot;
/**
* Returns a function that converts scaled plot coordinates (scaledX, scaledY) back to the original coordinates.
* @param {number} plotWidth - The width of the plot.
* @param {number} plotHeight - The height of the plot.
* @returns {function} - A function that takes (scaledX, scaledY) and returns original coordinates [x, y].
*/
var fromPlot = function (plotWidth, plotHeight) {
return function (scaledX, scaledY) {
var x = (scaledX / plotWidth) * plotWidth;
var y = plotHeight - 1 - (scaledY / plotHeight) * (plotHeight - 1);
return [Math.round(x), Math.round(y)];
};
};
exports.fromPlot = fromPlot;
/**
* Finds the maximum or minimum value in a single-line array of points.
* @param {SingleLine} arr - The single-line array to search for extrema.
* @param {'max' | 'min'} type - 'max' to find the maximum value, 'min' for minimum (default is 'max').
* @param {number} position - The position of the value within each point (default is 1).
* @returns {number} - The maximum or minimum value found in the array.
*/
var getExtrema = function (arr, type, position) {
if (type === void 0) { type = 'max'; }
if (position === void 0) { position = 1; }
return arr.reduce(function (previous, curr) { return Math[type](previous, curr[position]); }, type === 'max' ? Number.NEGATIVE_INFINITY : Number.POSITIVE_INFINITY);
};
exports.getExtrema = getExtrema;
/**
* Finds the maximum value in an array of numbers.
* @param {number[]} arr - The array of numbers.
* @returns {number} - The maximum value in the array.
*/
var getMax = function (arr) {
return arr.reduce(function (previous, curr) { return Math.max(previous, curr); }, Number.NEGATIVE_INFINITY);
};
exports.getMax = getMax;
/**
* Finds the minimum value in an array of numbers.
* @param {number[]} arr - The array of numbers.
* @returns {number} - The minimum value in the array.
*/
var getMin = function (arr) {
return arr.reduce(function (previous, curr) { return Math.min(previous, curr); }, Number.POSITIVE_INFINITY);
};
exports.getMin = getMin;
/**
* Returns a function that scales coordinates to fit within a specified range.
* @param {[number, number]} domain - The original value range (min and max).
* @param {[number, number]} range - The range to scale the values into.
* @returns {(value: number) => number} - A function for scaling coordinates.
*/
var scaler = function (_a, _b) {
var _c = __read(_a, 2), domainMin = _c[0], domainMax = _c[1];
var _d = __read(_b, 2), rangeMin = _d[0], rangeMax = _d[1];
var domainLength = Math.sqrt(Math.abs(Math.pow((domainMax - domainMin), 2))) || 1;
var rangeLength = Math.sqrt(Math.pow((rangeMax - rangeMin), 2));
return function (domainValue) {
return rangeMin + (rangeLength * (domainValue - domainMin)) / domainLength;
};
};
exports.scaler = scaler;
/**
* Scales a point's coordinates to fit within the specified plot dimensions.
* @param {Point} point - The point to scale.
* @param {number} plotWidth - The width of the plot.
* @param {number} plotHeight - The height of the plot.
* @param {number[]} rangeX - The range of x values.
* @param {number[]} rangeY - The range of y values.
* @returns {Point} - The scaled point.
*/
var toCoordinates = function (point, plotWidth, plotHeight, rangeX, rangeY) {
var getXCoord = (0, exports.scaler)(rangeX, [0, plotWidth - 1]);
var getYCoord = (0, exports.scaler)(rangeY, [0, plotHeight - 1]);
return [Math.round(getXCoord(point[0])), Math.round(getYCoord(point[1]))];
};
exports.toCoordinates = toCoordinates;
/**
* Scales a list of coordinates to fit within the specified plot dimensions.
* @param {SingleLine} coordinates - The list of coordinates to scale.
* @param {number} plotWidth - The width of the plot.
* @param {number} plotHeight - The height of the plot.
* @param {number[]} [rangeX] - The range of x values (defaults to min and max from coordinates).
* @param {number[]} [rangeY] - The range of y values (defaults to min and max from coordinates).
* @returns {SingleLine} - The scaled list of coordinates.
*/
var getPlotCoords = function (coordinates, plotWidth, plotHeight, rangeX, rangeY) {
var getXCoord = (0, exports.scaler)(rangeX || [(0, exports.getExtrema)(coordinates, 'min', 0), (0, exports.getExtrema)(coordinates, 'max', 0)], [0, plotWidth - 1]);
var getYCoord = (0, exports.scaler)(rangeY || [(0, exports.getExtrema)(coordinates, 'min'), (0, exports.getExtrema)(coordinates)], [
0,
plotHeight - 1,
]);
return coordinates.map(function (_a) {
var _b = __read(_a, 2), x = _b[0], y = _b[1];
return [getXCoord(x), getYCoord(y)];
});
};
exports.getPlotCoords = getPlotCoords;
/**
* Computes the axis center point based on specified plot dimensions and ranges.
* @param {MaybePoint} axisCenter - The center point for the axis.
* @param {number} plotWidth - The width of the plot.
* @param {number} plotHeight - The height of the plot.
* @param {number[]} rangeX - The range of x values.
* @param {number[]} rangeY - The range of y values.
* @param {number[]} initialValue - The initial axis values.
* @returns {Point} - The center point of the axis.
*/
var getAxisCenter = function (axisCenter, plotWidth, plotHeight, rangeX, rangeY, initialValue) {
var axis = { x: initialValue[0], y: initialValue[1] };
if (axisCenter) {
var _a = __read(axisCenter, 2), x = _a[0], y = _a[1];
if (typeof x === 'number') {
var xScaler = (0, exports.scaler)(rangeX, [0, plotWidth - 1]);
axis.x = Math.round(xScaler(x));
}
if (typeof y === 'number') {
var yScaler = (0, exports.scaler)(rangeY, [0, plotHeight - 1]);
axis.y = plotHeight - Math.round(yScaler(y));
}
}
return axis;
};
exports.getAxisCenter = getAxisCenter;