snow/aid/aid.go

83 lines
1.3 KiB
Go
Raw Normal View History

2023-11-01 00:05:17 +00:00
package aid
import (
"fmt"
2023-11-01 00:05:17 +00:00
"os"
"os/signal"
"regexp"
2023-12-13 22:52:16 +00:00
"strconv"
2023-11-01 00:05:17 +00:00
"syscall"
)
func WaitForExit() {
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-sc
}
2023-12-13 22:52:16 +00:00
func FormatNumber(number int) string {
str := ""
for i, char := range ReverseString(strconv.Itoa(number)) {
if i % 3 == 0 && i != 0 {
str += ","
}
str += string(char)
}
return ReverseString(str)
}
func FormatPrice(number int) string {
last := number % 100
number /= 100
str := fmt.Sprintf("%d.%02d", number, last)
return str
}
2023-12-13 22:52:16 +00:00
func ReverseString(input string) string {
str := ""
for _, char := range input {
str = string(char) + str
}
return str
2024-02-10 00:34:12 +00:00
}
func ToHex(number int) string {
inta := strconv.FormatInt(int64(number), 16)
if len(inta) == 1 {
return "0" + inta
}
return inta
}
func Regex(str, regex string) *string {
reg := regexp.MustCompile(regex).FindStringSubmatch(str)
if len(reg) > 1 {
return &reg[1]
}
return nil
2024-02-14 20:30:55 +00:00
}
// if condition is true, return a, else return b
2024-02-14 20:30:55 +00:00
func Ternary[T any](condition bool, a, b T) T {
if condition {
return a
}
return b
}
func ToInt(str string) int {
i, _ := strconv.Atoi(str)
return i
}
func Flatten[T any](arr [][]T) []T {
var flat []T
for _, a := range arr {
flat = append(flat, a...)
}
return flat
2023-11-01 00:05:17 +00:00
}