Буквенный хаос

This commit is contained in:
2023-03-28 01:27:30 +04:00
parent 49edfdca66
commit 0ca319ae0f
7 changed files with 129 additions and 25 deletions
+27 -17
View File
@@ -10,35 +10,45 @@ import $ from "jquery";
*/
export class CrazyPunctuationJoke extends Joke {
id = 'crazy_punctuation';
settings = new CrazyPunctuationJokeSettings;
private transform(text: string): string {
let newText = "";
punctuationSymbols: string[]|null = null;
for (let i = 0; i < text.length; i++) {
const currentChar = text[i];
const nextChar = text[i + 1];
if (this.settings.symbols.includes(currentChar)) {
newText += currentChar;
} else if (nextChar === " " || this.settings.symbols.includes(nextChar)) {
newText += currentChar;
}
private shufflePunctuation(text: string): string {
if (this.punctuationSymbols === null) {
this.punctuationSymbols = this.settings.symbols.split('');
}
const words = text.split(" ");
return newText;
}
const shuffledWords = words.map(word => {
const characters = word.split("");
private process(node: any): void {
// Filter out any non-punctuation characters
const punctuation = characters.filter(char => this.punctuationSymbols.includes(char));
// Shuffle the punctuation array
const shuffledPunctuation = punctuation.sort(() => Math.random() - 0.5);
// Loop through each character in the word and replace punctuation with shuffled punctuation
let index = 0;
return characters.map(char => {
if (this.punctuationSymbols.includes(char)) {
return shuffledPunctuation[index++];
}
return char;
}).join("");
});
// Join the shuffled words array into a single string with spaces
return shuffledWords.join(" ");
}
start(): void {
const items = $(this.settings.selector).find('*').contents().filter((_, node) => node.nodeType === Node.TEXT_NODE);
items.each((_, node) => {
console.log(node);
node.textContent = this.shufflePunctuation(node.textContent);
})
}
}
@@ -49,7 +59,7 @@ export class CrazyPunctuationJokeSettings implements JokeSettings {
/**
* Селектор всех элементов, которые будут затронуты
*/
selector: string = '.post-content';
selector: string = '.post-content p';
/**
* Мигрирующие знаки
+62
View File
@@ -0,0 +1,62 @@
import { Joke, JokeSettings, StopableJoke } from "../joke";
import $ from "jquery";
import { AprilFoolsJokeClass } from "../handler";
/**
* **Буквенный хаос**
*
* Все буквы в словах, кроме первой и последней, перемешены.
*
* @author Kozhilya
*/
export class LetterChaosJoke extends Joke {
id = 'letter_chaos';
settings = new LetterChaosJokeSettings;
punctuationSymbols: string[] | null = null;
private shuffleLetters(word: string): string {
// Check if the word has two or more letters
if ((word.length < 3) || !AprilFoolsJokeClass.check(this.settings.shuffle_chance)) {
return word;
}
// Split the word into an array of characters
const characters = word.split("");
// Shuffle the characters between the first and last letters
const middleCharacters = characters.slice(1, -1);
for (let i = middleCharacters.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[middleCharacters[i], middleCharacters[j]] = [middleCharacters[j], middleCharacters[i]];
}
// Join the shuffled characters back into a word
return [characters[0], ...middleCharacters, characters[characters.length - 1]].join("");
}
private processNode(node: any) {
node.textContent = node.textContent?.replace(/([А-Яа-яA-Za-z]+)/gm, this.shuffleLetters);
}
start(): void {
const items = $(this.settings.selector).find('*').contents().filter((_, node) => node.nodeType === Node.TEXT_NODE);
items.each((_, node) => this.processNode(node))
}
}
export class LetterChaosJokeSettings implements JokeSettings {
chance: number = 20;
/**
* Селектор всех элементов, которые будут затронуты
*/
selector: string = '.post-content';
/**
* Вероятность того, что буквы будут перемешаны
*/
shuffle_chance: number = 50;
}
+2
View File
@@ -6,6 +6,7 @@ import { MirrorJoke } from "./jokes/mirror";
import { CarnivalJoke } from "./jokes/carnival";
import { CrazyLettersJoke } from "./jokes/crazy_letters";
import { CrazyPunctuationJoke } from "./jokes/crazy_punctuation";
import { LetterChaosJoke } from "./jokes/letter_chaos";
(window as any).AprilJokes = ((jokes: JokeClassList): AprilFoolsJokeClass => {
const cl = new AprilFoolsJokeClass();
@@ -24,6 +25,7 @@ import { CrazyPunctuationJoke } from "./jokes/crazy_punctuation";
CarnivalJoke,
CrazyLettersJoke,
CrazyPunctuationJoke,
LetterChaosJoke,
]);
interface JokeClassList extends Array<new () => Joke> {}