summaryrefslogtreecommitdiff
path: root/identity/identity.go
blob: e1cd74df1558cfe783eb301f8a8d6719fa000071 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package identity

import "strings"

type Identity struct {
	Full    string
	Reduced string
}

// I return whether the other identity "means the same" as this one
func (i Identity) I(other Identity) bool {
	return i.Reduced == other.Reduced
}

// Is returns whether the other string "means the same" as this one
func (i Identity) Is(input string) bool {
	reduced := Reduce(input)
	return i.Reduced == reduced
}

// New creates a new Identity with default reduction
func New(input string) Identity {
	reduced := Reduce(input)
	return Identity{
		Full:    input,
		Reduced: reduced,
	}

}

// Reduce normalizes input (commands and filenames) to simplify them
func Reduce(input string) string {
	reduced := input
	reduced = strings.TrimPrefix(input, ".")
	if !strings.HasPrefix(reduced, ".") {
		reduced = strings.Split(reduced, ".")[0]
	}
	reduced = strings.ToLower(reduced)
	return reduced

}