Skip to content

Commit

Permalink
Fix var names
Browse files Browse the repository at this point in the history
  • Loading branch information
goloop committed Jul 2, 2023
1 parent d024843 commit e83145f
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 2 deletions.
4 changes: 2 additions & 2 deletions bank_iban.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ import (
"strings"
)

var ibanPatterns = map[string]int{
var ibanLenPatterns = map[string]int{
"AL": 28, "AD": 24, "AZ": 28, "BH": 22, "BR": 29, "BG": 22, "CY": 28,
"DO": 28, "EG": 29, "GE": 22, "GI": 23, "GR": 27, "GT": 28, "JO": 30,
"KZ": 20, "KW": 30, "LV": 21, "LB": 28, "LT": 20, "LU": 20, "MT": 31,
Expand Down Expand Up @@ -165,7 +165,7 @@ func Iban(iban string) bool {
iban = strings.ToUpper(strings.Replace(iban, " ", "", -1))

// Check if the length matches the expected length for the country
if l, ok := ibanPatterns[iban[0:2]]; !ok || l != len(iban) {
if l, ok := ibanLenPatterns[iban[0:2]]; !ok || l != len(iban) {
return false
}

Expand Down
57 changes: 57 additions & 0 deletions hash.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package is

import (
"fmt"
"regexp"

"github.com/goloop/g"
)

var (
// The base64StrictModeRegex is a regex pattern used to validate
// standard Base64 encoded strings.
// It matches strings that:
// - consist of sequences of 4 valid Base64 characters (A-Za-z0-9+/),
// repeated 0 or more times.
// - optionally, end with a sequence of 2 valid Base64 characters
// followed by '==', or 3 valid Base64 characters followed by '=',
// or 4 valid Base64 characters.
base64StrictModeRegex = regexp.MustCompile(
`^(?:[A-Za-z0-9+\\/]{4})*(?:[A-Za-z0-9+\\/]{2}==|` +
`[A-Za-z0-9+\\/]{3}=|[A-Za-z0-9+\\/]{4})$`,
)

// The base64SafeModeRegex is similar to base64Regex, but for URL-safe
// Base64 encoding. The '+' and '/' characters are replaced with '-'
// and '_' respectively. And can be missing paddings (== or =).
base64SafeModeRegex = regexp.MustCompile(
`^(?:[A-Za-z0-9-_]{4})*(?:[A-Za-z0-9-_]{2}|` +
`[A-Za-z0-9-_]{3}|[A-Za-z0-9-_]{4})$`,
)
)

// Base64 validates whether a given string is encoded in Base64.
// By default, the function checks for strict compliance with
// the base64 format. The second argument to the function can be
// as false to check that the base64 URL is safe.
//
// Example usage:
//
// // Strict mode.
// is.Base64("SGVsbG8sIHdvcmxkIQ==") // Returns: true
// is.Base64("SGVsbG8sIHdvcmxkIQ==", true) // Returns: true
// is.Base64("SGVsbG8sIHdvcmxkIQ") // Returns: false
// is.Base64("SGVsbG8sIHdvcmxkIQ", true) // Returns: false
//
// // Safe mode.
// is.Base64("SGVsbG8sIHdvcmxkIQ", false) // Returns: true
// is.Base64("SGVsbG8sIHdvcmxkIQ==", false) // Returns: true
func Base64(v string, strict ...bool) bool {
// Default mode is a strict.
if l := len(strict); l == 0 || l != 0 && g.All(strict...) {
return base64StrictModeRegex.MatchString(v)
}

fmt.Println("Safe")
return base64SafeModeRegex.MatchString(v)
}
66 changes: 66 additions & 0 deletions hash_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package is

import "testing"

// TestBase64 tests Base64 function.
func TestBase64(t *testing.T) {
tests := []struct {
name string
input string
strict []bool
want bool
}{
/*{
name: "Valid standard base64",
input: "SGVsbG8sIHdvcmxkIQ==",
strict: []bool{},
want: true,
},
{
name: "Valid standard base64 with strict mode",
input: "SGVsbG8sIHdvcmxkIQ==",
strict: []bool{true},
want: true,
},
{
name: "Valid standard base64 with strict mode as many params",
input: "SGVsbG8sIHdvcmxkIQ==",
strict: []bool{true, true},
want: true,
},
{
name: "Valid standard base64 with safe mode",
input: "SGVsbG8sIHdvcmxkIQ==",
strict: []bool{false},
want: true,
},
{
name: "Valid standard base64 with safe mode as many params",
input: "SGVsbG8sIHdvcmxkIQ==",
strict: []bool{true, true, false}, // any false is false exp.
want: true,
},
{
name: "Not valid standard base64 (missing padding)",
input: "SGVsbG8sIHdvcmxkIQ",
strict: []bool{},
want: false,
},*/
{
name: "Valid base64 with missing padding for safe mode",
input: "SGVsbG8sIHdvcmxkIQ",
strict: []bool{false},
want: true,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := Base64(test.input, test.strict...)
if got != test.want {
t.Errorf("Base64(%q, %v) = %v; want %v",
test.input, test.strict, got, test.want)
}
})
}
}

0 comments on commit e83145f

Please sign in to comment.