string-writer.js 674 B

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