-
Notifications
You must be signed in to change notification settings - Fork 1
/
writer.js
98 lines (74 loc) · 2.5 KB
/
writer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
;(function (root, factory) {
if (typeof exports === 'object') {
// CommonJS
factory(require, exports, module);
} else if (typeof define === 'function' && define.amd) {
// AMD
define(['require', 'exports', 'module', 'cla55'], factory);
} else {
console && console.error('Unsupported module environment.'); // jshint ignore:line
}
}(this, function (require, exports, module) {
'use strict';
var Cla55 = require('cla55'),
Writer;
Writer = Cla55.extend({
constructor: function constructor(options) {
if (!options) {
options = {};
}
this.options = {
indent: options.indent || ' ',
lineBreak: options.lineBreak || '\n'
};
this._content = '';
this._indent = 0;
},
option: function options(key, val) {
if (arguments.length === 2) {
this.options[key] = val;
}
return this.options[key];
},
read: function () {
return this._content;
},
wrap: function (before, after) {
this._content = before + this._content + after;
},
write: function write(chunk) {
var lineBreak = this.option('lineBreak'),
i = 0;
// Generate indent only for new line
if (this._content.substr(this._content.length - lineBreak.length, lineBreak.length) === lineBreak) {
while (i < this._indent) {
this._content += this.option('indent');
i++;
}
}
this._content += chunk;
return this;
},
writeString: function writeString(chunk) {
return this.write(JSON.stringify(chunk.toString()));
},
lineBreak: function lineBreak() {
var lineBreakVal = this.option('lineBreak');
// Prevent double line breaks
if (this._content.substr(this._content.length - lineBreakVal.length, lineBreakVal.length) === lineBreakVal) {
return this;
}
this._content += this.option('lineBreak');
return this;
},
indentInc: function indentInc() {
this._indent++;
return this;
},
indentDec: function indentDec() {
this._indent--;
return this;
}
}, {});
module.exports = Writer;
}));