diff --git a/CHANGELOG.md b/CHANGELOG.md index c597e7d..e1d7c98 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,24 +1,7 @@ # Changelog -### [0.2.1] - 2025-03-14 - -#### Features - -- Add Hash Generator - -#### Dev Tooling - -- Added a task to serve docs -- Added tasts to check code complexity - -#### Miscellaneous - -- Fixed some `go vet` complaints - ### [0.2.0] - 2025-03-14 -#### Features - - Add `IncrementalFormat`* Generators - Add `Slug`* Generators - Add `WithOriginalSlug`* Options diff --git a/Taskfile.yml b/Taskfile.yml index 474a888..025ff4b 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -26,7 +26,6 @@ tasks: desc: Vet go code sources: - '**/*.go' - deps: [gen] cmds: - go vet ./... @@ -44,26 +43,6 @@ tasks: cmds: - staticcheck ./... - cog-complex: - desc: Calculate cognitive complexity - sources: - - '**/*.go' - cmds: - - gocognit -over 5 . - - cyc-complex: - desc: Calculate cyclomatic complexity - sources: - - '**/*.go' - cmds: - - gocyclo -over 5 . - - complex: - desc: Calculate complexities - deps: - - cog-complex - - cyc-complex - vuln: desc: Check for vulnerabilities sources: @@ -77,12 +56,11 @@ tasks: - vet - critic - staticcheck - - complex - vuln test: desc: Run unit tests - deps: [fmt, vet, gen] + deps: [fmt, vet] sources: - '**/*.go' generates: @@ -105,9 +83,4 @@ tasks: deps: [coverage-report] cmds: - ip addr list | grep inet - - php -S 0.0.0.0:3434 -t build - - serve-docs: - desc: Serve the current docs - cmds: - - godoc -http=0.0.0.0:3434 -play + - php -S 0.0.0.0:3265 -t build diff --git a/generators.go b/generators.go index 8685662..4b98284 100644 --- a/generators.go +++ b/generators.go @@ -1,12 +1,8 @@ package nomino import ( - "crypto/md5" - "crypto/sha1" - "crypto/sha256" "errors" "fmt" - "hash" "strconv" "time" @@ -189,44 +185,3 @@ func SlugWithLang(lang string) Generator { return slug.MakeLang(name, lang), err } } - -// HashingFunc is a function that generates a hash.Hash -type HashingFunc func() hash.Hash - -//go:generate stringer -type=HashType - -// HashType represents a particular hashing algorithm -type HashType uint8 - -const ( - MD5 HashType = iota + 1 - SHA1 - SHA256 -) - -// ErrInvalidHashType is returned by the Hash generator when an invalid HashType is passed -var ErrInvalidHashType = errors.New("invalid hash type") - -var hashMap = map[HashType]HashingFunc{ - MD5: md5.New, - SHA1: sha1.New, - SHA256: sha256.New, -} - -// Hash generates a name from a hash of the filename. -// When this is used, the original filename will be removed from the final filename. -func Hash(t HashType) Generator { - f, ok := hashMap[t] - return func(c *Config) (string, error) { - if !ok { - return "", fmt.Errorf("%w: %s", ErrInvalidHashType, t) - } - name, err := getOriginal(c) - if err != nil { - return "", err - } - hs := f() - hs.Write([]byte(name)) - return fmt.Sprintf("%x", hs.Sum(nil)), nil - } -} diff --git a/generators_examples_test.go b/generators_examples_test.go index 11cfae2..0275877 100644 --- a/generators_examples_test.go +++ b/generators_examples_test.go @@ -161,33 +161,3 @@ func ExampleSlugWithLang() { // Output: diese-und-dass.txt } - -func ExampleHash_mD5() { - conf := NewConfig( - WithOriginal("foobar"), - WithGenerator(Hash(MD5)), - ) - str, _ := Make(conf) - fmt.Println(str) - // Output: 3858f62230ac3c915f300c664312c63f.txt -} - -func ExampleHash_sHA1() { - conf := NewConfig( - WithOriginal("foobar"), - WithGenerator(Hash(SHA1)), - ) - str, _ := Make(conf) - fmt.Println(str) - // Output: 8843d7f92416211de9ebb963ff4ce28125932878.txt -} - -func ExampleHash_sHA256() { - conf := NewConfig( - WithOriginal("foobar"), - WithGenerator(Hash(SHA256)), - ) - str, _ := Make(conf) - fmt.Println(str) - // Output: c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2.txt -} diff --git a/generators_test.go b/generators_test.go index c6b3bbe..fb28ce7 100644 --- a/generators_test.go +++ b/generators_test.go @@ -2,7 +2,6 @@ package nomino import ( "errors" - "fmt" "testing" "time" @@ -121,23 +120,3 @@ func TestSlugRemovesOriginal(t *testing.T) { assert.Equal(t, "hello-world", st) assert.NoError(t, err) } - -func TestHashBadHash(t *testing.T) { - conf := NewConfig(WithOriginal("foobar"), WithGenerator(Hash(0))) - st, err := conf.generator(&conf) - assert.Equal(t, "", st) - assert.ErrorIs(t, err, ErrInvalidHashType) - assert.ErrorContains(t, err, "invalid hash type: HashType(0)") -} - -func TestHashMissingOriginal(t *testing.T) { - conf := NewConfig(WithGenerator(Hash(MD5))) - st, err := conf.generator(&conf) - assert.Equal(t, "", st) - assert.ErrorIs(t, err, ErrMissingOriginal) -} - -func TestHashTypeStringer(t *testing.T) { - s := fmt.Sprintf("%s", MD5) - assert.Equal(t, "MD5", s) -} diff --git a/hashtype_string.go b/hashtype_string.go deleted file mode 100644 index 16aa752..0000000 --- a/hashtype_string.go +++ /dev/null @@ -1,26 +0,0 @@ -// Code generated by "stringer -type=HashType"; DO NOT EDIT. - -package nomino - -import "strconv" - -func _() { - // An "invalid array index" compiler error signifies that the constant values have changed. - // Re-run the stringer command to generate them again. - var x [1]struct{} - _ = x[MD5-1] - _ = x[SHA1-2] - _ = x[SHA256-3] -} - -const _HashType_name = "MD5SHA1SHA256" - -var _HashType_index = [...]uint8{0, 3, 7, 13} - -func (i HashType) String() string { - i -= 1 - if i >= HashType(len(_HashType_index)-1) { - return "HashType(" + strconv.FormatInt(int64(i+1), 10) + ")" - } - return _HashType_name[_HashType_index[i]:_HashType_index[i+1]] -} diff --git a/make.go b/make.go index 538c7e0..c2c222c 100644 --- a/make.go +++ b/make.go @@ -12,7 +12,7 @@ func Make(conf Config) (string, error) { } if conf.prefix != "" { - conf.prefix += conf.separator + conf.prefix = conf.prefix + conf.separator } if conf.original != "" { conf.original = conf.separator + conf.original diff --git a/make_test.go b/make_test.go index ffdecee..28096ce 100644 --- a/make_test.go +++ b/make_test.go @@ -36,9 +36,7 @@ func TestMake(t *testing.T) { for _, testcase := range testcases { t.Run(testcase.name, func(sub *testing.T) { - opts := testcase.opts - opts = append(opts, genOpt) - + opts := append(testcase.opts, genOpt) conf := NewConfig(opts...) st, err := Make(conf) assert.NoError(t, err)