@goast/core 0.1.0 → 0.2.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.
- package/cjs/index.js +1 -0
- package/cjs/lib/ast/index.js +4 -0
- package/cjs/lib/ast/node.js +27 -0
- package/cjs/lib/parse/deref-proxy.js +9 -3
- package/cjs/lib/parse/parser.js +1 -1
- package/cjs/lib/transform/transform-endpoint.js +1 -1
- package/cjs/lib/transform/transform-schema.js +10 -5
- package/cjs/lib/utils/array.utils.js +11 -0
- package/cjs/lib/utils/class.utils.js +33 -0
- package/cjs/lib/utils/common.utils.js +75 -1
- package/cjs/lib/utils/error.utils.js +7 -0
- package/cjs/lib/utils/index.js +4 -0
- package/cjs/lib/utils/object.utils.js +6 -2
- package/cjs/lib/utils/schema.utils.js +2 -1
- package/cjs/lib/utils/source-builder.js +171 -81
- package/cjs/lib/utils/string-builder/index.js +8 -0
- package/cjs/lib/utils/string-builder/options.js +7 -0
- package/cjs/lib/utils/string-builder/string-builder.js +142 -0
- package/cjs/lib/utils/string-builder/utils.js +38 -0
- package/cjs/lib/utils/string.utils.js +123 -107
- package/cjs/lib/utils/type.utils.js +9 -0
- package/esm/index.js +1 -0
- package/esm/lib/ast/index.js +1 -0
- package/esm/lib/ast/node.js +23 -0
- package/esm/lib/parse/deref-proxy.js +9 -3
- package/esm/lib/parse/parser.js +1 -1
- package/esm/lib/transform/transform-endpoint.js +1 -1
- package/esm/lib/transform/transform-schema.js +10 -5
- package/esm/lib/utils/array.utils.js +7 -0
- package/esm/lib/utils/class.utils.js +27 -0
- package/esm/lib/utils/common.utils.js +64 -0
- package/esm/lib/utils/error.utils.js +3 -0
- package/esm/lib/utils/index.js +4 -0
- package/esm/lib/utils/object.utils.js +6 -2
- package/esm/lib/utils/schema.utils.js +2 -1
- package/esm/lib/utils/source-builder.js +170 -80
- package/esm/lib/utils/string-builder/index.js +3 -0
- package/esm/lib/utils/string-builder/options.js +4 -0
- package/esm/lib/utils/string-builder/string-builder.js +138 -0
- package/esm/lib/utils/string-builder/utils.js +30 -0
- package/esm/lib/utils/string.utils.js +111 -105
- package/esm/lib/utils/type.utils.js +6 -1
- package/package.json +5 -5
- package/types/index.d.ts +1 -0
- package/types/lib/ast/index.d.ts +1 -0
- package/types/lib/ast/node.d.ts +14 -0
- package/types/lib/transform/api-types.d.ts +3 -1
- package/types/lib/utils/array.utils.d.ts +1 -0
- package/types/lib/utils/class.utils.d.ts +7 -0
- package/types/lib/utils/common.utils.d.ts +14 -2
- package/types/lib/utils/error.utils.d.ts +1 -0
- package/types/lib/utils/index.d.ts +4 -0
- package/types/lib/utils/object.utils.d.ts +1 -1
- package/types/lib/utils/source-builder.d.ts +55 -37
- package/types/lib/utils/string-builder/index.d.ts +3 -0
- package/types/lib/utils/string-builder/options.d.ts +8 -0
- package/types/lib/utils/string-builder/string-builder.d.ts +72 -0
- package/types/lib/utils/string-builder/utils.d.ts +21 -0
- package/types/lib/utils/string.utils.d.ts +20 -66
- package/types/lib/utils/type.utils.d.ts +43 -1
|
@@ -1,18 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.SourceBuilder = exports.defaultSourceBuilderOptions = void 0;
|
|
3
|
+
exports.builderTemplate = exports.SourceBuilder = exports.defaultSourceBuilderOptions = void 0;
|
|
4
4
|
const os_1 = require("os");
|
|
5
|
+
const common_utils_1 = require("./common.utils");
|
|
5
6
|
const condition_1 = require("./condition");
|
|
7
|
+
const string_builder_1 = require("./string-builder");
|
|
6
8
|
const string_utils_1 = require("./string.utils");
|
|
7
9
|
exports.defaultSourceBuilderOptions = {
|
|
8
10
|
indent: { type: 'spaces', count: 2 },
|
|
9
11
|
newLine: os_1.EOL,
|
|
10
|
-
charsTreatedAsEmptyLine: ['{'],
|
|
11
12
|
};
|
|
12
13
|
/**
|
|
13
14
|
* Represents an in-memory source file.
|
|
14
15
|
*/
|
|
15
|
-
class SourceBuilder extends
|
|
16
|
+
class SourceBuilder extends string_builder_1.StringBuilder {
|
|
16
17
|
/**
|
|
17
18
|
* Gets the options used by this instance.
|
|
18
19
|
*/
|
|
@@ -25,16 +26,11 @@ class SourceBuilder extends string_utils_1.StringBuilder {
|
|
|
25
26
|
*/
|
|
26
27
|
constructor(options) {
|
|
27
28
|
super(options);
|
|
28
|
-
this._linePrefix = '';
|
|
29
29
|
this._isLineIndented = false;
|
|
30
30
|
this._isLastLineEmpty = true;
|
|
31
31
|
this._isCurrentLineEmpty = true;
|
|
32
|
-
|
|
33
|
-
* Gets or sets the current indentation level.
|
|
34
|
-
*/
|
|
35
|
-
this.currentIndentLevel = 0;
|
|
32
|
+
this._linePrefixNodes = [];
|
|
36
33
|
this.__options = Object.assign(Object.assign({}, exports.defaultSourceBuilderOptions), options);
|
|
37
|
-
this._emptyLineCharRegex = new RegExp(`[\\s${this.__options.charsTreatedAsEmptyLine.join('')}]`);
|
|
38
34
|
this._indentString = this.__options.indent.type === 'tabs' ? '\t' : ' '.repeat(this.__options.indent.count);
|
|
39
35
|
}
|
|
40
36
|
/**
|
|
@@ -56,61 +52,66 @@ class SourceBuilder extends string_utils_1.StringBuilder {
|
|
|
56
52
|
*/
|
|
57
53
|
static build(buildAction, options) {
|
|
58
54
|
const builder = new SourceBuilder(options);
|
|
59
|
-
buildAction
|
|
55
|
+
builder.append(buildAction);
|
|
60
56
|
return builder.toString();
|
|
61
57
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
if (!str)
|
|
70
|
-
continue;
|
|
71
|
-
if (typeof str === 'function') {
|
|
72
|
-
str(this);
|
|
73
|
-
continue;
|
|
58
|
+
appendSingle(value) {
|
|
59
|
+
var _a;
|
|
60
|
+
if ((0, common_utils_1.isNullish)(value))
|
|
61
|
+
return;
|
|
62
|
+
if (Array.isArray(value)) {
|
|
63
|
+
for (const part of value) {
|
|
64
|
+
this.appendSingle(part);
|
|
74
65
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
if (typeof value === 'function') {
|
|
69
|
+
value(this);
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
if (typeof value === 'number' || typeof value === 'boolean') {
|
|
73
|
+
value = value.toString();
|
|
74
|
+
}
|
|
75
|
+
if (typeof value === 'object' && value !== null && '__type' in value && value.__type === 'append-value-group') {
|
|
76
|
+
this.forEach(value.values, (builder, part) => builder.append(part), { separator: (_a = value.separator) !== null && _a !== void 0 ? _a : undefined });
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
if (typeof value !== 'string') {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
let lineStartIndex = 0;
|
|
83
|
+
for (let i = 0; i < value.length; i++) {
|
|
84
|
+
if (value[i] === '\r')
|
|
85
|
+
continue;
|
|
86
|
+
if (value[i] === '\n') {
|
|
87
|
+
let lineLength = i - lineStartIndex;
|
|
88
|
+
for (let j = i; j > 0 && value[j - 1] === '\r'; j--) {
|
|
89
|
+
lineLength--;
|
|
90
|
+
}
|
|
91
|
+
if (lineLength > 0) {
|
|
92
|
+
if (!this._isLineIndented) {
|
|
93
93
|
this.appendIndent();
|
|
94
|
-
this._isLineIndented = true;
|
|
95
|
-
this._isCurrentLineEmpty = false;
|
|
96
|
-
}
|
|
97
|
-
else {
|
|
98
|
-
this._isLineIndented = false;
|
|
99
|
-
this._isCurrentLineEmpty = true;
|
|
100
94
|
}
|
|
101
|
-
lineStartIndex
|
|
95
|
+
this.str += (0, string_utils_1.removeStr)(value.substring(lineStartIndex, i), '\r');
|
|
102
96
|
}
|
|
103
|
-
|
|
104
|
-
|
|
97
|
+
this._isLastLineEmpty = this._isCurrentLineEmpty;
|
|
98
|
+
if (this._isCurrentLineEmpty && this._linePrefixNodes.some((x) => x.kind !== 'indent')) {
|
|
99
|
+
this.appendIndent();
|
|
105
100
|
}
|
|
101
|
+
this.str = (0, string_utils_1.trimEnd)(this.str, false) + '\n';
|
|
102
|
+
this._isLineIndented = false;
|
|
103
|
+
this._isCurrentLineEmpty = true;
|
|
104
|
+
lineStartIndex = i + 1;
|
|
106
105
|
}
|
|
107
|
-
if (
|
|
108
|
-
this.
|
|
109
|
-
this._isLineIndented = true;
|
|
106
|
+
if (value[i] !== '\r' && value[i] !== '\n' && value[i] !== ' ' && value[i] !== '\t') {
|
|
107
|
+
this._isCurrentLineEmpty = false;
|
|
110
108
|
}
|
|
111
|
-
super.append(str.substring(lineStartIndex).replace(/\r/g, ''));
|
|
112
109
|
}
|
|
113
|
-
|
|
110
|
+
if (!this._isLineIndented && lineStartIndex < value.length) {
|
|
111
|
+
this.appendIndent();
|
|
112
|
+
this._isLineIndented = true;
|
|
113
|
+
}
|
|
114
|
+
this.str += (0, string_utils_1.removeStr)(value.substring(lineStartIndex), '\r');
|
|
114
115
|
}
|
|
115
116
|
/**
|
|
116
117
|
* Appends one or more strings to the end of the current SourceBuilder if the specified condition is true.
|
|
@@ -119,7 +120,10 @@ class SourceBuilder extends string_utils_1.StringBuilder {
|
|
|
119
120
|
* @returns The current SourceBuilder.
|
|
120
121
|
*/
|
|
121
122
|
appendIf(condition, ...value) {
|
|
122
|
-
|
|
123
|
+
if ((0, condition_1.evalCondition)(condition)) {
|
|
124
|
+
return this.append(...value);
|
|
125
|
+
}
|
|
126
|
+
return this;
|
|
123
127
|
}
|
|
124
128
|
/**
|
|
125
129
|
* Appends one or more strings to the end of the current SourceBuilder, followed by a line terminator.
|
|
@@ -136,7 +140,10 @@ class SourceBuilder extends string_utils_1.StringBuilder {
|
|
|
136
140
|
* @returns The current SourceBuilder.
|
|
137
141
|
*/
|
|
138
142
|
appendLineIf(condition, ...value) {
|
|
139
|
-
|
|
143
|
+
if ((0, condition_1.evalCondition)(condition)) {
|
|
144
|
+
return this.appendLine(...value);
|
|
145
|
+
}
|
|
146
|
+
return this;
|
|
140
147
|
}
|
|
141
148
|
/**
|
|
142
149
|
* Appends the specified values to this source builder with the specified line prefix.
|
|
@@ -145,13 +152,12 @@ class SourceBuilder extends string_utils_1.StringBuilder {
|
|
|
145
152
|
* @returns {SourceBuilder} This source builder instance.
|
|
146
153
|
*/
|
|
147
154
|
appendWithLinePrefix(prefix, ...value) {
|
|
148
|
-
|
|
149
|
-
this._linePrefix += prefix;
|
|
155
|
+
this._linePrefixNodes.push({ chars: prefix, kind: 'prefix' });
|
|
150
156
|
try {
|
|
151
157
|
this.append(...value);
|
|
152
158
|
}
|
|
153
159
|
finally {
|
|
154
|
-
this.
|
|
160
|
+
this._linePrefixNodes.pop();
|
|
155
161
|
}
|
|
156
162
|
return this;
|
|
157
163
|
}
|
|
@@ -171,7 +177,10 @@ class SourceBuilder extends string_utils_1.StringBuilder {
|
|
|
171
177
|
* @returns The current SourceBuilder.
|
|
172
178
|
*/
|
|
173
179
|
prependIf(condition, ...value) {
|
|
174
|
-
|
|
180
|
+
if ((0, condition_1.evalCondition)(condition)) {
|
|
181
|
+
this.prepend(...value);
|
|
182
|
+
}
|
|
183
|
+
return this;
|
|
175
184
|
}
|
|
176
185
|
/**
|
|
177
186
|
* Prepends one or more strings to the beginning of the current SourceBuilder, followed by a line terminator, if the specified condition is true.
|
|
@@ -180,10 +189,13 @@ class SourceBuilder extends string_utils_1.StringBuilder {
|
|
|
180
189
|
* @returns The current SourceBuilder.
|
|
181
190
|
*/
|
|
182
191
|
prependLineIf(condition, ...value) {
|
|
183
|
-
|
|
192
|
+
if ((0, condition_1.evalCondition)(condition)) {
|
|
193
|
+
this.prependLine(...value);
|
|
194
|
+
}
|
|
195
|
+
return this;
|
|
184
196
|
}
|
|
185
197
|
/**
|
|
186
|
-
* Ensures that the current line is empty or consists only of whitespace characters.
|
|
198
|
+
* Ensures that the current line and the one before is empty or consists only of whitespace characters.
|
|
187
199
|
* @returns A reference to this instance.
|
|
188
200
|
*/
|
|
189
201
|
ensurePreviousLineEmpty() {
|
|
@@ -205,6 +217,23 @@ class SourceBuilder extends string_utils_1.StringBuilder {
|
|
|
205
217
|
}
|
|
206
218
|
return this;
|
|
207
219
|
}
|
|
220
|
+
/**
|
|
221
|
+
* Pretends that the current line and the one before is empty or consists only of whitespace characters.
|
|
222
|
+
* @returns A reference to this instance.
|
|
223
|
+
*/
|
|
224
|
+
pretendPreviousLineEmpty() {
|
|
225
|
+
this._isLastLineEmpty = true;
|
|
226
|
+
this._isCurrentLineEmpty = true;
|
|
227
|
+
return this;
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Pretends that the current line is empty or consists only of whitespace characters.
|
|
231
|
+
* @returns A reference to this instance.
|
|
232
|
+
*/
|
|
233
|
+
pretendCurrentLineEmpty() {
|
|
234
|
+
this._isCurrentLineEmpty = true;
|
|
235
|
+
return this;
|
|
236
|
+
}
|
|
208
237
|
/**
|
|
209
238
|
* Inserts specified content if the specified condition is true.
|
|
210
239
|
* @param condition The condition to check before inserting content.
|
|
@@ -213,11 +242,20 @@ class SourceBuilder extends string_utils_1.StringBuilder {
|
|
|
213
242
|
* @returns A reference to this instance.
|
|
214
243
|
*/
|
|
215
244
|
if(condition, builderFn, elseBuilderFn) {
|
|
216
|
-
|
|
217
|
-
|
|
245
|
+
return (0, condition_1.evalCondition)(condition) ? this.append(builderFn) : this.append(elseBuilderFn);
|
|
246
|
+
}
|
|
247
|
+
switch(value, cases, defaultBuilderFn, equals) {
|
|
248
|
+
var _a;
|
|
249
|
+
if ((typeof value === 'string' || typeof value === 'number') && !Array.isArray(cases)) {
|
|
250
|
+
return this.append((_a = cases[value]) !== null && _a !== void 0 ? _a : defaultBuilderFn);
|
|
218
251
|
}
|
|
219
|
-
else {
|
|
220
|
-
|
|
252
|
+
else if (Array.isArray(cases)) {
|
|
253
|
+
for (const c of cases) {
|
|
254
|
+
if (equals ? equals(value, c.case) : value === c.case) {
|
|
255
|
+
return this.append(c.value);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
return this.append(defaultBuilderFn);
|
|
221
259
|
}
|
|
222
260
|
return this;
|
|
223
261
|
}
|
|
@@ -228,12 +266,12 @@ class SourceBuilder extends string_utils_1.StringBuilder {
|
|
|
228
266
|
* @returns A reference to this instance.
|
|
229
267
|
*/
|
|
230
268
|
indent(value) {
|
|
231
|
-
this.
|
|
269
|
+
this._linePrefixNodes.push({ chars: this._indentString, kind: 'indent' });
|
|
232
270
|
try {
|
|
233
271
|
this.append(value);
|
|
234
272
|
}
|
|
235
273
|
finally {
|
|
236
|
-
this.
|
|
274
|
+
this._linePrefixNodes.pop();
|
|
237
275
|
}
|
|
238
276
|
return this;
|
|
239
277
|
}
|
|
@@ -244,7 +282,7 @@ class SourceBuilder extends string_utils_1.StringBuilder {
|
|
|
244
282
|
* @returns A reference to this instance.
|
|
245
283
|
*/
|
|
246
284
|
indentIf(condition, value) {
|
|
247
|
-
return
|
|
285
|
+
return (0, condition_1.evalCondition)(condition) ? this.indent(value) : this.append(value);
|
|
248
286
|
}
|
|
249
287
|
/**
|
|
250
288
|
* Adds parentheses around the specified content.
|
|
@@ -254,11 +292,28 @@ class SourceBuilder extends string_utils_1.StringBuilder {
|
|
|
254
292
|
* @returns A reference to this instance.
|
|
255
293
|
*/
|
|
256
294
|
parenthesize(brackets, value, options) {
|
|
257
|
-
var _a, _b, _c, _d;
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
295
|
+
var _a, _b, _c, _d, _e, _f;
|
|
296
|
+
const multiline = (_a = options === null || options === void 0 ? void 0 : options.multiline) !== null && _a !== void 0 ? _a : false;
|
|
297
|
+
const indent = (_b = options === null || options === void 0 ? void 0 : options.indent) !== null && _b !== void 0 ? _b : true;
|
|
298
|
+
const pretendEmpty = (_c = options === null || options === void 0 ? void 0 : options.pretendEmpty) !== null && _c !== void 0 ? _c : true;
|
|
299
|
+
if (multiline) {
|
|
300
|
+
this.appendLine((_d = brackets[0]) !== null && _d !== void 0 ? _d : '');
|
|
301
|
+
}
|
|
302
|
+
else {
|
|
303
|
+
this.append((_e = brackets[0]) !== null && _e !== void 0 ? _e : '');
|
|
304
|
+
}
|
|
305
|
+
if (pretendEmpty)
|
|
306
|
+
this.pretendPreviousLineEmpty();
|
|
307
|
+
if (indent !== false) {
|
|
308
|
+
this.indent(value);
|
|
309
|
+
}
|
|
310
|
+
else {
|
|
311
|
+
this.append(value);
|
|
312
|
+
}
|
|
313
|
+
if (multiline)
|
|
314
|
+
this.ensureCurrentLineEmpty();
|
|
315
|
+
this.append((_f = brackets[1]) !== null && _f !== void 0 ? _f : '');
|
|
316
|
+
return this;
|
|
262
317
|
}
|
|
263
318
|
/**
|
|
264
319
|
* Adds parentheses around the specified content if the specified condition is true.
|
|
@@ -269,7 +324,15 @@ class SourceBuilder extends string_utils_1.StringBuilder {
|
|
|
269
324
|
* @returns A reference to this instance.
|
|
270
325
|
*/
|
|
271
326
|
parenthesizeIf(condition, brackets, value, options) {
|
|
272
|
-
|
|
327
|
+
if ((0, condition_1.evalCondition)(condition)) {
|
|
328
|
+
return this.parenthesize(brackets, value, options);
|
|
329
|
+
}
|
|
330
|
+
else if (options === null || options === void 0 ? void 0 : options.indent) {
|
|
331
|
+
return this.indent(value);
|
|
332
|
+
}
|
|
333
|
+
else {
|
|
334
|
+
return this.append(value);
|
|
335
|
+
}
|
|
273
336
|
}
|
|
274
337
|
/**
|
|
275
338
|
* Adds content for each item in the specified array.
|
|
@@ -307,19 +370,46 @@ class SourceBuilder extends string_utils_1.StringBuilder {
|
|
|
307
370
|
return this;
|
|
308
371
|
}
|
|
309
372
|
forEachIf(condition, items, builderFn, options) {
|
|
310
|
-
|
|
373
|
+
if ((0, condition_1.evalCondition)(condition)) {
|
|
374
|
+
return this.forEach(items, builderFn, options);
|
|
375
|
+
}
|
|
376
|
+
return this;
|
|
311
377
|
}
|
|
312
378
|
appendSeparated(items, separator) {
|
|
313
379
|
return this.forEach(items, (builder, item) => builder.append(item), { separator });
|
|
314
380
|
}
|
|
315
381
|
appendSeparatedIf(condition, items, separator) {
|
|
316
|
-
|
|
382
|
+
if ((0, condition_1.evalCondition)(condition)) {
|
|
383
|
+
return this.appendSeparated(items, separator);
|
|
384
|
+
}
|
|
385
|
+
return this;
|
|
317
386
|
}
|
|
318
387
|
appendIndent() {
|
|
319
|
-
|
|
320
|
-
if (this._linePrefix) {
|
|
321
|
-
super.append(this._linePrefix);
|
|
322
|
-
}
|
|
388
|
+
this.str += this._linePrefixNodes.map((n) => n.chars).join('');
|
|
323
389
|
}
|
|
324
390
|
}
|
|
325
391
|
exports.SourceBuilder = SourceBuilder;
|
|
392
|
+
function _builderTemplate(template, ...substitutions) {
|
|
393
|
+
return (b) => {
|
|
394
|
+
var _a, _b;
|
|
395
|
+
let lastLine = undefined;
|
|
396
|
+
for (let i = template.length - 1; i >= 0; i--) {
|
|
397
|
+
if (template[i].includes('\n')) {
|
|
398
|
+
lastLine = template[i];
|
|
399
|
+
break;
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
const indent = (_b = (_a = lastLine === null || lastLine === void 0 ? void 0 : lastLine.match(/\n( *).*$/)) === null || _a === void 0 ? void 0 : _a[1]) !== null && _b !== void 0 ? _b : '';
|
|
403
|
+
const regex = new RegExp(`\\n {0,${indent.length}}`, 'g');
|
|
404
|
+
for (let i = 0; i < template.length; i++) {
|
|
405
|
+
b.append(template[i].replace(regex, '\n'));
|
|
406
|
+
if (i < substitutions.length)
|
|
407
|
+
b.append(substitutions[i]);
|
|
408
|
+
}
|
|
409
|
+
};
|
|
410
|
+
}
|
|
411
|
+
exports.builderTemplate = Object.assign(_builderTemplate, {
|
|
412
|
+
indent: (template, ...substitutions) => {
|
|
413
|
+
return (b) => b.indent(_builderTemplate(template, ...substitutions));
|
|
414
|
+
},
|
|
415
|
+
});
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StringBuilder = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
tslib_1.__exportStar(require("./options"), exports);
|
|
6
|
+
var string_builder_1 = require("./string-builder");
|
|
7
|
+
Object.defineProperty(exports, "StringBuilder", { enumerable: true, get: function () { return string_builder_1.StringBuilder; } });
|
|
8
|
+
tslib_1.__exportStar(require("./utils"), exports);
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StringBuilder = exports.AdditionalAppendsSymbol = void 0;
|
|
4
|
+
const options_1 = require("./options");
|
|
5
|
+
const common_utils_1 = require("../common.utils");
|
|
6
|
+
exports.AdditionalAppendsSymbol = Symbol('AdditionalAppends');
|
|
7
|
+
/**
|
|
8
|
+
* Represents a mutable string of characters.
|
|
9
|
+
*/
|
|
10
|
+
class StringBuilder {
|
|
11
|
+
[exports.AdditionalAppendsSymbol](_) {
|
|
12
|
+
throw new Error('This method should never be called.');
|
|
13
|
+
}
|
|
14
|
+
get options() {
|
|
15
|
+
return this._options;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Initializes a new instance of the StringBuilder class.
|
|
19
|
+
* @param options The options.
|
|
20
|
+
*/
|
|
21
|
+
constructor(options) {
|
|
22
|
+
this.str = '';
|
|
23
|
+
this._options = Object.assign(Object.assign({}, options_1.defaultStringBuilderOptions), options);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Creates a new StringBuilder instance and appends the specified string to it.
|
|
27
|
+
* @param str The string to append.
|
|
28
|
+
* @param options The options.
|
|
29
|
+
* @returns A new StringBuilder instance.
|
|
30
|
+
*/
|
|
31
|
+
static fromString(str, options) {
|
|
32
|
+
const builder = new StringBuilder(options);
|
|
33
|
+
builder.append(str);
|
|
34
|
+
return builder;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Builds a string using a StringBuilder build action.
|
|
38
|
+
* @param buildAction The build action to perform on the StringBuilder instance.
|
|
39
|
+
* @param options The options for the StringBuilder instance.
|
|
40
|
+
* @returns The built string.
|
|
41
|
+
*/
|
|
42
|
+
static build(buildAction, options) {
|
|
43
|
+
const builder = new StringBuilder(options);
|
|
44
|
+
builder.append(buildAction);
|
|
45
|
+
return builder.toString();
|
|
46
|
+
}
|
|
47
|
+
appendSingle(value) {
|
|
48
|
+
if ((0, common_utils_1.isNullish)(value))
|
|
49
|
+
return;
|
|
50
|
+
if (Array.isArray(value)) {
|
|
51
|
+
for (const part of value) {
|
|
52
|
+
this.appendSingle(part);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
else if (typeof value === 'string') {
|
|
56
|
+
this.str += value;
|
|
57
|
+
}
|
|
58
|
+
else if (typeof value === 'number' || typeof value === 'boolean') {
|
|
59
|
+
this.str += value.toString();
|
|
60
|
+
}
|
|
61
|
+
else if (typeof value === 'function') {
|
|
62
|
+
value(this);
|
|
63
|
+
}
|
|
64
|
+
else if (typeof value === 'object' &&
|
|
65
|
+
value !== null &&
|
|
66
|
+
'__type' in value &&
|
|
67
|
+
value.__type === 'append-value-group') {
|
|
68
|
+
let isFirst = true;
|
|
69
|
+
for (const part of value.values) {
|
|
70
|
+
if (!isFirst && value.separator) {
|
|
71
|
+
this.append(value.separator);
|
|
72
|
+
}
|
|
73
|
+
this.appendSingle(part);
|
|
74
|
+
isFirst = false;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Appends one or more strings to the end of the current StringBuilder.
|
|
80
|
+
* @param value The string(s) to append.
|
|
81
|
+
* @returns The current StringBuilder.
|
|
82
|
+
*/
|
|
83
|
+
append(...value) {
|
|
84
|
+
for (const part of value) {
|
|
85
|
+
this.appendSingle(part);
|
|
86
|
+
}
|
|
87
|
+
return this;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Appends one or more strings to the end of the current StringBuilder, followed by a line terminator.
|
|
91
|
+
* @param value
|
|
92
|
+
* @returns
|
|
93
|
+
*/
|
|
94
|
+
appendLine(...value) {
|
|
95
|
+
return this.append(...value, '\n');
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Prepends one or more strings to the beginning of the current StringBuilder.
|
|
99
|
+
* @param value The string(s) to prepend.
|
|
100
|
+
* @returns The current StringBuilder.
|
|
101
|
+
*/
|
|
102
|
+
prepend(...value) {
|
|
103
|
+
for (const part of value.reverse()) {
|
|
104
|
+
if ((0, common_utils_1.isNullish)(part))
|
|
105
|
+
continue;
|
|
106
|
+
if (typeof part === 'function') {
|
|
107
|
+
const builder = new StringBuilder(this.options);
|
|
108
|
+
part(builder);
|
|
109
|
+
this.str = builder.str + this.str;
|
|
110
|
+
}
|
|
111
|
+
else if (typeof part === 'string') {
|
|
112
|
+
this.str = part + this.str;
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
this.str = part.toString() + this.str;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return this;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Prepends one or more strings to the beginning of the current StringBuilder, followed by a line terminator.
|
|
122
|
+
* @param value The string(s) to prepend.
|
|
123
|
+
* @returns The current StringBuilder.
|
|
124
|
+
*/
|
|
125
|
+
prependLine(...value) {
|
|
126
|
+
return this.prepend(...value, '\n');
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Converts the value of this instance to a string.
|
|
130
|
+
* @returns A string whose value is the same as this instance.
|
|
131
|
+
*/
|
|
132
|
+
toString() {
|
|
133
|
+
return this.str.replace(/\r?\n/g, this._options.newLine);
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Removes all characters from the current StringBuilder.
|
|
137
|
+
*/
|
|
138
|
+
clear() {
|
|
139
|
+
this.str = '';
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
exports.StringBuilder = StringBuilder;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.concatAppendValues = exports.isAppendValueGroup = exports.isAppendValue = exports.appendValueGroup = exports.basicAppendValueGroup = void 0;
|
|
4
|
+
const common_utils_1 = require("../common.utils");
|
|
5
|
+
function basicAppendValueGroup(values, separator) {
|
|
6
|
+
var _a;
|
|
7
|
+
return {
|
|
8
|
+
__type: 'append-value-group',
|
|
9
|
+
values: (_a = values === null || values === void 0 ? void 0 : values.filter(common_utils_1.notNullish)) !== null && _a !== void 0 ? _a : [],
|
|
10
|
+
separator: separator !== null && separator !== void 0 ? separator : null,
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
exports.basicAppendValueGroup = basicAppendValueGroup;
|
|
14
|
+
function appendValueGroup(values, separator) {
|
|
15
|
+
var _a;
|
|
16
|
+
return {
|
|
17
|
+
__type: 'append-value-group',
|
|
18
|
+
values: (_a = values === null || values === void 0 ? void 0 : values.filter(common_utils_1.notNullish)) !== null && _a !== void 0 ? _a : [],
|
|
19
|
+
separator: separator !== null && separator !== void 0 ? separator : null,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
exports.appendValueGroup = appendValueGroup;
|
|
23
|
+
function isAppendValue(value) {
|
|
24
|
+
return (typeof value === 'string' ||
|
|
25
|
+
typeof value === 'number' ||
|
|
26
|
+
typeof value === 'boolean' ||
|
|
27
|
+
typeof value === 'function' ||
|
|
28
|
+
isAppendValueGroup(value));
|
|
29
|
+
}
|
|
30
|
+
exports.isAppendValue = isAppendValue;
|
|
31
|
+
function isAppendValueGroup(value) {
|
|
32
|
+
return typeof value === 'object' && value !== null && '__type' in value && value.__type === 'append-value-group';
|
|
33
|
+
}
|
|
34
|
+
exports.isAppendValueGroup = isAppendValueGroup;
|
|
35
|
+
function concatAppendValues(...values) {
|
|
36
|
+
return (0, common_utils_1.concatSingleOrMultiple)(...values);
|
|
37
|
+
}
|
|
38
|
+
exports.concatAppendValues = concatAppendValues;
|