Documentation
¶
Index ¶
- func Age(dob time.Time, rawFormat string) (string, error)
- func AgeOn(dob, date time.Time, rawFormat string) (string, error)
- func FullName(parts []string) string
- func FullNameDefault(parts []string, d string) string
- func FullNameDefaultFormatFunc(parts []string, d string, f func(string) string) string
- func FullNameFormatFunc(parts []string, f func(string) string) string
- func IsAdult(dob time.Time, adultAge int) (bool, error)
- func IsAdultOn(dob, date time.Time, adultAge int) (bool, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Age ¶ added in v0.2.0
Age returns persons age formatted using format. It calculates age based on provided date of birth (dob) and current date. It returns an error when the provided date of birth is in the future. For example 31 years, 2 months, 1 week, and 2 days.
func AgeOn ¶ added in v0.2.0
AgeOn returns persons age on a specific date formatted using format. It returns an error when provided date is before the date of birth (dob). For example 31 years, 2 months, 1 week, and 2 days.
Example ¶
package main
import (
"fmt"
"time"
"github.com/antklim/person"
)
func main() {
dob, _ := time.Parse("2006-01-02", "2000-01-01")
ondate, _ := time.Parse("2006-01-02", "2003-03-16")
age, _ := person.AgeOn(dob, ondate, "%Y %M %D")
fmt.Println(age)
}
Output: 3 years 2 months 15 days
func FullName ¶
FullName returns a person's full name as a result of joining parts of the name. Every name part trimmed from leading and trailing white spaces. Name part that have value of empty string is omitted from joining. Name parts joined with a single white space separator.
Example ¶
package main
import (
"fmt"
"github.com/antklim/person"
)
func main() {
nameParts := []string{" Johann", " ", " ", " Sebastian ", "Bach"}
fullName := person.FullName(nameParts)
fmt.Println(fullName)
}
Output: Johann Sebastian Bach
func FullNameDefault ¶
FullNameDefault returns a person's full name as a result of joining parts of the name. If the joining of name parts produces an empty string then default value d returned.
Example ¶
package main
import (
"fmt"
"github.com/antklim/person"
)
func main() {
nameParts := []string{" Johann", " ", " ", " Sebastian ", "Bach"}
fullName := person.FullNameDefault(nameParts, "unknown")
fmt.Println(fullName)
nameParts = []string{"", " ", " "}
fullName = person.FullNameDefault(nameParts, "unknown")
fmt.Println(fullName)
}
Output: Johann Sebastian Bach unknown
func FullNameDefaultFormatFunc ¶
FullNameDefaultFormatFunc returns a person's full name as a result of joining parts of the name. Every name part is formatted with format function f. Formatted name parts then joined. If the joining of name parts produces an empty string then default value d returned.
func FullNameFormatFunc ¶
FullNameFormatFunc returns a person's full name as a result of joining parts of the name. Every name part is formatted with format function f. Formatted name parts then joined.
Example ¶
package main
import (
"fmt"
"strings"
"github.com/antklim/person"
)
func main() {
f := func(s string) string {
s = strings.TrimSpace(s)
if s == "Sebastian" {
s = "-"
}
return s
}
nameParts := []string{" Johann", " ", " ", " Sebastian ", "Bach"}
fullName := person.FullNameFormatFunc(nameParts, f)
fmt.Println(fullName)
}
Output: Johann - Bach
func IsAdult ¶ added in v0.2.0
IsAdult returns if a person with provided date of birth is adult. Adult age parameter is the minimum amount of full year a person should have to be considered an adult. If the person's age in years is greater or equal to the adult age the function returns true. It returns an error when the provided date of birth is in the future.
Example ¶
package main
import (
"fmt"
"time"
"github.com/antklim/person"
)
func main() {
dob, _ := time.Parse("2006-01-02", "2000-01-01")
adultAge := 18
isAdult, _ := person.IsAdult(dob, adultAge)
fmt.Println(isAdult)
}
Output: true
func IsAdultOn ¶ added in v0.2.0
IsAdultOn returns if a person with provided date of birth is adult on a specific date. Adult age parameter is the minimum amount of full year a person should have to be considered an adult. If the person's age in years is greater or equal to the adult age the function returns true. It returns an error when provided date is before the date of birth (dob).
Types ¶
This section is empty.