-
-
Notifications
You must be signed in to change notification settings - Fork 167
/
FormatHelpers.ts
121 lines (110 loc) · 3.62 KB
/
FormatHelpers.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
/* eslint-disable no-unused-vars */
import {
camelCase,
pascalCase,
paramCase,
constantCase,
} from 'change-case';
export enum IndentationTypes {
TABS = 'tabs',
SPACES = 'spaces',
}
export class FormatHelpers {
/**
* Upper first char in given string value.
* @param {string} value to change
* @returns {string}
*/
static upperFirst(value: string): string {
return value.charAt(0).toUpperCase() + value.slice(1);
}
/**
* Transform into a string with the separator denoted by the next word capitalized.
* @param {string} value to transform
* @returns {string}
*/
static toCamelCase = camelCase;
/**
* Transform into a string of capitalized words without separators.
* @param {string} value to transform
* @returns {string}
*/
static toPascalCase = pascalCase;
/**
* Transform into a lower cased string with dashes between words.
* @param {string} value to transform
* @returns {string}
*/
static toParamCase = paramCase;
/**
* Transform into upper case string with an underscore between words.
* @param {string} value to transform
* @returns {string}
*/
static toConstantCase = constantCase;
/**
* Ensures breaking text into new lines according to newline char (`\n`) in text.
* @param {(string | string[])} lines to breaks
* @returns {string[]}
*/
static breakLines(lines: string | string[]): string[] {
lines = Array.isArray(lines) ? lines : [lines];
return lines.map(line => line.split('\n')).flatMap(line => line);
}
/**
* Ensures indentations are prepended to content.
* @param {string} content to prepend the indentation.
* @param {number} size the number of indendations to use. 1 by default
* @param {IndentationTypes} type the type of indendations to use. SPACES by default.
* @returns {string}
*/
static indent(content = '', size = 1, type: IndentationTypes = IndentationTypes.SPACES): string {
if (size < 1) {
return content;
}
// if the content includes new lines ensure that they have the added indentation as well.
if (content.includes('\n')) {
const newLineArray = content.split('\n');
return newLineArray.reduce((accumulator, value) => {
const newValue = value.trim() === '' ? value : `${this.getIndentation(size, type)}${value}`;
return accumulator === '' ? newValue : `${accumulator}\n${newValue}`;
}, '');
}
return `${this.getIndentation(size, type)}${content}`;
}
/**
* Get the indendation string based on how many and which type of indentation are requested.
* @private
* @param {number} size the number of indendations to use
* @param {IndentationTypes} type the type of indendations to use. SPACES by default.
* @returns {string}
*/
private static getIndentation(size = 0, type: IndentationTypes = IndentationTypes.SPACES): string {
const whitespaceChar = type === IndentationTypes.SPACES ? ' ' : '\t';
return Array(size).fill(whitespaceChar).join('');
}
/**
* Render given JSON Schema example to string
*
* @param {Array<Any>} examples to render
* @returns {string}
*/
static renderJSONExamples(examples: any[]): string {
let renderedExamples = '';
if (Array.isArray(examples)) {
examples.forEach(example => {
if (renderedExamples !== '') {renderedExamples += ', ';}
if (typeof example === 'object') {
try {
renderedExamples += JSON.stringify(example);
} catch (ignore) {
renderedExamples += example;
}
} else {
renderedExamples += example;
}
});
}
return renderedExamples;
}
}