let textManipulation = [
{
icon: 'https://img.icons8.com/color/1x/rules.png',
title: '字数统计',
description: (text, t_arr) => {
var cn = text.match(/[\u4e00-\u9fa5]/g)
var wd = text.match(/[a-zA-Z]+/g)
var le = text.match(/[a-zA-Z]/g)
var sg = text.match(/[~!@#$%^&*()_+<>?:,./;’,。、‘:“《》?!@#¥%…()]/g)
var nm = text.match(/\d/g)
var br = Array.from(new Set(t_arr))
// ${text.replace(/\s+/g, "").length}个字符;需去除字符串中的空格
return 共${br && t_arr.length || 1}行;
去重后${t_arr && br.length || 1}行;
${text.replace(/\s+/g, "").length}个字符;
${cn && cn.length || 0}个中文字符;
${le && le.length || 0}个英文字母;
${wd && wd.length || 0}个英文单词;
${sg && sg.length || 0}个符号;
${nm && nm.length || 0}个数字
}
},
{
title: 'SQL语句',
description: (text, t_arr) => {
// 数组去重
t_arr = Array.from(new Set(t_arr))
// 添加单引号
var str = "SELECT * FROM xxx WHERE id IN (";
t_arr.map(i =>
str += "'" + i + "', "
)
str = str.replace(/, $/, '');
str += ");";
return str;
}
},
{
title: '加逗号',
description: (text, t_arr) => {
// 数组去重
t_arr = Array.from(new Set(t_arr))
text = t_arr.join(", ")
return text;
}
},
{
title: '加单引号',
description: (text, t_arr) => {
// 数组去重
t_arr = Array.from(new Set(t_arr))
// 添加单引号
var str = "";
t_arr.map(i =>
str += "'" + i + "', "
)
str = str.replace(/, $/, '');
return str;
}
},
{
title: '加双引号',
description: (text, t_arr) => {
// 数组去重
t_arr = Array.from(new Set(t_arr))
// 添加双引号
var str = "";
t_arr.map(i =>
str += '"' + i + '", '
)
str = str.replace(/, $/, '');
return str;
}
},
{
title: '全部大写',
description: (text, t_arr) => text.toUpperCase()
},
{
title: '全部小写',
description: (text, t_arr) => text.toLowerCase()
},
{
title: '计算 MD5',
description: (text, t_arr) => require('crypto').createHash('md5').update(text).digest('hex')
},
{
title: '十六进制编码',
description: (text, t_arr) => '0x' + new Buffer(text).toString('hex')
},
{
title: 'base64 编码',
description: (text, t_arr) => new Buffer(text).toString('base64')
},
{
title: 'url 编码',
description: (text, t_arr) => encodeURI(text)
},
{
title: '文本逆转',
description: (text, t_arr) => text.split("").reverse().join("")
},
{
title: '\和/互转',
description: (text, t_arr) => text.includes("\") ? text.replace(/\/g, "/") : text.replace(/\//g, "\")
},
{
title: '词频统计',
description: (text, t_arr) => {
var o = {},
fq = [],
l = text.length,
t,
Inx = 0;
while (l--) {
t = text.substr(l, 1);
t in o ? ++fq[o[t]][1] : fq[o[t] = Inx++] = [t, 1];
}
fq = fq.sort(function(a, b) {
return b[1] - a[1];
});
return fq.map(f => f.join(": ")).join(",\n")
}
},
];
// 获取输入
let text = quickcommand.enterData.payload
// 去除字符串首尾空格
text = text.trim();
// 将字符串切割成数组, 并去除数组中为空的元素
var t_arr = get_t_arr(text);
// 判断字符串中是否包含字母, 动态显示隐藏【转大小写】
hasLetter(text, textManipulation);
// 执行文本处理
let options = textManipulation.map(t => {
return {
title: t.title,
description: t.description(text, t_arr)
}
})
// 渲染列表页面
quickcommand.showSelectList(options, { optionType: 'json' }).then(choise => {
console.log(choise.description)
utools.copyText(choise.description)
})
// 字符串转数组
function get_t_arr(text) {
let t_arr = []
// 如果只是数字(不含字母 && 不含中文)
if (!(/[\u4e00-\u9fa5]/g.test(text))) {
if (hasLetter(text) == 0) { // 0.不含字母 大于0.包含字母
if (text.indexOf(" ") > 0 text.indexOf(" ") > 0) { // 空格
t_arr = text.split(" ").filter(function (s) {
return s && s.trim();
});
}
}
}
if (isEmpty(t_arr)) {
if (text.indexOf("\n") > 0) { // 换行符
// 字符串转数组
t_arr = text.split("\n").filter(function (s) {
return s && s.trim();
});
}
if (text.indexOf("\t") > 0) { // 制表符
t_arr = text.split("\t").filter(function (s) {
return s && s.trim();
});
}
}
// 最后还为空
if (isEmpty(t_arr)) {
if (text.indexOf(" ") > 0 text.indexOf(" ") > 0) { // 空格
t_arr = text.split(" ").filter(function (s) {
return s && s.trim();
});
}
}
return t_arr;
}
// 判断字符串中是否包含字母
function hasLetter(str, textManipulation) {
var min = 0;
var max = 0;
for (var i in str) {
var asc = str.charCodeAt(i);
// A-Z(65-90) a-z(97-122)
if (asc >= 65 && asc <= 90) {
max = 1; // 大写
}
if (asc >= 97 && asc <= 122) {
min = 1; // 小写
}
}
var res = 0; // 0.没字母 1.小写 2.大写 3.大小写都有
if (min == 1) {
res = 1;
}
if (max == 1) {
res = 2;
}
if (min == 1 && max == 1) {
res = 3;
}
// 根据 res 结果隐藏【转大小写】
if (textManipulation != undefined && res < 3) {
delTextManipulation(textManipulation, res);
}
return res; // 0.没字母 1.小写 2.大写 3.大小写都有
}
// 删除命令列表中指定项
function delTextManipulation(textManipulation, res) {
if (res == 0) { // 如果字符串中不含字母
delete textManipulation[5]; // 大写
delete textManipulation[6]; // 小写
} else if (res == 1) {
delete textManipulation[6]; // 小写
} else if (res == 2) {
delete textManipulation[5]; // 大写
}
return textManipulation;
}
// 判断是否为空
function isEmpty(obj){
if(typeof obj == "undefined" obj == null obj == ""){
return true;
}else{
return false;
}
}