This repository was archived by the owner on Jul 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathlists_indexOf.js
More file actions
executable file
·54 lines (49 loc) · 1.88 KB
/
lists_indexOf.js
File metadata and controls
executable file
·54 lines (49 loc) · 1.88 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
import { translate } from '../../../../utils/lang/i18n';
Blockly.Blocks.lists_indexOf = {
init() {
this.jsonInit(this.definition());
},
definition(){
return {
message0: translate('in list %1 find %2 occurence of item %3'),
args0 : [
{
type: 'input_value',
name: 'VALUE',
},
{
type : 'field_dropdown',
name : 'END',
options: [[translate('first'), 'FIRST'], [translate('last'), 'LAST']],
},
{
type: 'input_value',
name: 'FIND',
},
],
output : 'Number',
outputShape : Blockly.OUTPUT_SHAPE_ROUND,
colour : Blockly.Colours.Binary.colour,
colourSecondary: Blockly.Colours.Binary.colourSecondary,
colourTertiary : Blockly.Colours.Binary.colourTertiary,
tooltip : translate('Index of List Tooltip'),
category : Blockly.Categories.List,
};
},
meta(){
return {
'display_name': translate('Index Of List'),
'description' : translate('Index of List Description'),
};
},
};
Blockly.JavaScript.lists_indexOf = block => {
const operator = block.getFieldValue('END') === 'FIRST' ? 'indexOf' : 'lastIndexOf';
const item = Blockly.JavaScript.valueToCode(block, 'FIND', Blockly.JavaScript.ORDER_NONE) || '\'\'';
const list = Blockly.JavaScript.valueToCode(block, 'VALUE', Blockly.JavaScript.ORDER_MEMBER) || '\'\'';
const code = `${list}.${operator}(${item})`;
if (block.workspace.options.oneBasedIndex) {
return [`${code} + 1`, Blockly.JavaScript.ORDER_ADDITION];
}
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
};