Skip to content

Commit

Permalink
Update t13n base
Browse files Browse the repository at this point in the history
  • Loading branch information
goloop committed May 8, 2022
1 parent 78a68e2 commit 3837f59
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 66 deletions.
2 changes: 1 addition & 1 deletion .godocdown.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func main() {
fmt.Printf("%s%s\n", h, s)

// Output:
// hello-shi-jie
// https://example.com/Hello-Shi-Jie
}
```

Expand Down
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

[![Go Report Card](https://goreportcard.com/badge/github.com/goloop/slug)](https://goreportcard.com/report/github.com/goloop/slug) [![License](https://img.shields.io/badge/license-BSD-blue)](https://github.com/goloop/slug/blob/master/LICENSE) [![License](https://img.shields.io/badge/godoc-YES-green)](https://godoc.org/github.com/goloop/slug)

*Version: v0.0.1-alpha*
*Version: v0.0.2*

# slug

Expand Down Expand Up @@ -43,7 +43,7 @@ func main() {
fmt.Printf("%s%s\n", h, s)

// Output:
// hello-shi-jie
// https://example.com/Hello-Shi-Jie
}
```

Expand All @@ -61,6 +61,12 @@ Lower returns slug in lowercase.

Make returns slug from string.

#### func Upper

func Upper(t string) string

Upper returns slug in uppercase.

#### func Version

func Version() string
Expand Down Expand Up @@ -98,3 +104,9 @@ Lower returns slug in lowercase.
func (s *Slug) Make(t string) string

Make returns slug from string.

#### func (*Slug) Upper

func (s *Slug) Upper(t string) string

Upper returns slug in uppercase.
2 changes: 1 addition & 1 deletion doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// URL-friendly slugify with multiple languages support.
package slug

const version = "0.0.1-alpha"
const version = "0.0.2"

// Version returns the version of the module.
func Version() string {
Expand Down
22 changes: 22 additions & 0 deletions doc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package slug

import (
"strings"
"testing"
)

// TestVersion tests the package version.
// Note: each time you change the major version, you need to fix the tests.
func TestVersion(t *testing.T) {
var expected = "v0." // change it for major version

version := Version()
if strings.HasPrefix(version, expected) != true {
t.Error("incorrect version")
}

if len(strings.Split(version, ".")) != 3 {
t.Error("version format should be as " +
"v{major_version}.{minor_version}.{patch_version}")
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module github.com/goloop/slug

go 1.17

require github.com/goloop/t13n v1.0.0
require github.com/goloop/t13n v1.2.1
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github.com/goloop/t13n v1.0.0 h1:FaX8xLhJ1MR+Ua1jc+WNcFFFqQgkzTcFWKAOwLmlPZ8=
github.com/goloop/t13n v1.0.0/go.mod h1:1A8l9eGVc905TSEVbUS36ctQSEFtW7n5FBOc1Tp32r8=
github.com/goloop/t13n v1.2.1 h1:ArpoIEGoUCmvHvNvL+R1YdtkmWJrNjWEI7mUpwLjXho=
github.com/goloop/t13n v1.2.1/go.mod h1:1A8l9eGVc905TSEVbUS36ctQSEFtW7n5FBOc1Tp32r8=
5 changes: 5 additions & 0 deletions methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,8 @@ func Make(t string) string {
func Lower(t string) string {
return strings.ToLower(Make(t))
}

// Upper returns slug in uppercase.
func Upper(t string) string {
return strings.ToUpper(Make(t))
}
41 changes: 15 additions & 26 deletions methods_test.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,9 @@
package slug

import (
"strings"
"testing"
)

// TestVersion tests the package version.
// Note: each time you change the major version, you need to fix the tests.
func TestVersion(t *testing.T) {
var expected = "v0." // change it for major version

version := Version()
if strings.HasPrefix(version, expected) != true {
t.Error("incorrect version")
}

if len(strings.Split(version, ".")) != 3 {
t.Error("version format should be as " +
"v{major_version}.{minor_version}.{patch_version}")
}
}

// TestMake tests Make function.
func TestMake(t *testing.T) {
var tests = []struct {
Expand Down Expand Up @@ -48,23 +31,29 @@ func TestMake(t *testing.T) {
// TestLower tests Lower function.
func TestLower(t *testing.T) {
var tests = []struct {
value string
expected string
value string
lowerExpected string
upperExpected string
}{
{
"Starlink Ілона Маска відкриє офіс в Україні",
"starlink-ilona-maska-vidkriie-ofis-v-ukrayini",
"STARLINK-ILONA-MASKA-VIDKRIIE-OFIS-V-UKRAYINI",
},
{"Hellö Wörld", "hello-world"},
{"你好世界", "ni-hao-shi-jie"},
{"[^你好世界$]", "ni-hao-shi-jie"},
{"This & that", "this-and-that"},
{"\tHellö \t Wörld\n ", "hello-world"},
{"Hellö Wörld", "hello-world", "HELLO-WORLD"},
{"你好世界", "ni-hao-shi-jie", "NI-HAO-SHI-JIE"},
{"[^你好世界$]", "ni-hao-shi-jie", "NI-HAO-SHI-JIE"},
{"This & that", "this-and-that", "THIS-AND-THAT"},
{"\tHellö \t Wörld\n ", "hello-world", "HELLO-WORLD"},
}

for _, test := range tests {
if v := Lower(test.value); v != test.expected {
t.Errorf("expected %s but %s", test.expected, v)
if v := Lower(test.value); v != test.lowerExpected {
t.Errorf("expected %s but %s", test.lowerExpected, v)
}

if v := Upper(test.value); v != test.upperExpected {
t.Errorf("expected %s but %s", test.upperExpected, v)
}
}
}
5 changes: 5 additions & 0 deletions slug.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,8 @@ func (s *Slug) Make(t string) string {
func (s *Slug) Lower(t string) string {
return strings.ToLower(s.Make(t))
}

// Upper returns slug in uppercase.
func (s *Slug) Upper(t string) string {
return strings.ToUpper(s.Make(t))
}
24 changes: 15 additions & 9 deletions slug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,31 @@ func TestObjMake(t *testing.T) {
// TestObjLower tests Lower method.
func TestObjLower(t *testing.T) {
var tests = []struct {
value string
expected string
value string
lowerExpected string
upperExpected string
}{
{
"Starlink Ілона Маска відкриє офіс в Україні",
"starlink-ilona-maska-vidkryie-ofis-v-ukraini",
"STARLINK-ILONA-MASKA-VIDKRYIE-OFIS-V-UKRAINI",
},
{"Hellö Wörld", "hello-world"},
{"你好世界", "ni-hao-shi-jie"},
{"[^你好世界$]", "ni-hao-shi-jie"},
{"This & that", "this-and-that"},
{"\tHellö \t Wörld\n ", "hello-world"},
{"Hellö Wörld", "hello-world", "HELLO-WORLD"},
{"你好世界", "ni-hao-shi-jie", "NI-HAO-SHI-JIE"},
{"[^你好世界$]", "ni-hao-shi-jie", "NI-HAO-SHI-JIE"},
{" This & that", "this-and-that", "THIS-AND-THAT"},
{"\tHellö \t Wörld\n ", "hello-world", "HELLO-WORLD"},
}

s := New()
s.Lang(lang.UK)
for _, test := range tests {
if v := s.Lower(test.value); v != test.expected {
t.Errorf("expected %s but %s", test.expected, v)
if v := s.Lower(test.value); v != test.lowerExpected {
t.Errorf("expected %s but %s", test.lowerExpected, v)
}

if v := s.Upper(test.value); v != test.upperExpected {
t.Errorf("expected %s but %s", test.upperExpected, v)
}
}
}
49 changes: 25 additions & 24 deletions tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,40 @@ import (
"github.com/goloop/t13n/lang"
)

// The slugRules sets additional transliteration rules
// The slugRules sets custom transliteration rules
// for the github.com/goloop/t13n module.
func slugRules(ts lang.TransState) (string, int, bool) {
var lib = map[string]string{
" ": "-",
"~": "-",
"_": "-",
"\t": "-",
"\n": "-",
"@": "at",
"&": "and",
"#": "harp",
"%": "percentage",
// Ignore ranges.
// Important: edit maps ascending order only!
var ignored = [][]int{
{0, 47},
{58, 64},
{91, 96},
{123, 141},
{143, 152},
{155, 155},
}

if v, ok := lib[ts.Value]; ok {
ts.Value = v
} else {
var ignore = [][]int{
{0, 47},
{58, 64},
{91, 96},
{123, 141},
{143, 152},
{155, 155},
}

switch ts.Value {
case " ", "~", "_", "\t", "\n":
ts.Value = "-"
case "@":
ts.Value = "at"
case "&":
ts.Value = "and"
case "#":
ts.Value = "sharp"
case "%":
ts.Value = "pct"
default:
id := int(ts.Curr)
for _, d := range ignore {
for _, d := range ignored {
// If the item isn't in the following ranges.
if id < d[0] {
break
}

// If the item is in the current range.
if id >= d[0] && id <= d[1] {
ts.Value = ""
break
Expand Down
12 changes: 12 additions & 0 deletions tools_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ func TestSlugRules(t *testing.T) {
seek: 0,
ok: true,
},
{
item: lang.TransState{Curr: '#', Value: "#"},
value: "sharp",
seek: 0,
ok: true,
},
{
item: lang.TransState{Curr: '%', Value: "%"},
value: "pct",
seek: 0,
ok: true,
},
{
item: lang.TransState{Curr: 'i', Value: "i"},
value: "i",
Expand Down

0 comments on commit 3837f59

Please sign in to comment.