You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: LANGUAGE.md
+133-1Lines changed: 133 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -574,6 +574,138 @@ evaluates as: `{ "$exec": "myExec" }` and the executor is not called.
574
574
The anonymous function defined above always returns the string `"foo"`. Without `$quote` it would have been the reference to the function with the name `foo`.
575
575
576
576
577
+
## Calculations
578
+
579
+
Predefined executor `calc` defines methods for arythmetic, comparison and logical operations. For all operations the arguments (`$args`) should be an array and operations are applied to the list:
580
+
581
+
```json
582
+
{ "$+": [ 1, 2, 3 ] }
583
+
```
584
+
585
+
or using the full syntax:
586
+
587
+
```json
588
+
{
589
+
"$exec": "calc",
590
+
"$method": "add",
591
+
"$args": [ 1, 2, 3 ]
592
+
}
593
+
```
594
+
595
+
Full syntax can be useful if you need to determine the required operation using some script:
596
+
597
+
```json
598
+
{
599
+
"$exec": "calc",
600
+
"$method": { "$data": "/method" },
601
+
"$args": [ 1, 2, 3 ]
602
+
}
603
+
```
604
+
605
+
For arythmetic and comparison operations arguments must be numbers, there is no type coercion.
606
+
607
+
For boolean operations arguments must be boolean values.
608
+
609
+
Equality operations can work with any type.
610
+
611
+
612
+
Defined operations:
613
+
614
+
| method|short syntax|evaluation|
615
+
|--------------|:---:|---|
616
+
| add |"$+" |add all arguments|
617
+
| subtract |"$-" |subtract arguments from the first argument|
618
+
| multiply |"$*" |multiply all arguments|
619
+
| divide |"$/" |divide the first argument by the rest|
620
+
| equal |"$=="|true if all arguments are equal|
621
+
| notEqual |"$!="|true if at least one argument is different|
622
+
| greater |"$>" |true if arguments are descending|
623
+
| greaterEqual |"$>="|true if arguments are not ascending|
624
+
| lesser |"$<" |true if arguments are ascending|
625
+
| lesserEqual |"$<="|true if arguments are not descending|
626
+
| and |"$&&"|true if all arguments are true|
627
+
| or |"$||"|true if one or more arguments are true and the rest are false|
628
+
| xor |"$^^"|true if exactly one argument is true and others are false|
629
+
| not |"$!" |negates boolean value|
630
+
631
+
632
+
## Array iteration
633
+
634
+
Predefined executor `array` implements methods for array iteration:
635
+
636
+
```json
637
+
{
638
+
"$$array.map": {
639
+
"data": [
640
+
"/resource/1",
641
+
"/resource/2",
642
+
"/resource/3"
643
+
],
644
+
"iterator": {
645
+
"$func": {
646
+
"$$router.get": { "path": { "$data": "/path" } }
647
+
},
648
+
"$args": ["path"]
649
+
}
650
+
}
651
+
}
652
+
```
653
+
654
+
The example above calls the method `get` of executor `router` for all paths. The result of evaluation of this script will be the array of responses.
655
+
656
+
Same script using full syntax:
657
+
658
+
```json
659
+
{
660
+
"$exec": "array",
661
+
"$method": "map",
662
+
"$args": {
663
+
"data": [
664
+
"/resource/1",
665
+
"/resource/2",
666
+
"/resource/3"
667
+
],
668
+
"iterator": {
669
+
"$func": {
670
+
"$exec": "router",
671
+
"$method": "get",
672
+
"$args": { "path": { "$data": "/path" } }
673
+
},
674
+
"$args": ["path"]
675
+
}
676
+
}
677
+
}
678
+
```
679
+
680
+
Defined array methods:
681
+
682
+
| method |evaluation result|
683
+
|--------|---|
684
+
| map |new array with function call results for each item|
685
+
| filter |new array of original items for which function calls return `true`|
686
+
| every |`true` if all function calls return `true`|
687
+
| some |`true` if at least one function call returns `true`|
688
+
689
+
690
+
This script filters only positive numbers from array:
691
+
692
+
```
693
+
{
694
+
"$$array.filter": {
695
+
"data": [ -2, -1, 0, 1, 2, 3 ],
696
+
"iterator": {
697
+
"$func": {
698
+
"$>": [ { "$data": "/num" }, 0 ]
699
+
},
700
+
"$args": ["num"]
701
+
}
702
+
}
703
+
}
704
+
```
705
+
706
+
For all methods iterator function is called with 3 parameters: array item, item index and the array itself.
707
+
708
+
577
709
## Macros
578
710
579
-
JSONScript defines several core macros to support short syntax for executor and function calls and for calculations. The interpreter may allow to define your own custom macros.
711
+
JSONScript defines several core macros to support short syntax for calculations and for calling executors methods and functions. The interpreter may allow to define your own custom macros.
0 commit comments