@goast/core 0.0.5 → 0.1.0

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.
@@ -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,136 +205,82 @@ 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
  }
240
221
  /**
241
222
  * Adds one indentation level. If the current line already contains characters, only subsequent lines are affected.
242
- * @param builderFn The function to add indented content.
223
+ * @param builderFn The function to add indented content or the content itself.
243
224
  * @param condition The condition to check before adding indentation.
244
225
  * @returns A reference to this instance.
245
226
  */
246
- indent(builderFn, condition = true) {
247
- const shouldIndent = evalCondition(condition);
248
- if (shouldIndent) {
249
- this.currentIndentLevel++;
250
- }
227
+ indent(value) {
228
+ this.currentIndentLevel++;
251
229
  try {
252
- builderFn(this);
230
+ this.append(value);
253
231
  }
254
232
  finally {
255
- if (shouldIndent) {
256
- this.currentIndentLevel--;
257
- }
233
+ this.currentIndentLevel--;
258
234
  }
259
235
  return this;
260
236
  }
261
237
  /**
262
- * Adds parentheses around the specified content and adds one indentation level.
263
- * @param brackets The brackets to use.
264
- * @param builderFn The function to add content inside parentheses.
265
- * @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.
266
241
  * @returns A reference to this instance.
267
242
  */
268
- parenthesize(brackets, builderFn, indent = true) {
269
- var _a, _b;
270
- this.append((_a = brackets[0]) !== null && _a !== void 0 ? _a : '');
271
- this.indent(builderFn, indent);
272
- this.append((_b = brackets[1]) !== null && _b !== void 0 ? _b : '');
273
- return this;
243
+ indentIf(condition, value) {
244
+ return this.if(condition, (builder) => builder.indent(value), (builder) => builder.append(value));
274
245
  }
275
246
  /**
276
- * Adds parentheses around the specified content if the specified condition is true.
277
- * @param condition The condition to check before adding parentheses.
247
+ * Adds parentheses around the specified content.
278
248
  * @param brackets The brackets to use.
279
- * @param builderFn The function to add content inside parentheses.
280
- * @param indent Whether to indent the content inside parentheses. Defaults to `true`.
281
- * @returns A reference to this instance.
282
- */
283
- parenthesizeIf(condition, brackets, builderFn, indent) {
284
- if (evalCondition(condition)) {
285
- this.parenthesize(brackets, builderFn, indent !== null && indent !== void 0 ? indent : true);
286
- }
287
- else {
288
- this.indent(builderFn, indent !== null && indent !== void 0 ? indent : false);
289
- }
290
- return this;
291
- }
292
- /**
293
- * Adds content for each item in the specified array.
294
- * @param items The items to add content for.
295
- * @param builderFn The function to add content for each item.
249
+ * @param value The function to add content inside parentheses or one string that should be parenthesized.
250
+ * @param options Options for the parentheses.
296
251
  * @returns A reference to this instance.
297
252
  */
298
- forEach(items, builderFn) {
299
- return this.forEachImpl(items, builderFn);
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 : '');
300
259
  }
301
260
  /**
302
- * Adds content for each item in the specified array.
303
- * @param items The items to add content for.
304
- * @param condition The condition to check before adding content.
305
- * @param builderFn The function to add content for each item.
306
- * @returns A reference to this instance.
307
- */
308
- forEachIf(items, condition, builderFn) {
309
- return evalCondition(condition) ? this.forEachImpl(items, builderFn) : this;
310
- }
311
- /**
312
- * Adds content for each item in the specified array.
313
- * @param items The items to add content for.
314
- * @param condition The condition to check before adding content for each item.
315
- * @param builderFn The function to add content for each item.
316
- * @returns A reference to this instance.
317
- */
318
- forEachMatching(items, condition, builderFn) {
319
- return this.forEachImpl(items, builderFn, undefined, condition);
320
- }
321
- /**
322
- * Adds content for each item in the specified array.
323
- * @param items The items to add content for.
324
- * @param seperator The seperator to add between items.
325
- * @param builderFn The function to add content for each item.
261
+ * Adds parentheses around the specified content if the specified condition is true.
262
+ * @param condition The condition to check before adding parentheses.
263
+ * @param brackets The brackets to use.
264
+ * @param value The function to add content inside parentheses or one string that should be parenthesized.
265
+ * @param options Options for the parentheses.
326
266
  * @returns A reference to this instance.
327
267
  */
328
- forEachSeparated(items, seperator, builderFn) {
329
- 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));
330
270
  }
331
271
  /**
332
272
  * Adds content for each item in the specified array.
333
273
  * @param items The items to add content for.
334
- * @param condition The condition to check before adding content for each item.
335
- * @param seperator The seperator to add between items.
336
274
  * @param builderFn The function to add content for each item.
275
+ * @param options The options for the loop.
337
276
  * @returns A reference to this instance.
338
277
  */
339
- forEachMatchingSeparated(items, condition, seperator, builderFn) {
340
- return this.forEachImpl(items, builderFn, seperator, condition);
341
- }
342
- forEachImpl(items, builderFn, seperator, condition = true) {
343
- 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)
344
282
  return this;
283
+ const seperator = options === null || options === void 0 ? void 0 : options.separator;
345
284
  let previousIndex = -1;
346
285
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
347
286
  let previousItem = undefined;
@@ -364,6 +303,15 @@ export class SourceBuilder extends StringBuilder {
364
303
  }
365
304
  return this;
366
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
+ }
367
315
  appendIndent() {
368
316
  super.append(this._indentString.repeat(this.currentIndentLevel));
369
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.5",
3
+ "version": "0.1.0",
4
4
  "repository": "https://github.com/MaSch0212/goast.git",
5
5
  "author": {
6
6
  "name": "Marc Schmidt (MaSch0212)",
@@ -1,3 +1,4 @@
1
1
  import { OpenApiCollectorData } from './types';
2
- export declare function collect<T extends Record<`x-${string}`, unknown>>(data: OpenApiCollectorData, obj: T | T[] | undefined, func: (data: OpenApiCollectorData, obj: NonNullable<T>) => void): void;
3
- export declare function collectRecord<T extends Record<`x-${string}`, unknown>>(data: OpenApiCollectorData, obj: Record<string, T> | undefined, func: (data: OpenApiCollectorData, obj: NonNullable<T>, key: string) => void): void;
2
+ import { Nullable } from '../utils';
3
+ export declare function collect<T extends Record<`x-${string}`, unknown>>(data: OpenApiCollectorData, obj: Nullable<T | Nullable<T>[]>, func: (data: OpenApiCollectorData, obj: NonNullable<T>) => void): void;
4
+ export declare function collectRecord<T extends Record<`x-${string}`, unknown>>(data: OpenApiCollectorData, obj: Nullable<Record<string, Nullable<T>>>, func: (data: OpenApiCollectorData, obj: NonNullable<T>, key: string) => void): void;
@@ -1,11 +1,14 @@
1
- import { OpenApiDocument, OpenApiReference } from './openapi-types';
1
+ import { OpenApiDiscriminator, OpenApiDocument, OpenApiReference, OpenApiSchema } from './openapi-types';
2
2
  export type DerefSource<T> = {
3
3
  file: string;
4
4
  path: string;
5
5
  document: Deref<OpenApiDocument>;
6
6
  originalComponent: T;
7
7
  };
8
- type _Deref<T> = {
8
+ type _DerefDiscriminator = Omit<OpenApiDiscriminator, 'mapping'> & {
9
+ mapping?: Record<string, Deref<OpenApiSchema>>;
10
+ };
11
+ type _Deref<T extends object> = T extends OpenApiDiscriminator ? _DerefDiscriminator : {
9
12
  [K in keyof T as K extends keyof OpenApiReference ? never : K]: Deref<T[K]>;
10
13
  } & {
11
14
  $src: DerefSource<T>;
@@ -88,6 +88,10 @@ export type ApiSchemaProperty = {
88
88
  name: string;
89
89
  schema: ApiSchema;
90
90
  };
91
+ export type ApiSchemaDiscriminator = {
92
+ propertyName: string;
93
+ mapping: Record<string, ApiSchema>;
94
+ };
91
95
  export type ApiSchemaComponent = ApiComponent<OpenApiSchema>;
92
96
  export type ApiSchemaBase = ApiSchemaComponent & {
93
97
  name: string;
@@ -103,6 +107,8 @@ export type ApiSchemaBase = ApiSchemaComponent & {
103
107
  required: Set<string>;
104
108
  custom: Record<string, unknown>;
105
109
  not: ApiSchema | undefined;
110
+ discriminator: ApiSchemaDiscriminator | undefined;
111
+ inheritedSchemas: ApiSchema[];
106
112
  };
107
113
  export type ApiSchema<T extends ApiSchemaKind = ApiSchemaKind> = ApiSchemaBase & ApiSchemaExtensions<T>;
108
114
  type AdditionalCombinedSchemaProperties = {
@@ -1,4 +1,4 @@
1
1
  import { ApiSchema } from './api-types';
2
2
  import { OpenApiTransformerContext } from './types';
3
3
  import { Deref, OpenApiSchema } from '../parse';
4
- export declare function transformSchema<T extends Deref<OpenApiSchema>>(context: OpenApiTransformerContext, schema: T, isReference?: boolean): ApiSchema;
4
+ export declare function transformSchema<T extends Deref<OpenApiSchema>>(context: OpenApiTransformerContext, schema: T): ApiSchema;