1
0
Fork 0
management/front/dkha-web-sz-main/node_modules/renderkid/lib/tools.js

106 lines
2.9 KiB
JavaScript
Raw Normal View History

2024-01-16 21:26:16 +08:00
"use strict";
2023-12-18 13:12:25 +08:00
2024-01-16 21:26:16 +08:00
// Generated by CoffeeScript 2.5.1
var cloneDeep, htmlparser, isPlainObject, merge, _objectToDom, self;
2023-12-18 13:12:25 +08:00
2024-01-16 21:26:16 +08:00
htmlparser = require('htmlparser2');
2023-12-18 13:12:25 +08:00
2024-01-16 21:26:16 +08:00
var _require = require('dom-converter');
2023-12-18 13:12:25 +08:00
2024-01-16 21:26:16 +08:00
_objectToDom = _require.objectToDom;
merge = require('lodash/merge');
cloneDeep = require('lodash/cloneDeep');
isPlainObject = require('lodash/isPlainObject');
2023-12-18 13:12:25 +08:00
module.exports = self = {
2024-01-16 21:26:16 +08:00
repeatString: function repeatString(str, times) {
2023-12-18 13:12:25 +08:00
var i, j, output, ref;
output = '';
2024-01-16 21:26:16 +08:00
2023-12-18 13:12:25 +08:00
for (i = j = 0, ref = times; 0 <= ref ? j < ref : j > ref; i = 0 <= ref ? ++j : --j) {
output += str;
}
2024-01-16 21:26:16 +08:00
2023-12-18 13:12:25 +08:00
return output;
},
2024-01-16 21:26:16 +08:00
cloneAndMergeDeep: function cloneAndMergeDeep(base, toAppend) {
return merge(cloneDeep(base), toAppend);
},
toDom: function toDom(subject) {
2023-12-18 13:12:25 +08:00
if (typeof subject === 'string') {
return self.stringToDom(subject);
2024-01-16 21:26:16 +08:00
} else if (isPlainObject(subject)) {
2023-12-18 13:12:25 +08:00
return self._objectToDom(subject);
} else {
throw Error("tools.toDom() only supports strings and objects");
}
},
2024-01-16 21:26:16 +08:00
stringToDom: function stringToDom(string) {
2023-12-18 13:12:25 +08:00
var handler, parser;
2024-01-16 21:26:16 +08:00
handler = new htmlparser.DomHandler();
2023-12-18 13:12:25 +08:00
parser = new htmlparser.Parser(handler);
parser.write(string);
parser.end();
return handler.dom;
},
2024-01-16 21:26:16 +08:00
_fixQuotesInDom: function _fixQuotesInDom(input) {
2023-12-18 13:12:25 +08:00
var j, len, node;
2024-01-16 21:26:16 +08:00
2023-12-18 13:12:25 +08:00
if (Array.isArray(input)) {
for (j = 0, len = input.length; j < len; j++) {
node = input[j];
2024-01-16 21:26:16 +08:00
2023-12-18 13:12:25 +08:00
self._fixQuotesInDom(node);
}
2024-01-16 21:26:16 +08:00
2023-12-18 13:12:25 +08:00
return input;
}
2024-01-16 21:26:16 +08:00
2023-12-18 13:12:25 +08:00
node = input;
2024-01-16 21:26:16 +08:00
2023-12-18 13:12:25 +08:00
if (node.type === 'text') {
return node.data = self._quoteNodeText(node.data);
} else {
return self._fixQuotesInDom(node.children);
}
},
2024-01-16 21:26:16 +08:00
objectToDom: function objectToDom(o) {
2023-12-18 13:12:25 +08:00
if (!Array.isArray(o)) {
2024-01-16 21:26:16 +08:00
if (!isPlainObject(o)) {
2023-12-18 13:12:25 +08:00
throw Error("objectToDom() only accepts a bare object or an array");
}
}
2024-01-16 21:26:16 +08:00
return self._fixQuotesInDom(_objectToDom(o));
2023-12-18 13:12:25 +08:00
},
2024-01-16 21:26:16 +08:00
quote: function quote(str) {
2023-12-18 13:12:25 +08:00
return String(str).replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\"/g, '&quot;').replace(/\ /g, '&sp;').replace(/\n/g, '<br />');
},
2024-01-16 21:26:16 +08:00
_quoteNodeText: function _quoteNodeText(text) {
2023-12-18 13:12:25 +08:00
return String(text).replace(/\&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\"/g, '&quot;').replace(/\ /g, '&sp;').replace(/\n/g, "&nl;");
},
2024-01-16 21:26:16 +08:00
getCols: function getCols() {
var cols, tty; // Based on https://github.com/jonschlinkert/window-size
2023-12-18 13:12:25 +08:00
tty = require('tty');
2024-01-16 21:26:16 +08:00
cols = function () {
2023-12-18 13:12:25 +08:00
try {
if (tty.isatty(1) && tty.isatty(2)) {
if (process.stdout.getWindowSize) {
return process.stdout.getWindowSize(1)[0];
} else if (tty.getWindowSize) {
return tty.getWindowSize()[1];
} else if (process.stdout.columns) {
return process.stdout.columns;
}
}
2024-01-16 21:26:16 +08:00
} catch (error) {}
}();
2023-12-18 13:12:25 +08:00
if (typeof cols === 'number' && cols > 30) {
return cols;
} else {
return 80;
}
}
2024-01-16 21:26:16 +08:00
};