-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
255 lines (233 loc) · 5.91 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
"use strict";
var emojiRegex = require('emoji-regex')();
/**
* Strip ansi characters
*
* @param {string} str
* @returns {string}
*/
function stripANSI(str) {
return ('' + str).replace(/\x1B\[\d+m/g, '');
}
/**
* Strip wrap characters
*
* @param {string} str
* @returns {string}
*/
function stripWRAP(str) {
return ('' + str).replace(/[\n\r]+/g, ' ');
}
/**
* Strip emoji characters
*
* @param {string} str
* @returns {string}
*/
function stripEmoji(str) {
return ('' + str).replace(emojiRegex, '');
}
/**
* Split if string given
*
* @param {array|string} source
* @param {string|regex} symbol
* @returns {array}
*/
function split(source, symbol) {
return isArray(source) ? source : ('' + source).split(symbol);
}
/**
* Is string
*
* @param t
* @returns {boolean}
*/
function isString(t) {
return typeof t === 'string';
}
/**
* Is Array
*
* @param t
* @returns {boolean}
*/
function isArray(t) {
return Array.isArray(t);
}
/**
* String real width
*
* @param {string} str
* @returns {number}
*
* Emoji: 2
* Double-byte character: 2
* Others: 1
*/
function realWidth(str) {
if (str == null)
return 0;
str = stripANSI(str);
return str.length + (stripEmoji(str).match(/[^\x00-\xff]/g) || []).length;
}
/**
* Main
*
* @param source
* @param config
* @constructor
*/
function PC(source, config) {
if (source == null)
return;
this.config = {
prefix: '',
suffix: '',
placeholder: ' ',
columnSeparation: ' ',
rowSeparation: "\n",
rowSplitSymbol: "\n",
columnSplitSymbol: "\t"
};
this._STATS = {
originalSource: source,
source: null,
formatted: null,
rows: 0,
columns: 0,
maxWidth: [],
width: [],
align: []
};
this.params(config);
this.parseSource();
this.AnalyticAlignment();
this.format();
}
var tp = PC.prototype;
/**
* Parse configuration
*
* @param {object} config
*/
tp.params = function params(config) {
Object.assign(this.config, config);
};
/**
* Parsing the original source and create this._STATS.source
*/
tp.parseSource = function parse() {
this._STATS.source = split(this._STATS.originalSource, this.config.rowSplitSymbol).map(function (row, rowNum) {
if (this._STATS.columns < row.length)
this._STATS.columns = row.length;
this._STATS.width[rowNum] = [];
return split(row, this.config.columnSplitSymbol).map(function (column, columnNum) {
var length = this._STATS.width[rowNum][columnNum] = realWidth(column);
if (!this._STATS.maxWidth.hasOwnProperty(columnNum) || this._STATS.maxWidth[columnNum] < length)
this._STATS.maxWidth[columnNum] = length;
return stripWRAP(column);
}.bind(this));
}.bind(this));
this._STATS.rows = this._STATS.source.length;
};
/**
* Analytic alignment
*/
tp.AnalyticAlignment = function fixAlign() {
var align = (new Array(this._STATS.columns)).fill(0);
var optAlign = [];
if (this.config.hasOwnProperty('align')) {
if (isString(this.config.align)) {
optAlign = this.config.align.split('');
} else if (isArray(this.config.align)) {
optAlign = this.config.align;
}
Object.assign(align, optAlign.slice(0, this._STATS.columns).map(function (item) {
switch (item) {
case 1:
case 'c':
case 'center':
return 1;
case 2:
case 'r':
case 'right':
return 2;
case 0:
case 'l':
case 'left':
default:
return 0;
}
}));
}
this._STATS.align = align;
};
/**
* Format source and create this._STATS.formatted
*/
tp.format = function format() {
var placeholder = this.config.placeholder;
var columnSeparation = this.config.columnSeparation;
var rowSeparation = this.config.rowSeparation;
this._STATS.formatted = this.config.prefix +
this._STATS.source.map(function (row, rowNum) {
return row.map(function (column, columnNum) {
var fillZero = this._STATS.maxWidth[columnNum] - this._STATS.width[rowNum][columnNum];
if (fillZero > 0) {
var fl, fr;
switch (this._STATS.align[columnNum]) {
case 0:
fl = 0;
fr = fillZero;
return column + (new Array(fr + 1)).join(placeholder);
case 1:
fl = parseInt(fillZero / 2);
fr = fillZero - fl;
return (new Array(fl + 1)).join(placeholder) + column + (new Array(fr + 1)).join(placeholder);
case 2:
fl = fillZero;
fr = 0;
return (new Array(fl + 1)).join(placeholder) + column;
}
}
return column;
}.bind(this)).join(columnSeparation)
}.bind(this)).join(rowSeparation) +
this.config.suffix;
};
/**
* Output
*/
tp.output = function output() {
console.log(this._STATS.formatted);
};
/**
* Export wrapper
*
* @param {string|array} source
* @param {object|null} config
* @returns {PC}
* @constructor
*/
function WRAP(source, config) {
return new PC(source, config || {});
}
/**
* Direct call of output
*
* @param {string|array} source
* @param {object|null} config
*/
WRAP.output = function (source, config) {
(new PC(source, config || {})).output();
};
/**
* Inject to console with given string
*
* @param {string} key default: "columns"
*/
WRAP.injectConsole = function (key) {
console[key || 'columns'] = WRAP.output;
};
module.exports = WRAP;