person

package module
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 5, 2022 License: MIT Imports: 4 Imported by: 0

README

person

codecov

The person package provides methods that allow you to work with personal information.

Installation

go get github.com/antklim/person

Usage

package main

import (
	"fmt"
	"time"

	"github.com/antklim/person"
)

func main() {
	nameParts := []string{" John", "     ", "   ", "	Smith  ", "Doe"}
	fullName := person.FullName(nameParts)
	fmt.Printf("Full name: %s\n", fullName)

	dob := time.Now().AddDate(-20, 0, 0)
	age, _ := person.Age(dob, "%Y")
	fmt.Printf("Age: %s\n", age)

	adultAge := 18
	isAdult, _ := person.IsAdult(dob, adultAge)
	fmt.Printf("Is adult: %t\n", isAdult)

	// Output:
	// Full name: John Smith Doe
	// Age: 20 years
	// Is adult: true
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Age added in v0.2.0

func Age(dob time.Time, rawFormat string) (string, error)

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

func AgeOn(dob, date time.Time, rawFormat string) (string, error)

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

func FullName(parts []string) string

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

func FullNameDefault(parts []string, d string) string

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

func FullNameDefaultFormatFunc(parts []string, d string, f func(string) string) string

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

func FullNameFormatFunc(parts []string, f func(string) string) string

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

func IsAdult(dob time.Time, adultAge int) (bool, error)

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

func IsAdultOn(dob, date time.Time, adultAge int) (bool, error)

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL