Skip to content

shomali11/util

Repository files navigation

util Build Status Go Report Card GoDoc License: MIT

A group of generic useful utility functions

Dependencies

Examples

Concurrency

package main

import (
	"github.com/shomali11/util/xconcurrency"
	"time"
	"fmt"
)

func main() {
    func1 := func() error {
		for char := 'a'; char < 'a' + 3; char++ {
			fmt.Printf("%c ", char)
		}
		return nil
    }
    
    func2 := func() error {
		for number := 1; number < 4; number++ {
			fmt.Printf("%d ", number)
		}
		return nil
    }
    
    xconcurrency.Parallelize(func1, func2)  // a 1 b 2 c 3
    
    xconcurrency.ParallelizeTimeout(time.Minute, func1, func2)  // a 1 b 2 c 3
}

Hashes

package main

import (
	"github.com/shomali11/util/xhashes"
	"fmt"
)

func main() {
	fmt.Println(xhashes.FNV32("Raed Shomali"))  // 424383802
	fmt.Println(xhashes.FNV32a("Raed Shomali")) // 3711360776
	fmt.Println(xhashes.FNV64("Raed Shomali"))  // 13852322269024953050
	fmt.Println(xhashes.FNV64a("Raed Shomali")) // 17869303103005344072
	fmt.Println(xhashes.MD5("Raed Shomali"))    // c313bc3b48fcfed9abc733429665b105
	fmt.Println(xhashes.SHA1("Raed Shomali"))   // e0d66f6f09de72942e83289cc994b3c721ab34c5
	fmt.Println(xhashes.SHA256("Raed Shomali")) // 75894b9be21065a833e57bfe4440b375fc216f120a965243c9be8b2dc36709c2
	fmt.Println(xhashes.SHA512("Raed Shomali")) // 406e8d495140187a8b09893c30d054cf385ad7359855db0d2e0386c7189ac1c4667a4816d1b63a19f3d8ccdcbace7861ec4cc6ff5e2a1659c8f4360bda699b42
}

Compressions

package main

import (
	"github.com/shomali11/util/xcompressions"
	"fmt"
)

func main() {
	fmt.Println(xcompressions.Compress([]byte("Raed Shomali")))
}

Encodings

package main

import (
	"github.com/shomali11/util/xencodings"
	"fmt"
)

func main() {
	fmt.Println(xencodings.Base32Encode([]byte("Raed Shomali")))
	fmt.Println(xencodings.Base64Encode([]byte("Raed Shomali")))
}

Strings

package main

import (
	"github.com/shomali11/util/xstrings"
	"fmt"
)

func main() {
	fmt.Println(xstrings.IsEmpty(""))     // true
	fmt.Println(xstrings.IsEmpty("text")) // false
	fmt.Println(xstrings.IsEmpty("	"))  // false

	fmt.Println(xstrings.IsNotEmpty(""))     // false
	fmt.Println(xstrings.IsNotEmpty("text")) // true
	fmt.Println(xstrings.IsNotEmpty("	")) // true

	fmt.Println(xstrings.IsBlank(""))     // true
	fmt.Println(xstrings.IsBlank("	"))  // true
	fmt.Println(xstrings.IsBlank("text")) // false

	fmt.Println(xstrings.IsNotBlank(""))     // false
	fmt.Println(xstrings.IsNotBlank("	")) // false
	fmt.Println(xstrings.IsNotBlank("text")) // true

	fmt.Println(xstrings.Left("", 5))            // "     "
	fmt.Println(xstrings.Left("X", 5))           // "X    "
	fmt.Println(xstrings.Left("😎⚽", 4))        // "😎⚽  "
	fmt.Println(xstrings.Left("ab\u0301cde", 8)) // "ab́cde   "

	fmt.Println(xstrings.Right("", 5))            // "     "
	fmt.Println(xstrings.Right("X", 5))           // "    X"
	fmt.Println(xstrings.Right("😎⚽", 4))        // "  😎⚽"
	fmt.Println(xstrings.Right("ab\u0301cde", 8)) // "   ab́cde"

	fmt.Println(xstrings.Center("", 5))            // "     "
	fmt.Println(xstrings.Center("X", 5))           // "  X  "
	fmt.Println(xstrings.Center("😎⚽", 4))        // " 😎⚽ "
	fmt.Println(xstrings.Center("ab\u0301cde", 8)) // "  ab́cde "

	fmt.Println(xstrings.Length(""))                                          // 0
	fmt.Println(xstrings.Length("X"))                                         // 1
	fmt.Println(xstrings.Length("b\u0301"))                                   // 1
	fmt.Println(xstrings.Length("😎⚽"))                                      // 2
	fmt.Println(xstrings.Length("Les Mise\u0301rables"))                      // 14
	fmt.Println(xstrings.Length("ab\u0301cde"))                               // 5
	fmt.Println(xstrings.Length("This `\xc5` is an invalid UTF8 character"))  // 37
	fmt.Println(xstrings.Length("The quick bròwn 狐 jumped over the lazy 犬")) // 40

	fmt.Println(xstrings.Reverse(""))                                            // ""
	fmt.Println(xstrings.Reverse("X"))                                           // "X"
	fmt.Println(xstrings.Reverse("😎⚽"))                                        // "⚽😎"
	fmt.Println(xstrings.Reverse("Les Mise\u0301rables"))                        // "selbare\u0301siM seL"
	fmt.Println(xstrings.Reverse("This `\xc5` is an invalid UTF8 character"))    // "retcarahc 8FTU dilavni na si `�` sihT"
	fmt.Println(xstrings.Reverse("The quick bròwn 狐 jumped over the lazy 犬"))  // "犬 yzal eht revo depmuj 狐 nwòrb kciuq ehT"
}

Conditions

package main

import (
	"github.com/shomali11/util/xconditions"
	"fmt"
)

func main() {
	fmt.Println(xconditions.IfThen(1 == 1, "Yes")) // "Yes"
	fmt.Println(xconditions.IfThen(1 != 1, "Woo")) // nil
	fmt.Println(xconditions.IfThen(1 < 2, "Less")) // "Less"

	fmt.Println(xconditions.IfThenElse(1 == 1, "Yes", false)) // "Yes"
	fmt.Println(xconditions.IfThenElse(1 != 1, nil, 1))       // 1
	fmt.Println(xconditions.IfThenElse(1 < 2, nil, "No"))     // nil

	fmt.Println(xconditions.DefaultIfNil(nil, nil))  // nil
	fmt.Println(xconditions.DefaultIfNil(nil, ""))   // ""
	fmt.Println(xconditions.DefaultIfNil("A", "B"))  // "A"
	fmt.Println(xconditions.DefaultIfNil(true, "B")) // true
	fmt.Println(xconditions.DefaultIfNil(1, false))  // 1

	fmt.Println(xconditions.FirstNonNil(nil, nil))                // nil
	fmt.Println(xconditions.FirstNonNil(nil, ""))                 // ""
	fmt.Println(xconditions.FirstNonNil("A", "B"))                // "A"
	fmt.Println(xconditions.FirstNonNil(true, "B"))               // true
	fmt.Println(xconditions.FirstNonNil(1, false))                // 1
	fmt.Println(xconditions.FirstNonNil(nil, nil, nil, 10))       // 10
	fmt.Println(xconditions.FirstNonNil(nil, nil, nil, nil, nil)) // nil
	fmt.Println(xconditions.FirstNonNil())                        // nil
}

Errors

package main

import (
	"github.com/shomali11/util/xerrors"
	"fmt"
)

func main() {
	fmt.Println(xerrors.DefaultErrorIfNil(nil, "Cool"))                // "Cool"
	fmt.Println(xerrors.DefaultErrorIfNil(errors.New("Oops"), "Cool")) // "Oops"
}

Manipulations

package main

import (
	"github.com/shomali11/util/xmanipulations"
	"math/rand"
	"time"
	"fmt"
)

func main() {
	source := rand.NewSource(time.Now().UnixNano())

	array := []interface{}{"a", "b", "c"}
	xmanipulations.Shuffle(array, source)

	fmt.Println(array) // [c b a]
}

Conversions

Return a pretty JSON representation of any interface

package main

import (
	"github.com/shomali11/util/xconversions"
	"fmt"
)

func main() {
    x := map[string]interface{}{"number": 1, "string": "cool", "bool": true, "float": 1.5}    
    fmt.Println(xconversions.PrettyJson(x))
}
{
	"bool": true,
	"float": 1.5,
	"number": 1,
	"string": "cool"
}

Convert any interface to a String

package main

import (
	"github.com/shomali11/util/xconversions"
	"fmt"
)

func main() {
    x := map[string]interface{}{"number": 1, "string": "cool", "bool": true, "float": 1.5}    
    fmt.Println(xconversions.Stringify(x))
}
{"bool":true,"float":1.5,"number":1,"string":"cool"} <nil>

Convert any string back to its original struct

package main

import (
	"github.com/shomali11/util/xconversions"
	"fmt"
)

func main() {
	x := "{\"bool\":true,\"float\":1.5,\"number\":1,\"string\":\"cool\"}"
	var results map[string]interface{}
    fmt.Println(xconversions.Structify(x, &results))
    fmt.Println(results)
}
<nil>
map[bool:true float:1.5 number:1 string:cool]