-
Notifications
You must be signed in to change notification settings - Fork 10
/
helpers.go
87 lines (75 loc) · 2.13 KB
/
helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package gedcom
import (
"strings"
)
type ParsedName struct {
Full string
Given string
Surname string
Suffix string
Nickname string
}
// SplitPersonalName parses a name in the format "First Name /Surname/ suffix" into its components.
func SplitPersonalName(name string) ParsedName {
name = strings.TrimSpace(name)
parts := strings.Split(name, "/")
if len(parts) == 1 {
return ParsedName{
Full: name,
Given: name,
}
}
// Find a part that was delimited by slashes with no whitespace after the leading slash or before the following slash
// That part is treated as the surname, anything before that part is treated as the given name, anything after is assumed to
// be a suffix.
for i := 1; i < len(parts); i++ {
p := parts[i]
if len(p) == 0 || p[0] == ' ' || p[len(p)-1] == ' ' {
continue
}
pn := ParsedName{
Given: strings.TrimSpace(strings.Join(parts[:i], "/")),
Surname: parts[i],
Suffix: strings.TrimSpace(strings.Join(parts[i+1:], "/")),
}
// Check for a nickname, which is usually in quotes after the given name
if strings.HasSuffix(pn.Given, `"`) {
pos := strings.Index(pn.Given, `"`)
if pos != -1 && pos < len(pn.Given)-2 {
pn.Nickname = strings.TrimSpace(pn.Given[pos+1 : len(pn.Given)-1])
pn.Given = strings.TrimSpace(pn.Given[:pos])
}
}
// See if there is a following part that could be part of the surname.
// Some surnames may have alternatives: smith/smyth
for j := i + 1; j < len(parts); j++ {
p := parts[j]
if len(p) == 0 || p[0] == ' ' || p[len(p)-1] == ' ' {
// This part can't be part of the surname
break
}
// Append this part to the surname and recalculate the suffix
pn.Surname += "/" + p
pn.Suffix = strings.TrimSpace(strings.Join(parts[j+1:], "/"))
}
pn.Full = pn.Given
if len(pn.Surname) > 0 {
if len(pn.Full) > 0 {
pn.Full += " "
}
pn.Full += pn.Surname
}
if len(pn.Suffix) > 0 {
if len(pn.Full) > 0 {
pn.Full += " "
}
pn.Full += pn.Suffix
}
return pn
}
// Could not find a surname
return ParsedName{
Full: strings.TrimRight(name, "/ "),
Given: strings.TrimRight(name, "/ "),
}
}