2 var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
5 Object.defineProperty(exports, "__esModule", { value: true });
6 exports.distributeUnevenly = exports.countSpaceSequence = exports.groupBySizes = exports.makeBorderConfig = exports.splitAnsi = exports.normalizeString = void 0;
7 const slice_ansi_1 = __importDefault(require("slice-ansi"));
8 const string_width_1 = __importDefault(require("string-width"));
9 const strip_ansi_1 = __importDefault(require("strip-ansi"));
10 const getBorderCharacters_1 = require("./getBorderCharacters");
12 * Converts Windows-style newline to Unix-style
16 const normalizeString = (input) => {
17 return input.replace(/\r\n/g, '\n');
19 exports.normalizeString = normalizeString;
21 * Splits ansi string by newlines
25 const splitAnsi = (input) => {
26 const lengths = (0, strip_ansi_1.default)(input).split('\n').map(string_width_1.default);
29 lengths.forEach((length) => {
30 result.push(length === 0 ? '' : (0, slice_ansi_1.default)(input, startIndex, startIndex + length));
31 // Plus 1 for the newline character itself
32 startIndex += length + 1;
36 exports.splitAnsi = splitAnsi;
38 * Merges user provided border characters with the default border ("honeywell") characters.
42 const makeBorderConfig = (border) => {
44 ...(0, getBorderCharacters_1.getBorderCharacters)('honeywell'),
48 exports.makeBorderConfig = makeBorderConfig;
50 * Groups the array into sub-arrays by sizes.
54 * groupBySizes(['a', 'b', 'c', 'd', 'e'], [2, 1, 2]) = [ ['a', 'b'], ['c'], ['d', 'e'] ]
56 const groupBySizes = (array, sizes) => {
58 return sizes.map((size) => {
59 const group = array.slice(startIndex, startIndex + size);
64 exports.groupBySizes = groupBySizes;
66 * Counts the number of continuous spaces in a string
70 * countGroupSpaces('a bc de f') = 3
72 const countSpaceSequence = (input) => {
74 return (_b = (_a = input.match(/\s+/g)) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0;
76 exports.countSpaceSequence = countSpaceSequence;
78 * Creates the non-increasing number array given sum and length
79 * whose the difference between maximum and minimum is not greater than 1
83 * distributeUnevenly(6, 3) = [2, 2, 2]
84 * distributeUnevenly(8, 3) = [3, 3, 2]
86 const distributeUnevenly = (sum, length) => {
87 const result = Array.from({ length }).fill(Math.floor(sum / length));
88 return result.map((element, index) => {
89 return element + (index < sum % length ? 1 : 0);
92 exports.distributeUnevenly = distributeUnevenly;
93 //# sourceMappingURL=utils.js.map