@zoodogood/utils 1.0.6-change.724 → 1.0.7-change.843
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -26,6 +26,10 @@ interface ITableCell {
|
|
|
26
26
|
options: ICellOptions;
|
|
27
27
|
}
|
|
28
28
|
type TCellSetSymbolCallback = (context: ITextTableGeneratorContext, index: number) => string;
|
|
29
|
+
interface ICellBuilderOptions {
|
|
30
|
+
minWidth?: number;
|
|
31
|
+
maxWidth?: number;
|
|
32
|
+
}
|
|
29
33
|
interface ITableOptions {
|
|
30
34
|
borderLeft: null | TCellSetSymbolCallback;
|
|
31
35
|
borderRight: null | TCellSetSymbolCallback;
|
|
@@ -52,8 +56,8 @@ declare class TextTableBuilder {
|
|
|
52
56
|
rows: TTableRow[];
|
|
53
57
|
protected options: ITableOptions;
|
|
54
58
|
setBorderOptions(callback?: TCellSetSymbolCallback, directions?: BorderDirectionEnum[]): this;
|
|
55
|
-
addRowWithElements(elements: ITableCell["value"][], optionsForEveryElement?: Partial<ICellOptions
|
|
56
|
-
addMultilineRowWithElements(elements: ITableCell["value"][], optionsForEveryElement?: Partial<ICellOptions
|
|
59
|
+
addRowWithElements(elements: ITableCell["value"][], optionsForEveryElement?: Partial<ICellOptions>, useOnAddingOptions?: ICellBuilderOptions): this;
|
|
60
|
+
addMultilineRowWithElements(elements: ITableCell["value"][], optionsForEveryElement?: Partial<ICellOptions>, useOnAddingOptions?: ICellBuilderOptions): this;
|
|
57
61
|
addEmptyRow(): this;
|
|
58
62
|
addRowSeparator(setSymbol?: TCellSetSymbolCallback): this;
|
|
59
63
|
addCellAtRow(rowIndex: number, cellValue: ITableCell["value"], cellOptions: ICellOptions): this;
|
|
@@ -7,6 +7,52 @@ function getRowType(row) {
|
|
|
7
7
|
function isCell(target) {
|
|
8
8
|
return "value" in target && "options" in target;
|
|
9
9
|
}
|
|
10
|
+
function addPaddingByCellAlign(original, totalForAdd, align) {
|
|
11
|
+
const addidableGapLeft = align === CellAlignEnum.Right
|
|
12
|
+
? totalForAdd
|
|
13
|
+
: align === CellAlignEnum.Center
|
|
14
|
+
? Math.floor(totalForAdd / 2)
|
|
15
|
+
: 0;
|
|
16
|
+
const addidableGapRight = align === CellAlignEnum.Left
|
|
17
|
+
? totalForAdd
|
|
18
|
+
: align === CellAlignEnum.Center
|
|
19
|
+
? Math.ceil(totalForAdd / 2)
|
|
20
|
+
: 0;
|
|
21
|
+
return `${" ".repeat(addidableGapLeft)}${original}${" ".repeat(addidableGapRight)}`;
|
|
22
|
+
}
|
|
23
|
+
function changeContentSetCellMinWidth(cell, minWidth) {
|
|
24
|
+
const currentMin = calculateCellMinWidth(cell);
|
|
25
|
+
if (minWidth <= currentMin) {
|
|
26
|
+
return cell;
|
|
27
|
+
}
|
|
28
|
+
const { value, options: { align }, } = cell;
|
|
29
|
+
const lack = minWidth - currentMin;
|
|
30
|
+
cell.value = addPaddingByCellAlign(value, lack, align);
|
|
31
|
+
return cell;
|
|
32
|
+
}
|
|
33
|
+
function changeContentSetCellMaxWidth(cell, maxWidth) {
|
|
34
|
+
const current = calculateCellMinWidth(cell);
|
|
35
|
+
if (maxWidth >= current) {
|
|
36
|
+
return cell;
|
|
37
|
+
}
|
|
38
|
+
let overage = current - maxWidth;
|
|
39
|
+
const gapDirection = cell.options.align === CellAlignEnum.Left ? "gapRight" : "gapLeft";
|
|
40
|
+
if (cell.options[gapDirection] > 1) {
|
|
41
|
+
cell.options[gapDirection]--;
|
|
42
|
+
overage--;
|
|
43
|
+
if (overage === 0) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
const suffix = "..";
|
|
48
|
+
const sliced = cell.value.slice(0, -(overage + suffix.length));
|
|
49
|
+
cell.value = sliced + suffix;
|
|
50
|
+
return cell;
|
|
51
|
+
}
|
|
52
|
+
function calculateCellMinWidth(cell) {
|
|
53
|
+
const { gapLeft, gapRight } = cell.options;
|
|
54
|
+
return cell.value.length + gapLeft + gapRight;
|
|
55
|
+
}
|
|
10
56
|
class TextTableGenerator {
|
|
11
57
|
constructor(rows, options) {
|
|
12
58
|
this.data = [];
|
|
@@ -53,9 +99,7 @@ class TextTableGenerator {
|
|
|
53
99
|
const metadata = this.context.metadata;
|
|
54
100
|
const columns = this.getColumns();
|
|
55
101
|
const columnsMetadata = columns.map((column) => ({
|
|
56
|
-
largestLength: Math.max(...column.map((element) => isCell(element)
|
|
57
|
-
? this.calculateCellMinWidth(element)
|
|
58
|
-
: 0)),
|
|
102
|
+
largestLength: Math.max(...column.map((element) => isCell(element) ? calculateCellMinWidth(element) : 0)),
|
|
59
103
|
}));
|
|
60
104
|
metadata.columns = columnsMetadata;
|
|
61
105
|
metadata.tableWidth = this.calculateTableWidth();
|
|
@@ -105,18 +149,10 @@ class TextTableGenerator {
|
|
|
105
149
|
drawCell(cell, expectedWidth) {
|
|
106
150
|
const { gapLeft, gapRight, align } = cell.options;
|
|
107
151
|
const { value } = cell;
|
|
108
|
-
const minContentLength =
|
|
109
|
-
const
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
? Math.floor((expectedWidth - minContentLength) / 2)
|
|
113
|
-
: 0;
|
|
114
|
-
const addidableGapRight = align === CellAlignEnum.Left
|
|
115
|
-
? expectedWidth - minContentLength
|
|
116
|
-
: align === CellAlignEnum.Center
|
|
117
|
-
? Math.ceil((expectedWidth - minContentLength) / 2)
|
|
118
|
-
: 0;
|
|
119
|
-
return `${" ".repeat(gapLeft + addidableGapLeft)}${value}${" ".repeat(gapRight + addidableGapRight)}`;
|
|
152
|
+
const minContentLength = calculateCellMinWidth(cell);
|
|
153
|
+
const lack = expectedWidth - minContentLength;
|
|
154
|
+
const contentWithPadding = addPaddingByCellAlign(value, lack, align);
|
|
155
|
+
return `${" ".repeat(gapLeft)}${contentWithPadding}${" ".repeat(gapRight)}`;
|
|
120
156
|
}
|
|
121
157
|
drawLine(setSymbol) {
|
|
122
158
|
let content = "";
|
|
@@ -144,9 +180,6 @@ class TextTableGenerator {
|
|
|
144
180
|
const borders = +!!this.options.borderLeft + +!!this.options.borderRight;
|
|
145
181
|
return cells + separators + borders;
|
|
146
182
|
}
|
|
147
|
-
calculateCellMinWidth(cell) {
|
|
148
|
-
return cell.value.length + cell.options.gapLeft + cell.options.gapRight;
|
|
149
|
-
}
|
|
150
183
|
}
|
|
151
184
|
var CellAlignEnum;
|
|
152
185
|
(function (CellAlignEnum) {
|
|
@@ -181,7 +214,7 @@ var SpecialRowTypeEnum;
|
|
|
181
214
|
class TextTableBuilder {
|
|
182
215
|
constructor() {
|
|
183
216
|
this.rows = [];
|
|
184
|
-
this.options = DEFAULT_TABLE_OPTIONS;
|
|
217
|
+
this.options = Object.assign({}, DEFAULT_TABLE_OPTIONS);
|
|
185
218
|
}
|
|
186
219
|
setBorderOptions(callback = () => "|", directions = [
|
|
187
220
|
BorderDirectionEnum.BorderLeft,
|
|
@@ -205,19 +238,19 @@ class TextTableBuilder {
|
|
|
205
238
|
}
|
|
206
239
|
return this;
|
|
207
240
|
}
|
|
208
|
-
addRowWithElements(elements, optionsForEveryElement) {
|
|
241
|
+
addRowWithElements(elements, optionsForEveryElement, useOnAddingOptions) {
|
|
209
242
|
const row = [];
|
|
210
243
|
for (const value of elements) {
|
|
211
|
-
this.pushCellToArray(row, value, optionsForEveryElement);
|
|
244
|
+
this.pushCellToArray(row, value, optionsForEveryElement, useOnAddingOptions);
|
|
212
245
|
}
|
|
213
246
|
this.pushRowToTable(row);
|
|
214
247
|
return this;
|
|
215
248
|
}
|
|
216
|
-
addMultilineRowWithElements(elements, optionsForEveryElement) {
|
|
249
|
+
addMultilineRowWithElements(elements, optionsForEveryElement, useOnAddingOptions) {
|
|
217
250
|
const separatedElements = elements.map((value) => value.split("\n"));
|
|
218
251
|
const largestHeight = Math.max(...separatedElements.map((sub) => sub.length));
|
|
219
252
|
for (let index = 0; index < largestHeight; index++) {
|
|
220
|
-
this.addRowWithElements(separatedElements.map((sub) => { var _a; return (_a = sub.at(index)) !== null && _a !== void 0 ? _a : ""; }), optionsForEveryElement);
|
|
253
|
+
this.addRowWithElements(separatedElements.map((sub) => { var _a; return (_a = sub.at(index)) !== null && _a !== void 0 ? _a : ""; }), optionsForEveryElement, useOnAddingOptions);
|
|
221
254
|
}
|
|
222
255
|
return this;
|
|
223
256
|
}
|
|
@@ -244,9 +277,13 @@ class TextTableBuilder {
|
|
|
244
277
|
this.pushCellToArray(row, cellValue, cellOptions);
|
|
245
278
|
return this;
|
|
246
279
|
}
|
|
247
|
-
pushCellToArray(array, cellValue, cellOptions = {}) {
|
|
280
|
+
pushCellToArray(array, cellValue, cellOptions = {}, pushOptions) {
|
|
248
281
|
const options = Object.assign({}, DEFAULT_CELL_OPTIONS, cellOptions);
|
|
249
|
-
|
|
282
|
+
const cell = { value: cellValue, options };
|
|
283
|
+
const { minWidth, maxWidth } = pushOptions !== null && pushOptions !== void 0 ? pushOptions : {};
|
|
284
|
+
minWidth && changeContentSetCellMinWidth(cell, minWidth);
|
|
285
|
+
maxWidth && changeContentSetCellMaxWidth(cell, maxWidth);
|
|
286
|
+
array.push(cell);
|
|
250
287
|
return this;
|
|
251
288
|
}
|
|
252
289
|
pushRowToTable(row) {
|