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 6 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
127 changes: 127 additions & 0 deletions extensions/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// ID: strings
// Description: Manipulate characters and text.
// Original: CST1229 <https://scratch.mit.edu/users/CST1229/>
// By: Man-o-Valor <https://scratch.mit.edu/users/man-o-valor/>
// License: MIT AND MPL-2.0

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

let splitCache;
Expand All @@ -37,6 +41,13 @@
}),
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 @@ -61,6 +72,20 @@
}),
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 @@ -186,9 +211,49 @@
},
},
},
{
opcode: 'surround',
blockType: Scratch.BlockType.REPORTER,
text: Scratch.translate("surround [TEXT] with [QUOTE]"),
arguments: {
TEXT: {
type: Scratch.ArgumentType.STRING,
defaultValue: "apple"
},
QUOTE: {
type: Scratch.ArgumentType.STRING,
menu: "QUOTES"
}
},
},

{
opcode: 'reverse',
blockType: Scratch.BlockType.REPORTER,
text: Scratch.translate("reverse [TEXT]"),
arguments: {
TEXT: {
type: Scratch.ArgumentType.STRING,
defaultValue: "apple"
}
},
},

"---",

{
opcode: 'enter',
blockType: Scratch.BlockType.REPORTER,
text: Scratch.translate("enter"),
yuri-kiss marked this conversation as resolved.
Show resolved Hide resolved
},
{
opcode: 'tab',
blockType: Scratch.BlockType.REPORTER,
text: Scratch.translate("tab"),
},

"---",

{
opcode: "unicodeof",
blockType: Scratch.BlockType.REPORTER,
Expand Down Expand Up @@ -378,6 +443,10 @@
acceptReporters: true,
items: this._initCaseMenu(),
},
quotes: {
acceptReporters: true,
items: ["\"", "'", "`", "-", "*", "()", "{}", "[]", "<>", "^$"],
},
},
};
}
Expand Down Expand Up @@ -574,6 +643,12 @@
word[0].toUpperCase() + word.substring(1).toLowerCase();
return word === titleCased;
});
case CaseParam.CAMELCASE:
return string.test(/^[^A-Z\s][^\s]*$/);
man-o-valor marked this conversation as resolved.
Show resolved Hide resolved
case CaseParam.RANDOMCASE:
return true;
case CaseParam.SENTENCECASE:
return string.test(/^[A-Z][^?.!]*(?:[?.!]\s+[A-Z][^?.!]*)*$/);
default:
return false;
}
Expand All @@ -582,6 +657,8 @@
toCase(args, util) {
const string = args.STRING.toString();
const textCase = args.TEXTCASE.toString();
let sum = "";
man-o-valor marked this conversation as resolved.
Show resolved Hide resolved
let capflag = false
switch (textCase) {
case CaseParam.LOWERCASE:
return string.toLowerCase();
Expand Down Expand Up @@ -609,10 +686,60 @@
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] ?? " ") && !capflag && string[i].toUpperCase() != string[i].toLowerCase()) {
sum +=string[i].toUpperCase();
capflag = true
} else {
if (string[i] == "." || string[i] == "!" || string[i] == "?") {
capflag = false
}
sum += string[i].toLowerCase();
}
}
return sum;
case CaseParam.RANDOMCASE:
for (let i = 0; i < string.length; i++) {
if (Math.random()>0.5) {
sum += string[i].toUpperCase()
} else {
sum += string[i].toLowerCase()
}
}
return sum;
case CaseParam.CAMELCASE:
for (let i = 0; i < string.length; i++) {
if (/^\s*$/.test(string[i-1] ?? "x")) {
sum += string[i].toUpperCase();
} else {
sum += string[i].toLowerCase();
}
}
return sum.replace(/\s/g, "");
default:
return string;
}
}

surround(args) {
if (args.QUOTE.length == 0) {
return args.TEXT;
} else if (args.QUOTE.length == 1) {
return args.QUOTE + args.TEXT + args.QUOTE;
} else if (args.QUOTE.length > 1) {
return args.QUOTE[0] + args.TEXT + args.QUOTE[1];
}
}
enter() {
return "\n";
}
tab() {
return " ";
}
reverse(args) {
return args.TEXT.split("").reverse().join("");
}
}

Scratch.extensions.register(new StringsExt());
Expand Down
Loading