Skip to content

Commit

Permalink
Add urns.New
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed May 7, 2024
1 parent e8695b3 commit b4340ad
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
3 changes: 2 additions & 1 deletion urns/phone.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ func parsePhoneOrShortcode(raw string, country i18n.Country) (string, error) {
return "", errors.New("unable to parse phone number or shortcode")
}

// ToLocalPhone converts a phone URN to a local phone number.. without any leading zeros
// ToLocalPhone converts a phone URN to a local phone number.. without any leading zeros. Kinda weird but used by
// Courier where channels want the number in that format.
func ToLocalPhone(u URN, country i18n.Country) string {
_, path, _, _ := u.ToParts()

Expand Down
7 changes: 6 additions & 1 deletion urns/urns.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func newFromParts(scheme, path, query, display string) URN {
}

// NewFromParts returns a validated URN for the given scheme, path, query and display
func NewFromParts(typ *Scheme, path string, query string, display string) (URN, error) {
func NewFromParts(typ *Scheme, path, query, display string) (URN, error) {
urn := newFromParts(typ.Prefix, path, query, display)

if err := urn.Validate(); err != nil {
Expand All @@ -49,6 +49,11 @@ func NewFromParts(typ *Scheme, path string, query string, display string) (URN,
return urn, nil
}

// New returns a validated URN for the given scheme and path
func New(typ *Scheme, path string) (URN, error) {
return NewFromParts(typ, path, "", "")
}

// Parse parses a URN from the given string. The returned URN is only guaranteed to be structurally valid.
func Parse(s string) (URN, error) {
parsed, err := parseURN(s)
Expand Down
5 changes: 5 additions & 0 deletions urns/urns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ func TestNewFromParts(t *testing.T) {
assert.NoError(t, err, "unexpected error for: %s, %s, %s", tc.scheme, tc.path, tc.display)
}
}

// test New shortcut
urn, err := urns.New(urns.Phone, "+250788383383")
assert.NoError(t, err)
assert.Equal(t, "tel:+250788383383", urn.String())
}

func TestNormalize(t *testing.T) {
Expand Down

0 comments on commit b4340ad

Please sign in to comment.