github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/internal/slug/slug.go (about) 1 // Copyright 2022 Harness, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Package slug provides utilities for working with slug values. 16 package slug 17 18 import ( 19 "strings" 20 "unicode" 21 22 "golang.org/x/text/unicode/norm" 23 ) 24 25 var safeRanges = []*unicode.RangeTable{ 26 unicode.Letter, 27 unicode.Number, 28 } 29 30 func safe(r rune) rune { 31 switch { 32 case unicode.IsOneOf(safeRanges, r): 33 return unicode.ToLower(r) 34 } 35 return -1 36 } 37 38 // Create creates a slug from a string. 39 func Create(s string) string { 40 s = norm.NFKD.String(s) 41 s = strings.Map(safe, s) 42 return s 43 }