]> Sergey Matveev's repositories - path-extractor.git/commitdiff
Match logo@2x.png but not email adress
authorEdgar HIPP <hipp.edg@gmail.com>
Sun, 30 Aug 2015 15:06:52 +0000 (17:06 +0200)
committerEdgar HIPP <hipp.edg@gmail.com>
Sun, 30 Aug 2015 15:06:52 +0000 (17:06 +0200)
extractor.go
pe_test.go
validators.go

index 0b036640ede67bd1bedea8b92f21d1cdf47e96f8..7d13650c008f6cc2b3907d23249ed5d58b8ddaec 100644 (file)
@@ -17,7 +17,7 @@ func GetAllMatches(input string) []string {
        matches = pathExtractor(input)
        for _, match := range matches {
                s = string(match[1])
-               if isDate(s) || isVersion(s) || isGitRange(s) || isGitInstruction(s) || containsInvalidString(s) || len(s) <= 2 {
+               if isEmail(s) || isDate(s) || isVersion(s) || isGitRange(s) || isGitInstruction(s) || containsInvalidString(s) || len(s) <= 2 {
                        continue
                }
                if isGitPath(s) {
index cc8ef4d98ffa172a79e2e247fa28c5fb4b8f901c..c10655aac70d99aa3af10813c3c04dea01475914 100644 (file)
@@ -18,6 +18,11 @@ func TestGitIgnore(t *testing.T) {
                t.Errorf("Matches email adresses", output[0])
        }
 
+       output = GetAllMatches(" logo@2x.png ")
+       if len(output) == 0 {
+               t.Errorf("Doesn't match retina asset", output[0])
+       }
+
        output = GetAllMatches("and/or")
        if len(output) != 0 {
                t.Errorf("Matches and/or adresses", output[0])
index cb09c4035ccb8b00c72bfce3e53a8f7e759e3525..bef5f6551fb36c07987424ab8006169af4d58d01 100644 (file)
@@ -5,6 +5,15 @@ import (
        "strings"
 )
 
+func stringInSlice(str string, list []string) bool {
+       for _, v := range list {
+               if v == str {
+                       return true
+               }
+       }
+       return false
+}
+
 func isGitRange(input string) bool {
        r := regexp.MustCompile("[0-9a-f]{3,}\\.\\.[0-9a-f]{3,}")
        return r.Match([]byte(input))
@@ -15,6 +24,16 @@ func isGitPath(input string) bool {
        return r.Match([]byte(input))
 }
 
+func isEmail(input string) bool {
+       r := regexp.MustCompile("[a-zA-Z0-9._%+-]+@(?:[a-zA-Z0-9-]+.)+([a-zA-Z]{2,4})")
+       result := r.FindSubmatch([]byte(input))
+       if result == nil {
+               return false
+       }
+       fileExtensions := []string{"png", "bmp", "jpeg"}
+       return !stringInSlice(string(result[1]), fileExtensions)
+}
+
 func isDate(input string) bool {
        r := regexp.MustCompile("^[0-9]+/[0-9]+/[0-9]+")
        return r.Match([]byte(input))
@@ -38,7 +57,7 @@ func isVersion(input string) bool {
 }
 
 func containsInvalidString(input string) bool {
-       invalidStrings := []string{"(", ")", "@", "and/or", "origin/", "{", "}", "<", ">", "$", "*"}
+       invalidStrings := []string{"(", ")", "and/or", "origin/", "{", "}", "<", ">", "$", "*"}
        for _, s := range invalidStrings {
                if strings.Contains(input, s) {
                        return true