@prisma/ts-builders 6.6.0-dev.59
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/LICENSE +201 -0
- package/README.md +5 -0
- package/dist/AnyDeclarationBuilder.d.ts +6 -0
- package/dist/AnyDeclarationBuilder.js +16 -0
- package/dist/AnyDeclarationBuilder.mjs +0 -0
- package/dist/ArraySpread.d.ts +8 -0
- package/dist/ArraySpread.js +42 -0
- package/dist/ArraySpread.mjs +17 -0
- package/dist/ArrayType.d.ts +8 -0
- package/dist/ArrayType.js +43 -0
- package/dist/ArrayType.mjs +18 -0
- package/dist/ArrayType.test.d.ts +1 -0
- package/dist/ArrayType.test.js +24 -0
- package/dist/ArrayType.test.mjs +23 -0
- package/dist/BasicBuilder.d.ts +4 -0
- package/dist/BasicBuilder.js +16 -0
- package/dist/BasicBuilder.mjs +0 -0
- package/dist/Class.d.ts +22 -0
- package/dist/Class.js +79 -0
- package/dist/Class.mjs +54 -0
- package/dist/Class.test.d.ts +1 -0
- package/dist/Class.test.js +56 -0
- package/dist/Class.test.mjs +55 -0
- package/dist/ConstDeclaration.d.ts +13 -0
- package/dist/ConstDeclaration.js +49 -0
- package/dist/ConstDeclaration.mjs +24 -0
- package/dist/ConstDeclaration.test.d.ts +1 -0
- package/dist/ConstDeclaration.test.js +19 -0
- package/dist/ConstDeclaration.test.mjs +18 -0
- package/dist/DocComment.d.ts +11 -0
- package/dist/DocComment.js +101 -0
- package/dist/DocComment.mjs +76 -0
- package/dist/DocComment.test.d.ts +1 -0
- package/dist/DocComment.test.js +80 -0
- package/dist/DocComment.test.mjs +79 -0
- package/dist/Export.d.ts +12 -0
- package/dist/Export.js +48 -0
- package/dist/Export.mjs +23 -0
- package/dist/ExportFrom.d.ts +32 -0
- package/dist/ExportFrom.js +98 -0
- package/dist/ExportFrom.mjs +69 -0
- package/dist/ExportFrom.test.d.ts +1 -0
- package/dist/ExportFrom.test.js +25 -0
- package/dist/ExportFrom.test.mjs +24 -0
- package/dist/File.d.ts +15 -0
- package/dist/File.js +57 -0
- package/dist/File.mjs +32 -0
- package/dist/FunctionType.d.ts +17 -0
- package/dist/FunctionType.js +60 -0
- package/dist/FunctionType.mjs +35 -0
- package/dist/FunctionType.test.d.ts +1 -0
- package/dist/FunctionType.test.js +32 -0
- package/dist/FunctionType.test.mjs +31 -0
- package/dist/GenericParameter.d.ts +15 -0
- package/dist/GenericParameter.js +60 -0
- package/dist/GenericParameter.mjs +35 -0
- package/dist/GenericParameter.test.d.ts +1 -0
- package/dist/GenericParameter.test.js +19 -0
- package/dist/GenericParameter.test.mjs +18 -0
- package/dist/Import.d.ts +36 -0
- package/dist/Import.js +119 -0
- package/dist/Import.mjs +90 -0
- package/dist/Import.test.d.ts +1 -0
- package/dist/Import.test.js +33 -0
- package/dist/Import.test.mjs +32 -0
- package/dist/Interface.d.ts +22 -0
- package/dist/Interface.js +79 -0
- package/dist/Interface.mjs +54 -0
- package/dist/Interface.test.d.ts +1 -0
- package/dist/Interface.test.js +56 -0
- package/dist/Interface.test.mjs +55 -0
- package/dist/KeyType.d.ts +9 -0
- package/dist/KeyType.js +44 -0
- package/dist/KeyType.mjs +19 -0
- package/dist/KeyType.test.d.ts +1 -0
- package/dist/KeyType.test.js +28 -0
- package/dist/KeyType.test.mjs +27 -0
- package/dist/KeyofType.d.ts +8 -0
- package/dist/KeyofType.js +47 -0
- package/dist/KeyofType.mjs +22 -0
- package/dist/KeyofType.test.d.ts +1 -0
- package/dist/KeyofType.test.js +28 -0
- package/dist/KeyofType.test.mjs +27 -0
- package/dist/Method.d.ts +20 -0
- package/dist/Method.js +75 -0
- package/dist/Method.mjs +50 -0
- package/dist/Method.test.d.ts +1 -0
- package/dist/Method.test.js +43 -0
- package/dist/Method.test.mjs +42 -0
- package/dist/NamedType.d.ts +10 -0
- package/dist/NamedType.js +50 -0
- package/dist/NamedType.mjs +25 -0
- package/dist/NamedType.test.d.ts +1 -0
- package/dist/NamedType.test.js +15 -0
- package/dist/NamedType.test.mjs +14 -0
- package/dist/NamespaceDeclaration.d.ts +13 -0
- package/dist/NamespaceDeclaration.js +48 -0
- package/dist/NamespaceDeclaration.mjs +23 -0
- package/dist/ObjectType.d.ts +18 -0
- package/dist/ObjectType.js +71 -0
- package/dist/ObjectType.mjs +46 -0
- package/dist/ObjectType.test.d.ts +1 -0
- package/dist/ObjectType.test.js +32 -0
- package/dist/ObjectType.test.mjs +31 -0
- package/dist/Parameter.d.ts +12 -0
- package/dist/Parameter.js +50 -0
- package/dist/Parameter.mjs +25 -0
- package/dist/Parameter.test.d.ts +1 -0
- package/dist/Parameter.test.js +14 -0
- package/dist/Parameter.test.mjs +13 -0
- package/dist/PrimitiveType.d.ts +18 -0
- package/dist/PrimitiveType.js +70 -0
- package/dist/PrimitiveType.mjs +35 -0
- package/dist/Property.d.ts +18 -0
- package/dist/Property.js +75 -0
- package/dist/Property.mjs +50 -0
- package/dist/Property.test.d.ts +1 -0
- package/dist/Property.test.js +37 -0
- package/dist/Property.test.mjs +36 -0
- package/dist/StringLiteralType.d.ts +8 -0
- package/dist/StringLiteralType.js +42 -0
- package/dist/StringLiteralType.mjs +17 -0
- package/dist/TupleType.d.ts +17 -0
- package/dist/TupleType.js +69 -0
- package/dist/TupleType.mjs +42 -0
- package/dist/TupleType.test.d.ts +1 -0
- package/dist/TupleType.test.js +21 -0
- package/dist/TupleType.test.mjs +20 -0
- package/dist/TypeBuilder.d.ts +11 -0
- package/dist/TypeBuilder.js +47 -0
- package/dist/TypeBuilder.mjs +23 -0
- package/dist/TypeDeclaration.d.ts +17 -0
- package/dist/TypeDeclaration.js +62 -0
- package/dist/TypeDeclaration.mjs +37 -0
- package/dist/TypeDeclaration.test.d.ts +1 -0
- package/dist/TypeDeclaration.test.js +28 -0
- package/dist/TypeDeclaration.test.mjs +27 -0
- package/dist/UnionType.d.ts +13 -0
- package/dist/UnionType.js +74 -0
- package/dist/UnionType.mjs +49 -0
- package/dist/UnionType.test.d.ts +1 -0
- package/dist/UnionType.test.js +28 -0
- package/dist/UnionType.test.mjs +27 -0
- package/dist/WellKnownSymbol.d.ts +9 -0
- package/dist/WellKnownSymbol.js +43 -0
- package/dist/WellKnownSymbol.mjs +17 -0
- package/dist/Writer.d.ts +94 -0
- package/dist/Writer.js +175 -0
- package/dist/Writer.mjs +150 -0
- package/dist/Writer.test.d.ts +1 -0
- package/dist/Writer.test.js +100 -0
- package/dist/Writer.test.mjs +99 -0
- package/dist/helpers.d.ts +6 -0
- package/dist/helpers.js +46 -0
- package/dist/helpers.mjs +19 -0
- package/dist/index.d.ts +32 -0
- package/dist/index.js +84 -0
- package/dist/index.mjs +32 -0
- package/dist/stringify.d.ts +7 -0
- package/dist/stringify.js +44 -0
- package/dist/stringify.mjs +20 -0
- package/package.json +41 -0
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { TypeBuilder } from "./TypeBuilder";
|
|
2
|
+
class UnionType extends TypeBuilder {
|
|
3
|
+
needsParenthesisWhenIndexed = true;
|
|
4
|
+
needsParenthesisInKeyof = true;
|
|
5
|
+
variants;
|
|
6
|
+
constructor(firstType) {
|
|
7
|
+
super();
|
|
8
|
+
this.variants = [firstType];
|
|
9
|
+
}
|
|
10
|
+
addVariant(variant) {
|
|
11
|
+
this.variants.push(variant);
|
|
12
|
+
return this;
|
|
13
|
+
}
|
|
14
|
+
addVariants(variants) {
|
|
15
|
+
for (const variant of variants) {
|
|
16
|
+
this.addVariant(variant);
|
|
17
|
+
}
|
|
18
|
+
return this;
|
|
19
|
+
}
|
|
20
|
+
write(writer) {
|
|
21
|
+
writer.writeJoined(" | ", this.variants, (variant, writer2) => {
|
|
22
|
+
if (variant.needsParenthesisInUnion) {
|
|
23
|
+
writer2.write("(").write(variant).write(")");
|
|
24
|
+
} else {
|
|
25
|
+
writer2.write(variant);
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
mapVariants(callback) {
|
|
30
|
+
return unionType(this.variants.map((v) => callback(v)));
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
function unionType(types) {
|
|
34
|
+
if (Array.isArray(types)) {
|
|
35
|
+
if (types.length === 0) {
|
|
36
|
+
throw new TypeError("Union types array can not be empty");
|
|
37
|
+
}
|
|
38
|
+
const union = new UnionType(types[0]);
|
|
39
|
+
for (let i = 1; i < types.length; i++) {
|
|
40
|
+
union.addVariant(types[i]);
|
|
41
|
+
}
|
|
42
|
+
return union;
|
|
43
|
+
}
|
|
44
|
+
return new UnionType(types);
|
|
45
|
+
}
|
|
46
|
+
export {
|
|
47
|
+
UnionType,
|
|
48
|
+
unionType
|
|
49
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var import_vitest = require("vitest");
|
|
3
|
+
var import_ArrayType = require("./ArrayType");
|
|
4
|
+
var import_FunctionType = require("./FunctionType");
|
|
5
|
+
var import_NamedType = require("./NamedType");
|
|
6
|
+
var import_stringify = require("./stringify");
|
|
7
|
+
var import_UnionType = require("./UnionType");
|
|
8
|
+
const A = (0, import_NamedType.namedType)("A");
|
|
9
|
+
const B = (0, import_NamedType.namedType)("B");
|
|
10
|
+
(0, import_vitest.test)("one type", () => {
|
|
11
|
+
(0, import_vitest.expect)((0, import_stringify.stringify)((0, import_UnionType.unionType)(A))).toMatchInlineSnapshot(`"A"`);
|
|
12
|
+
});
|
|
13
|
+
(0, import_vitest.test)("multiple types", () => {
|
|
14
|
+
(0, import_vitest.expect)((0, import_stringify.stringify)((0, import_UnionType.unionType)(A).addVariant(B))).toMatchInlineSnapshot(`"A | B"`);
|
|
15
|
+
});
|
|
16
|
+
(0, import_vitest.test)("from array", () => {
|
|
17
|
+
(0, import_vitest.expect)((0, import_stringify.stringify)((0, import_UnionType.unionType)([A, B]))).toMatchInlineSnapshot(`"A | B"`);
|
|
18
|
+
});
|
|
19
|
+
(0, import_vitest.test)("with function type", () => {
|
|
20
|
+
(0, import_vitest.expect)((0, import_stringify.stringify)((0, import_UnionType.unionType)([A, B, (0, import_FunctionType.functionType)()]))).toMatchInlineSnapshot(`"A | B | (() => void)"`);
|
|
21
|
+
});
|
|
22
|
+
(0, import_vitest.test)("fails with empty array", () => {
|
|
23
|
+
(0, import_vitest.expect)(() => (0, import_UnionType.unionType)([])).toThrowErrorMatchingInlineSnapshot(`[TypeError: Union types array can not be empty]`);
|
|
24
|
+
});
|
|
25
|
+
(0, import_vitest.test)("mapVariants", () => {
|
|
26
|
+
const union = (0, import_UnionType.unionType)([A, B]).mapVariants(import_ArrayType.array);
|
|
27
|
+
(0, import_vitest.expect)((0, import_stringify.stringify)(union)).toMatchInlineSnapshot(`"A[] | B[]"`);
|
|
28
|
+
});
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { expect, test } from "vitest";
|
|
2
|
+
import { array } from "./ArrayType";
|
|
3
|
+
import { functionType } from "./FunctionType";
|
|
4
|
+
import { namedType } from "./NamedType";
|
|
5
|
+
import { stringify } from "./stringify";
|
|
6
|
+
import { unionType } from "./UnionType";
|
|
7
|
+
const A = namedType("A");
|
|
8
|
+
const B = namedType("B");
|
|
9
|
+
test("one type", () => {
|
|
10
|
+
expect(stringify(unionType(A))).toMatchInlineSnapshot(`"A"`);
|
|
11
|
+
});
|
|
12
|
+
test("multiple types", () => {
|
|
13
|
+
expect(stringify(unionType(A).addVariant(B))).toMatchInlineSnapshot(`"A | B"`);
|
|
14
|
+
});
|
|
15
|
+
test("from array", () => {
|
|
16
|
+
expect(stringify(unionType([A, B]))).toMatchInlineSnapshot(`"A | B"`);
|
|
17
|
+
});
|
|
18
|
+
test("with function type", () => {
|
|
19
|
+
expect(stringify(unionType([A, B, functionType()]))).toMatchInlineSnapshot(`"A | B | (() => void)"`);
|
|
20
|
+
});
|
|
21
|
+
test("fails with empty array", () => {
|
|
22
|
+
expect(() => unionType([])).toThrowErrorMatchingInlineSnapshot(`[TypeError: Union types array can not be empty]`);
|
|
23
|
+
});
|
|
24
|
+
test("mapVariants", () => {
|
|
25
|
+
const union = unionType([A, B]).mapVariants(array);
|
|
26
|
+
expect(stringify(union)).toMatchInlineSnapshot(`"A[] | B[]"`);
|
|
27
|
+
});
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { BasicBuilder } from './BasicBuilder';
|
|
2
|
+
import { Writer } from './Writer';
|
|
3
|
+
export declare class WellKnownSymbol implements BasicBuilder {
|
|
4
|
+
readonly name: string;
|
|
5
|
+
constructor(name: string);
|
|
6
|
+
write(writer: Writer<undefined>): void;
|
|
7
|
+
}
|
|
8
|
+
export declare function wellKnownSymbol(name: string): WellKnownSymbol;
|
|
9
|
+
export declare const toStringTag: WellKnownSymbol;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var WellKnownSymbol_exports = {};
|
|
20
|
+
__export(WellKnownSymbol_exports, {
|
|
21
|
+
WellKnownSymbol: () => WellKnownSymbol,
|
|
22
|
+
toStringTag: () => toStringTag,
|
|
23
|
+
wellKnownSymbol: () => wellKnownSymbol
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(WellKnownSymbol_exports);
|
|
26
|
+
class WellKnownSymbol {
|
|
27
|
+
constructor(name) {
|
|
28
|
+
this.name = name;
|
|
29
|
+
}
|
|
30
|
+
write(writer) {
|
|
31
|
+
writer.write("Symbol.").write(this.name);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function wellKnownSymbol(name) {
|
|
35
|
+
return new WellKnownSymbol(name);
|
|
36
|
+
}
|
|
37
|
+
const toStringTag = wellKnownSymbol("toStringTag");
|
|
38
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
39
|
+
0 && (module.exports = {
|
|
40
|
+
WellKnownSymbol,
|
|
41
|
+
toStringTag,
|
|
42
|
+
wellKnownSymbol
|
|
43
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
class WellKnownSymbol {
|
|
2
|
+
constructor(name) {
|
|
3
|
+
this.name = name;
|
|
4
|
+
}
|
|
5
|
+
write(writer) {
|
|
6
|
+
writer.write("Symbol.").write(this.name);
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
function wellKnownSymbol(name) {
|
|
10
|
+
return new WellKnownSymbol(name);
|
|
11
|
+
}
|
|
12
|
+
const toStringTag = wellKnownSymbol("toStringTag");
|
|
13
|
+
export {
|
|
14
|
+
WellKnownSymbol,
|
|
15
|
+
toStringTag,
|
|
16
|
+
wellKnownSymbol
|
|
17
|
+
};
|
package/dist/Writer.d.ts
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import type { BasicBuilder } from './BasicBuilder';
|
|
2
|
+
export declare const INDENT_SIZE = 2;
|
|
3
|
+
/**
|
|
4
|
+
* Helper class for building long multi-line formatted strings from building blocks.
|
|
5
|
+
* Can use either plain strings or `Builder` objects, that would encapsulate formatting logic.
|
|
6
|
+
*/
|
|
7
|
+
export declare class Writer<ContextType = undefined> {
|
|
8
|
+
readonly context: ContextType;
|
|
9
|
+
private lines;
|
|
10
|
+
private currentLine;
|
|
11
|
+
private currentIndent;
|
|
12
|
+
private marginSymbol?;
|
|
13
|
+
private afterNextNewLineCallback?;
|
|
14
|
+
constructor(startingIndent: number | undefined, context: ContextType);
|
|
15
|
+
/**
|
|
16
|
+
* Adds provided value to the current line. Does not end the line.
|
|
17
|
+
*
|
|
18
|
+
* @param value
|
|
19
|
+
* @returns
|
|
20
|
+
*/
|
|
21
|
+
write(value: string | BasicBuilder<ContextType>): this;
|
|
22
|
+
/**
|
|
23
|
+
* Adds several `values` to the current line, separated by `separator`. Both values and separator
|
|
24
|
+
* can also be `Builder` instances for more advanced formatting.
|
|
25
|
+
*
|
|
26
|
+
* @param separator
|
|
27
|
+
* @param values
|
|
28
|
+
* @param writeItem allow to customize how individual item is written
|
|
29
|
+
* @returns
|
|
30
|
+
*/
|
|
31
|
+
writeJoined<T extends string | BasicBuilder<ContextType>>(separator: string | BasicBuilder<ContextType>, values: T[], writeItem?: (item: T, writer: this) => void): this;
|
|
32
|
+
/**
|
|
33
|
+
* Adds a string to current line, flushes current line and starts a new line.
|
|
34
|
+
* @param line
|
|
35
|
+
* @returns
|
|
36
|
+
*/
|
|
37
|
+
writeLine(line: string | BasicBuilder<ContextType>): this;
|
|
38
|
+
/**
|
|
39
|
+
* Flushes current line and starts a new line. New line starts at previously configured indentation level
|
|
40
|
+
* @returns
|
|
41
|
+
*/
|
|
42
|
+
newLine(): this;
|
|
43
|
+
/**
|
|
44
|
+
* Increases indentation level by 1, calls provided callback and then decreases indentation again.
|
|
45
|
+
* Could be used for writing indented blocks of text:
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```ts
|
|
49
|
+
* writer
|
|
50
|
+
* .writeLine('{')
|
|
51
|
+
* .withIndent(() => {
|
|
52
|
+
* writer.writeLine('foo: 123');
|
|
53
|
+
* writer.writeLine('bar: 456');
|
|
54
|
+
* })
|
|
55
|
+
* .writeLine('}')
|
|
56
|
+
* ```
|
|
57
|
+
* @param callback
|
|
58
|
+
* @returns
|
|
59
|
+
*/
|
|
60
|
+
withIndent(callback: (writer: this) => void): this;
|
|
61
|
+
/**
|
|
62
|
+
* Calls provided callback next time when new line is started.
|
|
63
|
+
* Callback is called after old line have already been flushed and a new
|
|
64
|
+
* line have been started. Can be used for adding "between the lines" decorations,
|
|
65
|
+
* such as underlines.
|
|
66
|
+
*
|
|
67
|
+
* @param callback
|
|
68
|
+
* @returns
|
|
69
|
+
*/
|
|
70
|
+
afterNextNewline(callback: () => void): this;
|
|
71
|
+
/**
|
|
72
|
+
* Increases indentation level of the current line by 1
|
|
73
|
+
* @returns
|
|
74
|
+
*/
|
|
75
|
+
indent(): this;
|
|
76
|
+
/**
|
|
77
|
+
* Decreases indentation level of the current line by 1, if it is possible
|
|
78
|
+
* @returns
|
|
79
|
+
*/
|
|
80
|
+
unindent(): this;
|
|
81
|
+
/**
|
|
82
|
+
* Adds a symbol, that will replace the first character of the current line (including indentation)
|
|
83
|
+
* when it is flushed. Can be used for adding markers to the line.
|
|
84
|
+
*
|
|
85
|
+
* Note: if indentation level of the line is 0, it will replace the first actually printed character
|
|
86
|
+
* of the line. Use with caution.
|
|
87
|
+
* @param symbol
|
|
88
|
+
* @returns
|
|
89
|
+
*/
|
|
90
|
+
addMarginSymbol(symbol: string): this;
|
|
91
|
+
toString(): string;
|
|
92
|
+
getCurrentLineLength(): number;
|
|
93
|
+
private indentedCurrentLine;
|
|
94
|
+
}
|
package/dist/Writer.js
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var Writer_exports = {};
|
|
20
|
+
__export(Writer_exports, {
|
|
21
|
+
INDENT_SIZE: () => INDENT_SIZE,
|
|
22
|
+
Writer: () => Writer
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(Writer_exports);
|
|
25
|
+
const INDENT_SIZE = 2;
|
|
26
|
+
class Writer {
|
|
27
|
+
constructor(startingIndent = 0, context) {
|
|
28
|
+
this.context = context;
|
|
29
|
+
this.currentIndent = startingIndent;
|
|
30
|
+
}
|
|
31
|
+
lines = [];
|
|
32
|
+
currentLine = "";
|
|
33
|
+
currentIndent = 0;
|
|
34
|
+
marginSymbol;
|
|
35
|
+
afterNextNewLineCallback;
|
|
36
|
+
/**
|
|
37
|
+
* Adds provided value to the current line. Does not end the line.
|
|
38
|
+
*
|
|
39
|
+
* @param value
|
|
40
|
+
* @returns
|
|
41
|
+
*/
|
|
42
|
+
write(value) {
|
|
43
|
+
if (typeof value === "string") {
|
|
44
|
+
this.currentLine += value;
|
|
45
|
+
} else {
|
|
46
|
+
value.write(this);
|
|
47
|
+
}
|
|
48
|
+
return this;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Adds several `values` to the current line, separated by `separator`. Both values and separator
|
|
52
|
+
* can also be `Builder` instances for more advanced formatting.
|
|
53
|
+
*
|
|
54
|
+
* @param separator
|
|
55
|
+
* @param values
|
|
56
|
+
* @param writeItem allow to customize how individual item is written
|
|
57
|
+
* @returns
|
|
58
|
+
*/
|
|
59
|
+
writeJoined(separator, values, writeItem = (item, w) => w.write(item)) {
|
|
60
|
+
const last = values.length - 1;
|
|
61
|
+
for (let i = 0; i < values.length; i++) {
|
|
62
|
+
writeItem(values[i], this);
|
|
63
|
+
if (i !== last) {
|
|
64
|
+
this.write(separator);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return this;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Adds a string to current line, flushes current line and starts a new line.
|
|
71
|
+
* @param line
|
|
72
|
+
* @returns
|
|
73
|
+
*/
|
|
74
|
+
writeLine(line) {
|
|
75
|
+
return this.write(line).newLine();
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Flushes current line and starts a new line. New line starts at previously configured indentation level
|
|
79
|
+
* @returns
|
|
80
|
+
*/
|
|
81
|
+
newLine() {
|
|
82
|
+
this.lines.push(this.indentedCurrentLine());
|
|
83
|
+
this.currentLine = "";
|
|
84
|
+
this.marginSymbol = void 0;
|
|
85
|
+
const afterNextNewLineCallback = this.afterNextNewLineCallback;
|
|
86
|
+
this.afterNextNewLineCallback = void 0;
|
|
87
|
+
afterNextNewLineCallback?.();
|
|
88
|
+
return this;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Increases indentation level by 1, calls provided callback and then decreases indentation again.
|
|
92
|
+
* Could be used for writing indented blocks of text:
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* ```ts
|
|
96
|
+
* writer
|
|
97
|
+
* .writeLine('{')
|
|
98
|
+
* .withIndent(() => {
|
|
99
|
+
* writer.writeLine('foo: 123');
|
|
100
|
+
* writer.writeLine('bar: 456');
|
|
101
|
+
* })
|
|
102
|
+
* .writeLine('}')
|
|
103
|
+
* ```
|
|
104
|
+
* @param callback
|
|
105
|
+
* @returns
|
|
106
|
+
*/
|
|
107
|
+
withIndent(callback) {
|
|
108
|
+
this.indent();
|
|
109
|
+
callback(this);
|
|
110
|
+
this.unindent();
|
|
111
|
+
return this;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Calls provided callback next time when new line is started.
|
|
115
|
+
* Callback is called after old line have already been flushed and a new
|
|
116
|
+
* line have been started. Can be used for adding "between the lines" decorations,
|
|
117
|
+
* such as underlines.
|
|
118
|
+
*
|
|
119
|
+
* @param callback
|
|
120
|
+
* @returns
|
|
121
|
+
*/
|
|
122
|
+
afterNextNewline(callback) {
|
|
123
|
+
this.afterNextNewLineCallback = callback;
|
|
124
|
+
return this;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Increases indentation level of the current line by 1
|
|
128
|
+
* @returns
|
|
129
|
+
*/
|
|
130
|
+
indent() {
|
|
131
|
+
this.currentIndent++;
|
|
132
|
+
return this;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Decreases indentation level of the current line by 1, if it is possible
|
|
136
|
+
* @returns
|
|
137
|
+
*/
|
|
138
|
+
unindent() {
|
|
139
|
+
if (this.currentIndent > 0) {
|
|
140
|
+
this.currentIndent--;
|
|
141
|
+
}
|
|
142
|
+
return this;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Adds a symbol, that will replace the first character of the current line (including indentation)
|
|
146
|
+
* when it is flushed. Can be used for adding markers to the line.
|
|
147
|
+
*
|
|
148
|
+
* Note: if indentation level of the line is 0, it will replace the first actually printed character
|
|
149
|
+
* of the line. Use with caution.
|
|
150
|
+
* @param symbol
|
|
151
|
+
* @returns
|
|
152
|
+
*/
|
|
153
|
+
addMarginSymbol(symbol) {
|
|
154
|
+
this.marginSymbol = symbol;
|
|
155
|
+
return this;
|
|
156
|
+
}
|
|
157
|
+
toString() {
|
|
158
|
+
return this.lines.concat(this.indentedCurrentLine()).join("\n");
|
|
159
|
+
}
|
|
160
|
+
getCurrentLineLength() {
|
|
161
|
+
return this.currentLine.length;
|
|
162
|
+
}
|
|
163
|
+
indentedCurrentLine() {
|
|
164
|
+
const line = this.currentLine.padStart(this.currentLine.length + INDENT_SIZE * this.currentIndent);
|
|
165
|
+
if (this.marginSymbol) {
|
|
166
|
+
return this.marginSymbol + line.slice(1);
|
|
167
|
+
}
|
|
168
|
+
return line;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
172
|
+
0 && (module.exports = {
|
|
173
|
+
INDENT_SIZE,
|
|
174
|
+
Writer
|
|
175
|
+
});
|
package/dist/Writer.mjs
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
const INDENT_SIZE = 2;
|
|
2
|
+
class Writer {
|
|
3
|
+
constructor(startingIndent = 0, context) {
|
|
4
|
+
this.context = context;
|
|
5
|
+
this.currentIndent = startingIndent;
|
|
6
|
+
}
|
|
7
|
+
lines = [];
|
|
8
|
+
currentLine = "";
|
|
9
|
+
currentIndent = 0;
|
|
10
|
+
marginSymbol;
|
|
11
|
+
afterNextNewLineCallback;
|
|
12
|
+
/**
|
|
13
|
+
* Adds provided value to the current line. Does not end the line.
|
|
14
|
+
*
|
|
15
|
+
* @param value
|
|
16
|
+
* @returns
|
|
17
|
+
*/
|
|
18
|
+
write(value) {
|
|
19
|
+
if (typeof value === "string") {
|
|
20
|
+
this.currentLine += value;
|
|
21
|
+
} else {
|
|
22
|
+
value.write(this);
|
|
23
|
+
}
|
|
24
|
+
return this;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Adds several `values` to the current line, separated by `separator`. Both values and separator
|
|
28
|
+
* can also be `Builder` instances for more advanced formatting.
|
|
29
|
+
*
|
|
30
|
+
* @param separator
|
|
31
|
+
* @param values
|
|
32
|
+
* @param writeItem allow to customize how individual item is written
|
|
33
|
+
* @returns
|
|
34
|
+
*/
|
|
35
|
+
writeJoined(separator, values, writeItem = (item, w) => w.write(item)) {
|
|
36
|
+
const last = values.length - 1;
|
|
37
|
+
for (let i = 0; i < values.length; i++) {
|
|
38
|
+
writeItem(values[i], this);
|
|
39
|
+
if (i !== last) {
|
|
40
|
+
this.write(separator);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return this;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Adds a string to current line, flushes current line and starts a new line.
|
|
47
|
+
* @param line
|
|
48
|
+
* @returns
|
|
49
|
+
*/
|
|
50
|
+
writeLine(line) {
|
|
51
|
+
return this.write(line).newLine();
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Flushes current line and starts a new line. New line starts at previously configured indentation level
|
|
55
|
+
* @returns
|
|
56
|
+
*/
|
|
57
|
+
newLine() {
|
|
58
|
+
this.lines.push(this.indentedCurrentLine());
|
|
59
|
+
this.currentLine = "";
|
|
60
|
+
this.marginSymbol = void 0;
|
|
61
|
+
const afterNextNewLineCallback = this.afterNextNewLineCallback;
|
|
62
|
+
this.afterNextNewLineCallback = void 0;
|
|
63
|
+
afterNextNewLineCallback?.();
|
|
64
|
+
return this;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Increases indentation level by 1, calls provided callback and then decreases indentation again.
|
|
68
|
+
* Could be used for writing indented blocks of text:
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```ts
|
|
72
|
+
* writer
|
|
73
|
+
* .writeLine('{')
|
|
74
|
+
* .withIndent(() => {
|
|
75
|
+
* writer.writeLine('foo: 123');
|
|
76
|
+
* writer.writeLine('bar: 456');
|
|
77
|
+
* })
|
|
78
|
+
* .writeLine('}')
|
|
79
|
+
* ```
|
|
80
|
+
* @param callback
|
|
81
|
+
* @returns
|
|
82
|
+
*/
|
|
83
|
+
withIndent(callback) {
|
|
84
|
+
this.indent();
|
|
85
|
+
callback(this);
|
|
86
|
+
this.unindent();
|
|
87
|
+
return this;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Calls provided callback next time when new line is started.
|
|
91
|
+
* Callback is called after old line have already been flushed and a new
|
|
92
|
+
* line have been started. Can be used for adding "between the lines" decorations,
|
|
93
|
+
* such as underlines.
|
|
94
|
+
*
|
|
95
|
+
* @param callback
|
|
96
|
+
* @returns
|
|
97
|
+
*/
|
|
98
|
+
afterNextNewline(callback) {
|
|
99
|
+
this.afterNextNewLineCallback = callback;
|
|
100
|
+
return this;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Increases indentation level of the current line by 1
|
|
104
|
+
* @returns
|
|
105
|
+
*/
|
|
106
|
+
indent() {
|
|
107
|
+
this.currentIndent++;
|
|
108
|
+
return this;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Decreases indentation level of the current line by 1, if it is possible
|
|
112
|
+
* @returns
|
|
113
|
+
*/
|
|
114
|
+
unindent() {
|
|
115
|
+
if (this.currentIndent > 0) {
|
|
116
|
+
this.currentIndent--;
|
|
117
|
+
}
|
|
118
|
+
return this;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Adds a symbol, that will replace the first character of the current line (including indentation)
|
|
122
|
+
* when it is flushed. Can be used for adding markers to the line.
|
|
123
|
+
*
|
|
124
|
+
* Note: if indentation level of the line is 0, it will replace the first actually printed character
|
|
125
|
+
* of the line. Use with caution.
|
|
126
|
+
* @param symbol
|
|
127
|
+
* @returns
|
|
128
|
+
*/
|
|
129
|
+
addMarginSymbol(symbol) {
|
|
130
|
+
this.marginSymbol = symbol;
|
|
131
|
+
return this;
|
|
132
|
+
}
|
|
133
|
+
toString() {
|
|
134
|
+
return this.lines.concat(this.indentedCurrentLine()).join("\n");
|
|
135
|
+
}
|
|
136
|
+
getCurrentLineLength() {
|
|
137
|
+
return this.currentLine.length;
|
|
138
|
+
}
|
|
139
|
+
indentedCurrentLine() {
|
|
140
|
+
const line = this.currentLine.padStart(this.currentLine.length + INDENT_SIZE * this.currentIndent);
|
|
141
|
+
if (this.marginSymbol) {
|
|
142
|
+
return this.marginSymbol + line.slice(1);
|
|
143
|
+
}
|
|
144
|
+
return line;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
export {
|
|
148
|
+
INDENT_SIZE,
|
|
149
|
+
Writer
|
|
150
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|