@zoodogood/utils 1.0.5-change.421 → 1.0.6-change.695

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.
@@ -0,0 +1,66 @@
1
+ interface ITextTableGeneratorContext {
2
+ content: string;
3
+ metadata: IContextMetadata;
4
+ currentRow: number;
5
+ }
6
+ interface IContextMetadata {
7
+ columns?: {
8
+ largestLength: number;
9
+ }[];
10
+ tableWidth?: number;
11
+ separatorsIndexesInRow?: number[];
12
+ }
13
+ declare enum CellAlignEnum {
14
+ Left = 0,
15
+ Right = 1,
16
+ Center = 2
17
+ }
18
+ interface ICellOptions {
19
+ gapLeft: number;
20
+ gapRight: number;
21
+ align: CellAlignEnum;
22
+ removeNextSeparator?: boolean;
23
+ }
24
+ interface ITableCell {
25
+ value: string;
26
+ options: ICellOptions;
27
+ }
28
+ type TCellSetSymbolCallback = (context: ITextTableGeneratorContext, index: number) => string;
29
+ interface ITableOptions {
30
+ borderLeft: null | TCellSetSymbolCallback;
31
+ borderRight: null | TCellSetSymbolCallback;
32
+ borderTop: null | TCellSetSymbolCallback;
33
+ borderBottom: null | TCellSetSymbolCallback;
34
+ separator: string;
35
+ }
36
+ declare enum BorderDirectionEnum {
37
+ BorderLeft = 0,
38
+ BorderRight = 1,
39
+ BorderTop = 2,
40
+ BorderBottom = 3
41
+ }
42
+ declare enum SpecialRowTypeEnum {
43
+ Default = 0,
44
+ Display = 1
45
+ }
46
+ interface ITableSpecialDisplayRow {
47
+ type: SpecialRowTypeEnum.Display;
48
+ display: TCellSetSymbolCallback;
49
+ }
50
+ type TTableRow = ITableCell[] | ITableSpecialDisplayRow;
51
+ declare class TextTableBuilder {
52
+ rows: TTableRow[];
53
+ protected options: ITableOptions;
54
+ setBorderOptions(callback?: TCellSetSymbolCallback, directions?: BorderDirectionEnum[]): this;
55
+ addRowWithElements(elements: ITableCell["value"][], optionsForEveryElement?: Partial<ICellOptions>): this;
56
+ addMultilineRowWithElements(elements: ITableCell["value"][], optionsForEveryElement?: Partial<ICellOptions>): this;
57
+ addEmptyRow(): this;
58
+ addRowSeparator(setSymbol?: TCellSetSymbolCallback): this;
59
+ addCellAtRow(rowIndex: number, cellValue: ITableCell["value"], cellOptions: ICellOptions): this;
60
+ pushCellToArray<T = ITableCell>(array: (T | ITableCell)[], cellValue: ITableCell["value"], cellOptions?: Partial<ICellOptions>): this;
61
+ pushRowToTable(row: TTableRow): this;
62
+ generateTextContent(): string;
63
+ }
64
+ export { TextTableBuilder };
65
+ export { SpecialRowTypeEnum, CellAlignEnum, BorderDirectionEnum };
66
+ export type { ICellOptions, IContextMetadata, ITableCell, ITableOptions, ITextTableGeneratorContext, TTableRow, };
@@ -0,0 +1,269 @@
1
+ function getRowType(row) {
2
+ if ("type" in row === false) {
3
+ return SpecialRowTypeEnum.Default;
4
+ }
5
+ return row.type;
6
+ }
7
+ function isCell(target) {
8
+ return "value" in target && "options" in target;
9
+ }
10
+ class TextTableGenerator {
11
+ constructor(rows, options) {
12
+ this.data = [];
13
+ this.context = {
14
+ content: "",
15
+ metadata: {},
16
+ currentRow: 0,
17
+ };
18
+ this.data = rows;
19
+ this.options = options;
20
+ this.parseMetadata();
21
+ }
22
+ generateTextContent() {
23
+ const context = this.context;
24
+ if (this.options.borderTop !== null) {
25
+ context.content += this.drawBlockBorder(BorderDirectionEnum.BorderTop);
26
+ context.content += "\n";
27
+ }
28
+ for (const row of this.data) {
29
+ context.currentRow = this.data.indexOf(row);
30
+ context.content += this.drawRow(row);
31
+ context.content += "\n";
32
+ }
33
+ if (this.options.borderBottom !== null) {
34
+ context.content += this.drawBlockBorder(BorderDirectionEnum.BorderBottom);
35
+ }
36
+ return context.content;
37
+ }
38
+ getColumns() {
39
+ const columns = [];
40
+ const rows = this.data;
41
+ const largestCellsCount = Math.max(...rows
42
+ .filter((row) => getRowType(row) === SpecialRowTypeEnum.Default)
43
+ .map((row) => row.length));
44
+ for (let index = 0; index < largestCellsCount; index++) {
45
+ const column = rows.map((row) => getRowType(row) === SpecialRowTypeEnum.Default
46
+ ? row.at(index)
47
+ : { row });
48
+ columns.push(column);
49
+ }
50
+ return columns;
51
+ }
52
+ parseMetadata() {
53
+ const metadata = this.context.metadata;
54
+ const columns = this.getColumns();
55
+ const columnsMetadata = columns.map((column) => ({
56
+ largestLength: Math.max(...column.map((element) => isCell(element)
57
+ ? this.calculateCellMinWidth(element)
58
+ : 0)),
59
+ }));
60
+ metadata.columns = columnsMetadata;
61
+ metadata.tableWidth = this.calculateTableWidth();
62
+ metadata.separatorsIndexesInRow = (() => {
63
+ const indexes = [];
64
+ let current = 0;
65
+ if (!!this.options.borderLeft) {
66
+ indexes.push(current);
67
+ }
68
+ for (const { largestLength } of columnsMetadata) {
69
+ current += largestLength + 1;
70
+ indexes.push(current);
71
+ }
72
+ if (!this.options.borderRight) {
73
+ indexes.pop();
74
+ }
75
+ return indexes;
76
+ })();
77
+ }
78
+ drawRow(row) {
79
+ var _a, _b, _c, _d, _e, _f;
80
+ let content = "";
81
+ const context = this.context;
82
+ const metadata = context.metadata;
83
+ if (getRowType(row) === SpecialRowTypeEnum.Default) {
84
+ row = row;
85
+ content += (_c = (_b = (_a = this.options).borderLeft) === null || _b === void 0 ? void 0 : _b.call(_a, context, context.currentRow)) !== null && _c !== void 0 ? _c : "";
86
+ for (const cellIndex in row) {
87
+ const cell = row.at(+cellIndex);
88
+ const expectedWidth = metadata.columns.at(+cellIndex).largestLength;
89
+ content += this.drawCell(cell, expectedWidth);
90
+ if (+cellIndex !== row.length - 1) {
91
+ content += !cell.options.removeNextSeparator
92
+ ? this.options.separator
93
+ : " ";
94
+ }
95
+ }
96
+ content += (_f = (_e = (_d = this.options).borderRight) === null || _e === void 0 ? void 0 : _e.call(_d, context, context.currentRow)) !== null && _f !== void 0 ? _f : "";
97
+ return content;
98
+ }
99
+ if (getRowType(row) === SpecialRowTypeEnum.Display) {
100
+ row = row;
101
+ content += this.drawLine(row.display);
102
+ }
103
+ return content;
104
+ }
105
+ drawCell(cell, expectedWidth) {
106
+ const { gapLeft, gapRight, align } = cell.options;
107
+ const { value } = cell;
108
+ const minContentLength = this.calculateCellMinWidth(cell);
109
+ const addidableGapLeft = align === CellAlignEnum.Right
110
+ ? expectedWidth - minContentLength
111
+ : align === CellAlignEnum.Center
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)}`;
120
+ }
121
+ drawLine(setSymbol) {
122
+ let content = "";
123
+ const context = this.context;
124
+ const length = this.calculateTableWidth();
125
+ for (const index in [...new Array(length)]) {
126
+ const symbol = setSymbol(context, +index);
127
+ content += symbol;
128
+ }
129
+ return content;
130
+ }
131
+ drawBlockBorder(borderDirection) {
132
+ const callback = borderDirection === BorderDirectionEnum.BorderTop
133
+ ? this.options.borderTop
134
+ : this.options.borderBottom;
135
+ return this.drawLine(callback);
136
+ }
137
+ calculateTableWidth() {
138
+ const { columns } = this.context.metadata;
139
+ if (!columns) {
140
+ throw new Error("No columns field; Tip: Use <this>.parseMetadata() previous");
141
+ }
142
+ const cells = columns.reduce((acc, current) => current.largestLength + acc, 0);
143
+ const separators = columns.length - 1;
144
+ const borders = +!!this.options.borderLeft + +!!this.options.borderRight;
145
+ return cells + separators + borders;
146
+ }
147
+ calculateCellMinWidth(cell) {
148
+ return cell.value.length + cell.options.gapLeft + cell.options.gapRight;
149
+ }
150
+ }
151
+ var CellAlignEnum;
152
+ (function (CellAlignEnum) {
153
+ CellAlignEnum[CellAlignEnum["Left"] = 0] = "Left";
154
+ CellAlignEnum[CellAlignEnum["Right"] = 1] = "Right";
155
+ CellAlignEnum[CellAlignEnum["Center"] = 2] = "Center";
156
+ })(CellAlignEnum || (CellAlignEnum = {}));
157
+ const DEFAULT_CELL_OPTIONS = {
158
+ gapLeft: 2,
159
+ gapRight: 2,
160
+ align: CellAlignEnum.Left,
161
+ };
162
+ var BorderDirectionEnum;
163
+ (function (BorderDirectionEnum) {
164
+ BorderDirectionEnum[BorderDirectionEnum["BorderLeft"] = 0] = "BorderLeft";
165
+ BorderDirectionEnum[BorderDirectionEnum["BorderRight"] = 1] = "BorderRight";
166
+ BorderDirectionEnum[BorderDirectionEnum["BorderTop"] = 2] = "BorderTop";
167
+ BorderDirectionEnum[BorderDirectionEnum["BorderBottom"] = 3] = "BorderBottom";
168
+ })(BorderDirectionEnum || (BorderDirectionEnum = {}));
169
+ const DEFAULT_TABLE_OPTIONS = {
170
+ borderLeft: null,
171
+ borderRight: null,
172
+ borderTop: null,
173
+ borderBottom: null,
174
+ separator: "|",
175
+ };
176
+ var SpecialRowTypeEnum;
177
+ (function (SpecialRowTypeEnum) {
178
+ SpecialRowTypeEnum[SpecialRowTypeEnum["Default"] = 0] = "Default";
179
+ SpecialRowTypeEnum[SpecialRowTypeEnum["Display"] = 1] = "Display";
180
+ })(SpecialRowTypeEnum || (SpecialRowTypeEnum = {}));
181
+ class TextTableBuilder {
182
+ constructor() {
183
+ this.rows = [];
184
+ this.options = DEFAULT_TABLE_OPTIONS;
185
+ }
186
+ setBorderOptions(callback = () => "|", directions = [
187
+ BorderDirectionEnum.BorderLeft,
188
+ BorderDirectionEnum.BorderRight,
189
+ ]) {
190
+ for (const direction of directions) {
191
+ switch (direction) {
192
+ case BorderDirectionEnum.BorderLeft:
193
+ this.options.borderLeft = callback;
194
+ break;
195
+ case BorderDirectionEnum.BorderRight:
196
+ this.options.borderRight = callback;
197
+ break;
198
+ case BorderDirectionEnum.BorderTop:
199
+ this.options.borderTop = callback;
200
+ break;
201
+ case BorderDirectionEnum.BorderBottom:
202
+ this.options.borderBottom = callback;
203
+ break;
204
+ }
205
+ }
206
+ return this;
207
+ }
208
+ addRowWithElements(elements, optionsForEveryElement) {
209
+ const row = [];
210
+ for (const value of elements) {
211
+ this.pushCellToArray(row, value, optionsForEveryElement);
212
+ }
213
+ this.pushRowToTable(row);
214
+ return this;
215
+ }
216
+ addMultilineRowWithElements(elements, optionsForEveryElement) {
217
+ const separatedElements = elements.map((value) => value.split("\n"));
218
+ const largestHeight = Math.max(...separatedElements.map((sub) => sub.length));
219
+ 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);
221
+ }
222
+ return this;
223
+ }
224
+ addEmptyRow() {
225
+ this.pushRowToTable([]);
226
+ return this;
227
+ }
228
+ addRowSeparator(setSymbol = () => "-") {
229
+ const row = {
230
+ display: setSymbol,
231
+ type: SpecialRowTypeEnum.Display,
232
+ };
233
+ this.pushRowToTable(row);
234
+ return this;
235
+ }
236
+ addCellAtRow(rowIndex, cellValue, cellOptions) {
237
+ const row = this.rows.at(rowIndex);
238
+ if (!row) {
239
+ throw new RangeError("");
240
+ }
241
+ if (getRowType(row) !== SpecialRowTypeEnum.Default) {
242
+ throw new Error("Is not default row: without cells");
243
+ }
244
+ this.pushCellToArray(row, cellValue, cellOptions);
245
+ return this;
246
+ }
247
+ pushCellToArray(array, cellValue, cellOptions = {}) {
248
+ const options = Object.assign({}, DEFAULT_CELL_OPTIONS, cellOptions);
249
+ array.push({ value: cellValue, options });
250
+ return this;
251
+ }
252
+ pushRowToTable(row) {
253
+ this.rows.push(row);
254
+ return this;
255
+ }
256
+ generateTextContent() {
257
+ return new TextTableGenerator(this.rows, this.options).generateTextContent();
258
+ }
259
+ }
260
+ export { TextTableBuilder };
261
+ export { SpecialRowTypeEnum, CellAlignEnum, BorderDirectionEnum };
262
+ const builder = new TextTableBuilder()
263
+ .setBorderOptions()
264
+ .addRowWithElements(["1", "212", "3"], { align: CellAlignEnum.Left })
265
+ .addRowWithElements(["1", "2", "3"], { align: CellAlignEnum.Right })
266
+ .addRowSeparator()
267
+ .addRowWithElements(["777", "555", "1999"], { align: CellAlignEnum.Center });
268
+ // the example has been simplified
269
+ console.log(builder.generateTextContent());
@@ -1,3 +1,4 @@
1
+ export * from './TextTableBuilder.js';
1
2
  interface IEndingOptions {
2
3
  unite?: (quantity: number, word: string) => string;
3
4
  }
@@ -1,3 +1,4 @@
1
+ export * from './TextTableBuilder.js';
1
2
  function ending(quantity = 0, base, multiple, alone, double, options = {}) {
2
3
  if (isNaN(quantity))
3
4
  return NaN;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@zoodogood/utils",
3
3
  "type": "module",
4
- "version": "1.0.5-change.421",
4
+ "version": "1.0.6-change.695",
5
5
  "description": "",
6
6
  "main": "lib/index.js",
7
7
  "types": "lib/index.d.ts",