Skip to content

Commit

Permalink
Add Lily/Cast extension (#605)
Browse files Browse the repository at this point in the history
  • Loading branch information
LilyMakesThings authored Jul 8, 2023
1 parent 5b9aac1 commit bfb68ac
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
73 changes: 73 additions & 0 deletions extensions/Lily/Cast.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
(function (Scratch) {
'use strict';

const Cast = Scratch.Cast;

class CastUtil {
getInfo() {
return {
id: 'lmsCast',
name: 'Cast',
blocks: [
{
opcode: 'toType',
blockType: Scratch.BlockType.REPORTER,
text: 'cast [INPUT] to [TYPE]',
allowDropAnywhere: true,
disableMonitor: true,
arguments: {
INPUT: {
type: Scratch.ArgumentType.STRING,
defaultValue: 'apple'
},
TYPE: {
type: Scratch.ArgumentType.STRING,
menu: 'type'
}
}
},
{
opcode: 'typeOf',
blockType: Scratch.BlockType.REPORTER,
text: 'type of [INPUT]',
disableMonitor: true,
arguments: {
INPUT: {
type: Scratch.ArgumentType.STRING,
defaultValue: 'apple'
}
}
}
],
menus: {
type: {
acceptReporters: true,
items: ['number', 'string', 'boolean', 'default']
}
}
};
}

toType(args) {
const input = args.INPUT;
switch (args.TYPE) {
case ('number'): return Cast.toNumber(input);
case ('string'): return Cast.toString(input);
case ('boolean'): return Cast.toBoolean(input);
default: return input;
}
}

typeOf(args) {
const input = args.INPUT;
switch (typeof input) {
case ('number'): return 'number';
case ('string'): return 'string';
case ('boolean'): return 'boolean';
default: return '';
}
}
}

Scratch.extensions.register(new CastUtil());
})(Scratch);
Loading

0 comments on commit bfb68ac

Please sign in to comment.