From 59d4ff8372d089ca714df0cc0089901d26bd4579 Mon Sep 17 00:00:00 2001 From: Sepehr Date: Sun, 2 Jun 2024 20:27:44 +0200 Subject: [PATCH] Add vehicle provider --- .golangci.yml | 3 --- README.md | 8 ++++++++ providers/vehicle/vehicle.go | 28 ++++++++++++++++++++++++++++ providers/vehicle/vehicle_test.go | 26 ++++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 providers/vehicle/vehicle.go create mode 100644 providers/vehicle/vehicle_test.go diff --git a/.golangci.yml b/.golangci.yml index c1362ad..91c34bf 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -9,9 +9,6 @@ linters: - unused - gosec - stylecheck - - structcheck - - varcheck - - deadcode - typecheck - gocritic - gofmt diff --git a/README.md b/README.md index 3304cf3..8757486 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/providers/vehicle/vehicle.go b/providers/vehicle/vehicle.go new file mode 100644 index 0000000..1bd2016 --- /dev/null +++ b/providers/vehicle/vehicle.go @@ -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) +} diff --git a/providers/vehicle/vehicle_test.go b/providers/vehicle/vehicle_test.go new file mode 100644 index 0000000..2375aff --- /dev/null +++ b/providers/vehicle/vehicle_test.go @@ -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) + } +}