refactor: strip word and from duration (#5412)

Signed-off-by: James Elliott <james-d-elliott@users.noreply.github.com>
pull/5411/head^2
James Elliott 2023-05-08 15:57:11 +10:00 committed by GitHub
parent 41afaa5cc2
commit 998ffe5255
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 7 deletions

View File

@ -28,11 +28,14 @@ If you supply an integer, it is considered a representation of seconds. If you s
blocks of quantities and units (number followed by a unit letter). For example `5h` indicates a quantity of 5 units
of `h`.
The following is ignored:
The following is ignored or stripped from the input:
- all spaces
- leading zeros
- the word `and`
While you can use multiple of these blocks in combination, we suggest keeping it simple and use a single value.
While you can use multiple of these blocks in combination, we suggest keeping it simple and use a single value. In
addition it's important to note that the format while somewhat human readable still requires you closely follow the
expected formats.
#### Unit Legend
@ -52,11 +55,11 @@ v4.38.0 or newer.
#### Examples
| Desired Value | Configuration Examples |
|:---------------------:|:-------------------------------------:|
| 1 hour and 30 minutes | `90m` or `1h30m` or `5400` or `5400s` |
| 1 day | `1d` or `24h` or `86400` or `86400s` |
| 10 hours | `10h` or `600m` or `9h60m` or `36000` |
| Desired Value | Configuration Examples (Short) | Configuration Examples (Long) |
|:---------------------:|:-------------------------------------:|:---------------------------------------:|
| 1 hour and 30 minutes | `90m` or `1h30m` or `5400` or `5400s` | `1 hour and 30 mninutes` |
| 1 day | `1d` or `24h` or `86400` or `86400s` | `1 day` |
| 10 hours | `10h` or `600m` or `9h60m` or `36000` | `10 hours` |
### Address

View File

@ -13,6 +13,8 @@ func StandardizeDurationString(input string) (output string, err error) {
return "0s", nil
}
input = strings.ReplaceAll(input, "and", "")
matches := reDurationStandard.FindAllStringSubmatch(strings.ReplaceAll(input, " ", ""), -1)
if len(matches) == 0 {

View File

@ -109,6 +109,23 @@ func TestParseDurationString(t *testing.T) {
}
}
func TestStandardizeDurationString(t *testing.T) {
var (
actual string
err error
)
actual, err = StandardizeDurationString("1 hour and 20 minutes")
assert.NoError(t, err)
assert.Equal(t, "1h20m", actual)
actual, err = StandardizeDurationString("1 hour and 20 minutes")
assert.NoError(t, err)
assert.Equal(t, "1h20m", actual)
}
func TestParseDurationString_ShouldNotParseDurationStringWithOutOfOrderQuantitiesAndUnits(t *testing.T) {
duration, err := ParseDurationString("h1")