Skip to content

Commit

Permalink
Merge pull request #10 from Teamwork/unique-list
Browse files Browse the repository at this point in the history
On listing of addresses, return unique slice/list
  • Loading branch information
ready4god2513 authored Oct 15, 2021
2 parents aad6afe + adb8120 commit 67b2155
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 12 deletions.
53 changes: 43 additions & 10 deletions list.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (l *List) UnmarshalJSON(data []byte) error {
var alias Alias
err := json.Unmarshal(data, &alias)
if err == nil {
*l = List(alias)
*l = List(alias).uniq()
return nil
}

Expand All @@ -47,34 +47,67 @@ func (l *List) UnmarshalJSON(data []byte) error {
}

*l, _ = ParseList(str)
*l = l.uniq()
return nil
}

*l = FromSlice(slice)
*l = FromSlice(slice).uniq()
return nil
}

// StringEncoded makes a string that *is* RFC 2047 encoded.
// uniq returns only the unique addresses from a list. Order is preserved
func (l List) uniq() List {
a := List{}
for _, addr := range l {
found := false
for _, add := range a {
if strings.EqualFold(add.Address, addr.Address) {
found = true
break
}
}

if found {
continue
}

a = append(a, addr)
}

return a
}

// StringEncoded makes a string that *is* RFC 2047 encoded. Duplicates are ignored.
func (l List) StringEncoded() string {
var out []string
for _, a := range l {
out = append(out, a.StringEncoded())
val := a.StringEncoded()
out = append(out, val)
}
return strings.Join(out, ", ")
}

// Append adds a new Address to the list.
// Append adds a new Address to the list. If the address already exists in the
// list this will be a noop.
func (l *List) Append(name, address string) {
*l = append(*l, New(name, address))
e := New(name, address)

for _, addr := range *l {
if strings.EqualFold(addr.Address, e.Address) {
return
}
}

*l = append(*l, e)
}

// Slice gets all valid addresses in a []string slice. The names are lost and
// invalid addresses are skipped.
// invalid addresses are skipped. Duplicates are ignored.
func (l List) Slice() []string {
mails := []string{}
for _, m := range l {
if m.Valid() {
mails = append(mails, m.Address)
for _, e := range l {
if e.Valid() {
mails = append(mails, e.Address)
}
}
return mails
Expand Down
7 changes: 6 additions & 1 deletion mailaddress.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ package mailaddress

// ParseList will parse one or more addresses.
func ParseList(str string) (l List, haveError bool) {
return parse(str)
l, haveError = parse(str)
if haveError {
return l, haveError
}

return l.uniq(), false
}

// Parse will parse exactly one address. More than one addresses is an error,
Expand Down
2 changes: 1 addition & 1 deletion parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func TestParseListAddress(t *testing.T) {
newTest += ", " + pick
newExpected = append(newExpected, validAddresses[pick])
}
mixed[newTest] = newExpected
mixed[newTest] = List(newExpected).uniq()
}

for test, expected := range mixed {
Expand Down

0 comments on commit 67b2155

Please sign in to comment.