-
Notifications
You must be signed in to change notification settings - Fork 539
Expand file tree
/
Copy pathcast.js
More file actions
92 lines (87 loc) · 2.37 KB
/
cast.js
File metadata and controls
92 lines (87 loc) · 2.37 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
class CastingExample {
getInfo() {
return {
id: 'castexample',
name: Scratch.translate('Casting Example'),
blocks: [
{
opcode: 'toNumber',
blockType: Scratch.BlockType.REPORTER,
text: Scratch.translate('convert [INPUT] to number'),
arguments: {
INPUT: {
type: Scratch.ArgumentType.STRING,
defaultValue: '3.0'
}
}
},
{
// The opcode "toString" should work but given it's special
// treatment in JS, it seems a bit dangerous to use
opcode: 'castToString',
blockType: Scratch.BlockType.REPORTER,
text: Scratch.translate('convert [INPUT] to string'),
arguments: {
INPUT: {
type: Scratch.ArgumentType.STRING,
defaultValue: 'Hello'
}
}
},
{
opcode: 'toBoolean',
blockType: Scratch.BlockType.BOOLEAN,
text: Scratch.translate('convert [INPUT] to boolean'),
arguments: {
INPUT: {
type: Scratch.ArgumentType.STRING,
defaultValue: '1'
}
}
},
{
opcode: 'compare',
blockType: Scratch.BlockType.REPORTER,
text: Scratch.translate('compare [A] to [B]'),
arguments: {
A: {
type: Scratch.ArgumentType.STRING,
defaultValue: '3'
},
B: {
type: Scratch.ArgumentType.STRING,
defaultValue: '5'
}
}
}
]
};
}
toNumber({INPUT}) {
// highlight-next-line
return Scratch.Cast.toNumber(INPUT);
}
castToString({INPUT}) {
// highlight-next-line
return Scratch.Cast.toString(INPUT);
}
toBoolean({INPUT}) {
// highlight-next-line
return Scratch.Cast.toBoolean(INPUT);
}
compare({A, B}) {
// highlight-start
const comparison = Scratch.Cast.compare(A, B);
// You need to use < 0, > 0, or === 0 here.
// Do not use === 1 or === -1! That will not work in some cases.
if (comparison === 0) {
return 'Equal';
} else if (comparison > 0) {
return 'A is greater';
} else if (comparison < 0) {
return 'B is greater';
}
// highlight-end
}
}
Scratch.extensions.register(new CastingExample());