-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cast.js
74 lines (69 loc) · 1.79 KB
/
Cast.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
(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]',
isDynamic: true,
output: null,
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);