tools.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. "use strict";
  2. // Generated by CoffeeScript 2.5.1
  3. var cloneDeep, htmlparser, isPlainObject, merge, _objectToDom, self;
  4. htmlparser = require('htmlparser2');
  5. var _require = require('dom-converter');
  6. _objectToDom = _require.objectToDom;
  7. merge = require('lodash/merge');
  8. cloneDeep = require('lodash/cloneDeep');
  9. isPlainObject = require('lodash/isPlainObject');
  10. module.exports = self = {
  11. repeatString: function repeatString(str, times) {
  12. var i, j, output, ref;
  13. output = '';
  14. for (i = j = 0, ref = times; 0 <= ref ? j < ref : j > ref; i = 0 <= ref ? ++j : --j) {
  15. output += str;
  16. }
  17. return output;
  18. },
  19. cloneAndMergeDeep: function cloneAndMergeDeep(base, toAppend) {
  20. return merge(cloneDeep(base), toAppend);
  21. },
  22. toDom: function toDom(subject) {
  23. if (typeof subject === 'string') {
  24. return self.stringToDom(subject);
  25. } else if (isPlainObject(subject)) {
  26. return self._objectToDom(subject);
  27. } else {
  28. throw Error("tools.toDom() only supports strings and objects");
  29. }
  30. },
  31. stringToDom: function stringToDom(string) {
  32. var handler, parser;
  33. handler = new htmlparser.DomHandler();
  34. parser = new htmlparser.Parser(handler);
  35. parser.write(string);
  36. parser.end();
  37. return handler.dom;
  38. },
  39. _fixQuotesInDom: function _fixQuotesInDom(input) {
  40. var j, len, node;
  41. if (Array.isArray(input)) {
  42. for (j = 0, len = input.length; j < len; j++) {
  43. node = input[j];
  44. self._fixQuotesInDom(node);
  45. }
  46. return input;
  47. }
  48. node = input;
  49. if (node.type === 'text') {
  50. return node.data = self._quoteNodeText(node.data);
  51. } else {
  52. return self._fixQuotesInDom(node.children);
  53. }
  54. },
  55. objectToDom: function objectToDom(o) {
  56. if (!Array.isArray(o)) {
  57. if (!isPlainObject(o)) {
  58. throw Error("objectToDom() only accepts a bare object or an array");
  59. }
  60. }
  61. return self._fixQuotesInDom(_objectToDom(o));
  62. },
  63. quote: function quote(str) {
  64. return String(str).replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\"/g, '&quot;').replace(/\ /g, '&sp;').replace(/\n/g, '<br />');
  65. },
  66. _quoteNodeText: function _quoteNodeText(text) {
  67. return String(text).replace(/\&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\"/g, '&quot;').replace(/\ /g, '&sp;').replace(/\n/g, "&nl;");
  68. },
  69. getCols: function getCols() {
  70. var cols, tty; // Based on https://github.com/jonschlinkert/window-size
  71. tty = require('tty');
  72. cols = function () {
  73. try {
  74. if (tty.isatty(1) && tty.isatty(2)) {
  75. if (process.stdout.getWindowSize) {
  76. return process.stdout.getWindowSize(1)[0];
  77. } else if (tty.getWindowSize) {
  78. return tty.getWindowSize()[1];
  79. } else if (process.stdout.columns) {
  80. return process.stdout.columns;
  81. }
  82. }
  83. } catch (error) {}
  84. }();
  85. if (typeof cols === 'number' && cols > 30) {
  86. return cols;
  87. } else {
  88. return 80;
  89. }
  90. }
  91. };