Giter Club home page Giter Club logo

goutil's Introduction

Build StatusGoDoc

goutil

Collection of small reusable Go functions

Install

Install with

go get github.com/fegoa89/goutils

Table of Contents

String

View contents

Slice

View contents

Timespan

View contents

String

Import

import "github.com/fegoa89/goutils/string"

Capitalize

Capitalizes the first letter of a string.

Capitalize("hello") // Hello
Examples
Capitalize("my string") // My string

CapitalizeTitle

Capitalizes string excluding words such as "and", "a", "with", "or". You can provide an optional string slice with words that will be excluded too

CapitalizeTitle("foo and bar") // Foo and Bar

UpperCamelCase

Convert string to UpperCamelCase

UpperCamelCase("foo and bar") // FooAndBar
Examples
UpperCamelCase("foo and bar") // FooAndBar
UpperCamelCase("foo.and.bar") // FooAndBar
UpperCamelCase("foo-and-bar") // FooAndBar
UpperCamelCase("foo_and_bar") // FooAndBar
UpperCamelCase("Foo and bar") // FooAndBar

LowerCamelCase

Convert string to LowerCamelCase

LowerCamelCase("foo and bar") // fooAndBar
Examples
LowerCamelCase("foo and bar") // fooAndBar
LowerCamelCase("foo.and.bar") // fooAndBar
LowerCamelCase("foo-and-bar") // fooAndBar
LowerCamelCase("foo_and_bar") // fooAndBar
LowerCamelCase("Foo and bar") // fooAndBar

ToSnakeCase

Convert string to to SnakeCase

ToSnakeCase("fooAndBar") // foo_and_bar
Examples
ToSnakeCase("fooAndBar")   // foo_and_bar
ToSnakeCase("foo.AndBar")  // foo_and_bar
ToSnakeCase("Foo-And-Bar") // foo_and_bar
ToSnakeCase("foo And Bar") // foo_and_bar
ToSnakeCase("你好")         // 你_好

ToKebabCase

Convert string to ToKebabCase

ToKebabCase("fooAndBar") // foo-and-bar
Examples
ToKebabCase("fooAndBar")   // foo-and-bar
ToSnakeCase("foo.AndBar")  // foo-and-bar
ToSnakeCase("Foo-And-Bar") // foo-and-bar
ToSnakeCase("foo And Bar") // foo-and-bar
ToSnakeCase("你好")         // 你-好

Humanize

Converts a computerized string into a human-friendly one

Humanize("fooAndBar") // foo and bar
Examples
Humanize("fooAndBar")    // foo and bar
Humanize("foo_and_bar")  // foo and bar
Humanize(" foo-and-bar") // foo and bar

Truncate

Truncates a string up to a specified string length

Truncate("King Gizzard & The Lizard Wizard", 15) // "King Gizzard..."
Examples
Truncate("En un lugar de la Mancha, de cuyo nombre no quiero acordarme", 27) // "En un lugar de la Mancha..."

IsEmptyOrBlank

Returns true if string is empty or blank

IsEmptyOrBlank("") // true
Examples
IsEmptyOrBlank("") // true
IsEmptyOrBlank(" ") // true
IsEmptyOrBlank("my string") // false

IsBlank

Returns true if string is blank

IsBlank("") // true
Examples
IsBlank("") // true
IsBlank(" ") // true
IsBlank("my string") // false

IsEmpty

Returns true if string is empty

IsEmpty("") // true
Examples
IsEmpty("") // true
IsEmpty(" ") // false
IsEmpty("my string") // false

DeleteWhitespace

Deletes all whitespaces from a String

DeleteWhitespace("foo and bar") // fooandbar
Examples
DeleteWhitespace("my string") // mystring
DeleteWhitespace(" my string ") // mystring

CaseInsensitiveEquals

Return true if both strings are case insensitive equal

CaseInsensitiveEquals("my string", "My String") // true
Examples
CaseInsensitiveEquals("my string", "My String") // true
CaseInsensitiveEquals("my string", "My-String") // false

IsBoolean

Determines whether the string is a boolean representation

IsBoolean("true") // true
Examples
IsBoolean("false") // true
IsBoolean("I am a dog") // false

IsInteger

Determines whether the string is a integer representation

IsInteger("1") // true
Examples
IsInteger("344") // true
IsInteger("I am a cat") // false

IsFloat

Determines whether the string is a float representation

IsFloat("1.4") // true
Examples
IsFloat("34.4") // true
IsFloat("nothing") // false

IndexOf

Returns index of the specified character or substring in a particular String

IndexOf("dog", "o") // 1
Examples
IndexOf("dog", "o") // 1
IndexOf("airport", "po") // 3

RemoveAccents

Removes the accents from a string, converting them to their non-accented corresponding characters.

RemoveAccents("ÀÁÂÃÄÅ") // AAAAAA
Examples
RemoveAccents("ÀÁÂÃÄÅ") // AAAAAA
RemoveAccents("ÒÓÔÕÖØỐṌṒ") // OOOOOØOOO

GetMD5Hash

converts a string to an MD5 hash

GetMD5Hash("hola") // 4d186321c1a7f0f354b297e8914ab240

UnifyWordSeries

Transforms a list of words into a word series, returning a string containing all words separated by commas and the conjunction

UnifyWordSeries([]string{"dogs", "cats", "pikachus"}, "and") // dogs, cats and pikachus
Examples
UnifyWordSeries([]string{"dogs"}, "and") // dogs
UnifyWordSeries([]string{"dogs", "cats"}, "or") // dogs or cats
UnifyWordSeries([]string{"dogs", "cats"}, "and") // dogs and cats

Slice

Import

import "github.com/fegoa89/goutils/slice"

SlicePush

Adds an elements to the end of a slice and returns the new length of the slice.

mySlice := []interface{}{}
SlicePush(&mySlice, "hello") // 1

SlicePop

Removes the last element from an slice and returns that removed element

mySlice := []interface{}{"red", "fox"}
SlicePop(&mySlice) // "fox"

SliceShift

Removes the first element from an slice and returns that removed element.

mySlice := []interface{}{"red", "fox"}
SliceShift(&mySlice) // "red"

RemoveItemByIndex

Removes an item by index position and returns the new length of the slice.

mySlice := []interface{}{"red", "fox"}
RemoveItemByIndex(&mySlice, 0) // "1"

SliceUnique

Assigns to the slice a duplicate-free version of itself.

mySlice := []interface{}{"red", "red", "fox"}
SliceUnique(&mySlice) // "1"

SliceDiff

Returns a slice containing all the entries from slice1 that are not present in slice2.

firstSlice := []interface{}{"red", "dog", "fox"}
secondSlice := []interface{}{"red", "fox"}

SliceDiff(&firstSlice, &secondSlice) // []interface{}{"dog"}

SliceIntersect

Returns a slice containing all the entries from slice1 that are present in slice2.

firstSlice := []interface{}{"red", "dog", "fox"}
secondSlice := []interface{}{"dog"}

SliceIntersect(&firstSlice, &secondSlice) // []interface{}{"dog"}

Timespan

Import

import "github.com/fegoa89/goutils/timespan"

Start

Returns the start time.

startTimeObject, _ := time.Parse("2006 01 02 15 04", "2015 11 11 00 00")
endTimeObject, _ := time.Parse("2006 01 02 15 04", "2015 11 11 16 00")
timeSpanStruct := TimeSpanStruct(startTimeObject, endTimeObject)
timeSpanStruct.Start() // 2015-11-11 00:00:00 +0000 UTC

End

Returns the end time.

startTimeObject, _ := time.Parse("2006 01 02 15 04", "2015 11 11 00 00")
endTimeObject, _ := time.Parse("2006 01 02 15 04", "2015 11 11 16 00")
timeSpanStruct := TimeSpanStruct(startTimeObject, endTimeObject)
timeSpanStruct.End() // 2015-11-11 16:00:00 +0000 UTC

ValidDateRange

Checks if the start date is older than end date.

startTimeObject, _ := time.Parse("2006 01 02 15 04", "2015 11 11 00 00")
endTimeObject, _ := time.Parse("2006 01 02 15 04", "2015 11 11 16 00")
timeSpanStruct := TimeSpanStruct(startTimeObject, endTimeObject)
timeSpanStruct.ValidDateRange() // true

DaysBetween

Returns the quantity of days between two time objects.

startTimeObject, _ := time.Parse("2006 01 02 15 04", "2015 11 11 00 00")
endTimeObject, _ := time.Parse("2006 01 02 15 04", "2015 11 14 16 00")
timeSpanStruct := TimeSpanStruct(startTimeObject, endTimeObject)
timeSpanStruct.DaysBetween() // 33

HoursBetween

Returns the quantity of hours between two time objects.

startTimeObject, _ := time.Parse("2006 01 02 15 04", "2015 11 11 00 00")
endTimeObject, _ := time.Parse("2006 01 02 15 04", "2015 11 11 16 00")
timeSpanStruct := TimeSpanStruct(startTimeObject, endTimeObject)
timeSpanStruct.HoursBetween() // 16

MinutesBetween

Returns the quantity of minutes between two time objects.

startTimeObject, _ := time.Parse("2006 01 02 15 04", "2015 11 11 00 00")
endTimeObject, _ := time.Parse("2006 01 02 15 04", "2015 11 11 16 00")
timeSpanStruct := TimeSpanStruct(startTimeObject, endTimeObject)
timeSpanStruct.MinutesBetween() // 16

SecondsBetween

Returns the quantity of seconds between two time objects.

startTimeObject, _ := time.Parse("2006 01 02 15 04", "2015 11 11 00 00")
endTimeObject, _ := time.Parse("2006 01 02 15 04", "2015 11 11 16 00")
timeSpanStruct := TimeSpanStruct(startTimeObject, endTimeObject)
timeSpanStruct.SecondsBetween() // 57600

MonthsBetween

Returns the quantity of months between two time objects.

startTimeObject, _ := time.Parse("2006 01 02 15 04", "2015 11 11 00 00")
endTimeObject, _ := time.Parse("2006 01 02 15 04", "2015 12 11 16 00")
timeSpanStruct := TimeSpanStruct(startTimeObject, endTimeObject)
timeSpanStruct.MonthsBetween() // 1


⬆ Back to top

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.