Skip to content

Commit

Permalink
代码重构
Browse files Browse the repository at this point in the history
  • Loading branch information
NitroRCr committed Apr 17, 2020
1 parent 8f3311e commit c73a685
Showing 1 changed file with 54 additions and 45 deletions.
99 changes: 54 additions & 45 deletions js/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
$('.start-mixin').click(function () {
var text = $('#textin').val();
var mixin = '\u200b';
function WordsAway() {}
WordsAway.prototype.mixin = function (text, mixin = '\u200b', missBrackets = true) {
var result = '';
var inBrackets = false;
var missBrackets = $('#miss-brackets')[0].checked;
if (missBrackets) {
text = text.replace(/(http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?)/g, '[$1]');
for (let i in text) {
let x = text[i];
if (inBrackets) {
Expand All @@ -24,50 +21,62 @@ $('.start-mixin').click(function () {
result += (mixin + text[i]);
}
}
if ($('#twice-turn-over')[0].checked) {
var rows = result.split('\n');
result = '';
for (let i in rows) {
inBrackets = false;
let before;
let x = rows[i];
let newRow = '';
for (let j in x) {
let y = x[j];
if (y == '[' && missBrackets) {
before = j;
inBrackets = true;
} else if (y == ']' && missBrackets) {
inBrackets = false;
newRow = x.slice(before, parseInt(j) + 1) + newRow;
} else if (!inBrackets) {
newRow =
((y == '(') ? ')' :
(y == ')') ? '(' :
(y == '(') ? ')' :
(y == ')') ? '(' :
(y == '{') ? '}' :
(y == '}') ? '{' :
(y == '《') ? '》' :
(y == '》') ? '《' :
(y == '<') ? '>' :
(y == '>') ? '<' :
(y == '【') ? '】' :
(y == '】') ? '【' :
y) +
newRow;
}
return result;
}
WordsAway.prototype.turnOver = function (text, missBrackets = true) {
var rows = text.split('\n');
result = '';
for (let i in rows) {
inBrackets = false;
let before;
let x = rows[i];
let newRow = '';
for (let j in x) {
let y = x[j];
if (y == '[' && missBrackets) {
before = j;
inBrackets = true;
} else if (y == ']' && missBrackets) {
inBrackets = false;
newRow = x.slice(before, parseInt(j) + 1) + newRow;
} else if (!inBrackets) {
newRow =
((y == '(') ? ')' :
(y == ')') ? '(' :
(y == '(') ? ')' :
(y == ')') ? '(' :
(y == '{') ? '}' :
(y == '}') ? '{' :
(y == '《') ? '》' :
(y == '》') ? '《' :
(y == '<') ? '>' :
(y == '>') ? '<' :
(y == '【') ? '】' :
(y == '】') ? '【' :
y) +
newRow;
}
newRow = '\u202e' + newRow + '\n';
result += newRow;
}
newRow = '\u202e' + newRow + '\n';
result += newRow;
}
if (missBrackets) {
result = result.replace(/\[(http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?)\]/g, '$1');
}
$('pre.result').text(result);
$('.to-copy').attr('data-clipboard-text', result);
return result;
}

var wordsAway = new WordsAway();

$('.start-mixin').click(function () {
var text = $('#textin').val();
var mixin = '\u200b';
var missBrackets = $('#miss-brackets')[0].checked;
text = (missBrackets) ? text.replace(/(http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?)/g, '[$1]') : text;
text = wordsAway.mixin(text, mixin, missBrackets);
text = ($('#twice-turn-over')[0].checked) ? wordsAway.turnOver(text, missBrackets) : text;
text = (missBrackets) ? text.replace(/\[(http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?)\]/g, '$1') : text;
$('pre.result').text(text);
$('.to-copy').attr('data-clipboard-text', text);
})

new ClipboardJS('.to-copy');
$('.to-copy').click(function () {
M.toast({
Expand Down

0 comments on commit c73a685

Please sign in to comment.