Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
Improve readability
Browse files Browse the repository at this point in the history
  • Loading branch information
cvhariharan committed May 29, 2020
1 parent 6cfd3c5 commit e765ad2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
28 changes: 16 additions & 12 deletions language/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,28 @@ import (

var decimal = "\\b\\d+([\\.,]\\d+)?"

// FindRangeLimits finds the range for random numbers and returns a sorted integer array
func FindRangeLimits(local, entry string) ([]int, error) {
decimalsRegex := regexp.MustCompile(decimal)
limitStrArr := decimalsRegex.FindAllString(entry, 2)
limitArr := make([]int, 0)
if limitStrArr != nil {
if len(limitStrArr) != 2 {
return nil, errors.New("Need 2 numbers, a lower and upper limit")
}
for _, v := range limitStrArr {
num, err := strconv.Atoi(v)
if err != nil {
return nil, errors.New("Non integer range")
}
limitArr = append(limitArr, num)
}
} else {

if limitStrArr == nil {
return make([]int, 0), errors.New("No range")
}

if len(limitStrArr) != 2 {
return nil, errors.New("Need 2 numbers, a lower and upper limit")
}

for _, v := range limitStrArr {
num, err := strconv.Atoi(v)
if err != nil {
return nil, errors.New("Non integer range")
}
limitArr = append(limitArr, num)
}

sort.Ints(limitArr)
return limitArr, nil
}
2 changes: 2 additions & 0 deletions modules/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ func RandomNumberReplacer(locale, entry, response, _ string) (string, string) {
if limitArr != nil {
return RandomTag, fmt.Sprintf(response, strconv.Itoa(rand.Intn(100)))
}

responseTag := "no random range"
return responseTag, util.GetMessage(locale, responseTag)
}

min := limitArr[0]
max := limitArr[1]
randNum := rand.Intn((max - min)) + min
Expand Down

0 comments on commit e765ad2

Please sign in to comment.