Skip to content

Commit

Permalink
Add vehicle provider
Browse files Browse the repository at this point in the history
  • Loading branch information
sepisoltani committed Jun 2, 2024
1 parent d80466d commit 59d4ff8
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 3 deletions.
3 changes: 0 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ linters:
- unused
- gosec
- stylecheck
- structcheck
- varcheck
- deadcode
- typecheck
- gocritic
- gofmt
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@ var digit = faker.Digit.Digit()
var billType = faker.Bill.BillType()
```

### Vehicle Provider

* Generate a random car plate number:

```go
var carPlateNumber = faker.Vehicle.CarPlateNumber()
```

## Contributing

We welcome contributions to improve the package. If you have suggestions or improvements, please fork the repository and
Expand Down
28 changes: 28 additions & 0 deletions providers/vehicle/vehicle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package vehicle

import (
"math/rand"
"time"
)

var rng = rand.New(rand.NewSource(time.Now().UnixNano()))

// CarPlateNumber generates a random Persian car plate number.
func CarPlateNumber() string {
letterRunes := []rune("الفبپتثجچحخدذرزژسشصضطظعغفقکگلمنوهی")
numbers := make([]rune, 0, 10)
for i := '۰'; i <= '۹'; i++ {
numbers = append(numbers, i)
}

plate := make([]rune, 0, 8)
plate = append(plate, numbers[rng.Intn(len(numbers))], numbers[rng.Intn(len(numbers))])
plate = append(plate, ' ')
plate = append(plate, letterRunes[rng.Intn(len(letterRunes))])
plate = append(plate, ' ')
plate = append(plate, numbers[rng.Intn(len(numbers))], numbers[rng.Intn(len(numbers))], numbers[rng.Intn(len(numbers))])
plate = append(plate, ' ')
plate = append(plate, numbers[rng.Intn(len(numbers))], numbers[rng.Intn(len(numbers))])

return string(plate)
}
26 changes: 26 additions & 0 deletions providers/vehicle/vehicle_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package vehicle

import (
"regexp"
"testing"
)

func TestCarPlateNumber(t *testing.T) {
plate := CarPlateNumber()
t.Logf("Generated Car Plate: %s", plate)

// Persian numbers and letters
numbers := "۰۱۲۳۴۵۶۷۸۹"
letters := "الفبپتثجچحخدذرزژسشصضطظعغفقکگلمنوهی"

// Regex to match the format: "DD L DDD DD"
// D: Persian digit, L: Persian letter
plateRegex := `^[` + numbers + `]{2} [` + letters + `] [` + numbers + `]{3} [` + numbers + `]{2}$`

// Compile the regex
re := regexp.MustCompile(plateRegex)

if !re.MatchString(plate) {
t.Errorf("Car plate does not match expected format: %s", plate)
}
}

0 comments on commit 59d4ff8

Please sign in to comment.