Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more expressions #179

Merged
merged 9 commits into from
Jun 29, 2023
Merged
2 changes: 1 addition & 1 deletion assets/datasets/expression_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"anyOf": [
{
"type": "string",
"enum": ["COALESCE", "JOIN", "CONCAT", "COUPLE"]
"enum": ["COALESCE", "JOIN", "CONCAT", "COUPLE", "PAD", "INSERT", "REPLACE"]
},
{
"type": "string",
Expand Down
106 changes: 89 additions & 17 deletions assets/datasets/question_catalog.json
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,13 @@
"unit": "Centimeter"
},
"constructor": {
"height": ["CONCAT", "$input", " cm"]
"height": [
"REPLACE", "/\\.?0{1,2}$/", "", [
"INSERT", ".", "-2", [
"PAD", "0", "3", "$input"
]
]
]
}
},
"conditions": [
Expand Down Expand Up @@ -830,13 +836,13 @@
"type": "Number",
"input": {
"placeholder": "Plattform-Breite",
"decimals": 0,
"min": 30,
"max": 1500,
"unit": "Centimeter"
"decimals": 2,
"min": 0,
"max": 15,
"unit": "Meter"
},
"constructor": {
"width": ["CONCAT", "$input", " cm"]
"width": ["$input"]
}
},
"conditions": [
Expand Down Expand Up @@ -954,7 +960,13 @@
"unit": "Centimeter"
},
"constructor": {
"wheelchair:portable_ramp:length": ["CONCAT", "$input", " cm"]
"wheelchair:portable_ramp:length": [
"REPLACE", "/\\.?0{1,2}$/", "", [
"INSERT", ".", "-2", [
"PAD", "0", "3", "$input"
]
]
]
}
},
"conditions": [
Expand Down Expand Up @@ -1058,7 +1070,13 @@
"unit": "Centimeter"
},
"constructor": {
"wheelchair:portable_lift:length": ["CONCAT", "$input", " cm"]
"wheelchair:portable_lift:length": [
"REPLACE", "/\\.?0{1,2}$/", "", [
"INSERT", ".", "-2", [
"PAD", "0", "3", "$input"
]
]
]
}
},
"conditions": [
Expand Down Expand Up @@ -1440,7 +1458,13 @@
"unit": "Centimeter"
},
"constructor": {
"width": ["CONCAT", "$input", " cm"]
"width": [
"REPLACE", "/\\.?0{1,2}$/", "", [
"INSERT", ".", "-2", [
"PAD", "0", "3", "$input"
]
]
]
}
},
"conditions": [
Expand Down Expand Up @@ -1670,7 +1694,13 @@
"unit": "Centimeter"
},
"constructor": {
"maxwidth:physical": ["CONCAT", "$input", " cm"]
"maxwidth:physical": [
"REPLACE", "/\\.?0{1,2}$/", "", [
"INSERT", ".", "-2", [
"PAD", "0", "3", "$input"
]
]
]
}
},
"conditions": [
Expand Down Expand Up @@ -1707,7 +1737,13 @@
"unit": "Centimeter"
},
"constructor": {
"opening": ["CONCAT", "$input", " cm"]
"opening": [
"REPLACE", "/\\.?0{1,2}$/", "", [
"INSERT", ".", "-2", [
"PAD", "0", "3", "$input"
]
]
]
}
},
"conditions": [
Expand Down Expand Up @@ -1744,7 +1780,13 @@
"unit": "Centimeter"
},
"constructor": {
"spacing": ["CONCAT", "$input", " cm"]
"spacing": [
"REPLACE", "/\\.?0{1,2}$/", "", [
"INSERT", ".", "-2", [
"PAD", "0", "3", "$input"
]
]
]
}
},
"conditions": [
Expand Down Expand Up @@ -1815,7 +1857,13 @@
"unit": "Centimeter"
},
"constructor": {
"wheelchair:lift:length": ["CONCAT", "$input", " cm"]
"wheelchair:lift:length": [
"REPLACE", "/\\.?0{1,2}$/", "", [
"INSERT", ".", "-2", [
"PAD", "0", "3", "$input"
]
]
]
}
},
"conditions": [
Expand Down Expand Up @@ -1847,7 +1895,13 @@
"unit": "Centimeter"
},
"constructor": {
"length": ["CONCAT", "$input", " cm"]
"length": [
"REPLACE", "/\\.?0{1,2}$/", "", [
"INSERT", ".", "-2", [
"PAD", "0", "3", "$input"
]
]
]
}
},
"conditions": [
Expand Down Expand Up @@ -1880,7 +1934,13 @@
"unit": "Centimeter"
},
"constructor": {
"wheelchair:lift:width": ["CONCAT", "$input", " cm"]
"wheelchair:lift:width": [
"REPLACE", "/\\.?0{1,2}$/", "", [
"INSERT", ".", "-2", [
"PAD", "0", "3", "$input"
]
]
]
}
},
"conditions": [
Expand Down Expand Up @@ -1912,7 +1972,13 @@
"unit": "Centimeter"
},
"constructor": {
"width": ["CONCAT", "$input", " cm"]
"width": [
"REPLACE", "/\\.?0{1,2}$/", "", [
"INSERT", ".", "-2", [
"PAD", "0", "3", "$input"
]
]
]
}
},
"conditions": [
Expand Down Expand Up @@ -2004,7 +2070,13 @@
"unit": "Centimeter"
},
"constructor": {
"step:height": ["CONCAT", "$input", " cm"]
"step:height": [
"REPLACE", "/\\.?0{1,2}$/", "", [
"INSERT", ".", "-2", [
"PAD", "0", "3", "$input"
]
]
]
}
},
"conditions": [
Expand Down
92 changes: 92 additions & 0 deletions docs/QUESTION_CATALOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,62 @@ output: `operator=A_suffix`
constructor: `"operator": ["COUPLE", "$input", "_suffix"]`
output: `operator=NULL`

#### `INSERT` expression

Inserts one String into another String at a certain position.

First argument represents the insertion String.
Second argument specifies the position/index where the String should be inserted into the target String. Negative positions are treated as insertions starting at the end of the String. So -1 means insert before the last character of the target String. If the index exceeds the length of the target String, it will be returned without any modifications.
Third argument resembles the target String.

**Examples:**
- input: `[tag_value]`
constructor: `"operator": ["INSERT", "X", "1", "$input"]`
output: `operator=tXag_value`
- input: `[tag_value]`
constructor: `"operator": ["INSERT", "X", "-5", "$input"]`
output: `operator=tag_Xvalue`
- input: `[tag_value]`
constructor: `"operator": ["INSERT", "X", "20", "$input"]`
output: `operator=tag_value`

#### `PAD` expression

Adds a given String to a target String for each time the target String length is less than a given width.

First argument represents the padding String.
Second argument specifies the desired width. Positive values will prepend, negative values will append to the target String. Remember that the final String length may be greater than the desired width when the padding String contains more than one character.
Third argument resembles the target String.

**Examples:**
- input: `[1]`
constructor: `"operator": ["PAD", "0", "3", "$input"]`
output: `operator=001`
- input: `[1]`
constructor: `"operator": ["PAD", "0", "-3", "$input"]`
output: `operator=100`
- input: `[value]`
constructor: `"operator": ["PAD", "XXX", "9", "$input"]`
output: `operator=XXXXXXvalue`

#### `REPLACE` expression

Replaces a given Pattern (either String or RegExp) in a target String by a given replacement String.
RegExp are denoted by a `/` at the start and end of the String.
First argument represents the Pattern the target String should be matched against.
Second argument defines the replacement String.
Third argument resembles the target String.

**Examples:**
- input: `[sometimes]`
constructor: `"operator": ["REPLACE", "times", "thing", "$input"]`
output: `operator=something`
- input: `[sometimes]`
constructor: `"operator": ["REPLACE", "e", "#", "$input"]`
output: `operator=som#tim#s`
- input: `[value]`
constructor: `"operator": ["REPLACE", "/^.|.$/", "_", "$input"]`
output: `operator=_alu_`

### Answer examples

Expand Down Expand Up @@ -335,6 +391,42 @@ The example will write the three tags *bus*, *tram* & *train*. For unselected op
}
```

#### Using expressions to convert centimeters to meters
The example makes use of the 3 expressions PAD, INSERT and REPLACE to convert from centimeters to meters. The REPLACE expression is only used to remove any pending zeros (and potentially the decimal point). Note that this expression combination only works for positive integers (not for negative or decimal numbers) and does a conversion by exactly two decimal places to the left.

**Explanation:** The expressions evaluate from the inner most to the outer most as shown in the table below.

| $input | PAD | INSERT | REPLACE |
| ------ | ----- | ------ | ------- |
| 1 | 001 | 0.01 | 0.01 |
| 1000 | 1000 | 10.00 | 10 |
| 12 | 012 | 0.12 | 0.12 |
| 0 | 000 | 0.00 | 0 |
| 102 | 102 | 1.02 | 1.02 |
| 120 | 120 | 1.20 | 1.2 |


```jsonc
"answer": {
"type": "Number",
"input": {
"placeholder": "Step height",
"decimals": 0,
"min": 0,
"max": 40,
"unit": "Centimeter"
},
"constructor": {
"height": [
"REPLACE", "/\\.?0{1,2}$/", "", [
"INSERT", ".", "-2", [
"PAD", "0", "3", "$input"
]
]
]
}
}
```


## The `conditions` part
Expand Down
Loading