soiz1 commited on
Commit
fdd16a1
·
verified ·
1 Parent(s): 28e4ace

Update src/extensions/jg_json/index.js

Browse files
Files changed (1) hide show
  1. src/extensions/jg_json/index.js +277 -45
src/extensions/jg_json/index.js CHANGED
@@ -3,14 +3,16 @@ const formatMessage = require('format-message');
3
  const BlockType = require('../../extension-support/block-type');
4
  const ArgumentType = require('../../extension-support/argument-type');
5
  const Cast = require('../../util/cast');
6
- const {
7
- validateJSON,
8
- validateArray,
9
- stringToEqivalint,
10
  valueToString,
11
- validateRegex
12
  } = require('../../util/json-block-utilities');
13
 
 
 
14
  // const Cast = require('../../util/cast');
15
 
16
  /**
@@ -45,8 +47,18 @@ class JgJSONBlocks {
45
  defaultValue: "{}"
46
  }
47
  },
48
- text: 'is json [json] valid?'
49
- },
 
 
 
 
 
 
 
 
 
 
50
  "---",
51
  {
52
  opcode: 'getValueFromJSON',
@@ -70,7 +82,25 @@ class JgJSONBlocks {
70
  type: ArgumentType.STRING,
71
  defaultValue: '{"key": "value"}'
72
  }
73
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  },
75
  {
76
  opcode: 'getTreeValueFromJSON',
@@ -86,7 +116,25 @@ class JgJSONBlocks {
86
  type: ArgumentType.STRING,
87
  defaultValue: '{"first": {"second": 2, "third": 3}}'
88
  }
89
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
  },
91
  {
92
  opcode: 'setValueToKeyInJSON',
@@ -118,8 +166,31 @@ class JgJSONBlocks {
118
  type: ArgumentType.STRING,
119
  defaultValue: "{}"
120
  }
121
- }
122
- },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
123
  {
124
  opcode: 'json_delete',
125
  blockType: BlockType.REPORTER,
@@ -137,8 +208,33 @@ class JgJSONBlocks {
137
  })
138
  }
139
  },
140
- text: 'in json [json] delete key [key]'
141
- },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142
  {
143
  opcode: 'json_values',
144
  blockType: BlockType.REPORTER,
@@ -148,8 +244,13 @@ class JgJSONBlocks {
148
  defaultValue: "{}"
149
  }
150
  },
151
- text: 'get all values from json [json]'
152
- },
 
 
 
 
 
153
  {
154
  opcode: 'json_keys',
155
  blockType: BlockType.REPORTER,
@@ -159,8 +260,13 @@ class JgJSONBlocks {
159
  defaultValue: "{}"
160
  }
161
  },
162
- text: 'get all keys from json [json]'
163
- },
 
 
 
 
 
164
  {
165
  opcode: 'json_has',
166
  blockType: BlockType.BOOLEAN,
@@ -178,7 +284,7 @@ class JgJSONBlocks {
178
  })
179
  }
180
  },
181
- text: 'json [json] has key [key] ?'
182
  },
183
  {
184
  opcode: 'json_combine',
@@ -208,8 +314,18 @@ class JgJSONBlocks {
208
  defaultValue: "[]"
209
  }
210
  },
211
- text: 'is array [array] valid?'
212
- },
 
 
 
 
 
 
 
 
 
 
213
  {
214
  opcode: 'json_array_split',
215
  blockType: BlockType.REPORTER,
@@ -223,8 +339,18 @@ class JgJSONBlocks {
223
  defaultValue: ', '
224
  }
225
  },
226
- text: 'create an array from text [text] with delimeter [delimeter]'
227
- },
 
 
 
 
 
 
 
 
 
 
228
  {
229
  opcode: 'json_array_join',
230
  blockType: BlockType.REPORTER,
@@ -238,8 +364,18 @@ class JgJSONBlocks {
238
  defaultValue: ', '
239
  }
240
  },
241
- text: 'create text from array [array] with delimeter [delimeter]'
242
- },
 
 
 
 
 
 
 
 
 
 
243
  "---",
244
  {
245
  opcode: 'json_array_push',
@@ -258,7 +394,14 @@ class JgJSONBlocks {
258
  })
259
  }
260
  },
261
- text: 'in array [array] add [item]'
 
 
 
 
 
 
 
262
  },
263
  "---",
264
  {
@@ -274,7 +417,12 @@ class JgJSONBlocks {
274
  defaultValue: "[\"D\", \"E\", \"F\"]"
275
  }
276
  },
277
- text: 'add items from array [array2] to array [array1]'
 
 
 
 
 
278
  },
279
  {
280
  opcode: 'json_array_concatLayer2',
@@ -293,8 +441,13 @@ class JgJSONBlocks {
293
  defaultValue: "[\"G\", \"H\", \"I\"]"
294
  }
295
  },
296
- text: 'add items from array [array2] and array [array3] to array [array1]'
297
- },
 
 
 
 
 
298
  "---",
299
  {
300
  opcode: 'json_array_delete',
@@ -309,7 +462,14 @@ class JgJSONBlocks {
309
  defaultValue: 2
310
  }
311
  },
312
- text: 'in array [array] delete [index]'
 
 
 
 
 
 
 
313
  },
314
  {
315
  opcode: 'json_array_reverse',
@@ -321,7 +481,7 @@ class JgJSONBlocks {
321
  }
322
  },
323
  text: 'reverse array [array]'
324
- },
325
  {
326
  opcode: 'json_array_insert',
327
  blockType: BlockType.REPORTER,
@@ -343,7 +503,14 @@ class JgJSONBlocks {
343
  })
344
  }
345
  },
346
- text: 'in array [array] insert [value] at [index]'
 
 
 
 
 
 
 
347
  },
348
  {
349
  opcode: 'json_array_set',
@@ -366,8 +533,15 @@ class JgJSONBlocks {
366
  })
367
  }
368
  },
369
- text: 'in array [array] set [index] to [value]'
370
- },
 
 
 
 
 
 
 
371
  "---",
372
  {
373
  opcode: 'json_array_get',
@@ -382,7 +556,24 @@ class JgJSONBlocks {
382
  defaultValue: 2
383
  }
384
  },
385
- text: 'in array [array] get [index]'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
386
  },
387
  {
388
  opcode: 'json_array_indexofNostart',
@@ -397,8 +588,15 @@ class JgJSONBlocks {
397
  defaultValue: "value"
398
  }
399
  },
400
- text: 'in array [array] get index of [value]'
401
- },
 
 
 
 
 
 
 
402
  {
403
  opcode: 'json_array_indexof',
404
  blockType: BlockType.REPORTER,
@@ -420,8 +618,25 @@ class JgJSONBlocks {
420
  })
421
  }
422
  },
423
- text: 'in array [array] from [number] get index of [value]'
424
- },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
425
  {
426
  opcode: 'json_array_length',
427
  blockType: BlockType.REPORTER,
@@ -432,7 +647,7 @@ class JgJSONBlocks {
432
  }
433
  },
434
  text: 'length of array [array]'
435
- },
436
  {
437
  opcode: 'json_array_contains',
438
  blockType: BlockType.BOOLEAN,
@@ -450,7 +665,7 @@ class JgJSONBlocks {
450
  })
451
  }
452
  },
453
- text: 'array [array] contains [value] ?'
454
  },
455
  "---",
456
  {
@@ -486,8 +701,25 @@ class JgJSONBlocks {
486
  defaultValue: 2
487
  }
488
  },
489
- text: 'in array [array] get all items from [index1] to [index2]'
490
- },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
491
  "---",
492
  {
493
  opcode: 'json_array_isempty',
@@ -499,7 +731,7 @@ class JgJSONBlocks {
499
  }
500
  },
501
  text: 'is array [array] empty?'
502
- },
503
  "---",
504
  {
505
  opcode: 'json_array_listtoarray',
@@ -685,7 +917,7 @@ class JgJSONBlocks {
685
 
686
  return array.indexOf(stringToEqivalint(value), number);
687
  }
688
-
689
  json_array_indexofNostart (args) {
690
  const array = validateArray(args.array).array;
691
  const value = args.value;
@@ -821,4 +1053,4 @@ class JgJSONBlocks {
821
  }
822
  }
823
 
824
- module.exports = JgJSONBlocks;
 
3
  const BlockType = require('../../extension-support/block-type');
4
  const ArgumentType = require('../../extension-support/argument-type');
5
  const Cast = require('../../util/cast');
6
+ const {
7
+ validateJSON,
8
+ validateArray,
9
+ stringToEqivalint,
10
  valueToString,
11
+ validateRegex
12
  } = require('../../util/json-block-utilities');
13
 
14
+ const noopSwitch = { isNoop: true };
15
+
16
  // const Cast = require('../../util/cast');
17
 
18
  /**
 
47
  defaultValue: "{}"
48
  }
49
  },
50
+ text: 'is json [json] valid?',
51
+ switches: [
52
+ noopSwitch,
53
+ {
54
+ opcode: 'json_array_validate',
55
+ remapArguments: {
56
+ json: 'array'
57
+ }
58
+ }
59
+ ],
60
+ switchText: 'is json valid?',
61
+ },
62
  "---",
63
  {
64
  opcode: 'getValueFromJSON',
 
82
  type: ArgumentType.STRING,
83
  defaultValue: '{"key": "value"}'
84
  }
85
+ },
86
+ switches: [
87
+ noopSwitch,
88
+ 'getTreeValueFromJSON',
89
+ {
90
+ opcode: 'setValueToKeyInJSON',
91
+ remapArguments: {
92
+ VALUE: 'KEY'
93
+ }
94
+ },
95
+ {
96
+ opcode: 'json_delete',
97
+ remapArguments: {
98
+ VALUE: 'key',
99
+ JSON: 'json'
100
+ }
101
+ },
102
+ ],
103
+ switchText: 'get key from json'
104
  },
105
  {
106
  opcode: 'getTreeValueFromJSON',
 
116
  type: ArgumentType.STRING,
117
  defaultValue: '{"first": {"second": 2, "third": 3}}'
118
  }
119
+ },
120
+ switches: [
121
+ 'getValueFromJSON',
122
+ noopSwitch,
123
+ {
124
+ opcode: 'setValueToKeyInJSON',
125
+ remapArguments: {
126
+ VALUE: 'KEY'
127
+ }
128
+ },
129
+ {
130
+ opcode: 'json_delete',
131
+ remapArguments: {
132
+ VALUE: 'key',
133
+ JSON: 'json'
134
+ }
135
+ },
136
+ ],
137
+ switchText: 'get path from json'
138
  },
139
  {
140
  opcode: 'setValueToKeyInJSON',
 
166
  type: ArgumentType.STRING,
167
  defaultValue: "{}"
168
  }
169
+ },
170
+ switches: [
171
+ {
172
+ opcode: 'getValueFromJSON',
173
+ remapArguments: {
174
+ KEY: 'VALUE'
175
+ }
176
+ },
177
+ {
178
+ opcode: 'getTreeValueFromJSON',
179
+ remapArguments: {
180
+ KEY: 'VALUE'
181
+ }
182
+ },
183
+ noopSwitch,
184
+ {
185
+ opcode: 'json_delete',
186
+ remapArguments: {
187
+ KEY: 'key',
188
+ JSON: 'json'
189
+ }
190
+ }
191
+ ],
192
+ switchText: 'set key to value in json'
193
+ },
194
  {
195
  opcode: 'json_delete',
196
  blockType: BlockType.REPORTER,
 
208
  })
209
  }
210
  },
211
+ text: 'in json [json] delete key [key]',
212
+ switches: [
213
+ {
214
+ opcode: 'getValueFromJSON',
215
+ remapArguments: {
216
+ key: 'VALUE',
217
+ json: 'JSON'
218
+ }
219
+ },
220
+ {
221
+ opcode: 'getTreeValueFromJSON',
222
+ remapArguments: {
223
+ key: 'VALUE',
224
+ json: 'JSON'
225
+ }
226
+ },
227
+ {
228
+ opcode: 'setValueToKeyInJSON',
229
+ remapArguments: {
230
+ key: 'KEY',
231
+ json: 'JSON'
232
+ }
233
+ },
234
+ noopSwitch,
235
+ ],
236
+ switchText: 'in json delete key'
237
+ },
238
  {
239
  opcode: 'json_values',
240
  blockType: BlockType.REPORTER,
 
244
  defaultValue: "{}"
245
  }
246
  },
247
+ text: 'get all values from json [json]',
248
+ switches: [
249
+ noopSwitch,
250
+ 'json_keys',
251
+ ],
252
+ switchText: 'get all values from json',
253
+ },
254
  {
255
  opcode: 'json_keys',
256
  blockType: BlockType.REPORTER,
 
260
  defaultValue: "{}"
261
  }
262
  },
263
+ text: 'get all keys from json [json]',
264
+ switches: [
265
+ 'json_values',
266
+ noopSwitch,
267
+ ],
268
+ switchText: 'get all keys from json',
269
+ },
270
  {
271
  opcode: 'json_has',
272
  blockType: BlockType.BOOLEAN,
 
284
  })
285
  }
286
  },
287
+ text: 'json [json] has key [key]?'
288
  },
289
  {
290
  opcode: 'json_combine',
 
314
  defaultValue: "[]"
315
  }
316
  },
317
+ text: 'is array [array] valid?',
318
+ switches: [
319
+ {
320
+ opcode: 'json_validate',
321
+ remapArguments: {
322
+ array: 'json'
323
+ }
324
+ },
325
+ noopSwitch,
326
+ ],
327
+ switchText: 'is array valid?',
328
+ },
329
  {
330
  opcode: 'json_array_split',
331
  blockType: BlockType.REPORTER,
 
339
  defaultValue: ', '
340
  }
341
  },
342
+ text: 'create an array from text [text] with delimeter [delimeter]',
343
+ switches: [
344
+ noopSwitch,
345
+ {
346
+ opcode: 'json_array_join',
347
+ remapArguments: {
348
+ text: 'array'
349
+ }
350
+ }
351
+ ],
352
+ switchText: 'create array from text'
353
+ },
354
  {
355
  opcode: 'json_array_join',
356
  blockType: BlockType.REPORTER,
 
364
  defaultValue: ', '
365
  }
366
  },
367
+ text: 'create text from array [array] with delimeter [delimeter]',
368
+ switches: [
369
+ {
370
+ opcode: 'json_array_split',
371
+ remapArguments: {
372
+ array: 'text'
373
+ }
374
+ },
375
+ noopSwitch,
376
+ ],
377
+ switchText: 'create text from array'
378
+ },
379
  "---",
380
  {
381
  opcode: 'json_array_push',
 
394
  })
395
  }
396
  },
397
+ text: 'in array [array] add [item]',
398
+ switches: [
399
+ noopSwitch,
400
+ 'json_array_delete',
401
+ 'json_array_insert',
402
+ 'json_array_set',
403
+ ],
404
+ switchText: 'in array add item',
405
  },
406
  "---",
407
  {
 
417
  defaultValue: "[\"D\", \"E\", \"F\"]"
418
  }
419
  },
420
+ text: 'add items from array [array2] to array [array1]',
421
+ switches: [
422
+ noopSwitch,
423
+ 'json_array_concatLayer2'
424
+ ],
425
+ switchText: 'add items from array to array',
426
  },
427
  {
428
  opcode: 'json_array_concatLayer2',
 
441
  defaultValue: "[\"G\", \"H\", \"I\"]"
442
  }
443
  },
444
+ text: 'add items from array [array2] and array [array3] to array [array1]',
445
+ switches: [
446
+ 'json_array_concatLayer1',
447
+ noopSwitch,
448
+ ],
449
+ switchText: 'add items from array and array to array',
450
+ },
451
  "---",
452
  {
453
  opcode: 'json_array_delete',
 
462
  defaultValue: 2
463
  }
464
  },
465
+ text: 'in array [array] delete [index]',
466
+ switches: [
467
+ 'json_array_push',
468
+ noopSwitch,
469
+ 'json_array_insert',
470
+ 'json_array_set',
471
+ ],
472
+ switchText: 'in array delete index',
473
  },
474
  {
475
  opcode: 'json_array_reverse',
 
481
  }
482
  },
483
  text: 'reverse array [array]'
484
+ },
485
  {
486
  opcode: 'json_array_insert',
487
  blockType: BlockType.REPORTER,
 
503
  })
504
  }
505
  },
506
+ text: 'in array [array] insert [value] at [index]',
507
+ switches: [
508
+ 'json_array_push',
509
+ 'json_array_delete',
510
+ noopSwitch,
511
+ 'json_array_set',
512
+ ],
513
+ switchText: 'in array insert value at index',
514
  },
515
  {
516
  opcode: 'json_array_set',
 
533
  })
534
  }
535
  },
536
+ text: 'in array [array] set [index] to [value]',
537
+ switches: [
538
+ 'json_array_push',
539
+ 'json_array_delete',
540
+ 'json_array_insert',
541
+ noopSwitch,
542
+ ],
543
+ switchText: 'in array set index to value',
544
+ },
545
  "---",
546
  {
547
  opcode: 'json_array_get',
 
556
  defaultValue: 2
557
  }
558
  },
559
+ text: 'in array [array] get [index]',
560
+ switches: [
561
+ noopSwitch,
562
+ 'json_array_indexofNostart',
563
+ {
564
+ opcode: 'json_array_indexof',
565
+ remapArguments: {
566
+ index: 'number',
567
+ },
568
+ },
569
+ {
570
+ opcode: 'json_array_getrange',
571
+ remapArguments: {
572
+ index: 'index1'
573
+ }
574
+ }
575
+ ],
576
+ switchText: 'in array get index',
577
  },
578
  {
579
  opcode: 'json_array_indexofNostart',
 
588
  defaultValue: "value"
589
  }
590
  },
591
+ text: 'in array [array] get index of [value]',
592
+ switches: [
593
+ 'json_array_get',
594
+ noopSwitch,
595
+ 'json_array_indexof',
596
+ 'json_array_getrange',
597
+ ],
598
+ switchText: 'in arrray get index of value',
599
+ },
600
  {
601
  opcode: 'json_array_indexof',
602
  blockType: BlockType.REPORTER,
 
618
  })
619
  }
620
  },
621
+ text: 'in array [array] from [number] get index of [value]',
622
+ switches: [
623
+ {
624
+ opcode: 'json_array_get',
625
+ remapArguments: {
626
+ number: 'index'
627
+ }
628
+ },
629
+ 'json_array_indexofNostart',
630
+ noopSwitch,
631
+ {
632
+ opcode: 'json_array_getrange',
633
+ remapArguments: {
634
+ number: 'index1'
635
+ }
636
+ }
637
+ ],
638
+ switchText: 'in array from index get index of value',
639
+ },
640
  {
641
  opcode: 'json_array_length',
642
  blockType: BlockType.REPORTER,
 
647
  }
648
  },
649
  text: 'length of array [array]'
650
+ },
651
  {
652
  opcode: 'json_array_contains',
653
  blockType: BlockType.BOOLEAN,
 
665
  })
666
  }
667
  },
668
+ text: 'array [array] contains [value]?'
669
  },
670
  "---",
671
  {
 
701
  defaultValue: 2
702
  }
703
  },
704
+ text: 'in array [array] get all items from [index1] to [index2]',
705
+ switches: [
706
+ {
707
+ opcode: 'json_array_get',
708
+ remapArguments: {
709
+ index1: 'index'
710
+ }
711
+ },
712
+ 'json_array_indexofNostart',
713
+ {
714
+ opcode: 'json_array_get',
715
+ remapArguments: {
716
+ index1: 'number'
717
+ }
718
+ },
719
+ noopSwitch
720
+ ],
721
+ switchText: 'in array get items from index to index',
722
+ },
723
  "---",
724
  {
725
  opcode: 'json_array_isempty',
 
731
  }
732
  },
733
  text: 'is array [array] empty?'
734
+ },
735
  "---",
736
  {
737
  opcode: 'json_array_listtoarray',
 
917
 
918
  return array.indexOf(stringToEqivalint(value), number);
919
  }
920
+
921
  json_array_indexofNostart (args) {
922
  const array = validateArray(args.array).array;
923
  const value = args.value;
 
1053
  }
1054
  }
1055
 
1056
+ module.exports = JgJSONBlocks;