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