-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a0a1bd4
Showing
5 changed files
with
246 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
|
||
Пример использования | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/pallid/fio" | ||
) | ||
|
||
func main() { | ||
s := []string{"Иванов Сергей Александрович", "Алиев Ахмед Октай оглы"} | ||
for _, str := range s { | ||
p := &fio.PartsName{} | ||
p.SetFullName(str) | ||
p.GetPartsOfFullName() | ||
p.GetLastNameAndInitials() | ||
fmt.Printf("Устанавливаемое значение: %s\n", str) | ||
fmt.Printf("Фамилия: %s\n", p.LastName) | ||
fmt.Printf("Имя: %s\n", p.FirstName) | ||
fmt.Printf("Отчество: %s\n", p.MiddleName) | ||
fmt.Printf("ФамилияИО: %s\n", p.LastNameAndInitials) | ||
} | ||
} | ||
``` | ||
|
||
``` | ||
Устанавливаемое значение: Иванов Сергей Александрович | ||
Фамилия: Иванов | ||
Имя: Сергей | ||
Отчество: Александрович | ||
ФамилияИО: Иванов С.А. | ||
Устанавливаемое значение: Алиев Ахмед Октай оглы | ||
Фамилия: Алиев | ||
Имя: Ахмед | ||
Отчество: Октай оглы | ||
ФамилияИО: Алиев А.О. | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/pallid/fio" | ||
) | ||
|
||
func main() { | ||
s := []string{"Иванов Сергей Александрович", "Алиев Ахмед Октай оглы"} | ||
for _, str := range s { | ||
p := &fio.PartsName{} | ||
p.SetFullName(str) | ||
p.GetPartsOfFullName() | ||
p.GetLastNameAndInitials() | ||
fmt.Printf("Устанавливаемое значение: %s\n", str) | ||
fmt.Printf("Фамилия: %s\n", p.LastName) | ||
fmt.Printf("Имя: %s\n", p.FirstName) | ||
fmt.Printf("Отчество: %s\n", p.MiddleName) | ||
fmt.Printf("ФамилияИО: %s\n", p.LastNameAndInitials) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package fio | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// SetFullName устанавливает полное ФИО в поле FullName | ||
func (p *PartsName) SetFullName(fullName string) { | ||
p.FullName = fullName | ||
} | ||
|
||
// GetPartsOfFullName раскладывает полное имя физического лица на составные части - фамилию, имя и отчество. | ||
// Если в конце полного имени встречаются "оглы", "улы", "уулу", "кызы" или "гызы", | ||
// то они также считаются частью отчества. | ||
// Заполняются поля LastName, FirstName, MiddleName | ||
func (p *PartsName) GetPartsOfFullName() { | ||
s := strings.Split(p.FullName, " ") | ||
if len(s) >= 1 { | ||
p.LastName = s[0] | ||
} | ||
if len(s) >= 2 { | ||
p.FirstName = s[1] | ||
} | ||
if len(s) >= 3 { | ||
p.MiddleName = s[2] | ||
} | ||
if len(s) > 3 { | ||
if _, ok := additionalParts[s[3]]; ok { | ||
m := p.MiddleName | ||
p.MiddleName = fmt.Sprintf("%s %s", m, s[3]) | ||
} | ||
} | ||
} | ||
|
||
// GetLastNameAndInitials формирует краткое представление из полного имени физического лица. | ||
// Заполняется поле LastNameAndInitials | ||
func (p *PartsName) GetLastNameAndInitials() { | ||
var lastNameAndInitials string | ||
if p.LastName == "" { | ||
p.GetPartsOfFullName() | ||
} | ||
if p.FirstName == "" { | ||
lastNameAndInitials = p.LastName | ||
} else if p.MiddleName == "" { | ||
lastNameAndInitials = fmt.Sprintf("%s %s.", p.LastName, cut(p.FirstName, 1)) | ||
} else { | ||
lastNameAndInitials = fmt.Sprintf("%s %s.%s.", p.LastName, cut(p.FirstName, 1), cut(p.MiddleName, 1)) | ||
} | ||
p.LastNameAndInitials = lastNameAndInitials | ||
} | ||
|
||
func cut(text string, limit int) string { | ||
runes := []rune(text) | ||
return string(runes[:limit]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package fio | ||
|
||
import "testing" | ||
|
||
var fioTests = []struct { | ||
input string | ||
firstName string | ||
middleName string | ||
lastName string | ||
fullName string | ||
lastNameAndInitials string | ||
}{ | ||
{ | ||
input: "Иванов Сергей Александрович", | ||
firstName: "Сергей", | ||
middleName: "Александрович", | ||
lastName: "Иванов", | ||
fullName: "Иванов Сергей Александрович", | ||
lastNameAndInitials: "Иванов С.А.", | ||
}, | ||
{ | ||
input: "Алиев Ахмед Октай оглы", | ||
firstName: "Ахмед", | ||
middleName: "Октай оглы", | ||
lastName: "Алиев", | ||
fullName: "Алиев Ахмед Октай оглы", | ||
lastNameAndInitials: "Алиев А.О.", | ||
}, | ||
{ | ||
input: "Степанов Андрей", | ||
firstName: "Андрей", | ||
middleName: "", | ||
lastName: "Степанов", | ||
fullName: "Степанов Андрей", | ||
lastNameAndInitials: "Степанов А.", | ||
}, | ||
{ | ||
input: "Афанасенко", | ||
firstName: "", | ||
middleName: "", | ||
lastName: "Афанасенко", | ||
fullName: "Афанасенко", | ||
lastNameAndInitials: "Афанасенко", | ||
}, | ||
} | ||
|
||
func TestSetFullName(t *testing.T) { | ||
for _, tt := range fioTests { | ||
p := &PartsName{} | ||
p.SetFullName(tt.input) | ||
if p.FullName != tt.fullName { | ||
t.Fatalf("SetFullName. Expected [%v], Actual [%v]", tt.fullName, p.FullName) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkSetFullName(b *testing.B) { | ||
p := &PartsName{} | ||
for i := 0; i < b.N; i++ { | ||
for _, tt := range fioTests { | ||
p.SetFullName(tt.input) | ||
} | ||
} | ||
} | ||
|
||
func TestGetPartsOfFullName(t *testing.T) { | ||
for _, tt := range fioTests { | ||
p := &PartsName{FullName: tt.input} | ||
p.GetPartsOfFullName() | ||
if p.LastName != tt.lastName { | ||
t.Fatalf("GetPartsOfFullName. Expected [%v], Actual [%v]", tt.lastName, p.LastName) | ||
} | ||
if p.FirstName != tt.firstName { | ||
t.Fatalf("GetPartsOfFullName. Expected [%v], Actual [%v]", tt.firstName, p.FirstName) | ||
} | ||
if p.MiddleName != tt.middleName { | ||
t.Fatalf("GetPartsOfFullName. Expected [%v], Actual [%v]", tt.middleName, p.MiddleName) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkGetPartsOfFullName(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tt := range fioTests { | ||
p := &PartsName{FullName: tt.input} | ||
p.GetPartsOfFullName() | ||
} | ||
} | ||
} | ||
|
||
func TestGetLastNameAndInitials(t *testing.T) { | ||
for _, tt := range fioTests { | ||
p := &PartsName{FullName: tt.input} | ||
p.GetLastNameAndInitials() | ||
if p.LastNameAndInitials != tt.lastNameAndInitials { | ||
t.Fatalf("GetLastNameAndInitials. Expected [%v], Actual [%v]", tt.lastNameAndInitials, p.LastNameAndInitials) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkGetLastNameAndInitials(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tt := range fioTests { | ||
p := &PartsName{FullName: tt.input} | ||
p.GetLastNameAndInitials() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package fio | ||
|
||
// PartsName ... | ||
type PartsName struct { | ||
FullName string //Полное ФИО, Фамилия Имя Отчество | ||
LastName string //Фамилия | ||
FirstName string //Имя | ||
MiddleName string //Отчество | ||
LastNameAndInitials string //Фамилия и инициалы, Фамилия И.О. | ||
} | ||
|
||
// additionalParts дополнительные части отчества | ||
var additionalParts = map[string]bool{ | ||
"оглы": true, | ||
"улы": true, | ||
"уулу": true, | ||
"кызы": true, | ||
"гызы": true, | ||
"угли": true, | ||
} |