UNPKG

671 BJavaScriptView Raw
1'use strict';
2
3function spacerStr (len) {
4 var str = '';
5 for(var i = 0; i < len; i += 1) {
6 str += ' ';
7 }
8 return str;
9}
10
11function StringWriter (config) {
12 this.lines = [];
13 this.lineSeparator = config.lineSeparator;
14 this.regex = new RegExp(this.lineSeparator, 'g');
15 this.spacer = spacerStr(config.outputOffset);
16}
17
18StringWriter.prototype.write = function (str) {
19 this.lines.push(this.spacer + str.replace(this.regex, this.lineSeparator + this.spacer));
20};
21
22StringWriter.prototype.flush = function () {
23 var str = this.lines.join(this.lineSeparator);
24 this.lines.length = 0;
25 return str;
26};
27
28module.exports = StringWriter;