/
index.js
203 lines (182 loc) · 5.06 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
/* eslint-disable func-names */
module.exports = ( function () {
/**
* Blueprint class for validation errors
* @class ValidationError
*/
class ValidationError {
constructor() {
this.name = this.constructor.name;
this.message = `${this.name}::`;
}
}
/**
* Error class for wrong number of arguments
* @class WrongNumberArgsError
* @extends {ValidationError}
*/
class WrongNumberArgsError extends ValidationError {
constructor(fncName, numberArgs) {
super()
this.message += ` ${fncName} expects at least ${numberArgs} arguments`;
}
}
/**
* Error class for wrong type of arguments
* @class WrongTypeArgsError
* @extends {ValidationError}
*/
class WrongTypeArgsError extends ValidationError {
constructor(fncName, typeArg) {
super()
this.message += ` ${fncName} expects a ${typeArg} argument`;
}
}
/**
* Error class for wrong value of arguments
* @class WrongValueArgsError
* @extends {ValidationError}
*/
class WrongValueArgsError extends ValidationError {
constructor(fncName, typeArg) {
super()
this.message += ` ${typeArg} argument is not a valid value for ${fncName}`;
}
}
/**
* @name newLineAndTabsBuilder
* @description Creates a string with a new line and as tabs
* as it's been specified in the parameter
* @param {Number} numberTabs
*
* @returns String
*/
function newLineAndTabsBuilder( numberTabs ) {
if (isNaN(Number(numberTabs))) {
throw new WrongTypeArgsError('newLineAndTabsBuilder', 'Number');
}
return '\n'.concat('\t'.repeat(numberTabs));
}
/**
* @name newLineAndSpacesBuilder
* @description Creates a string with a new line and as spaces
* as it's been specified in the parameter
* @param {Number} [numberSpaces = 6]
*
* @returns String
*/
function newLineAndSpacesBuilder( numberSpaces = 6 ) {
if (isNaN(Number(numberSpaces))) {
throw new WrongTypeArgsError('newLineAndSpacesBuilder', 'Number');
}
return '\n'.concat(' '.repeat(numberSpaces));
}
/**
* @name whiteSpaceBuilder
* @description generates white space depending as per argument passed
*
* @param {Number} numberSpaces
*
* @returns String
*/
function whiteSpaceBuilder(numberSpaces) {
if (arguments.length < 1) {
throw new WrongNumberArgsError('whiteSpaceBuilder', 1);
}
if (isNaN(Number(numberSpaces))) {
throw new WrongTypeArgsError('whiteSpaceBuilder', 'Number');
}
return ''.concat(' '.repeat(numberSpaces));
}
/**
* @name addTabSpaceOrBlank
* @description calls a method depending the argument type
* calls [newLineAndTabsBuilder, newLineAndSpacesBuilder, whiteSpaceBuilder] respectively
*
* @param {Boolean} condition
* @param {String} type > tab, space or whiteSpace
* @param {Number} counter
*
* @returns String
*/
function addTabSpaceOrBlank(condition, type, counter) {
if (arguments.length < 2) {
throw new WrongNumberArgsError('addTabSpaceOrBlank', 2);
}
const methods = {
tab: newLineAndTabsBuilder,
space: newLineAndSpacesBuilder,
whiteSpace: whiteSpaceBuilder
}
if (!Object.keys(methods).includes(type)) {
throw new WrongValueArgsError('addTabSpaceOrBlank', type);
}
return condition ? methods[type](counter) : '';
}
/**
* @name htmlCommentBuilder
* @description Creates an html comment with possible tabs
* @param {string} text
* @param {number} [withTab=0]
*
* @returns String
*/
function htmlCommentBuilder(text, withTab = 0) {
const tabs = ''.concat('\t'.repeat(withTab));
return `${tabs}<!-- ${text} -->`;
}
/**
* @name dividerBuilder
* @description Some markup that could be used to differentiate
* sections in CV, makes sense use it when rendering the markup
*
* @param {string} [className='divider']
*
* @returns String
*/
function dividerBuilder(className = 'divider') {
return `<div class="${className}"></div>`;
}
/**
* @name htmlTagBuilder
* @description creates html tags
*
* @param {*} content
* @param {*} tag
* @param {number} [numberTabs=2]
*
* @returns String
*/
function htmlTagBuilder( content, tag, numberTabs = 6 ) {
if (arguments.length < 2) {
throw new WrongNumberArgsError('htmlTagBuilder', 2);
}
const spaces = newLineAndSpacesBuilder( numberTabs );
return content.reduce(( acc, curr, index ) => {
const addSpaces = index !== 0 ? `${spaces}` : '';
return acc.concat( `${addSpaces}<${tag}>${curr}</${tag}>` );
}, '' );
}
/**
* @name isLastItem
* @description Checks if actual index is the last item in an Array
*
* @param {Array} array
* @param {Number} index
*
* @returns Boolean
*/
function isLastItem(array, index) {
return (index + 1) === array.length;
}
return {
newLineAndTabsBuilder,
newLineAndSpacesBuilder,
htmlTagBuilder,
htmlCommentBuilder,
dividerBuilder,
addTabSpaceOrBlank,
isLastItem,
whiteSpaceBuilder
};
}());