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

More blocks for text extension #1672

Closed
wants to merge 2 commits into from
Closed
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
104 changes: 97 additions & 7 deletions extensions/text.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Name: Text
// ID: strings
// Description: Manipulate characters and text.
// Original: CST1229 <https://scratch.mit.edu/users/CST1229/>
// By: CST1229 <https://scratch.mit.edu/users/CST1229/>
// By: BludIsAnLemon <https://scratch.mit.edu/users/BludIsAnLemon/>
// License: MIT AND MPL-2.0

(function (Scratch) {
Expand Down Expand Up @@ -74,7 +75,7 @@
opcode: "letters_of",
blockType: Scratch.BlockType.REPORTER,
text: Scratch.translate(
"letters [LETTER1] to [LETTER2] of [STRING]"
"letters [LETTER1] to [LETTER2] of [STRING]",
),
arguments: {
LETTER1: {
Expand Down Expand Up @@ -154,7 +155,7 @@
opcode: "replace",
blockType: Scratch.BlockType.REPORTER,
text: Scratch.translate(
"replace [SUBSTRING] in [STRING] with [REPLACE]"
"replace [SUBSTRING] in [STRING] with [REPLACE]",
),
arguments: {
SUBSTRING: {
Expand Down Expand Up @@ -217,7 +218,7 @@
opcode: "replaceRegex",
blockType: Scratch.BlockType.REPORTER,
text: Scratch.translate(
"replace regex /[REGEX]/[FLAGS] in [STRING] with [REPLACE]"
"replace regex /[REGEX]/[FLAGS] in [STRING] with [REPLACE]",
),
arguments: {
REGEX: {
Expand Down Expand Up @@ -372,12 +373,79 @@
},
},
},

"---",

{
opcode: "endsAt",
blockType: Scratch.BlockType.BOOLEAN,
text: Scratch.translate("[STRING] ends with [SUBSTRING]?"),
arguments: {
STRING: {
type: Scratch.ArgumentType.STRING,
defaultValue: "apple",
},
SUBSTRING: {
type: Scratch.ArgumentType.STRING,
defaultValue: "banana",
},
},
},

"---",

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

"---",

{
opcode: "trim",
blockType: Scratch.BlockType.REPORTER,
text: Scratch.translate("trim [STRING] at [METHOD]"),
arguments: {
STRING: {
type: Scratch.ArgumentType.STRING,
defaultValue: " apple ",
},
METHOD: {
type: Scratch.ArgumentType.STRING,
menu: "trimMethod",
},
},
},
],
menus: {
textCase: {
acceptReporters: true,
items: this._initCaseMenu(),
},
trimMethod: {
acceptReporters: true,
items: [
{
text: Scratch.translate("both sides"),
value: "both",
},
{
text: Scratch.translate("the end"),
value: "front",
},
{
text: Scratch.translate("the start"),
value: "back",
},
],
},
},
};
}
Expand Down Expand Up @@ -440,7 +508,7 @@
STRING: args.STRING,
ITEM: 0,
},
util
util,
);
return splitCache.arr.length - 1 || 0;
}
Expand Down Expand Up @@ -485,7 +553,7 @@

return args.STRING.replace(
new RegExp(args.REGEX, args.FLAGS),
args.REPLACE
args.REPLACE,
);
} catch (e) {
console.error(e);
Expand Down Expand Up @@ -590,7 +658,7 @@
case CaseParam.MIXEDCASE:
return Array.from(string)
.map((char, index) =>
index % 2 === 0 ? char.toUpperCase() : char.toLowerCase()
index % 2 === 0 ? char.toUpperCase() : char.toLowerCase(),
)
.join("");
case CaseParam.TITLECASE:
Expand All @@ -613,6 +681,28 @@
return string;
}
}
endsAt(args) {
let STRING = args.STRING;
return STRING.endsWith(args.SUBSTRING);
}
backwards(args) {
let backwardsText = Array.from(args.STRING);
backwardsText.reverse();
return backwardsText.join("");
}
trim(args) {
let method = args.METHOD;
let STRING = args.STRING;
if (method == "both") {
return STRING.trim();
} else if (method == "front") {
return STRING.trimEnd();
} else if (method == "back") {
return STRING.trimStart();
} else {
return "Please select a valid method!";
}
}
}

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