GithubHelp home page GithubHelp logo

shomali11 / util Goto Github PK

View Code? Open in Web Editor NEW
287.0 14.0 39.0 138 KB

A collection of useful utility functions

License: MIT License

Go 100.00%
utility utilities string strings string-manipulation conditional-statements conditional condition conditions errors

util's People

Contributors

raed-shomali avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

util's Issues

Error handling

Hello @shomali11,
It's very useful util that you have written, but in this case how you are handling errors. For example, if func1 breaks I don't wanna execute the other two func.

func1 := func() {
    f(0)
}

func2 = func() {
    f(1)
}

func3 = func() {
    f(2)
}

Parallelize(func1, func2, func3)

Reverse broken for flag emoji

Inspired by a comment on this SO about string-reversal in Python:

For example, "๐Ÿ‡ฌ๐Ÿ‡ง"[::-1] yields "๐Ÿ‡ง๐Ÿ‡ฌ".

I thought I'd try your Reverse() func:

assert.Equal(t, Reverse("๐Ÿ‡ฌ๐Ÿ‡ง"), "๐Ÿ‡ฌ๐Ÿ‡ง")

and I got:

Error Trace:	xstrings_test.go:86
    
Error:      	Not equal: 
    
                expected: "๐Ÿ‡ง๐Ÿ‡ฌ"
    
                actual  : "๐Ÿ‡ฌ๐Ÿ‡ง"
    
Test:       	TestReverse

Flags are special in that they are specific ordering of two letters to designate the ISO-3166 country code, which for some systems and fonts is rendered as that country's flag1:

Regional indicator symbols
These characters can be used in pairs to represent regional
codes. In some emoji implementations, certain pairs may be
recognized and displayed by alternate means; for instance,
an implementation might recognize F + R and display this
combination with a symbol representing the flag of France

Footnotes

  1. https://www.unicode.org/charts/PDF/U1F100.pdf โ†ฉ

Add Multiple If ElseIf ElseIf Else...

I only see the IfThenElse, and this only take 3 arguments.
So I made this:

func ifThenElseIfElse(args ...interface{}) interface{} {
	if len(args) < 3 || len(args)%2 == 0 {
		panic("Need to pass at least 3 arguments, and argument length must be an odd number")
	} else {
		for i, value := range args {
			if i%2 == 0 && len(args) > 3 { //loop even index only
				if reflect.ValueOf(args[i]).Kind() != reflect.Bool {
					if i != len(args)-1 {
						panic(fmt.Sprintf("Value at param %d need to be a boolean!", i+1))
					}
				} else {
					if value.(bool) {
						return args[i+1]
					}
				}
			}
		}
	}
	return args[len(args)-1]
}

With that, we can do a complex one like:

if (1 < 2) ? 1 : (2 > 3) ? 2 : (3 > 3) ? 3 : 4

Test case:
https://go.dev/play/p/nK0p-U3An__2

I'm new to Golang, so that function might look like a mess, but you got the idea.
Thanks

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.