@sc-voice/tools 3.20.0 → 3.22.0
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.
- package/package.json +1 -1
- package/src/text/list.mjs +99 -24
package/package.json
CHANGED
package/src/text/list.mjs
CHANGED
|
@@ -11,11 +11,19 @@ export class ListFactory {
|
|
|
11
11
|
nColumns = 0,
|
|
12
12
|
nRows = 0,
|
|
13
13
|
nLists = 0,
|
|
14
|
+
rowSeparator = '\n',
|
|
15
|
+
colSeparator = ' ',
|
|
16
|
+
order = 'column-major',
|
|
17
|
+
precision = 3,
|
|
14
18
|
} = opts;
|
|
15
19
|
Object.assign(this, {
|
|
16
20
|
nColumns,
|
|
17
21
|
nRows,
|
|
18
22
|
nLists,
|
|
23
|
+
order,
|
|
24
|
+
precision,
|
|
25
|
+
rowSeparator,
|
|
26
|
+
colSeparator,
|
|
19
27
|
});
|
|
20
28
|
}
|
|
21
29
|
|
|
@@ -27,7 +35,13 @@ export class ListFactory {
|
|
|
27
35
|
}
|
|
28
36
|
|
|
29
37
|
createList(opts = {}) {
|
|
30
|
-
let {
|
|
38
|
+
let {
|
|
39
|
+
name, // title
|
|
40
|
+
values=[],
|
|
41
|
+
separator = ',', // join() separator
|
|
42
|
+
widths, // element string widths
|
|
43
|
+
precision = this.precision, // numeric precision
|
|
44
|
+
} = opts;
|
|
31
45
|
|
|
32
46
|
let list = [...values];
|
|
33
47
|
|
|
@@ -37,46 +51,68 @@ export class ListFactory {
|
|
|
37
51
|
}
|
|
38
52
|
|
|
39
53
|
Object.defineProperty(list, 'name', {
|
|
40
|
-
|
|
54
|
+
writable: true,
|
|
41
55
|
value: name,
|
|
42
56
|
});
|
|
57
|
+
Object.defineProperty(list, 'precision', {
|
|
58
|
+
writable: true,
|
|
59
|
+
value: precision,
|
|
60
|
+
});
|
|
43
61
|
Object.defineProperty(list, 'separator', {
|
|
44
|
-
|
|
62
|
+
writable: true,
|
|
45
63
|
value: separator,
|
|
46
64
|
});
|
|
65
|
+
Object.defineProperty(list, 'widths', {
|
|
66
|
+
writable: true,
|
|
67
|
+
value: widths,
|
|
68
|
+
});
|
|
47
69
|
Object.defineProperty(list, 'toString', {
|
|
48
70
|
value: () => {
|
|
49
|
-
|
|
71
|
+
let strs = list.toStrings();
|
|
72
|
+
return strs.join(list.separator);
|
|
50
73
|
}
|
|
51
74
|
});
|
|
52
75
|
Object.defineProperty(list, 'toStrings', {
|
|
53
76
|
value: () => {
|
|
54
77
|
let s5s = [];
|
|
55
78
|
let { showName = false } = opts;
|
|
56
|
-
let { name } = list;
|
|
79
|
+
let { name, widths } = list;
|
|
57
80
|
|
|
58
81
|
if (showName) {
|
|
59
82
|
s5s.push(name);
|
|
60
83
|
}
|
|
61
84
|
for (let i = 0; i < list.length; i++) {
|
|
62
85
|
let v = list[i];
|
|
86
|
+
let s = '';
|
|
63
87
|
switch (typeof v) {
|
|
64
88
|
case 'object':
|
|
65
89
|
if (
|
|
66
90
|
v?.constructor !== Object &&
|
|
67
91
|
typeof v?.toString === 'function'
|
|
68
92
|
) {
|
|
69
|
-
|
|
93
|
+
s = v.toString();
|
|
70
94
|
} else {
|
|
71
|
-
|
|
95
|
+
s = JSON.stringify(v);
|
|
72
96
|
}
|
|
73
97
|
break;
|
|
98
|
+
case 'number': {
|
|
99
|
+
let sRaw = precision ? v.toFixed(precision) : v+'';
|
|
100
|
+
let sShort = sRaw.replace(/\.?0+$/, '');
|
|
101
|
+
s = Number(sShort) === v ? sShort : sRaw;
|
|
102
|
+
}
|
|
103
|
+
break;
|
|
74
104
|
default:
|
|
75
|
-
|
|
105
|
+
s += v;
|
|
76
106
|
break;
|
|
77
107
|
}
|
|
108
|
+
let width = widths?.[i];
|
|
109
|
+
if (width) {
|
|
110
|
+
s = s.substring(0,width).padEnd(width);
|
|
111
|
+
}
|
|
112
|
+
s5s.push(s);
|
|
78
113
|
}
|
|
79
114
|
|
|
115
|
+
|
|
80
116
|
return s5s;
|
|
81
117
|
}
|
|
82
118
|
|
|
@@ -86,23 +122,45 @@ export class ListFactory {
|
|
|
86
122
|
}
|
|
87
123
|
|
|
88
124
|
createColumn(opts = {}) {
|
|
89
|
-
let {
|
|
125
|
+
let {
|
|
126
|
+
name,
|
|
127
|
+
values=[],
|
|
128
|
+
separator = '\n',
|
|
129
|
+
precision = this.precision,
|
|
130
|
+
} = opts;
|
|
90
131
|
|
|
91
132
|
this.nColumns++;
|
|
92
133
|
if (name == null) {
|
|
93
134
|
name = 'column' + this.nColumns;
|
|
94
135
|
}
|
|
95
|
-
return this.createList({
|
|
136
|
+
return this.createList({
|
|
137
|
+
name,
|
|
138
|
+
precision,
|
|
139
|
+
separator,
|
|
140
|
+
values,
|
|
141
|
+
});
|
|
96
142
|
}
|
|
97
143
|
|
|
98
144
|
createRow(opts = {}) {
|
|
99
|
-
let {
|
|
145
|
+
let {
|
|
146
|
+
name,
|
|
147
|
+
values=[],
|
|
148
|
+
separator = '\t',
|
|
149
|
+
widths,
|
|
150
|
+
precision = this.precision,
|
|
151
|
+
} = opts;
|
|
100
152
|
|
|
101
153
|
this.nRows++;
|
|
102
154
|
if (name == null) {
|
|
103
155
|
name = 'row' + this.nRows;
|
|
104
156
|
}
|
|
105
|
-
return this.createList({
|
|
157
|
+
return this.createList({
|
|
158
|
+
name,
|
|
159
|
+
precision,
|
|
160
|
+
separator,
|
|
161
|
+
values,
|
|
162
|
+
widths,
|
|
163
|
+
});
|
|
106
164
|
}
|
|
107
165
|
|
|
108
166
|
wrapList(list, opts = {}) {
|
|
@@ -110,22 +168,28 @@ export class ListFactory {
|
|
|
110
168
|
const dbg = 0;
|
|
111
169
|
let {
|
|
112
170
|
name,
|
|
113
|
-
|
|
171
|
+
maxValues = 2,
|
|
114
172
|
namePrefix = 'column',
|
|
115
|
-
order =
|
|
116
|
-
|
|
173
|
+
order = this.order,
|
|
174
|
+
rowSeparator = this.rowSeparator,
|
|
175
|
+
colSeparator = this.colSeparator,
|
|
176
|
+
precision = this.precision,
|
|
117
177
|
} = opts;
|
|
118
178
|
|
|
119
|
-
let singleList =
|
|
179
|
+
let singleList = this.createColumn({
|
|
180
|
+
name,
|
|
181
|
+
separator:rowSeparator,
|
|
182
|
+
precision,
|
|
183
|
+
});
|
|
184
|
+
let newRow = (separator) => this.createRow({ separator, precision });
|
|
120
185
|
name = name || singleList.name;
|
|
121
|
-
separator = separator || singleList.separator;
|
|
122
186
|
switch (order) {
|
|
123
187
|
case 'col-major':
|
|
124
188
|
case 'column-major':
|
|
125
189
|
{
|
|
126
190
|
let transpose = [];
|
|
127
191
|
let col;
|
|
128
|
-
let nRows = Math.ceil(list.length /
|
|
192
|
+
let nRows = Math.ceil(list.length / maxValues);
|
|
129
193
|
dbg > 1 && cc.fyi1(msg + 0.1, { nRows });
|
|
130
194
|
for (let i = 0; i < list.length; i++) {
|
|
131
195
|
let ir = i % nRows;
|
|
@@ -143,15 +207,15 @@ export class ListFactory {
|
|
|
143
207
|
transpose.push(col);
|
|
144
208
|
}
|
|
145
209
|
let row;
|
|
146
|
-
for (let i = 0; i < nRows *
|
|
147
|
-
let ic = i %
|
|
210
|
+
for (let i = 0; i < nRows * maxValues; i++) {
|
|
211
|
+
let ic = i % maxValues;
|
|
148
212
|
if (ic === 0) {
|
|
149
213
|
if (row) {
|
|
150
214
|
singleList.push(row);
|
|
151
215
|
}
|
|
152
|
-
row =
|
|
216
|
+
row = newRow(colSeparator);
|
|
153
217
|
}
|
|
154
|
-
let ir = Math.floor(i /
|
|
218
|
+
let ir = Math.floor(i / maxValues);
|
|
155
219
|
dbg > 1 && cc.fyi1(msg + 0.2, { ic, ir });
|
|
156
220
|
let vc = transpose[ic];
|
|
157
221
|
if (vc !== undefined) {
|
|
@@ -169,12 +233,12 @@ export class ListFactory {
|
|
|
169
233
|
{
|
|
170
234
|
let row;
|
|
171
235
|
for (let i = 0; i < list.length; i++) {
|
|
172
|
-
let ic = i %
|
|
236
|
+
let ic = i % maxValues;
|
|
173
237
|
if (ic === 0) {
|
|
174
238
|
if (row) {
|
|
175
239
|
singleList.push(row);
|
|
176
240
|
}
|
|
177
|
-
row =
|
|
241
|
+
row = newRow(colSeparator);
|
|
178
242
|
}
|
|
179
243
|
row.push(list[i]);
|
|
180
244
|
}
|
|
@@ -185,6 +249,17 @@ export class ListFactory {
|
|
|
185
249
|
break;
|
|
186
250
|
}
|
|
187
251
|
|
|
252
|
+
// compute row element widths
|
|
253
|
+
let widths = new Array(maxValues).fill(0);
|
|
254
|
+
singleList.forEach(row => {
|
|
255
|
+
let strs = row.toStrings();
|
|
256
|
+
strs.map((s,i)=> {
|
|
257
|
+
widths[i] = Math.max(s.length, widths[i]);
|
|
258
|
+
});
|
|
259
|
+
});
|
|
260
|
+
singleList.forEach(row => row.widths = widths);
|
|
261
|
+
dbg && cc.ok1(msg+1, widths[0], maxValues, 'widths:', widths);
|
|
262
|
+
|
|
188
263
|
return singleList;
|
|
189
264
|
}
|
|
190
265
|
}
|