-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparameter.js
More file actions
174 lines (161 loc) · 4.43 KB
/
parameter.js
File metadata and controls
174 lines (161 loc) · 4.43 KB
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
/**
* Represents a parameter.
* @class
*/
export default class Parameter {
#name = null
#description = null
#value = ''
#defaultValue = ''
#availableValues = []
#possibleValues = []
#optional = false
#type = 'text'
/**
* @param {String} name - The name of the parameter.
* @returns {Parameter} The created parameter instance.
*/
constructor(name) {
if (typeof name !== 'string' || name.trim() === '') {
throw new Error('name must be a non-empty string.')
}
if (!/^[A-Z0-9_]+$/.test(name)) {
throw new Error('name expected uppercase letters, numbers, and underscores.')
}
this.#name = name
}
/**
* @returns {String} The name of the parameter.
*/
getName() {
return this.#name
}
/**
* @param {Boolean} optional - If the parameter is optional.
* @returns {Parameter} The updated parameter instance.
*/
setOptional(optional) {
if (typeof optional !== 'boolean') {
throw new Error('optional must be a boolean.')
}
this.#optional = optional
return this
}
/**
* @returns {Boolean} If the parameter is optional.
*/
isOptional() {
return this.#optional
}
/**
* @param {String} description - The description of the parameter.
* @returns {Parameter} The updated parameter instance.
*/
setDescription(description) {
if (typeof description !== 'string' || description.trim() === '') {
throw new Error('description must be a non-empty string.')
}
this.#description = description
return this
}
/**
* @param {String} defaultValue - The default value of the parameter.
* @returns {Parameter} The updated parameter instance.
*/
setDefaultValue(defaultValue) {
if (typeof defaultValue !== 'string') {
throw new Error('defaultValue must be a string.')
}
this.#defaultValue = defaultValue
return this
}
/**
* @param {String} type - The type of the parameter.
* @returns {Parameter} The updated parameter instance.
*/
setType(type) {
const allowedTypes = [
'text',
'password',
'textarea',
'email',
'search',
'tel',
'number',
'url',
'time',
'date',
'datetime-local',
'select',
'color',
]
if (!allowedTypes.includes(type)) {
throw new Error(`type must be one of the following strings: ${allowedTypes.join('|')}`)
}
this.#type = type
return this
}
/**
* @returns {String} The value of the parameter.
*/
getValue() {
return process.env[this.#name] ?? (this.#value || this.#defaultValue)
}
/**
* @param {String} value - The value of the parameter.
* @returns {Parameter} The updated parameter instance.
*/
setValue(value) {
if (typeof value !== 'string') {
throw new Error('value must be a string.')
}
this.#value = value
return this
}
/**
* @returns {Boolean} If the parameter is not empty.
*/
hasValue() {
return this.getValue() !== ''
}
/**
* @param {Array<String>} availableValues - The available values of the parameter.
* @returns {Parameter} The updated parameter instance.
*/
setAvailableValues(availableValues) {
if (!Array.isArray(availableValues) || availableValues.length === 0) {
throw new Error('availableValues must be a non-empty array.')
}
if (!availableValues.every((value) => typeof value === 'string' && value.trim() !== '')) {
throw new Error('Each item in availableValues must be a non-empty string.')
}
this.#availableValues = availableValues
return this
}
/**
* @param {Array<String>} possibleValues - The possible values of the parameter.
* @returns {Parameter} The updated parameter instance.
*/
setPossibleValues(possibleValues) {
if (!Array.isArray(possibleValues) || possibleValues.length === 0) {
throw new Error('possibleValues must be a non-empty array.')
}
if (!possibleValues.every((value) => typeof value === 'string' && value.trim() !== '')) {
throw new Error('Each item in possibleValues must be a non-empty string.')
}
this.#possibleValues = possibleValues
return this
}
toJSON() {
return {
name: this.#name,
description: this.#description,
env: process.env[this.#name] !== undefined,
defaultValue: this.#defaultValue,
availableValues: this.#availableValues,
possibleValues: this.#possibleValues,
optional: this.#optional,
type: this.#type,
}
}
}