Documentation ¶
Overview ¶
Package cfw contains ports of a few selected CFWheels helpers that are used for string manipulation and have no Go equivalent. © Ben Garrett https://github.com/bengarrett/cfw
Index ¶
- func DeObfuscate(s string) string
- func Excerpt(s, replace, phrase string, n int) string
- func Humanize(s string, except ...string) string
- func Hyphenize(s string) string
- func Obfuscate(s string) string
- func ReverseInt(i int) (int, error)
- func StripLinks(s string) string
- func StripTags(s string) string
- func TimeDistance(from, to time.Time, seconds bool) string
- func Truncate(s, replace string, n int) string
- func WordTruncate(s, replace string, n int) string
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DeObfuscate ¶
Deobfuscate the obfuscated string, or return the original string. This function is a port of a CFWheels framework function programmed in ColdFusion (CFML). See: https://github.com/cfwheels/cfwheels/blob/cf8e6da4b9a216b642862e7205345dd5fca34b54/wheels/global/misc.cfm#L508
Example ¶
package main import ( "fmt" "github.com/bengarrett/cfw" ) func main() { fmt.Println(cfw.DeObfuscate("9b1c6")) }
Output: 1
func Excerpt ¶
Excerpt replaces n characters from s, which match the first instance of a given phrase. This function is a port of a CFWheels framework function programmed in ColdFusion (CFML). See: https://github.com/cfwheels/cfwheels/blob/cf8e6da4b9a216b642862e7205345dd5fca34b54/wheels/global/misc.cfm#L68
Example ¶
package main import ( "fmt" "github.com/bengarrett/cfw" ) func main() { pangram := "The quick brown fox jumps over the lazy dog" fmt.Println(cfw.Excerpt(pangram, "...", "The quick brown fox", 0)) fmt.Println(cfw.Excerpt(pangram, "...", "", 19)) fmt.Println(cfw.Excerpt(pangram, "🦊", "The quick brown ", 0)) }
Output: The quick brown fox... The quick brown fox... The quick brown 🦊
func Humanize ¶
Humanize returns readable text by separating camelCase strings to multiple, capitalized words. This function is a port of a CFWheels framework function programmed in ColdFusion (CFML). https://github.com/cfwheels/cfwheels/blob/632ea90547da368cddd77cefe17f42a7eda871e0/wheels/global/util.cfm#L53
Example ¶
package main import ( "fmt" "github.com/bengarrett/cfw" ) func main() { fmt.Println(cfw.Humanize("helloWorldExample", []string{}...)) fmt.Println(cfw.Humanize("golangModOrVgo?", []string{"MOD", "VGO"}...)) }
Output: Hello World Example Golang MOD Or VGO?
func Hyphenize ¶
Hyphenize converts camelCase strings to a lowercase hyphened string.
Example ¶
package main import ( "fmt" "github.com/bengarrett/cfw" ) func main() { fmt.Println(cfw.Hyphenize("aTourOfGo")) }
Output: a-tour-of-go
func Obfuscate ¶
Obfuscate a numeric string to insecurely hide database primary key values when passed along a URL. This function is a port of a CFWheels framework function programmed in ColdFusion (CFML). https://github.com/cfwheels/cfwheels/blob/cf8e6da4b9a216b642862e7205345dd5fca34b54/wheels/global/misc.cfm#L483
Example ¶
package main import ( "fmt" "github.com/bengarrett/cfw" ) func main() { fmt.Println(cfw.Obfuscate("1")) fmt.Println(cfw.Obfuscate("5551234")) }
Output: 9b1c6 b3da865e
func ReverseInt ¶ added in v1.3.0
ReverseInt reverses an integer.
func StripLinks ¶
StripLinks removes all HTML links from a string leaving just the link text. This function is a port of a CFWheels framework function programmed in ColdFusion (CFML). https://github.com/cfwheels/cfwheels/blob/daa7c43fc993cab00f52cf8ac881e6cc93c02fe1/wheels/view/sanitize.cfm#L3
Example ¶
package main import ( "fmt" "github.com/bengarrett/cfw" ) func main() { fmt.Println(cfw.StripLinks(`<a href="https://golang.org">The Go Programming Language</a>.`)) }
Output: The Go Programming Language.
func StripTags ¶
StripTags removes all HTML tags from a string. This function is a port of a CFWheels framework function programmed in ColdFusion (CFML). https://github.com/cfwheels/cfwheels/blob/daa7c43fc993cab00f52cf8ac881e6cc93c02fe1/wheels/view/sanitize.cfm#L21
Example ¶
package main import ( "fmt" "github.com/bengarrett/cfw" ) func main() { fmt.Println(cfw.StripTags(`<h1><a href="https://golang.org">The Go Programming Language</a>.</h1>`)) }
Output: The Go Programming Language.
func TimeDistance ¶
TimeDistance describes the difference between two time values. This function is a port of a CFWheels framework function programmed in ColdFusion (CFML). https://github.com/cfwheels/cfwheels/blob/cf8e6da4b9a216b642862e7205345dd5fca34b54/wheels/global/misc.cfm#L112
Example ¶
package main import ( "fmt" "log" "time" "github.com/bengarrett/cfw" ) func main() { const layout = "2006 Jan 2" f, err := time.Parse(layout, "2000 Jan 1") if err != nil { log.Fatal(err) } t, err := time.Parse(layout, "2020 Jun 30") if err != nil { log.Fatal(err) } fmt.Println(cfw.TimeDistance(f, t, false)) }
Output: over 20 years
Example (Seconds) ¶
package main import ( "fmt" "log" "time" "github.com/bengarrett/cfw" ) func main() { f, err := time.Parse(time.RFC3339, "2006-01-02T15:04:06+07:00") if err != nil { log.Fatal(err) } t, err := time.Parse(time.RFC3339, "2006-01-02T15:04:10+07:00") if err != nil { log.Fatal(err) } fmt.Println(cfw.TimeDistance(f, t, true)) }
Output: less than 5 seconds
func Truncate ¶
Truncate a string to the specified number and replace the trailing characters. This function is a port of a CFWheels framework function programmed in ColdFusion (CFML). https://github.com/cfwheels/cfwheels/blob/cf8e6da4b9a216b642862e7205345dd5fca34b54/wheels/global/misc.cfm#L20
Example ¶
package main import ( "fmt" "github.com/bengarrett/cfw" ) func main() { fmt.Println(cfw.Truncate("Go is an open source programming language", "", 8)) fmt.Println(cfw.Truncate("Go is an open source programming language", "?", 6)) }
Output: Go is... Go is?
func WordTruncate ¶
WordTruncate truncates a string to the specified number of words and replaces the trailing characters. This function is a port of a CFWheels framework function programmed in ColdFusion (CFML). https://github.com/cfwheels/cfwheels/blob/cf8e6da4b9a216b642862e7205345dd5fca34b54/wheels/global/misc.cfm#L40
Example ¶
package main import ( "fmt" "github.com/bengarrett/cfw" ) func main() { fmt.Println(cfw.WordTruncate("Go is an open source programming language", "", 2)) fmt.Println(cfw.WordTruncate("Go is an open source programming language", "?", 2)) }
Output: Go is... Go is?
Types ¶
This section is empty.