-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.ts
377 lines (313 loc) · 11.8 KB
/
index.ts
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
const EOL = '\r\n';
export const emptyString = '';
export function isNullOrWhiteSpace(value: string | null): boolean {
return String.isNullOrWhiteSpace(value);
}
export function joinString(delimiter: string, ...args: (string | object | Array<any>)[]): string {
return String.join(delimiter, ...args);
}
export function formatString(format: string, ...args: any[]): string {
return String.format(format, ...args);
}
export class String {
private static readonly regexNumber = /{(\d+(:\w*)?)}/g;
private static readonly regexObject = /{(\w+(:\w*)?)}/g;
public static empty = '';
/**
* @deprecated The property should not be used, and will be removed in future versions! Use `String.empty` instead.
*/
public static Empty = '';
/**
* @deprecated The method should not be used, and will be removed in future versions! Use `String.isNullOrWhiteSpace()` instead.
*/
public static IsNullOrWhiteSpace(value: string | null | undefined): boolean {
return String.isNullOrWhiteSpace(value);
}
/**
* @deprecated The method should not be used, and will be removed in future versions! Use `String.join()` instead.
*/
public static Join(delimiter: string, ...args: (string | object | Array<any>)[]): string {
return String.join(delimiter, ...args);
}
/**
* @deprecated The method should not be used, and will be removed in future version!s Use `String.format()` instead.
*/
public static Format(format: string, ...args: any[]): string {
return String.format(format, ...args);
}
public static isNullOrWhiteSpace(value: string | null): boolean {
try {
if (value == null || value == 'undefined') {
return true;
}
return value.toString().replace(/\s/g, '').length < 1;
}
catch (e) {
console.log(e);
return false;
}
}
public static join(delimiter: string, ...args: (string | object | Array<any>)[]): string {
try {
const firstArg = args[0];
if (Array.isArray(firstArg) || firstArg instanceof Array) {
let tempString = String.empty;
for (let i = 0; i < firstArg.length; i++) {
const current = firstArg[i];
if (i < firstArg.length - 1) {
tempString += current + delimiter;
}
else {
tempString += current;
}
}
return tempString;
}
else if (typeof firstArg === 'object') {
let tempString = String.empty;
const objectArg = firstArg;
const keys = Object.keys(firstArg); //get all Properties of the Object as Array
keys.forEach(element => { tempString += (<any>objectArg)[element] + delimiter; });
tempString = tempString.slice(0, tempString.length - delimiter.length); //remove last delimiter
return tempString;
}
const stringArray = <string[]>args;
return String.joinString(delimiter, ...stringArray);
}
catch (e) {
console.log(e);
return String.empty;
}
}
public static format(format: string, ...args: any[]): string {
try {
if (format.match(String.regexNumber)) {
return String.formatString(String.regexNumber, format, args);
}
if (format.match(String.regexObject)) {
return String.formatString(String.regexObject, format, args, true);
}
return format;
}
catch (e) {
console.log(e);
return String.empty;
}
}
private static formatString(regex: any, format: string, args: any, parseByObject = false): string {
return format.replace(regex, function (match, x) { //0
const s = match.split(':');
if (s.length > 1) {
x = s[0].replace('{', '');
match = s[1].replace('}', ''); //U
}
let arg;
if (parseByObject) {
arg = args[0][x];
}
else {
arg = args[x];
}
if (arg == null || arg == undefined || match.match(/{\d+}/)) {
return arg;
}
arg = String.parsePattern(match, arg);
return typeof arg != 'undefined' && arg != null ? arg : String.empty;
});
}
private static parsePattern(match: 'L' | 'U' | 'd' | 's' | 'n' | 'x' | 'X' | string, arg: string | Date | number | any): string {
switch (match) {
case 'L': {
arg = arg.toLocaleLowerCase();
return arg;
}
case 'U': {
arg = arg.toLocaleUpperCase();
return arg;
}
case 'd': {
if (typeof (arg) === 'string') {
return String.getDisplayDateFromString(arg);
}
else if (arg instanceof Date) {
return String.format('{0:00}.{1:00}.{2:0000}', arg.getDate(), arg.getMonth(), arg.getFullYear());
}
break;
}
case 's': {
if (typeof (arg) === 'string') {
return String.getSortableDateFromString(arg);
}
else if (arg instanceof Date) {
return String.format('{0:0000}-{1:00}-{2:00}', arg.getFullYear(), arg.getMonth(), arg.getDate());
}
break;
}
case 'n': {//Tausender Trennzeichen
if (typeof (arg) !== 'string')
arg = arg.toString();
const replacedString = arg.replace(/,/g, '.');
if (isNaN(parseFloat(replacedString)) || replacedString.length <= 3) {
break;
}
const numberparts = replacedString.split(/\D+/g);
let parts = numberparts;
if (numberparts.length > 1) {
parts = [String.joinString('', ...(numberparts.splice(0, numberparts.length - 1))), numberparts[numberparts.length - 1]];
}
const integer = parts[0];
const mod = integer.length % 3;
let output = (mod > 0 ? (integer.substring(0, mod)) : String.empty);
const remainingGroups = integer.substring(mod).match(/.{3}/g);
output = output + '.' + String.join('.', remainingGroups);
arg = output + (parts.length > 1 ? ',' + parts[1] : '');
return arg;
}
case 'x': {
return this.decimalToHexString(arg);
}
case 'X': {
return this.decimalToHexString(arg, true);
}
default: {
break;
}
}
if ((typeof (arg) === 'number' || !isNaN(arg)) && !isNaN(+match) && !String.isNullOrWhiteSpace(arg)) {
return String.formatNumber(arg, match);
}
return arg;
}
private static decimalToHexString(value: string, upperCase = false) {
const parsed = parseFloat(value);
const hexNumber = parsed.toString(16);
return upperCase ? hexNumber.toLocaleUpperCase() : hexNumber;
}
private static getDisplayDateFromString(input: string): string {
const splitted: string[] = input.split('-');
if (splitted.length <= 1) {
return input;
}
let day = splitted[splitted.length - 1];
const month = splitted[splitted.length - 2];
const year = splitted[splitted.length - 3];
day = day.split('T')[0];
day = day.split(' ')[0];
return `${day}.${month}.${year}`;
}
private static getSortableDateFromString(input: string): string {
const splitted = input.replace(',', '').split('.');
if (splitted.length <= 1) {
return input;
}
const times = splitted[splitted.length - 1].split(' ');
let time = String.empty;
if (times.length > 1) {
time = times[times.length - 1];
}
const year = splitted[splitted.length - 1].split(' ')[0];
const month = splitted[splitted.length - 2];
const day = splitted[splitted.length - 3];
let result = `${year}-${month}-${day}`;
if (!String.isNullOrWhiteSpace(time) && time.length > 1) {
result += `T${time}`;
}
else {
result += 'T00:00:00';
}
return result;
}
private static formatNumber(input: number, formatTemplate: string): string {
const count = formatTemplate.length;
const stringValue = input.toString();
if (count <= stringValue.length) {
return stringValue;
}
let remainingCount = count - stringValue.length;
remainingCount += 1; //Array must have an extra entry
return new Array(remainingCount).join('0') + stringValue;
}
private static joinString(delimiter: string, ...args: string[]): string {
let temp = String.empty;
for (let i = 0; i < args.length; i++) {
if ((typeof args[i] == 'string' && String.isNullOrWhiteSpace(args[i]))
|| (typeof args[i] != 'number' && typeof args[i] != 'string')) {
continue;
}
const arg = '' + args[i];
temp += arg;
for (let i2 = i + 1; i2 < args.length; i2++) {
if (String.isNullOrWhiteSpace(args[i2])) {
continue;
}
temp += delimiter;
i = i2 - 1;
break;
}
}
return temp;
}
}
export class StringBuilder {
public Values: string[];
constructor(value = '') {
this.Values = [];
if (!String.isNullOrWhiteSpace(value)) {
this.Values = new Array(value);
}
}
public toString() {
return this.Values.join(String.empty);
}
/**
* @deprecated The method should not be used, and will be removed in future versions! Use `toString()` instead.
*/
public ToString() {
return this.toString();
}
public append(value: string) {
this.Values.push(value);
}
/**
* @deprecated The method should not be used, and will be removed in future versions! Use `append()` instead.
*/
public Append(value: string) {
this.append(value);
}
public appendLine(value: string) {
this.Values.push(EOL + value);
}
/**
* @deprecated The method should not be used, and will be removed in future versions! Use `appendLine()` instead.
*/
public AppendLine(value: string) {
this.appendLine(value);
}
public appendFormat(format: string, ...args: any[]) {
this.Values.push(String.format(format, ...args));
}
/**
* @deprecated The method should not be used, and will be removed in future versions! Use `appendFormat()` instead.
*/
public AppendFormat(format: string, ...args: any[]) {
this.appendFormat(format, ...args);
}
public appendLineFormat(format: string, ...args: any[]) {
this.Values.push(EOL + String.format(format, ...args));
}
/**
* @deprecated The method should not be used, and will be removed in future versions! Use `appendLineFormat()` instead.
*/
public AppendLineFormat(format: string, ...args: any[]) {
return this.appendLineFormat(format, ...args);
}
public clear() {
this.Values = [];
}
/**
* @deprecated The method should not be used, and will be removed in future versions! Use `clear()` instead.
*/
public Clear() {
this.clear();
}
}