Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sentence reporters to Text #1768

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions extensions/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Description: Manipulate characters and text.
// Original: CST1229 <https://scratch.mit.edu/users/CST1229/>
// By: BludIsAnLemon <https://scratch.mit.edu/users/BludIsAnLemon/>
// By: Man-o-Valor <https://scratch.mit.edu/users/man-o-valor/>
// License: MIT AND MPL-2.0

(function (Scratch) {
Expand All @@ -14,6 +15,9 @@
MIXEDCASE: "mixedcase",
TITLECASE: "titlecase",
EXACTTITLECASE: "exacttitlecase",
RANDOMCASE: "randomcase",
SENTENCECASE: "sentencecase",
CAMELCASE: "camelcase",
};

let splitCache;
Expand All @@ -38,6 +42,14 @@
}),
value: CaseParam.UPPERCASE,
},
{
text: Scratch.translate({
default: "Sentence case",
description:
"Starts words after ., !, and ? with captialized letters",
}),
value: CaseParam.SENTENCECASE,
},
{
text: Scratch.translate({
default: "Title Case",
Expand All @@ -62,6 +74,22 @@
}),
value: CaseParam.MIXEDCASE,
},
{
text: Scratch.translate({
default: "RAndoMCaSe",
description:
"If your language has randomcase, style it accordingly",
}),
value: CaseParam.RANDOMCASE,
},
{
text: Scratch.translate({
default: "camelCase",
description:
"Removes all spaces and capitalizes all words after the first",
}),
value: CaseParam.CAMELCASE,
},
];
}

Expand Down Expand Up @@ -658,6 +686,12 @@
word[0].toUpperCase() + word.substring(1).toLowerCase();
return word === titleCased;
});
case CaseParam.CAMELCASE:
return /^[^A-Z\s][^\s]*$/.test(string);
case CaseParam.RANDOMCASE:
return true;
case CaseParam.SENTENCECASE:
return /^[A-Z][^?.!]*(?:[?.!]\s+[A-Z][^?.!]*)*$/.test(string);
default:
return false;
}
Expand All @@ -666,6 +700,8 @@
toCase(args, util) {
const string = args.STRING.toString();
const textCase = args.TEXTCASE.toString();
let workingText = "";
let sentenceCapitalFlag = false;
switch (textCase) {
case CaseParam.LOWERCASE:
return string.toLowerCase();
Expand Down Expand Up @@ -693,6 +729,41 @@
return word[0].toUpperCase() + word.substring(1).toLowerCase();
})
.join("");
case CaseParam.SENTENCECASE:
for (let i = 0; i < string.length; i++) {
if (
/^\s*$/.test(string[i - 1] ?? " ") &&
!sentenceCapitalFlag &&
string[i].toUpperCase() != string[i].toLowerCase()
) {
workingText += string[i].toUpperCase();
sentenceCapitalFlag = true;
} else {
if (string[i] == "." || string[i] == "!" || string[i] == "?") {
sentenceCapitalFlag = false;
}
workingText += string[i].toLowerCase();
}
}
return workingText;
case CaseParam.RANDOMCASE:
for (let i = 0; i < string.length; i++) {
if (Math.random() > 0.5) {
workingText += string[i].toUpperCase();
} else {
workingText += string[i].toLowerCase();
}
}
return workingText;
case CaseParam.CAMELCASE:
for (let i = 0; i < string.length; i++) {
if (/^\s*$/.test(string[i - 1] ?? "x")) {
workingText += string[i].toUpperCase();
} else {
workingText += string[i].toLowerCase();
}
}
return workingText.replace(/\s/g, "");
default:
return string;
}
Expand Down
Loading