@zoodogood/utils 1.0.6-change.724 → 1.0.7-change.854
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,11 +56,11 @@ 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
|
-
addCellAtRow(rowIndex: number, cellValue: ITableCell["value"], cellOptions: ICellOptions): this;
|
|
63
|
+
addCellAtRow(rowIndex: number, cellValue: ITableCell["value"], cellOptions: ICellOptions, useOnAddingOptions: ICellBuilderOptions): this;
|
|
60
64
|
private pushCellToArray;
|
|
61
65
|
private pushRowToTable;
|
|
62
66
|
generateTextContent(): string;
|
|
@@ -7,6 +7,58 @@ function getRowType(row) {
|
|
|
7
7
|
function isCell(target) {
|
|
8
8
|
return "value" in target && "options" in target;
|
|
9
9
|
}
|
|
10
|
+
function emptyCell(value = "") {
|
|
11
|
+
return {
|
|
12
|
+
value,
|
|
13
|
+
options: DEFAULT_CELL_OPTIONS,
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
function addPaddingByCellAlign(original, totalForAdd, align) {
|
|
17
|
+
const addidableGapLeft = align === CellAlignEnum.Right
|
|
18
|
+
? totalForAdd
|
|
19
|
+
: align === CellAlignEnum.Center
|
|
20
|
+
? Math.floor(totalForAdd / 2)
|
|
21
|
+
: 0;
|
|
22
|
+
const addidableGapRight = align === CellAlignEnum.Left
|
|
23
|
+
? totalForAdd
|
|
24
|
+
: align === CellAlignEnum.Center
|
|
25
|
+
? Math.ceil(totalForAdd / 2)
|
|
26
|
+
: 0;
|
|
27
|
+
return `${" ".repeat(addidableGapLeft)}${original}${" ".repeat(addidableGapRight)}`;
|
|
28
|
+
}
|
|
29
|
+
function changeContentSetCellMinWidth(cell, minWidth) {
|
|
30
|
+
const currentMin = calculateCellMinWidth(cell);
|
|
31
|
+
if (minWidth <= currentMin) {
|
|
32
|
+
return cell;
|
|
33
|
+
}
|
|
34
|
+
const { value, options: { align }, } = cell;
|
|
35
|
+
const lack = minWidth - currentMin;
|
|
36
|
+
cell.value = addPaddingByCellAlign(value, lack, align);
|
|
37
|
+
return cell;
|
|
38
|
+
}
|
|
39
|
+
function changeContentSetCellMaxWidth(cell, maxWidth) {
|
|
40
|
+
const current = calculateCellMinWidth(cell);
|
|
41
|
+
if (maxWidth >= current) {
|
|
42
|
+
return cell;
|
|
43
|
+
}
|
|
44
|
+
let overage = current - maxWidth;
|
|
45
|
+
const gapDirection = cell.options.align === CellAlignEnum.Left ? "gapRight" : "gapLeft";
|
|
46
|
+
if (cell.options[gapDirection] > 1) {
|
|
47
|
+
cell.options[gapDirection]--;
|
|
48
|
+
overage--;
|
|
49
|
+
if (overage === 0) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
const suffix = "..";
|
|
54
|
+
const sliced = cell.value.slice(0, -(overage + suffix.length));
|
|
55
|
+
cell.value = sliced + suffix;
|
|
56
|
+
return cell;
|
|
57
|
+
}
|
|
58
|
+
function calculateCellMinWidth(cell) {
|
|
59
|
+
const { gapLeft, gapRight } = cell.options;
|
|
60
|
+
return cell.value.length + gapLeft + gapRight;
|
|
61
|
+
}
|
|
10
62
|
class TextTableGenerator {
|
|
11
63
|
constructor(rows, options) {
|
|
12
64
|
this.data = [];
|
|
@@ -42,9 +94,12 @@ class TextTableGenerator {
|
|
|
42
94
|
.filter((row) => getRowType(row) === SpecialRowTypeEnum.Default)
|
|
43
95
|
.map((row) => row.length));
|
|
44
96
|
for (let index = 0; index < largestCellsCount; index++) {
|
|
45
|
-
const column = rows.map((row) =>
|
|
46
|
-
|
|
47
|
-
|
|
97
|
+
const column = rows.map((row) => {
|
|
98
|
+
var _a;
|
|
99
|
+
return getRowType(row) === SpecialRowTypeEnum.Default
|
|
100
|
+
? (_a = row.at(index)) !== null && _a !== void 0 ? _a : emptyCell()
|
|
101
|
+
: { row };
|
|
102
|
+
});
|
|
48
103
|
columns.push(column);
|
|
49
104
|
}
|
|
50
105
|
return columns;
|
|
@@ -53,9 +108,7 @@ class TextTableGenerator {
|
|
|
53
108
|
const metadata = this.context.metadata;
|
|
54
109
|
const columns = this.getColumns();
|
|
55
110
|
const columnsMetadata = columns.map((column) => ({
|
|
56
|
-
largestLength: Math.max(...column.map((element) => isCell(element)
|
|
57
|
-
? this.calculateCellMinWidth(element)
|
|
58
|
-
: 0)),
|
|
111
|
+
largestLength: Math.max(...column.map((element) => isCell(element) ? calculateCellMinWidth(element) : 0)),
|
|
59
112
|
}));
|
|
60
113
|
metadata.columns = columnsMetadata;
|
|
61
114
|
metadata.tableWidth = this.calculateTableWidth();
|
|
@@ -105,18 +158,10 @@ class TextTableGenerator {
|
|
|
105
158
|
drawCell(cell, expectedWidth) {
|
|
106
159
|
const { gapLeft, gapRight, align } = cell.options;
|
|
107
160
|
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)}`;
|
|
161
|
+
const minContentLength = calculateCellMinWidth(cell);
|
|
162
|
+
const lack = expectedWidth - minContentLength;
|
|
163
|
+
const contentWithPadding = addPaddingByCellAlign(value, lack, align);
|
|
164
|
+
return `${" ".repeat(gapLeft)}${contentWithPadding}${" ".repeat(gapRight)}`;
|
|
120
165
|
}
|
|
121
166
|
drawLine(setSymbol) {
|
|
122
167
|
let content = "";
|
|
@@ -144,9 +189,6 @@ class TextTableGenerator {
|
|
|
144
189
|
const borders = +!!this.options.borderLeft + +!!this.options.borderRight;
|
|
145
190
|
return cells + separators + borders;
|
|
146
191
|
}
|
|
147
|
-
calculateCellMinWidth(cell) {
|
|
148
|
-
return cell.value.length + cell.options.gapLeft + cell.options.gapRight;
|
|
149
|
-
}
|
|
150
192
|
}
|
|
151
193
|
var CellAlignEnum;
|
|
152
194
|
(function (CellAlignEnum) {
|
|
@@ -181,7 +223,7 @@ var SpecialRowTypeEnum;
|
|
|
181
223
|
class TextTableBuilder {
|
|
182
224
|
constructor() {
|
|
183
225
|
this.rows = [];
|
|
184
|
-
this.options = DEFAULT_TABLE_OPTIONS;
|
|
226
|
+
this.options = Object.assign({}, DEFAULT_TABLE_OPTIONS);
|
|
185
227
|
}
|
|
186
228
|
setBorderOptions(callback = () => "|", directions = [
|
|
187
229
|
BorderDirectionEnum.BorderLeft,
|
|
@@ -205,19 +247,19 @@ class TextTableBuilder {
|
|
|
205
247
|
}
|
|
206
248
|
return this;
|
|
207
249
|
}
|
|
208
|
-
addRowWithElements(elements, optionsForEveryElement) {
|
|
250
|
+
addRowWithElements(elements, optionsForEveryElement, useOnAddingOptions) {
|
|
209
251
|
const row = [];
|
|
210
252
|
for (const value of elements) {
|
|
211
|
-
this.pushCellToArray(row, value, optionsForEveryElement);
|
|
253
|
+
this.pushCellToArray(row, value, optionsForEveryElement, useOnAddingOptions);
|
|
212
254
|
}
|
|
213
255
|
this.pushRowToTable(row);
|
|
214
256
|
return this;
|
|
215
257
|
}
|
|
216
|
-
addMultilineRowWithElements(elements, optionsForEveryElement) {
|
|
258
|
+
addMultilineRowWithElements(elements, optionsForEveryElement, useOnAddingOptions) {
|
|
217
259
|
const separatedElements = elements.map((value) => value.split("\n"));
|
|
218
260
|
const largestHeight = Math.max(...separatedElements.map((sub) => sub.length));
|
|
219
261
|
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);
|
|
262
|
+
this.addRowWithElements(separatedElements.map((sub) => { var _a; return (_a = sub.at(index)) !== null && _a !== void 0 ? _a : ""; }), optionsForEveryElement, useOnAddingOptions);
|
|
221
263
|
}
|
|
222
264
|
return this;
|
|
223
265
|
}
|
|
@@ -233,20 +275,24 @@ class TextTableBuilder {
|
|
|
233
275
|
this.pushRowToTable(row);
|
|
234
276
|
return this;
|
|
235
277
|
}
|
|
236
|
-
addCellAtRow(rowIndex, cellValue, cellOptions) {
|
|
278
|
+
addCellAtRow(rowIndex, cellValue, cellOptions, useOnAddingOptions) {
|
|
237
279
|
const row = this.rows.at(rowIndex);
|
|
238
280
|
if (!row) {
|
|
239
|
-
throw new RangeError(
|
|
281
|
+
throw new RangeError();
|
|
240
282
|
}
|
|
241
283
|
if (getRowType(row) !== SpecialRowTypeEnum.Default) {
|
|
242
284
|
throw new Error("Is not default row: without cells");
|
|
243
285
|
}
|
|
244
|
-
this.pushCellToArray(row, cellValue, cellOptions);
|
|
286
|
+
this.pushCellToArray(row, cellValue, cellOptions, useOnAddingOptions);
|
|
245
287
|
return this;
|
|
246
288
|
}
|
|
247
|
-
pushCellToArray(array, cellValue, cellOptions = {}) {
|
|
289
|
+
pushCellToArray(array, cellValue, cellOptions = {}, pushOptions) {
|
|
248
290
|
const options = Object.assign({}, DEFAULT_CELL_OPTIONS, cellOptions);
|
|
249
|
-
|
|
291
|
+
const cell = { value: cellValue, options };
|
|
292
|
+
const { minWidth, maxWidth } = pushOptions !== null && pushOptions !== void 0 ? pushOptions : {};
|
|
293
|
+
minWidth && changeContentSetCellMinWidth(cell, minWidth);
|
|
294
|
+
maxWidth && changeContentSetCellMaxWidth(cell, maxWidth);
|
|
295
|
+
array.push(cell);
|
|
250
296
|
return this;
|
|
251
297
|
}
|
|
252
298
|
pushRowToTable(row) {
|