@goast/core 0.0.6 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -31,6 +31,7 @@ class OpenApiSchemasGenerationProviderBase extends generator_1.OpenApiGeneration
31
31
  result = this.generateSchema(ctx, schema);
32
32
  }
33
33
  (0, internal_utils_1.addSourceIfTest)(ctx.config, result, () => `${schema.$src.file}#${schema.$src.path}`);
34
+ ctx.existingSchemaResults.set(schema.id, result);
34
35
  this.addSchemaResult(ctx, schema, result);
35
36
  return result;
36
37
  }
@@ -14,16 +14,22 @@ class DerefProxyHandler {
14
14
  this._overwrittenValues = {};
15
15
  }
16
16
  get(target, prop) {
17
- var _a, _b, _c;
17
+ var _a;
18
18
  if (prop === '$ref') {
19
19
  return this._ref;
20
20
  }
21
21
  else if (prop === '$src') {
22
22
  return this._source;
23
23
  }
24
- return ((_b = (_a = this._overwrittenValues[prop]) !== null && _a !== void 0 ? _a : target[prop]) !== null && _b !== void 0 ? _b : (propertiesNotDerivedFromRef.includes(prop)
24
+ if (this._overwrittenValues[prop] !== undefined) {
25
+ return this._overwrittenValues[prop];
26
+ }
27
+ if (target[prop] !== undefined) {
28
+ return target[prop];
29
+ }
30
+ return propertiesNotDerivedFromRef.includes(prop)
25
31
  ? undefined
26
- : (_c = this._ref) === null || _c === void 0 ? void 0 : _c[prop]));
32
+ : (_a = this._ref) === null || _a === void 0 ? void 0 : _a[prop];
27
33
  }
28
34
  set(target, prop, value) {
29
35
  if (prop === '$ref' && (typeof value === 'object' || typeof value === 'undefined')) {
@@ -17,14 +17,14 @@ function transformSchema(context, schema) {
17
17
  if (existing)
18
18
  return existing;
19
19
  let kind = (0, helpers_1.determineSchemaKind)(schema);
20
- let nullable = false;
20
+ let nullable = kind === 'null';
21
21
  if (kind === 'multi-type') {
22
22
  const types = schema.type;
23
23
  let isSingleType = types.length === 1;
24
24
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
25
- if (types.length === 2 && (types.includes('null') || types.includes(null))) {
25
+ if (types.includes('null') || types.includes(null)) {
26
26
  nullable = true;
27
- isSingleType = true;
27
+ isSingleType = types.length === 2;
28
28
  }
29
29
  if (isSingleType) {
30
30
  const newType = types.filter((t) => t !== 'null' && t !== null)[0];
@@ -32,6 +32,9 @@ function transformSchema(context, schema) {
32
32
  schema.type = newType;
33
33
  kind = (0, helpers_1.determineSchemaKind)(schema);
34
34
  }
35
+ else {
36
+ schema.type = types.filter((t) => t !== 'null' && t !== null);
37
+ }
35
38
  }
36
39
  const id = context.idGenerator.generateId('schema');
37
40
  const nameInfo = (0, helpers_1.determineSchemaName)(schema, id);
@@ -65,58 +65,69 @@ class SourceBuilder extends string_utils_1.StringBuilder {
65
65
  * @returns The current SourceBuilder.
66
66
  */
67
67
  append(...value) {
68
- if (value.length === 0 || !value.some((v) => v && v.length > 0)) {
69
- return this;
70
- }
71
- const str = value.join('');
72
- let lineStartIndex = 0;
73
- for (let i = 0; i < str.length; i++) {
74
- if (str[i] === '\r')
68
+ for (const str of value) {
69
+ if (!str)
75
70
  continue;
76
- if (str[i] === '\n') {
77
- let lineLength = i - lineStartIndex;
78
- for (let j = i; j > 0 && str[j - 1] === '\r'; j--) {
79
- lineLength--;
80
- }
81
- if (lineLength > 0) {
82
- if (!this._isLineIndented) {
71
+ if (typeof str === 'function') {
72
+ str(this);
73
+ continue;
74
+ }
75
+ let lineStartIndex = 0;
76
+ for (let i = 0; i < str.length; i++) {
77
+ if (str[i] === '\r')
78
+ continue;
79
+ if (str[i] === '\n') {
80
+ let lineLength = i - lineStartIndex;
81
+ for (let j = i; j > 0 && str[j - 1] === '\r'; j--) {
82
+ lineLength--;
83
+ }
84
+ if (lineLength > 0) {
85
+ if (!this._isLineIndented) {
86
+ this.appendIndent();
87
+ }
88
+ super.append(str.substring(lineStartIndex, i).replace(/\r/g, ''));
89
+ }
90
+ this._isLastLineEmpty = this._isCurrentLineEmpty;
91
+ super.append(this.__options.newLine);
92
+ if (this._linePrefix) {
83
93
  this.appendIndent();
94
+ this._isLineIndented = true;
95
+ this._isCurrentLineEmpty = false;
84
96
  }
85
- super.append(str.substring(lineStartIndex, i).replace(/\r/g, ''));
97
+ else {
98
+ this._isLineIndented = false;
99
+ this._isCurrentLineEmpty = true;
100
+ }
101
+ lineStartIndex = i + 1;
102
+ }
103
+ if (!this._emptyLineCharRegex.test(str[i])) {
104
+ this._isCurrentLineEmpty = false;
86
105
  }
87
- this._isLastLineEmpty = this._isCurrentLineEmpty;
88
- super.append(this.__options.newLine);
89
- this._isCurrentLineEmpty = true;
90
- this._isLineIndented = false;
91
- lineStartIndex = i + 1;
92
106
  }
93
- if (!this._emptyLineCharRegex.test(str[i])) {
94
- this._isCurrentLineEmpty = false;
107
+ if (!this._isLineIndented && (lineStartIndex < str.length || this._linePrefix)) {
108
+ this.appendIndent();
109
+ this._isLineIndented = true;
95
110
  }
111
+ super.append(str.substring(lineStartIndex).replace(/\r/g, ''));
96
112
  }
97
- if (!this._isLineIndented && lineStartIndex < str.length) {
98
- this.appendIndent();
99
- this._isLineIndented = true;
100
- }
101
- super.append(str.substring(lineStartIndex).replace(/\r/g, ''));
102
113
  return this;
103
114
  }
104
115
  /**
105
- * Appends one or more strings to the end of the current SourceBuilder, followed by a line terminator.
116
+ * Appends one or more strings to the end of the current SourceBuilder if the specified condition is true.
117
+ * @param condition The condition to check before appending the specified value.
106
118
  * @param value The string(s) to append.
107
119
  * @returns The current SourceBuilder.
108
120
  */
109
- appendLine(...value) {
110
- return this.append(...value, '\n');
121
+ appendIf(condition, ...value) {
122
+ return this.if(condition, (builder) => builder.append(...value));
111
123
  }
112
124
  /**
113
- * Appends one or more strings to the end of the current SourceBuilder if the specified condition is true.
114
- * @param condition The condition to check before appending the specified value.
125
+ * Appends one or more strings to the end of the current SourceBuilder, followed by a line terminator.
115
126
  * @param value The string(s) to append.
116
127
  * @returns The current SourceBuilder.
117
128
  */
118
- appendIf(condition, ...value) {
119
- return (0, condition_1.evalCondition)(condition) ? this.append(...value) : this;
129
+ appendLine(...value) {
130
+ return this.append(...value, '\n');
120
131
  }
121
132
  /**
122
133
  * Appends one or more strings to the end of the current SourceBuilder, followed by a line terminator, if the specified condition is true.
@@ -125,7 +136,7 @@ class SourceBuilder extends string_utils_1.StringBuilder {
125
136
  * @returns The current SourceBuilder.
126
137
  */
127
138
  appendLineIf(condition, ...value) {
128
- return (0, condition_1.evalCondition)(condition) ? this.appendLine(...value) : this;
139
+ return this.if(condition, (builder) => builder.appendLine(...value));
129
140
  }
130
141
  /**
131
142
  * Appends the specified values to this source builder with the specified line prefix.
@@ -134,7 +145,15 @@ class SourceBuilder extends string_utils_1.StringBuilder {
134
145
  * @returns {SourceBuilder} This source builder instance.
135
146
  */
136
147
  appendWithLinePrefix(prefix, ...value) {
137
- return this.applyWithLinePrefix(prefix, (builder) => builder.append(...value));
148
+ const previousLinePrefix = this._linePrefix;
149
+ this._linePrefix += prefix;
150
+ try {
151
+ this.append(...value);
152
+ }
153
+ finally {
154
+ this._linePrefix = previousLinePrefix;
155
+ }
156
+ return this;
138
157
  }
139
158
  /**
140
159
  * Appends the specified values to this source builder as a new line with the specified line prefix.
@@ -143,7 +162,7 @@ class SourceBuilder extends string_utils_1.StringBuilder {
143
162
  * @returns {SourceBuilder} This source builder instance.
144
163
  */
145
164
  appendLineWithLinePrefix(prefix, ...value) {
146
- return this.applyWithLinePrefix(prefix, (builder) => builder.appendLine(...value));
165
+ return this.appendWithLinePrefix(prefix, ...value, '\n');
147
166
  }
148
167
  /**
149
168
  * Prepends one or more strings to the beginning of the current SourceBuilder if the specified condition is true.
@@ -152,7 +171,7 @@ class SourceBuilder extends string_utils_1.StringBuilder {
152
171
  * @returns The current SourceBuilder.
153
172
  */
154
173
  prependIf(condition, ...value) {
155
- return (0, condition_1.evalCondition)(condition) ? this.prepend(...value) : this;
174
+ return this.if(condition, (builder) => builder.prepend(...value));
156
175
  }
157
176
  /**
158
177
  * Prepends one or more strings to the beginning of the current SourceBuilder, followed by a line terminator, if the specified condition is true.
@@ -161,33 +180,7 @@ class SourceBuilder extends string_utils_1.StringBuilder {
161
180
  * @returns The current SourceBuilder.
162
181
  */
163
182
  prependLineIf(condition, ...value) {
164
- return (0, condition_1.evalCondition)(condition) ? this.prependLine(...value) : this;
165
- }
166
- /**
167
- * Applies the provided builder function to this source builder.
168
- * @param builderFn The builder function to apply.
169
- * @returns This source builder instance.
170
- */
171
- apply(builderFn) {
172
- builderFn(this);
173
- return this;
174
- }
175
- /**
176
- * Applies the provided builder function to this source builder with the specified line prefix.
177
- * @param prefix The line prefix to use.
178
- * @param builderFn The builder function to apply.
179
- * @returns This source builder instance.
180
- */
181
- applyWithLinePrefix(prefix, builderFn) {
182
- const previousLinePrefix = this._linePrefix;
183
- this._linePrefix += prefix;
184
- try {
185
- builderFn(this);
186
- }
187
- finally {
188
- this._linePrefix = previousLinePrefix;
189
- }
190
- return this;
183
+ return this.if(condition, (builder) => builder.prependLine(...value));
191
184
  }
192
185
  /**
193
186
  * Ensures that the current line is empty or consists only of whitespace characters.
@@ -215,28 +208,16 @@ class SourceBuilder extends string_utils_1.StringBuilder {
215
208
  /**
216
209
  * Inserts specified content if the specified condition is true.
217
210
  * @param condition The condition to check before inserting content.
218
- * @param builderFn The function to add content.
219
- * @returns A reference to this instance.
220
- */
221
- applyIf(condition, builderFn) {
222
- if ((0, condition_1.evalCondition)(condition)) {
223
- builderFn(this);
224
- }
225
- return this;
226
- }
227
- /**
228
- * Inserts specified content depending on the specified condition.
229
- * @param condition The condition to check before inserting content.
230
211
  * @param builderFn The function to add content if the condition is true.
231
212
  * @param elseBuilderFn The function to add content if the condition is false.
232
213
  * @returns A reference to this instance.
233
214
  */
234
- applyIfElse(condition, builderFn, elseBuilderFn) {
215
+ if(condition, builderFn, elseBuilderFn) {
235
216
  if ((0, condition_1.evalCondition)(condition)) {
236
217
  builderFn(this);
237
218
  }
238
219
  else {
239
- elseBuilderFn(this);
220
+ elseBuilderFn === null || elseBuilderFn === void 0 ? void 0 : elseBuilderFn(this);
240
221
  }
241
222
  return this;
242
223
  }
@@ -246,142 +227,63 @@ class SourceBuilder extends string_utils_1.StringBuilder {
246
227
  * @param condition The condition to check before adding indentation.
247
228
  * @returns A reference to this instance.
248
229
  */
249
- indent(builderFnOrString, condition = true) {
250
- const shouldIndent = (0, condition_1.evalCondition)(condition);
251
- if (shouldIndent) {
252
- this.currentIndentLevel++;
253
- }
230
+ indent(value) {
231
+ this.currentIndentLevel++;
254
232
  try {
255
- if (typeof builderFnOrString === 'string') {
256
- this.append(builderFnOrString);
257
- }
258
- else {
259
- builderFnOrString(this);
260
- }
233
+ this.append(value);
261
234
  }
262
235
  finally {
263
- if (shouldIndent) {
264
- this.currentIndentLevel--;
265
- }
236
+ this.currentIndentLevel--;
266
237
  }
267
238
  return this;
268
239
  }
269
240
  /**
270
- * Adds parentheses around the specified content and adds one indentation level.
271
- * @param brackets The brackets to use.
272
- * @param builderFnOrString The function to add content inside parentheses or one string that should be parenthesized.
273
- * @param indent Whether to indent the content inside parentheses. Defaults to `true`.
241
+ * Adds one indentation level if the specified condition is true. If the current line already contains characters, only subsequent lines are affected.
242
+ * @param builderFn The function to add indented content or the content itself.
243
+ * @param condition The condition to check before adding indentation.
274
244
  * @returns A reference to this instance.
275
245
  */
276
- parenthesize(brackets, builderFnOrString, indent = true) {
277
- var _a, _b;
278
- this.append((_a = brackets[0]) !== null && _a !== void 0 ? _a : '');
279
- this.indent(builderFnOrString, indent);
280
- this.append((_b = brackets[1]) !== null && _b !== void 0 ? _b : '');
281
- return this;
246
+ indentIf(condition, value) {
247
+ return this.if(condition, (builder) => builder.indent(value), (builder) => builder.append(value));
282
248
  }
283
249
  /**
284
- * Adds parentheses around the specified content and adds one indentation level. Line breaks are added after start and before end of the parentheses.
250
+ * Adds parentheses around the specified content.
285
251
  * @param brackets The brackets to use.
286
- * @param builderFnOrString The function to add content inside parentheses or one string that should be parenthesized.
287
- * @param indent Whether to indent the content inside parentheses. Defaults to `true`.
252
+ * @param value The function to add content inside parentheses or one string that should be parenthesized.
253
+ * @param options Options for the parentheses.
288
254
  * @returns A reference to this instance.
289
255
  */
290
- parenthesizeMultiline(brackets, builderFnOrString, indent = true) {
291
- var _a, _b;
292
- this.appendLine((_a = brackets[0]) !== null && _a !== void 0 ? _a : '');
293
- this.indent(builderFnOrString, indent);
294
- this.ensureCurrentLineEmpty();
295
- this.append((_b = brackets[1]) !== null && _b !== void 0 ? _b : '');
296
- return this;
256
+ parenthesize(brackets, value, options) {
257
+ var _a, _b, _c, _d;
258
+ return this.if((_a = options === null || options === void 0 ? void 0 : options.multiline) !== null && _a !== void 0 ? _a : false, (b) => { var _a; return b.appendLine((_a = brackets[0]) !== null && _a !== void 0 ? _a : ''); }, (b) => { var _a; return b.append((_a = brackets[0]) !== null && _a !== void 0 ? _a : ''); })
259
+ .indentIf((_b = options === null || options === void 0 ? void 0 : options.indent) !== null && _b !== void 0 ? _b : true, value)
260
+ .if((_c = options === null || options === void 0 ? void 0 : options.multiline) !== null && _c !== void 0 ? _c : false, (b) => b.ensureCurrentLineEmpty())
261
+ .append((_d = brackets[1]) !== null && _d !== void 0 ? _d : '');
297
262
  }
298
263
  /**
299
264
  * Adds parentheses around the specified content if the specified condition is true.
300
265
  * @param condition The condition to check before adding parentheses.
301
266
  * @param brackets The brackets to use.
302
- * @param builderFnOrString The function to add content inside parentheses or one string that should be parenthesized.
303
- * @param indent Whether to indent the content inside parentheses. Defaults to `true`.
304
- * @returns A reference to this instance.
305
- */
306
- parenthesizeIf(condition, brackets, builderFnOrString, indent) {
307
- if ((0, condition_1.evalCondition)(condition)) {
308
- this.parenthesize(brackets, builderFnOrString, indent !== null && indent !== void 0 ? indent : true);
309
- }
310
- else {
311
- this.indent(builderFnOrString, indent !== null && indent !== void 0 ? indent : false);
312
- }
313
- return this;
314
- }
315
- /**
316
- * Adds parentheses around the specified content if the specified condition is true. Line breaks are added after start and before end of the parentheses.
317
- * @param condition The condition to check before adding parentheses.
318
- * @param brackets The brackets to use.
319
- * @param builderFnOrString The function to add content inside parentheses or one string that should be parenthesized.
320
- * @param indent Whether to indent the content inside parentheses. Defaults to `true`.
321
- * @returns A reference to this instance.
322
- */
323
- parenthesizeMultilineIf(condition, brackets, builderFnOrString, indent) {
324
- if ((0, condition_1.evalCondition)(condition)) {
325
- this.parenthesizeMultiline(brackets, builderFnOrString, indent !== null && indent !== void 0 ? indent : true);
326
- }
327
- else {
328
- this.indent(builderFnOrString, indent !== null && indent !== void 0 ? indent : false);
329
- }
330
- return this;
331
- }
332
- /**
333
- * Adds content for each item in the specified array.
334
- * @param items The items to add content for.
335
- * @param builderFn The function to add content for each item.
336
- * @returns A reference to this instance.
337
- */
338
- forEach(items, builderFn) {
339
- return this.forEachImpl(items, builderFn);
340
- }
341
- /**
342
- * Adds content for each item in the specified array.
343
- * @param items The items to add content for.
344
- * @param condition The condition to check before adding content.
345
- * @param builderFn The function to add content for each item.
346
- * @returns A reference to this instance.
347
- */
348
- forEachIf(items, condition, builderFn) {
349
- return (0, condition_1.evalCondition)(condition) ? this.forEachImpl(items, builderFn) : this;
350
- }
351
- /**
352
- * Adds content for each item in the specified array.
353
- * @param items The items to add content for.
354
- * @param condition The condition to check before adding content for each item.
355
- * @param builderFn The function to add content for each item.
356
- * @returns A reference to this instance.
357
- */
358
- forEachMatching(items, condition, builderFn) {
359
- return this.forEachImpl(items, builderFn, undefined, condition);
360
- }
361
- /**
362
- * Adds content for each item in the specified array.
363
- * @param items The items to add content for.
364
- * @param seperator The seperator to add between items.
365
- * @param builderFn The function to add content for each item.
267
+ * @param value The function to add content inside parentheses or one string that should be parenthesized.
268
+ * @param options Options for the parentheses.
366
269
  * @returns A reference to this instance.
367
270
  */
368
- forEachSeparated(items, seperator, builderFn) {
369
- return this.forEachImpl(items, builderFn, seperator);
271
+ parenthesizeIf(condition, brackets, value, options) {
272
+ return this.if(condition, (builder) => builder.parenthesize(brackets, value, options), (builder) => builder.indentIf((options === null || options === void 0 ? void 0 : options.indent) === true, value));
370
273
  }
371
274
  /**
372
275
  * Adds content for each item in the specified array.
373
276
  * @param items The items to add content for.
374
- * @param condition The condition to check before adding content for each item.
375
- * @param seperator The seperator to add between items.
376
277
  * @param builderFn The function to add content for each item.
278
+ * @param options The options for the loop.
377
279
  * @returns A reference to this instance.
378
280
  */
379
- forEachMatchingSeparated(items, condition, seperator, builderFn) {
380
- return this.forEachImpl(items, builderFn, seperator, condition);
381
- }
382
- forEachImpl(items, builderFn, seperator, condition = true) {
383
- if (condition === false)
281
+ forEach(items, builderFn, options) {
282
+ var _a;
283
+ const condition = (_a = options === null || options === void 0 ? void 0 : options.condition) !== null && _a !== void 0 ? _a : true;
284
+ if ((options === null || options === void 0 ? void 0 : options.condition) === false)
384
285
  return this;
286
+ const seperator = options === null || options === void 0 ? void 0 : options.separator;
385
287
  let previousIndex = -1;
386
288
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
387
289
  let previousItem = undefined;
@@ -404,6 +306,15 @@ class SourceBuilder extends string_utils_1.StringBuilder {
404
306
  }
405
307
  return this;
406
308
  }
309
+ forEachIf(condition, items, builderFn, options) {
310
+ return this.if(condition, (b) => b.forEach(items, builderFn, options));
311
+ }
312
+ appendSeparated(items, separator) {
313
+ return this.forEach(items, (builder, item) => builder.append(item), { separator });
314
+ }
315
+ appendSeparatedIf(condition, items, separator) {
316
+ return this.forEachIf(condition, items, (builder, item) => builder.append(item), { separator });
317
+ }
407
318
  appendIndent() {
408
319
  super.append(this._indentString.repeat(this.currentIndentLevel));
409
320
  if (this._linePrefix) {
@@ -277,9 +277,14 @@ class StringBuilder {
277
277
  */
278
278
  append(...value) {
279
279
  for (const part of value) {
280
- if ((0, common_utils_1.isNullish)(part) || part.length === 0)
280
+ if (!part)
281
281
  continue;
282
- this._str += part;
282
+ if (typeof part === 'string') {
283
+ this._str += part;
284
+ }
285
+ else {
286
+ part(this);
287
+ }
283
288
  }
284
289
  return this;
285
290
  }
@@ -28,6 +28,7 @@ export class OpenApiSchemasGenerationProviderBase extends OpenApiGenerationProvi
28
28
  result = this.generateSchema(ctx, schema);
29
29
  }
30
30
  addSourceIfTest(ctx.config, result, () => `${schema.$src.file}#${schema.$src.path}`);
31
+ ctx.existingSchemaResults.set(schema.id, result);
31
32
  this.addSchemaResult(ctx, schema, result);
32
33
  return result;
33
34
  }
@@ -10,16 +10,22 @@ class DerefProxyHandler {
10
10
  this._overwrittenValues = {};
11
11
  }
12
12
  get(target, prop) {
13
- var _a, _b, _c;
13
+ var _a;
14
14
  if (prop === '$ref') {
15
15
  return this._ref;
16
16
  }
17
17
  else if (prop === '$src') {
18
18
  return this._source;
19
19
  }
20
- return ((_b = (_a = this._overwrittenValues[prop]) !== null && _a !== void 0 ? _a : target[prop]) !== null && _b !== void 0 ? _b : (propertiesNotDerivedFromRef.includes(prop)
20
+ if (this._overwrittenValues[prop] !== undefined) {
21
+ return this._overwrittenValues[prop];
22
+ }
23
+ if (target[prop] !== undefined) {
24
+ return target[prop];
25
+ }
26
+ return propertiesNotDerivedFromRef.includes(prop)
21
27
  ? undefined
22
- : (_c = this._ref) === null || _c === void 0 ? void 0 : _c[prop]));
28
+ : (_a = this._ref) === null || _a === void 0 ? void 0 : _a[prop];
23
29
  }
24
30
  set(target, prop, value) {
25
31
  if (prop === '$ref' && (typeof value === 'object' || typeof value === 'undefined')) {
@@ -14,14 +14,14 @@ export function transformSchema(context, schema) {
14
14
  if (existing)
15
15
  return existing;
16
16
  let kind = determineSchemaKind(schema);
17
- let nullable = false;
17
+ let nullable = kind === 'null';
18
18
  if (kind === 'multi-type') {
19
19
  const types = schema.type;
20
20
  let isSingleType = types.length === 1;
21
21
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
22
- if (types.length === 2 && (types.includes('null') || types.includes(null))) {
22
+ if (types.includes('null') || types.includes(null)) {
23
23
  nullable = true;
24
- isSingleType = true;
24
+ isSingleType = types.length === 2;
25
25
  }
26
26
  if (isSingleType) {
27
27
  const newType = types.filter((t) => t !== 'null' && t !== null)[0];
@@ -29,6 +29,9 @@ export function transformSchema(context, schema) {
29
29
  schema.type = newType;
30
30
  kind = determineSchemaKind(schema);
31
31
  }
32
+ else {
33
+ schema.type = types.filter((t) => t !== 'null' && t !== null);
34
+ }
32
35
  }
33
36
  const id = context.idGenerator.generateId('schema');
34
37
  const nameInfo = determineSchemaName(schema, id);
@@ -62,58 +62,69 @@ export class SourceBuilder extends StringBuilder {
62
62
  * @returns The current SourceBuilder.
63
63
  */
64
64
  append(...value) {
65
- if (value.length === 0 || !value.some((v) => v && v.length > 0)) {
66
- return this;
67
- }
68
- const str = value.join('');
69
- let lineStartIndex = 0;
70
- for (let i = 0; i < str.length; i++) {
71
- if (str[i] === '\r')
65
+ for (const str of value) {
66
+ if (!str)
72
67
  continue;
73
- if (str[i] === '\n') {
74
- let lineLength = i - lineStartIndex;
75
- for (let j = i; j > 0 && str[j - 1] === '\r'; j--) {
76
- lineLength--;
77
- }
78
- if (lineLength > 0) {
79
- if (!this._isLineIndented) {
68
+ if (typeof str === 'function') {
69
+ str(this);
70
+ continue;
71
+ }
72
+ let lineStartIndex = 0;
73
+ for (let i = 0; i < str.length; i++) {
74
+ if (str[i] === '\r')
75
+ continue;
76
+ if (str[i] === '\n') {
77
+ let lineLength = i - lineStartIndex;
78
+ for (let j = i; j > 0 && str[j - 1] === '\r'; j--) {
79
+ lineLength--;
80
+ }
81
+ if (lineLength > 0) {
82
+ if (!this._isLineIndented) {
83
+ this.appendIndent();
84
+ }
85
+ super.append(str.substring(lineStartIndex, i).replace(/\r/g, ''));
86
+ }
87
+ this._isLastLineEmpty = this._isCurrentLineEmpty;
88
+ super.append(this.__options.newLine);
89
+ if (this._linePrefix) {
80
90
  this.appendIndent();
91
+ this._isLineIndented = true;
92
+ this._isCurrentLineEmpty = false;
81
93
  }
82
- super.append(str.substring(lineStartIndex, i).replace(/\r/g, ''));
94
+ else {
95
+ this._isLineIndented = false;
96
+ this._isCurrentLineEmpty = true;
97
+ }
98
+ lineStartIndex = i + 1;
99
+ }
100
+ if (!this._emptyLineCharRegex.test(str[i])) {
101
+ this._isCurrentLineEmpty = false;
83
102
  }
84
- this._isLastLineEmpty = this._isCurrentLineEmpty;
85
- super.append(this.__options.newLine);
86
- this._isCurrentLineEmpty = true;
87
- this._isLineIndented = false;
88
- lineStartIndex = i + 1;
89
103
  }
90
- if (!this._emptyLineCharRegex.test(str[i])) {
91
- this._isCurrentLineEmpty = false;
104
+ if (!this._isLineIndented && (lineStartIndex < str.length || this._linePrefix)) {
105
+ this.appendIndent();
106
+ this._isLineIndented = true;
92
107
  }
108
+ super.append(str.substring(lineStartIndex).replace(/\r/g, ''));
93
109
  }
94
- if (!this._isLineIndented && lineStartIndex < str.length) {
95
- this.appendIndent();
96
- this._isLineIndented = true;
97
- }
98
- super.append(str.substring(lineStartIndex).replace(/\r/g, ''));
99
110
  return this;
100
111
  }
101
112
  /**
102
- * Appends one or more strings to the end of the current SourceBuilder, followed by a line terminator.
113
+ * Appends one or more strings to the end of the current SourceBuilder if the specified condition is true.
114
+ * @param condition The condition to check before appending the specified value.
103
115
  * @param value The string(s) to append.
104
116
  * @returns The current SourceBuilder.
105
117
  */
106
- appendLine(...value) {
107
- return this.append(...value, '\n');
118
+ appendIf(condition, ...value) {
119
+ return this.if(condition, (builder) => builder.append(...value));
108
120
  }
109
121
  /**
110
- * Appends one or more strings to the end of the current SourceBuilder if the specified condition is true.
111
- * @param condition The condition to check before appending the specified value.
122
+ * Appends one or more strings to the end of the current SourceBuilder, followed by a line terminator.
112
123
  * @param value The string(s) to append.
113
124
  * @returns The current SourceBuilder.
114
125
  */
115
- appendIf(condition, ...value) {
116
- return evalCondition(condition) ? this.append(...value) : this;
126
+ appendLine(...value) {
127
+ return this.append(...value, '\n');
117
128
  }
118
129
  /**
119
130
  * Appends one or more strings to the end of the current SourceBuilder, followed by a line terminator, if the specified condition is true.
@@ -122,7 +133,7 @@ export class SourceBuilder extends StringBuilder {
122
133
  * @returns The current SourceBuilder.
123
134
  */
124
135
  appendLineIf(condition, ...value) {
125
- return evalCondition(condition) ? this.appendLine(...value) : this;
136
+ return this.if(condition, (builder) => builder.appendLine(...value));
126
137
  }
127
138
  /**
128
139
  * Appends the specified values to this source builder with the specified line prefix.
@@ -131,7 +142,15 @@ export class SourceBuilder extends StringBuilder {
131
142
  * @returns {SourceBuilder} This source builder instance.
132
143
  */
133
144
  appendWithLinePrefix(prefix, ...value) {
134
- return this.applyWithLinePrefix(prefix, (builder) => builder.append(...value));
145
+ const previousLinePrefix = this._linePrefix;
146
+ this._linePrefix += prefix;
147
+ try {
148
+ this.append(...value);
149
+ }
150
+ finally {
151
+ this._linePrefix = previousLinePrefix;
152
+ }
153
+ return this;
135
154
  }
136
155
  /**
137
156
  * Appends the specified values to this source builder as a new line with the specified line prefix.
@@ -140,7 +159,7 @@ export class SourceBuilder extends StringBuilder {
140
159
  * @returns {SourceBuilder} This source builder instance.
141
160
  */
142
161
  appendLineWithLinePrefix(prefix, ...value) {
143
- return this.applyWithLinePrefix(prefix, (builder) => builder.appendLine(...value));
162
+ return this.appendWithLinePrefix(prefix, ...value, '\n');
144
163
  }
145
164
  /**
146
165
  * Prepends one or more strings to the beginning of the current SourceBuilder if the specified condition is true.
@@ -149,7 +168,7 @@ export class SourceBuilder extends StringBuilder {
149
168
  * @returns The current SourceBuilder.
150
169
  */
151
170
  prependIf(condition, ...value) {
152
- return evalCondition(condition) ? this.prepend(...value) : this;
171
+ return this.if(condition, (builder) => builder.prepend(...value));
153
172
  }
154
173
  /**
155
174
  * Prepends one or more strings to the beginning of the current SourceBuilder, followed by a line terminator, if the specified condition is true.
@@ -158,33 +177,7 @@ export class SourceBuilder extends StringBuilder {
158
177
  * @returns The current SourceBuilder.
159
178
  */
160
179
  prependLineIf(condition, ...value) {
161
- return evalCondition(condition) ? this.prependLine(...value) : this;
162
- }
163
- /**
164
- * Applies the provided builder function to this source builder.
165
- * @param builderFn The builder function to apply.
166
- * @returns This source builder instance.
167
- */
168
- apply(builderFn) {
169
- builderFn(this);
170
- return this;
171
- }
172
- /**
173
- * Applies the provided builder function to this source builder with the specified line prefix.
174
- * @param prefix The line prefix to use.
175
- * @param builderFn The builder function to apply.
176
- * @returns This source builder instance.
177
- */
178
- applyWithLinePrefix(prefix, builderFn) {
179
- const previousLinePrefix = this._linePrefix;
180
- this._linePrefix += prefix;
181
- try {
182
- builderFn(this);
183
- }
184
- finally {
185
- this._linePrefix = previousLinePrefix;
186
- }
187
- return this;
180
+ return this.if(condition, (builder) => builder.prependLine(...value));
188
181
  }
189
182
  /**
190
183
  * Ensures that the current line is empty or consists only of whitespace characters.
@@ -212,28 +205,16 @@ export class SourceBuilder extends StringBuilder {
212
205
  /**
213
206
  * Inserts specified content if the specified condition is true.
214
207
  * @param condition The condition to check before inserting content.
215
- * @param builderFn The function to add content.
216
- * @returns A reference to this instance.
217
- */
218
- applyIf(condition, builderFn) {
219
- if (evalCondition(condition)) {
220
- builderFn(this);
221
- }
222
- return this;
223
- }
224
- /**
225
- * Inserts specified content depending on the specified condition.
226
- * @param condition The condition to check before inserting content.
227
208
  * @param builderFn The function to add content if the condition is true.
228
209
  * @param elseBuilderFn The function to add content if the condition is false.
229
210
  * @returns A reference to this instance.
230
211
  */
231
- applyIfElse(condition, builderFn, elseBuilderFn) {
212
+ if(condition, builderFn, elseBuilderFn) {
232
213
  if (evalCondition(condition)) {
233
214
  builderFn(this);
234
215
  }
235
216
  else {
236
- elseBuilderFn(this);
217
+ elseBuilderFn === null || elseBuilderFn === void 0 ? void 0 : elseBuilderFn(this);
237
218
  }
238
219
  return this;
239
220
  }
@@ -243,142 +224,63 @@ export class SourceBuilder extends StringBuilder {
243
224
  * @param condition The condition to check before adding indentation.
244
225
  * @returns A reference to this instance.
245
226
  */
246
- indent(builderFnOrString, condition = true) {
247
- const shouldIndent = evalCondition(condition);
248
- if (shouldIndent) {
249
- this.currentIndentLevel++;
250
- }
227
+ indent(value) {
228
+ this.currentIndentLevel++;
251
229
  try {
252
- if (typeof builderFnOrString === 'string') {
253
- this.append(builderFnOrString);
254
- }
255
- else {
256
- builderFnOrString(this);
257
- }
230
+ this.append(value);
258
231
  }
259
232
  finally {
260
- if (shouldIndent) {
261
- this.currentIndentLevel--;
262
- }
233
+ this.currentIndentLevel--;
263
234
  }
264
235
  return this;
265
236
  }
266
237
  /**
267
- * Adds parentheses around the specified content and adds one indentation level.
268
- * @param brackets The brackets to use.
269
- * @param builderFnOrString The function to add content inside parentheses or one string that should be parenthesized.
270
- * @param indent Whether to indent the content inside parentheses. Defaults to `true`.
238
+ * Adds one indentation level if the specified condition is true. If the current line already contains characters, only subsequent lines are affected.
239
+ * @param builderFn The function to add indented content or the content itself.
240
+ * @param condition The condition to check before adding indentation.
271
241
  * @returns A reference to this instance.
272
242
  */
273
- parenthesize(brackets, builderFnOrString, indent = true) {
274
- var _a, _b;
275
- this.append((_a = brackets[0]) !== null && _a !== void 0 ? _a : '');
276
- this.indent(builderFnOrString, indent);
277
- this.append((_b = brackets[1]) !== null && _b !== void 0 ? _b : '');
278
- return this;
243
+ indentIf(condition, value) {
244
+ return this.if(condition, (builder) => builder.indent(value), (builder) => builder.append(value));
279
245
  }
280
246
  /**
281
- * Adds parentheses around the specified content and adds one indentation level. Line breaks are added after start and before end of the parentheses.
247
+ * Adds parentheses around the specified content.
282
248
  * @param brackets The brackets to use.
283
- * @param builderFnOrString The function to add content inside parentheses or one string that should be parenthesized.
284
- * @param indent Whether to indent the content inside parentheses. Defaults to `true`.
249
+ * @param value The function to add content inside parentheses or one string that should be parenthesized.
250
+ * @param options Options for the parentheses.
285
251
  * @returns A reference to this instance.
286
252
  */
287
- parenthesizeMultiline(brackets, builderFnOrString, indent = true) {
288
- var _a, _b;
289
- this.appendLine((_a = brackets[0]) !== null && _a !== void 0 ? _a : '');
290
- this.indent(builderFnOrString, indent);
291
- this.ensureCurrentLineEmpty();
292
- this.append((_b = brackets[1]) !== null && _b !== void 0 ? _b : '');
293
- return this;
253
+ parenthesize(brackets, value, options) {
254
+ var _a, _b, _c, _d;
255
+ return this.if((_a = options === null || options === void 0 ? void 0 : options.multiline) !== null && _a !== void 0 ? _a : false, (b) => { var _a; return b.appendLine((_a = brackets[0]) !== null && _a !== void 0 ? _a : ''); }, (b) => { var _a; return b.append((_a = brackets[0]) !== null && _a !== void 0 ? _a : ''); })
256
+ .indentIf((_b = options === null || options === void 0 ? void 0 : options.indent) !== null && _b !== void 0 ? _b : true, value)
257
+ .if((_c = options === null || options === void 0 ? void 0 : options.multiline) !== null && _c !== void 0 ? _c : false, (b) => b.ensureCurrentLineEmpty())
258
+ .append((_d = brackets[1]) !== null && _d !== void 0 ? _d : '');
294
259
  }
295
260
  /**
296
261
  * Adds parentheses around the specified content if the specified condition is true.
297
262
  * @param condition The condition to check before adding parentheses.
298
263
  * @param brackets The brackets to use.
299
- * @param builderFnOrString The function to add content inside parentheses or one string that should be parenthesized.
300
- * @param indent Whether to indent the content inside parentheses. Defaults to `true`.
301
- * @returns A reference to this instance.
302
- */
303
- parenthesizeIf(condition, brackets, builderFnOrString, indent) {
304
- if (evalCondition(condition)) {
305
- this.parenthesize(brackets, builderFnOrString, indent !== null && indent !== void 0 ? indent : true);
306
- }
307
- else {
308
- this.indent(builderFnOrString, indent !== null && indent !== void 0 ? indent : false);
309
- }
310
- return this;
311
- }
312
- /**
313
- * Adds parentheses around the specified content if the specified condition is true. Line breaks are added after start and before end of the parentheses.
314
- * @param condition The condition to check before adding parentheses.
315
- * @param brackets The brackets to use.
316
- * @param builderFnOrString The function to add content inside parentheses or one string that should be parenthesized.
317
- * @param indent Whether to indent the content inside parentheses. Defaults to `true`.
318
- * @returns A reference to this instance.
319
- */
320
- parenthesizeMultilineIf(condition, brackets, builderFnOrString, indent) {
321
- if (evalCondition(condition)) {
322
- this.parenthesizeMultiline(brackets, builderFnOrString, indent !== null && indent !== void 0 ? indent : true);
323
- }
324
- else {
325
- this.indent(builderFnOrString, indent !== null && indent !== void 0 ? indent : false);
326
- }
327
- return this;
328
- }
329
- /**
330
- * Adds content for each item in the specified array.
331
- * @param items The items to add content for.
332
- * @param builderFn The function to add content for each item.
333
- * @returns A reference to this instance.
334
- */
335
- forEach(items, builderFn) {
336
- return this.forEachImpl(items, builderFn);
337
- }
338
- /**
339
- * Adds content for each item in the specified array.
340
- * @param items The items to add content for.
341
- * @param condition The condition to check before adding content.
342
- * @param builderFn The function to add content for each item.
343
- * @returns A reference to this instance.
344
- */
345
- forEachIf(items, condition, builderFn) {
346
- return evalCondition(condition) ? this.forEachImpl(items, builderFn) : this;
347
- }
348
- /**
349
- * Adds content for each item in the specified array.
350
- * @param items The items to add content for.
351
- * @param condition The condition to check before adding content for each item.
352
- * @param builderFn The function to add content for each item.
353
- * @returns A reference to this instance.
354
- */
355
- forEachMatching(items, condition, builderFn) {
356
- return this.forEachImpl(items, builderFn, undefined, condition);
357
- }
358
- /**
359
- * Adds content for each item in the specified array.
360
- * @param items The items to add content for.
361
- * @param seperator The seperator to add between items.
362
- * @param builderFn The function to add content for each item.
264
+ * @param value The function to add content inside parentheses or one string that should be parenthesized.
265
+ * @param options Options for the parentheses.
363
266
  * @returns A reference to this instance.
364
267
  */
365
- forEachSeparated(items, seperator, builderFn) {
366
- return this.forEachImpl(items, builderFn, seperator);
268
+ parenthesizeIf(condition, brackets, value, options) {
269
+ return this.if(condition, (builder) => builder.parenthesize(brackets, value, options), (builder) => builder.indentIf((options === null || options === void 0 ? void 0 : options.indent) === true, value));
367
270
  }
368
271
  /**
369
272
  * Adds content for each item in the specified array.
370
273
  * @param items The items to add content for.
371
- * @param condition The condition to check before adding content for each item.
372
- * @param seperator The seperator to add between items.
373
274
  * @param builderFn The function to add content for each item.
275
+ * @param options The options for the loop.
374
276
  * @returns A reference to this instance.
375
277
  */
376
- forEachMatchingSeparated(items, condition, seperator, builderFn) {
377
- return this.forEachImpl(items, builderFn, seperator, condition);
378
- }
379
- forEachImpl(items, builderFn, seperator, condition = true) {
380
- if (condition === false)
278
+ forEach(items, builderFn, options) {
279
+ var _a;
280
+ const condition = (_a = options === null || options === void 0 ? void 0 : options.condition) !== null && _a !== void 0 ? _a : true;
281
+ if ((options === null || options === void 0 ? void 0 : options.condition) === false)
381
282
  return this;
283
+ const seperator = options === null || options === void 0 ? void 0 : options.separator;
382
284
  let previousIndex = -1;
383
285
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
384
286
  let previousItem = undefined;
@@ -401,6 +303,15 @@ export class SourceBuilder extends StringBuilder {
401
303
  }
402
304
  return this;
403
305
  }
306
+ forEachIf(condition, items, builderFn, options) {
307
+ return this.if(condition, (b) => b.forEach(items, builderFn, options));
308
+ }
309
+ appendSeparated(items, separator) {
310
+ return this.forEach(items, (builder, item) => builder.append(item), { separator });
311
+ }
312
+ appendSeparatedIf(condition, items, separator) {
313
+ return this.forEachIf(condition, items, (builder, item) => builder.append(item), { separator });
314
+ }
404
315
  appendIndent() {
405
316
  super.append(this._indentString.repeat(this.currentIndentLevel));
406
317
  if (this._linePrefix) {
@@ -265,9 +265,14 @@ export class StringBuilder {
265
265
  */
266
266
  append(...value) {
267
267
  for (const part of value) {
268
- if (isNullish(part) || part.length === 0)
268
+ if (!part)
269
269
  continue;
270
- this._str += part;
270
+ if (typeof part === 'string') {
271
+ this._str += part;
272
+ }
273
+ else {
274
+ part(this);
275
+ }
271
276
  }
272
277
  return this;
273
278
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@goast/core",
3
- "version": "0.0.6",
3
+ "version": "0.1.1",
4
4
  "repository": "https://github.com/MaSch0212/goast.git",
5
5
  "author": {
6
6
  "name": "Marc Schmidt (MaSch0212)",
@@ -1,5 +1,5 @@
1
1
  import { Condition } from './condition';
2
- import { StringBuilder, StringBuilderOptions } from './string.utils';
2
+ import { BuilderFn, StringBuilder, StringBuilderOptions, TextOrBuilderFn } from './string.utils';
3
3
  import { Nullable } from './type.utils';
4
4
  export type IndentOptions = {
5
5
  readonly type: 'tabs';
@@ -16,6 +16,36 @@ type KnownParatheses = '()' | '[]' | '{}' | '<>';
16
16
  export type Paratheses = KnownParatheses | Omit<string, KnownParatheses> | [open: Nullable<string>, close: Nullable<string>];
17
17
  export type SeparatorBuidlerFn<TBuilder extends SourceBuilder, TItem> = (builder: TBuilder, previousItem: TItem, nextItem: TItem, previousItemIndex: number, nextItemIndex: number) => void;
18
18
  export type Separator<TBuilder extends SourceBuilder, TItem> = string | SeparatorBuidlerFn<TBuilder, TItem>;
19
+ /**
20
+ * Options for the `parenthesize` methods of the `SourceBuilder`.
21
+ * @see SourceBuilder.parenthesize
22
+ * @see SourceBuilder.parenthesizeIf
23
+ */
24
+ export type ParenthesizeOptions = {
25
+ /**
26
+ * Whether to add line breaks after start and before end of the parentheses. Defaults to `false`.
27
+ */
28
+ readonly multiline?: boolean;
29
+ /**
30
+ * Whether to indent the content inside parentheses. Defaults to `true`.
31
+ */
32
+ readonly indent?: boolean;
33
+ };
34
+ /**
35
+ * Options for the `forEach` methods of the `SourceBuilder`.
36
+ * @see SourceBuilder.forEach
37
+ * @see SourceBuilder.forEachIf
38
+ */
39
+ export type ForEachOptions<TBuilder extends SourceBuilder, TItem> = {
40
+ /**
41
+ * The condition to check before adding content for each item. Defaults to `true`.
42
+ */
43
+ readonly condition?: Condition<TItem>;
44
+ /**
45
+ * The seperator to add between items. Default to `undefined`.
46
+ */
47
+ readonly separator?: Separator<TBuilder, TItem>;
48
+ };
19
49
  /**
20
50
  * Represents an in-memory source file.
21
51
  */
@@ -53,74 +83,61 @@ export declare class SourceBuilder extends StringBuilder {
53
83
  * @param options The options to use for the `SourceBuilder` instance.
54
84
  * @returns The string built by the `SourceBuilder` instance.
55
85
  */
56
- static build(buildAction: (builder: SourceBuilder) => void, options?: Partial<SourceBuilderOptions>): string;
86
+ static build(buildAction: BuilderFn<SourceBuilder>, options?: Partial<SourceBuilderOptions>): string;
57
87
  /**
58
88
  * Appends one or more strings to the end of the current SourceBuilder.
59
89
  * @param value The string(s) to append.
60
90
  * @returns The current SourceBuilder.
61
91
  */
62
- append(...value: Nullable<string>[]): this;
92
+ append(...value: Nullable<TextOrBuilderFn<this>>[]): this;
63
93
  /**
64
- * Appends one or more strings to the end of the current SourceBuilder, followed by a line terminator.
94
+ * Appends one or more strings to the end of the current SourceBuilder if the specified condition is true.
95
+ * @param condition The condition to check before appending the specified value.
65
96
  * @param value The string(s) to append.
66
97
  * @returns The current SourceBuilder.
67
98
  */
68
- appendLine(...value: Nullable<string>[]): this;
99
+ appendIf(condition: Condition, ...value: Nullable<TextOrBuilderFn<this>>[]): this;
69
100
  /**
70
- * Appends one or more strings to the end of the current SourceBuilder if the specified condition is true.
71
- * @param condition The condition to check before appending the specified value.
101
+ * Appends one or more strings to the end of the current SourceBuilder, followed by a line terminator.
72
102
  * @param value The string(s) to append.
73
103
  * @returns The current SourceBuilder.
74
104
  */
75
- appendIf(condition: Condition, ...value: Nullable<string>[]): this;
105
+ appendLine(...value: Nullable<TextOrBuilderFn<this>>[]): this;
76
106
  /**
77
107
  * Appends one or more strings to the end of the current SourceBuilder, followed by a line terminator, if the specified condition is true.
78
108
  * @param condition The condition to check before appending the specified value.
79
109
  * @param value The string(s) to append.
80
110
  * @returns The current SourceBuilder.
81
111
  */
82
- appendLineIf(condition: Condition, ...value: Nullable<string>[]): this;
112
+ appendLineIf(condition: Condition, ...value: Nullable<TextOrBuilderFn<this>>[]): this;
83
113
  /**
84
114
  * Appends the specified values to this source builder with the specified line prefix.
85
115
  * @param {string} prefix - The line prefix to use.
86
116
  * @param {...(string|null|undefined)} value - The values to append.
87
117
  * @returns {SourceBuilder} This source builder instance.
88
118
  */
89
- appendWithLinePrefix(prefix: string, ...value: Nullable<string>[]): this;
119
+ appendWithLinePrefix(prefix: string, ...value: Nullable<TextOrBuilderFn<this>>[]): this;
90
120
  /**
91
121
  * Appends the specified values to this source builder as a new line with the specified line prefix.
92
122
  * @param {string} prefix - The line prefix to use.
93
123
  * @param {...(string|null|undefined)} value - The values to append.
94
124
  * @returns {SourceBuilder} This source builder instance.
95
125
  */
96
- appendLineWithLinePrefix(prefix: string, ...value: Nullable<string>[]): this;
126
+ appendLineWithLinePrefix(prefix: string, ...value: Nullable<TextOrBuilderFn<this>>[]): this;
97
127
  /**
98
128
  * Prepends one or more strings to the beginning of the current SourceBuilder if the specified condition is true.
99
129
  * @param condition The condition to check before prepending the specified value.
100
130
  * @param value The string(s) to prepend.
101
131
  * @returns The current SourceBuilder.
102
132
  */
103
- prependIf(condition: Condition, ...value: Nullable<string | ((builder: StringBuilder) => void)>[]): this;
133
+ prependIf(condition: Condition, ...value: Nullable<TextOrBuilderFn<StringBuilder>>[]): this;
104
134
  /**
105
135
  * Prepends one or more strings to the beginning of the current SourceBuilder, followed by a line terminator, if the specified condition is true.
106
136
  * @param condition The condition to check before appending the specified value.
107
137
  * @param value The string(s) to prepend.
108
138
  * @returns The current SourceBuilder.
109
139
  */
110
- prependLineIf(condition: Condition, ...value: Nullable<string | ((builder: StringBuilder) => void)>[]): this;
111
- /**
112
- * Applies the provided builder function to this source builder.
113
- * @param builderFn The builder function to apply.
114
- * @returns This source builder instance.
115
- */
116
- apply(builderFn: (builder: this) => void): this;
117
- /**
118
- * Applies the provided builder function to this source builder with the specified line prefix.
119
- * @param prefix The line prefix to use.
120
- * @param builderFn The builder function to apply.
121
- * @returns This source builder instance.
122
- */
123
- applyWithLinePrefix(prefix: string, builderFn: (builder: this) => void): this;
140
+ prependLineIf(condition: Condition, ...value: Nullable<TextOrBuilderFn<StringBuilder>>[]): this;
124
141
  /**
125
142
  * Ensures that the current line is empty or consists only of whitespace characters.
126
143
  * @returns A reference to this instance.
@@ -134,100 +151,53 @@ export declare class SourceBuilder extends StringBuilder {
134
151
  /**
135
152
  * Inserts specified content if the specified condition is true.
136
153
  * @param condition The condition to check before inserting content.
137
- * @param builderFn The function to add content.
138
- * @returns A reference to this instance.
139
- */
140
- applyIf(condition: Condition, builderFn: (builder: this) => void): this;
141
- /**
142
- * Inserts specified content depending on the specified condition.
143
- * @param condition The condition to check before inserting content.
144
154
  * @param builderFn The function to add content if the condition is true.
145
155
  * @param elseBuilderFn The function to add content if the condition is false.
146
156
  * @returns A reference to this instance.
147
157
  */
148
- applyIfElse(condition: Condition, builderFn: (builder: this) => void, elseBuilderFn: (builder: this) => void): this;
158
+ if(condition: Condition, builderFn: BuilderFn<this>, elseBuilderFn?: BuilderFn<this>): this;
149
159
  /**
150
160
  * Adds one indentation level. If the current line already contains characters, only subsequent lines are affected.
151
161
  * @param builderFn The function to add indented content or the content itself.
152
162
  * @param condition The condition to check before adding indentation.
153
163
  * @returns A reference to this instance.
154
164
  */
155
- indent(builderFnOrString: ((builder: this) => void) | string, condition?: Condition): this;
165
+ indent(value: Nullable<TextOrBuilderFn<this>>): this;
156
166
  /**
157
- * Adds parentheses around the specified content and adds one indentation level.
158
- * @param brackets The brackets to use.
159
- * @param builderFnOrString The function to add content inside parentheses or one string that should be parenthesized.
160
- * @param indent Whether to indent the content inside parentheses. Defaults to `true`.
167
+ * Adds one indentation level if the specified condition is true. If the current line already contains characters, only subsequent lines are affected.
168
+ * @param builderFn The function to add indented content or the content itself.
169
+ * @param condition The condition to check before adding indentation.
161
170
  * @returns A reference to this instance.
162
171
  */
163
- parenthesize(brackets: Paratheses, builderFnOrString: ((builder: this) => void) | string, indent?: boolean): this;
172
+ indentIf(condition: Condition, value: Nullable<TextOrBuilderFn<this>>): this;
164
173
  /**
165
- * Adds parentheses around the specified content and adds one indentation level. Line breaks are added after start and before end of the parentheses.
174
+ * Adds parentheses around the specified content.
166
175
  * @param brackets The brackets to use.
167
- * @param builderFnOrString The function to add content inside parentheses or one string that should be parenthesized.
168
- * @param indent Whether to indent the content inside parentheses. Defaults to `true`.
176
+ * @param value The function to add content inside parentheses or one string that should be parenthesized.
177
+ * @param options Options for the parentheses.
169
178
  * @returns A reference to this instance.
170
179
  */
171
- parenthesizeMultiline(brackets: Paratheses, builderFnOrString: ((builder: this) => void) | string, indent?: boolean): this;
180
+ parenthesize(brackets: Paratheses, value: Nullable<TextOrBuilderFn<this>>, options?: ParenthesizeOptions): this;
172
181
  /**
173
182
  * Adds parentheses around the specified content if the specified condition is true.
174
183
  * @param condition The condition to check before adding parentheses.
175
184
  * @param brackets The brackets to use.
176
- * @param builderFnOrString The function to add content inside parentheses or one string that should be parenthesized.
177
- * @param indent Whether to indent the content inside parentheses. Defaults to `true`.
178
- * @returns A reference to this instance.
179
- */
180
- parenthesizeIf(condition: Condition, brackets: Paratheses, builderFnOrString: ((builder: this) => void) | string, indent?: boolean): this;
181
- /**
182
- * Adds parentheses around the specified content if the specified condition is true. Line breaks are added after start and before end of the parentheses.
183
- * @param condition The condition to check before adding parentheses.
184
- * @param brackets The brackets to use.
185
- * @param builderFnOrString The function to add content inside parentheses or one string that should be parenthesized.
186
- * @param indent Whether to indent the content inside parentheses. Defaults to `true`.
187
- * @returns A reference to this instance.
188
- */
189
- parenthesizeMultilineIf(condition: Condition, brackets: Paratheses, builderFnOrString: ((builder: this) => void) | string, indent?: boolean): this;
190
- /**
191
- * Adds content for each item in the specified array.
192
- * @param items The items to add content for.
193
- * @param builderFn The function to add content for each item.
194
- * @returns A reference to this instance.
195
- */
196
- forEach<T>(items: Iterable<T>, builderFn: (builder: this, item: T, index: number) => void): this;
197
- /**
198
- * Adds content for each item in the specified array.
199
- * @param items The items to add content for.
200
- * @param condition The condition to check before adding content.
201
- * @param builderFn The function to add content for each item.
202
- * @returns A reference to this instance.
203
- */
204
- forEachIf<T>(items: Iterable<T>, condition: Condition, builderFn: (builder: this, item: T, index: number) => void): this;
205
- /**
206
- * Adds content for each item in the specified array.
207
- * @param items The items to add content for.
208
- * @param condition The condition to check before adding content for each item.
209
- * @param builderFn The function to add content for each item.
210
- * @returns A reference to this instance.
211
- */
212
- forEachMatching<T>(items: Iterable<T>, condition: Condition<T>, builderFn: (builder: this, item: T, index: number) => void): this;
213
- /**
214
- * Adds content for each item in the specified array.
215
- * @param items The items to add content for.
216
- * @param seperator The seperator to add between items.
217
- * @param builderFn The function to add content for each item.
185
+ * @param value The function to add content inside parentheses or one string that should be parenthesized.
186
+ * @param options Options for the parentheses.
218
187
  * @returns A reference to this instance.
219
188
  */
220
- forEachSeparated<T>(items: Iterable<T>, seperator: Separator<this, T>, builderFn: (builder: this, item: T, index: number) => void): this;
189
+ parenthesizeIf(condition: Condition, brackets: Paratheses, value: TextOrBuilderFn<this>, options?: ParenthesizeOptions): this;
221
190
  /**
222
191
  * Adds content for each item in the specified array.
223
192
  * @param items The items to add content for.
224
- * @param condition The condition to check before adding content for each item.
225
- * @param seperator The seperator to add between items.
226
193
  * @param builderFn The function to add content for each item.
194
+ * @param options The options for the loop.
227
195
  * @returns A reference to this instance.
228
196
  */
229
- forEachMatchingSeparated<T>(items: Iterable<T>, condition: Condition<T>, seperator: Separator<this, T>, builderFn: (builder: this, item: T, index: number) => void): this;
230
- private forEachImpl;
197
+ forEach<T>(items: Iterable<T>, builderFn: (builder: this, item: T, index: number) => void, options?: ForEachOptions<this, T>): this;
198
+ forEachIf<T>(condition: Condition, items: Iterable<T>, builderFn: (builder: this, item: T, index: number) => void, options?: ForEachOptions<this, T>): this;
199
+ appendSeparated(items: Iterable<Nullable<TextOrBuilderFn<this>>>, separator: string): this;
200
+ appendSeparatedIf(condition: Condition, items: Iterable<Nullable<TextOrBuilderFn<this>>>, separator: string): this;
231
201
  private appendIndent;
232
202
  }
233
203
  export {};
@@ -111,6 +111,8 @@ export type StringBuilderOptions = {
111
111
  readonly newLine: string;
112
112
  };
113
113
  export declare const defaultStringBuilderOptions: StringBuilderOptions;
114
+ export type BuilderFn<TBuilder extends StringBuilder> = (builder: TBuilder) => void;
115
+ export type TextOrBuilderFn<TBuilder extends StringBuilder> = string | BuilderFn<TBuilder>;
114
116
  /**
115
117
  * Represents a mutable string of characters.
116
118
  */
@@ -142,25 +144,25 @@ export declare class StringBuilder {
142
144
  * @param value The string(s) to append.
143
145
  * @returns The current StringBuilder.
144
146
  */
145
- append(...value: Nullable<string>[]): this;
147
+ append(...value: Nullable<TextOrBuilderFn<this>>[]): this;
146
148
  /**
147
149
  * Appends one or more strings to the end of the current StringBuilder, followed by a line terminator.
148
150
  * @param value
149
151
  * @returns
150
152
  */
151
- appendLine(...value: Nullable<string>[]): this;
153
+ appendLine(...value: Nullable<TextOrBuilderFn<this>>[]): this;
152
154
  /**
153
155
  * Prepends one or more strings to the beginning of the current StringBuilder.
154
156
  * @param value The string(s) to prepend.
155
157
  * @returns The current StringBuilder.
156
158
  */
157
- prepend(...value: Nullable<string | ((builder: StringBuilder) => void)>[]): this;
159
+ prepend(...value: Nullable<TextOrBuilderFn<StringBuilder>>[]): this;
158
160
  /**
159
161
  * Prepends one or more strings to the beginning of the current StringBuilder, followed by a line terminator.
160
162
  * @param value The string(s) to prepend.
161
163
  * @returns The current StringBuilder.
162
164
  */
163
- prependLine(...value: Nullable<string | ((builder: StringBuilder) => void)>[]): this;
165
+ prependLine(...value: Nullable<TextOrBuilderFn<StringBuilder>>[]): this;
164
166
  /**
165
167
  * Converts the value of this instance to a string.
166
168
  * @returns A string whose value is the same as this instance.
@@ -3,6 +3,7 @@ type MergeTwo<T, U> = {
3
3
  } & U;
4
4
  export type Merge<U extends unknown[]> = U extends [infer A, ...infer R] ? MergeTwo<A, Merge<R>> : {};
5
5
  export type OptionalProperties<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
6
+ export type RequiredProperties<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>;
6
7
  export type ArrayItem<T> = T extends (infer U)[] ? U : never;
7
8
  export type Nullable<T> = T | null | undefined;
8
9
  export interface Constructor<T, TArgs extends any[]> extends Function {