UNPKG

4.36 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var tslib_1 = require("tslib");
4var typeNameConvertor_1 = require("./typeNameConvertor");
5var defaultOptions = {
6 indentChar: ' ',
7 indentSize: 4,
8 normalizeTypeName: typeNameConvertor_1.normalizeTypeName,
9};
10var WriteProcessor = (function () {
11 function WriteProcessor(options) {
12 this.indent = 0;
13 this.results = '';
14 this.alreadyIndentThisLine = false;
15 this.options = tslib_1.__assign({}, defaultOptions, options);
16 }
17 WriteProcessor.prototype.clear = function () {
18 this.indent = 0;
19 this.results = '';
20 this.alreadyIndentThisLine = false;
21 };
22 WriteProcessor.prototype.output = function (str) {
23 this.doIndent();
24 this.results += str;
25 return this;
26 };
27 WriteProcessor.prototype.outputType = function (type, primitive) {
28 if (primitive === void 0) { primitive = false; }
29 type = this.options.normalizeTypeName(type, primitive);
30 this.output(type);
31 return this;
32 };
33 WriteProcessor.prototype.outputKey = function (name, optional) {
34 if (optional === void 0) { optional = false; }
35 if (/[^0-9A-Za-z_$]/.test(name) || /^\d/.test(name)) {
36 this.output("\"" + name + "\"");
37 }
38 else {
39 this.output(name);
40 }
41 if (optional) {
42 this.output('?');
43 }
44 return this;
45 };
46 WriteProcessor.prototype.outputLine = function (str) {
47 this.doIndent();
48 if (str) {
49 this.output(str);
50 }
51 this.output('\n');
52 this.alreadyIndentThisLine = false;
53 return this;
54 };
55 WriteProcessor.prototype.protectComment = function (str) {
56 return str.replace(/\*\//g, '*\u200B/');
57 };
58 WriteProcessor.prototype.outputJSDoc = function () {
59 var _this = this;
60 var comments = [];
61 for (var _i = 0; _i < arguments.length; _i++) {
62 comments[_i] = arguments[_i];
63 }
64 var e_1, _a;
65 var lines = [];
66 comments
67 .filter(function (comment) { return comment != null; })
68 .map(function (comment) { return comment.toString().split('\n').map(function (line) { return _this.protectComment(line); }); })
69 .forEach(function (ls) {
70 lines = lines.concat(ls);
71 });
72 if (lines.length > 0) {
73 this.outputLine('/**');
74 try {
75 for (var lines_1 = tslib_1.__values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
76 var line = lines_1_1.value;
77 this.output(' * ').outputLine(line);
78 }
79 }
80 catch (e_1_1) { e_1 = { error: e_1_1 }; }
81 finally {
82 try {
83 if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
84 }
85 finally { if (e_1) throw e_1.error; }
86 }
87 this.outputLine(' */');
88 }
89 return this;
90 };
91 WriteProcessor.prototype.doIndent = function () {
92 if (!this.alreadyIndentThisLine) {
93 var indent = this.getIndent();
94 this.results += indent;
95 this.alreadyIndentThisLine = true;
96 }
97 return this;
98 };
99 Object.defineProperty(WriteProcessor.prototype, "indentLevel", {
100 get: function () {
101 return this.indent;
102 },
103 enumerable: true,
104 configurable: true
105 });
106 WriteProcessor.prototype.increaseIndent = function () {
107 this.indent++;
108 return this;
109 };
110 WriteProcessor.prototype.decreaseIndent = function () {
111 this.indent--;
112 return this;
113 };
114 WriteProcessor.prototype.getIndent = function () {
115 return this.repeatString(this.indent * this.options.indentSize, this.options.indentChar);
116 };
117 WriteProcessor.prototype.repeatString = function (n, s) {
118 var result = '';
119 for (var i = 0; i < n; i++) {
120 result += s;
121 }
122 return result;
123 };
124 WriteProcessor.prototype.toDefinition = function () {
125 return this.results;
126 };
127 return WriteProcessor;
128}());
129exports.default = WriteProcessor;
130//# sourceMappingURL=writeProcessor.js.map
\No newline at end of file