dmitri.shuralyov.com/text/kebabcase

Initial commit.
dmitshur committed 6 years ago commit a1d95f8919b5af73cefaaba6f6932ade70a2a505
Showing partial commit. Full Commit
Collapse all
kebabcase.go
@@ -0,0 +1,18 @@
// Package kebabcase provides a parser for identifier names
// using kebab-case naming convention.
//
// Reference: https://en.wikipedia.org/wiki/Naming_convention_(programming)#Multiple-word_identifiers.
package kebabcase

import (
	"strings"

	"github.com/shurcooL/graphql/ident"
)

// Parse parses a kebab-case identifier name.
//
// E.g., "client-mutation-id" -> {"client", "mutation", "id"}.
func Parse(name string) ident.Name {
	return ident.Name(strings.Split(name, "-"))
}
kebabcase_test.go
@@ -0,0 +1,36 @@
package kebabcase_test

import (
	"fmt"
	"reflect"
	"testing"

	"dmitri.shuralyov.com/kebabcase"
	"github.com/shurcooL/graphql/ident"
)

func Example_kebabCaseToMixedCaps() {
	fmt.Println(kebabcase.Parse("client-mutation-id").ToMixedCaps())

	// Output: ClientMutationID
}

func TestParse(t *testing.T) {
	tests := []struct {
		in   string
		want ident.Name
	}{
		{in: "book", want: ident.Name{"book"}},
		{in: "bookmark", want: ident.Name{"bookmark"}},
		{in: "arrow-right", want: ident.Name{"arrow", "right"}},
		{in: "arrow-small-right", want: ident.Name{"arrow", "small", "right"}},
		{in: "device-camera-video-audio", want: ident.Name{"device", "camera", "video", "audio"}},
		{in: "rss", want: ident.Name{"rss"}},
	}
	for _, tc := range tests {
		got := kebabcase.Parse(tc.in)
		if !reflect.DeepEqual(got, tc.want) {
			t.Errorf("got: %q, want: %q", got, tc.want)
		}
	}
}