github.com/seeker-insurance/kit@v0.0.13/address/address.go (about)

     1  //Package address contains tools for working with and comparing street addresses.
     2  package address
     3  
     4  import (
     5  	"strings"
     6  
     7  	"github.com/seeker-insurance/kit/stringslice"
     8  )
     9  
    10  //Address represents a physical location
    11  type Address struct {
    12  	Street     string `json:"street,omitempty"`
    13  	Extension  string `json:"extension,omitempty"`
    14  	POBox      string `json:"po_box,omitempty"`
    15  	Locality   string `json:"locality,omitempty"`
    16  	Region     string `json:"region,omitempty"`
    17  	PostalCode string `json:"post_code,omitempty"`
    18  	Country    string `json:"country,omitempty"`
    19  }
    20  
    21  //filterOutComponentsMissingFromReciever returns a copy of b, except that where a.component == "", b.component == ""
    22  func (a *Address) filterOutComponentsMissingFromReciever(b Address) Address {
    23  	if a.Street == "" {
    24  		b.Street = ""
    25  	}
    26  	if a.Extension == "" {
    27  		b.Extension = ""
    28  	}
    29  	if a.POBox == "" {
    30  		b.POBox = ""
    31  	}
    32  	if a.Locality == "" {
    33  		b.Locality = ""
    34  	}
    35  	if a.Region == "" {
    36  		b.Region = ""
    37  	}
    38  	if a.PostalCode == "" {
    39  		b.PostalCode = ""
    40  	}
    41  	if a.Country == "" {
    42  		b.Country = ""
    43  	}
    44  	return b
    45  }
    46  
    47  //SharedComponents returns copies of a and b, except if a.component == "" || b.component == "",
    48  //c.component == "" && d.component == ""
    49  func SharedComponents(a, b Address) (Address, Address) {
    50  	b = a.filterOutComponentsMissingFromReciever(b)
    51  	a = b.filterOutComponentsMissingFromReciever(a)
    52  	return a, b
    53  }
    54  
    55  //StringSliceFromNonempty returns a stringslice of the non-empty components of a
    56  func (a *Address) StringSliceFromNonempty() []string {
    57  	return stringslice.AppendIfNonEmpty(make([]string, 0, 7),
    58  		a.Street, a.Extension, a.POBox, a.Locality, a.Region, a.PostalCode, a.Country)
    59  }
    60  
    61  //Array turns an address into a [7]string
    62  func (a *Address) Array() [7]string {
    63  	return [7]string{0: a.Street, 1: a.Extension, 2: a.POBox,
    64  		3: a.Locality, 4: a.Region, 5: a.PostalCode, 6: a.Country}
    65  }
    66  
    67  //NonEmptyComponents is the number of nonempty components of a; that is, an address with only a steet has 1,
    68  //street and locality is 2, ...
    69  func (a *Address) NonEmptyComponents() int {
    70  	return countNonEmpty(a.Street, a.Extension, a.POBox,
    71  		a.Locality, a.Region, a.PostalCode, a.Country)
    72  }
    73  
    74  func countNonEmpty(a ...string) int {
    75  	n := 0
    76  	for _, s := range a {
    77  		if s != "" {
    78  			n++
    79  		}
    80  	}
    81  	return n
    82  }
    83  
    84  //StringSlice turns an address into a []string
    85  func (a *Address) StringSlice() []string {
    86  	return []string{0: a.Street, 1: a.Extension, 2: a.POBox,
    87  		3: a.Locality, 4: a.Region, 5: a.PostalCode, 6: a.Country}
    88  }
    89  
    90  //Empty returns true if all components of the address are ""
    91  func (a *Address) Empty() bool {
    92  	isEmpty := func(s string) bool {
    93  		return s == ""
    94  	}
    95  	return stringslice.All(a.StringSlice(), isEmpty)
    96  
    97  }
    98  func (a *Address) String() string {
    99  	components := []string{
   100  		a.Street, a.Extension, a.POBox,
   101  		a.Locality, a.Region, a.PostalCode, a.Country,
   102  	}
   103  	return strings.Join(stringslice.NonEmpty(components), ", ")
   104  }