goshakastringhelper

package module
v0.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 16, 2023 License: MIT Imports: 12 Imported by: 0

README

Go string helpers inspered by Laravel's :)

Requirements

  • Go version 1.19

Setup

Clone the project

  • Clone this project by executing git clone https://github.com/auliawiguna/goshaka-stringhelper
  • Execute go mod download && go mod verify

Usage in your projects

  • Note that you need to include the v in the version tag. Execute go get github.com/auliawiguna/goshaka-stringhelper@v0.1.1

Functions

After

To get string after given mark

After(source, mark string) string
Parameter Type Description
source string Required. Source text
mark string Required. Mark text

Usage:

After("This is World of Oz", "is World")

Result: of Oz

AfterLast

To get string after last occurence of given mark

  AfterLast(source, mark string)
Parameter Type Description
source string Required. Source text
mark string Required. Mark text

Usage:

AfterLast("This is World of Oz and World of Colony and the World of Irony", "World of")

Result: Irony

Before

To get string before given mark

  Before(source, mark string)
Parameter Type Description
source string Required. Source text
mark string Required. Mark text

Usage:

Before("This is World of Oz", "is World")

Result: This

BeforeLast

To get string before the last occurence of given mark

  BeforeLast(source, mark string)
Parameter Type Description
source string Required. Source text
mark string Required. Mark text

Usage:

BeforeLast("This is World of Oz and World of Colony and the World of Irony", "World of")

Result: This is World of Oz and World of Colony and the

Between

To get string between the first occurence of "start" and the last occurence of "stop"

  Between(source, start, stop string)
Parameter Type Description
source string Required. Source text
start string Required. Mark text
stop string Required. Mark text

Usage:

Between("This is World of Oz", "This", "of")

Result: is World

BetweenFirst

To get the smallest possible portion of a string between "start" and "stop"

  BetweenFirst(source, start, stop string)
Parameter Type Description
source string Required. Source text
start string Required. Mark text
stop string Required. Mark text

Usage:

BetweenFirst("[a] [b] XX [c]", "[", "]")

Result: a

Camel

To convert str into camel case (camelCase)

  Camel(source string)
Parameter Type Description
source string Required. Source text

Usage:

Camel("i feel good")

Result: iFeelGood

Pascal

To convert str into pascal case (PascalCase)

  Pascal(source string)
Parameter Type Description
source string Required. Source text

Usage:

Pascal("i feel good")

Result: IFeelGood

Snake

To convert str into snake case (snake_case)

  Snake(source string)
Parameter Type Description
source string Required. Source text

Usage:

Snake("i feel good")

Result: i_feel_good

Kebab

To convert str into kebab case (kebab-case)

  Kebab(source string)
Parameter Type Description
source string Required. Source text

Usage:

Kebab("i feel good")

Result: i-feel-good

Headline

To convert str into headline case (Headline Case)

  Headline(source string)
Parameter Type Description
source string Required. Source text

Usage:

Headline("i feel good")

Result: I Feel Good

Contains

To determine if target is exists in str

  Contains(str, target string)
Parameter Type Description
str string Required. Source text
target string Required. Target text

Usage:

Contains("i feel good", "feel")
ContainsAll

To determine if array of target are/is exists in str

  ContainsAll(str string, target []string)
Parameter Type Description
str string Required. Source text
target []string Required. Array of the target text

Usage:

ContainsAll("i feel good", []string{"i", "feel", "good"})

Result: true

EndsWith

To determine if str is ends with target

  EndsWith(str string, target {}interface)
Parameter Type Description
str string Required. Source text
target array OR string Required. Target text

Usage:

EndsWith("This Is Title", "Not")

Result: false

Finish

To adds a "add" to "str" if it does not already end with "add"

  Finish(str, add string)
Parameter Type Description
str string Required. Source text
add string Required. Target text

Usage:

Finish("This Is Title", "Title")

Result: This Is Title

IsAscii

To checks if param is ascii

  IsAscii(param {}interface)
Parameter Type Description
param int OR string Required. param

Usage:

IsAscii("X")

Result: true

Is

To checks if s is match with pattern

  Is(s, pattern string)
Parameter Type Description
s string Required. Source text
pattern string Required. Pattern text

Usage:

Is("Prambanan", "Pram*")

Result: true

Trim

To trim spaces

  Trim(param {}interface)
Parameter Type Description
target string OR array Required. Source text

Usage:

Trim("Prambanan ")

Result: Prambanan

IsEmpty

To check whether the given target is empty or not

  IsEmpty(target string)
Parameter Type Description
target string Required. Source text

Usage:

IsEmpty("    ")

Result: true

IsNotEmpty

To check whether the given target is empty or not

  IsNotEmpty(target string)
Parameter Type Description
target string Required. Source text

Usage:

IsNotEmpty(" x    ")

Result: true

IsJson

To check whether the given target is a valid JSON

  IsJson(target string)
Parameter Type Description
target string Required. Source text

Usage:

IsJson("{"name":"John", "age":30, "city":"New York"}")

Result: true

IsUlid

To check whether the given target is a valid ULID

  IsUlid(target string)
Parameter Type Description
target string Required. Source text

Usage:

IsUlid("01EY7ZRSN1A8CB7WJ08N0Q2QH2")

Result: true

IsUuid

To check whether the given target is a valid ULID

  IsUuid(target string)
Parameter Type Description
target string Required. Source text

Usage:

IsUuid("4b48d94c-9887-46c8-9eb7-d9fcb9fb55f3")

Result: true

Lcfirst

To returns the given string with the first character lowercased

  Lcfirst(target {}interface)
Parameter Type Description
target array OR string Required. Target text

Usage:

Lcfirst("This Is It")

Result: this Is It

Lcfirst

To returns the given string with the first character lowercased

  Lcfirst(target {}interface)
Parameter Type Description
target array OR string Required. Target text

Usage:

Lcfirst("This Is It")

Result: this Is It

Limit

To truncates the given string to the specified length

  Limit(target string, length int, placeholder string)
Parameter Type Description
target string Required. Source text
length int Required. Length
placeholder string Required. Placeholder

Usage:

Limit("Crazy fox over the top", 9, "...")

Result: Crazy fox...

Lower

To returns lowercase the given string

  Lower(target {}interface)
Parameter Type Description
target array OR string Required. Target text

Usage:

Lower("This Is It")

Result: this is it

Ltrim

To trims the left side of the string

  Ltrim(str, cutset string)
Parameter Type Description
str string Required. Source text
cutset int Required. Ltrim cut set

Usage:

Ltrim("-----------------This Is It", "-")

Result: This Is It

Rtrim

To trims the right side of the string

  Rtrim(str, cutset string)
Parameter Type Description
str string Required. Source text
cutset int Required. Ltrim cut set

Usage:

Rtrim(This Is It("-----------------", "-")

Result: This Is It

Markdown

To convert given markdown to the HTML format

  Markdown(str string)
Parameter Type Description
str string Required. Source text

Usage:

Markdown("# Goshaka")

Result: <h1>Goshaka</h1>\n

Mask

To masks a given string string with a repeated character

  Mask(str, mask string, start int)
Parameter Type Description
str string Required. Source text
mask string Required. Mask text
start int Required. Initial index

Usage:

Mask("+62890989999", "*", 3)

Result: +62*********

IsMatch

To check whether str matches a given pattern

  IsMatch(str, pattern string)
Parameter Type Description
str string Required. Source text
pattern string Required. Match pattern

Usage:

IsMatch("foo bar", "foo (.*)")

Result: true

PadLeft

To pad the left side of a string with another string until the final string reaches the desired length

  PadLeft(str, pad string, length int)
Parameter Type Description
str string Required. Source text
pad string Required. Pad pattern
length int Required. Length of pad

Usage:

PadLeft("foo bar", "+-", 7)

Result: +-+-+-+foo bar

PadRight

To pad the right side of a string with another string until the final string reaches the desired length

  PadRight(str, pad string, length int)
Parameter Type Description
str string Required. Source text
pad string Required. Pad pattern
length int Required. Length of pad

Usage:

PadRight("foo bar", "+-", 7)

Result: foo bar+-+-+-+

Remove

To remove target from str

  Remove(str, target string)
Parameter Type Description
str string Required. Source text
target string Required. Target text

Usage:

Remove("cinta brontosaurus", "a")

Result: cint brontosurus

Replace

To replace target in str into replace

  Replace(str, target, replace string)
Parameter Type Description
str string Required. Source text
target string Required. Target text
replace string Required. Replace text

Usage:

Replace("cinta brontosaurus", "a", "b")

Result: cintb brontosburus

RemoveSymbol

To remove all symbol and space from str

  RemoveSymbol(str string)
Parameter Type Description
str string Required. Source text

Usage:

RemoveSymbol("This is an example with spaces, commas, and a period.")

Result: Thisisanexamplewithspacescommasandaperiod

RemoveSymbolExceptSpace

To remove all symbol from str

  RemoveSymbolExceptSpace(str string)
Parameter Type Description
str string Required. Source text

Usage:

RemoveSymbolExceptSpace("This is an example with spaces, commas, and a period.")

Result: This is an example with spaces commas and a period

Split

To split string into an array

  Split(param, delimiter string)
Parameter Type Description
param string Required. Source text
delimiter int Required. Delimiter

Usage:

Split("apple,sony,samsung,polytron", ",")

Result: [apple sony samsung polytron]

Squish

To squish extra spaces from a string

  Squish(param string)
Parameter Type Description
param string Required. Source text

Usage:

Squish("This  is   a  test  string   with extra   spaces.")

Result: This is a test string with extra spaces.

Substr

To get a substring from a string

  Substr(str string, start, end int)
Parameter Type Description
str string Required. Source text
start int Required. Start index
end int Required. Finish index

Usage:

Substr("Crazy Frog", 0, 5)

Result: Crazy

Reverse

To reverse a string

  Reverse(str string)
Parameter Type Description
str string Required. Source text

Usage:

Reverse("Crazy")

Result: yzarC

IsPalindrome

To check whether the given target is palindrome or not

  IsPalindrome(str string)
Parameter Type Description
str string Required. Source text

Usage:

IsPalindrome("tamat")

Result: true

WordCount

Returns the number of words that a string contains

  WordCount(str string)
Parameter Type Description
str string Required. Source text

Usage:

WordCount("this is it")

Result: 3

Upper

To returns uppercase the given string

  Upper(target {}interface)
Parameter Type Description
target array OR string Required. Target text

Usage:

Upper("This Is Title")

Result: THIS IS TITLE

Random

To returns random alphanumeric characters

  Random(length int)
Parameter Type Description
length int Required. Length of the result

Usage:

Random(4)

Result: JiFyN

Running Tests

To run tests, run the following command

  go test

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func After

func After(source, mark string) string

To get string after given mark

param source string param mark string return string

func AfterLast

func AfterLast(source, mark string) string

To get string after last occurence of given mark

param source string param mark string return string

func Before

func Before(source, mark string) string

To get string before given mark

param source string param mark string return string

func BeforeLast

func BeforeLast(source, mark string) string

To get string before the last occurence of given mark

param source string param mark string return string

func Between

func Between(source, start, stop string) string

To get string between the first occurence of "start" and the last occurence of "stop"

param source string param start string param stop string return string

func BetweenFirst

func BetweenFirst(source, start, stop string) string

To get the smallest possible portion of a string between "start" and "stop"

param source string param start string param stop string return string

func Camel added in v0.1.1

func Camel(str string) string

To convert str into camel case (camelCase)

param str string return string

func Contains added in v0.1.1

func Contains(str, target string) bool

To determine if target is exists in str

param str string param target string return bool

func ContainsAll added in v0.1.1

func ContainsAll(str string, target []string) bool

To determine if target is exists in str

param str string param target string return bool

func EndsWith added in v0.1.1

func EndsWith(str string, target interface{}) bool

To determine if str is ends with target

param str string param target string|array return bool

func Finish added in v0.1.1

func Finish(str, add string) string

To adds a "add" to "str" if it does not already end with "add"

param str string param add string return string

func Headline added in v0.1.1

func Headline(str string) string

To convert str into headline case

param str string return string

func Is added in v0.1.1

func Is(s, pattern string) bool

To checks if s is match with pattern

param s string param pattern string return bool

func IsAscii added in v0.1.1

func IsAscii(param interface{}) bool

To checks if param is ascii

param param int|string return bool

func IsEmpty added in v0.1.1

func IsEmpty(target string) bool

To check whether the given target is empty or not

param target string|array return bool

func IsJson added in v0.1.1

func IsJson(target string) bool

To check whether the given target is a valid JSON

param target string return bool

func IsMatch added in v0.1.1

func IsMatch(str, pattern string) bool

To check whether str matches a given pattern

param str string param pattern string return bool

func IsNotEmpty added in v0.1.1

func IsNotEmpty(target string) bool

To check whether the given target is empty or not param target string|array return bool

func IsPalindrome added in v0.1.1

func IsPalindrome(str string) bool

To check whether the given target is palindrome or not

param str string return bool

func IsUlid added in v0.1.1

func IsUlid(target string) bool

To check whether the given target is a valid ULID

param target string return bool

func IsUuid added in v0.1.1

func IsUuid(target string) bool

To check whether the given target is a valid UUID

param target string return bool

func Kebab added in v0.1.1

func Kebab(str string) string

To convert str into kebab case (kebab-case)

param str string return string

func Lcfirst added in v0.1.1

func Lcfirst(target interface{}) interface{}

To returns the given string with the first character lowercased

param target string|array return interface

func Limit added in v0.1.1

func Limit(target string, length int, placeholder string) string

To truncates the given string to the specified length

param target string param length int param placeholder string return string

func Lower added in v0.1.1

func Lower(target interface{}) interface{}

To returns lowercase the given string

param target string|array return interface

func Ltrim added in v0.1.1

func Ltrim(str, cutset string) string

To trims the left side of the string

param str string param cutset string return string

func Markdown added in v0.1.1

func Markdown(str string) string

To convert given markdown to the HTML format

param str string return string

func Mask added in v0.1.1

func Mask(str, mask string, start int) string

To masks a given string string with a repeated character

param str string param mask string param start int return string

func PadLeft added in v0.1.1

func PadLeft(str, pad string, length int) string

To pad the left side of a string with another string until the final string reaches the desired length

param str string param pad string param length int return string

func PadRight added in v0.1.1

func PadRight(str, pad string, length int) string

To pad the right side of a string with another string until the final string reaches the desired length

param str string param pad string param length int return string

func Pascal added in v0.1.1

func Pascal(str string) string

To convert str into camel case (PascalCase)

param str string return string

func Random added in v0.1.2

func Random(length int) string

To returns random alphanumeric characters

param length int return interface

func Remove added in v0.1.1

func Remove(str, target string) string

To remove target from str

param str string param target string return string

func RemoveSymbol added in v0.1.1

func RemoveSymbol(str string) string

To remove all symbol and space from str

param str string return string

func RemoveSymbolExceptSpace added in v0.1.1

func RemoveSymbolExceptSpace(str string) string

To remove all symbol from str

param str string return string

func Replace added in v0.1.1

func Replace(str, target, replace string) string

To replace target in str into replace

param str string param target string param replace string return string

func Reverse added in v0.1.1

func Reverse(str string) (res string)

To reverse a string

param str string return string

func Rtrim added in v0.1.1

func Rtrim(str, cutset string) string

To trims the right side of the string

param str string param cutset string return string

func Snake added in v0.1.1

func Snake(str string) string

To convert str into snake case (snake_case)

param str string return string

func Split added in v0.1.1

func Split(str, delimiter string) []string

To split string into an array

param str string param delimiter string return array of string

func Squish added in v0.1.1

func Squish(str string) string

To squish extra spaces from a string

param str string return string

func Substr added in v0.1.1

func Substr(str string, start, end int) string

To get a substring from a string

param str string param start int param end int return string

func Trim added in v0.1.1

func Trim(target interface{}) interface{}

To trim spaces

param target string|array return bool

func Upper added in v0.1.1

func Upper(target interface{}) interface{}

To returns uppercase the given string

param target string|array return interface

func WordCount added in v0.1.1

func WordCount(str string) int

Returns the number of words that a string contains

param str string return int

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL