-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
145 lines (134 loc) · 4.83 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package messengerutils
import (
"fmt"
"regexp"
"strconv"
"strings"
"sync"
"time"
)
type MessengerUtils struct {
Verbose bool
}
// PrintInfo prints the provided message to the console with the prefix "INFO" in bold, if the MessengerUtils's verbose flag is set to true. The function accepts a variadic string parameter and concatenates all strings before printing.
// @param {string} message - One or more strings to print as the message.
// @returns {void}
func (m *MessengerUtils) PrintInfo(message ...interface{}) {
if m.Verbose {
finalMessage := formatMessage(message...)
fmt.Printf("\033[1m%s\033[0m: %s\n", "INFO", finalMessage)
}
}
// PrintError formats and prints an error message to standard output.
// @param {string} message - A string representing the error message to display.
// @param {Error} err - An error object providing more information about the error.
// The function formats the error message with the provided message and error object,
// then prints it to the console with a bold "ERROR:" label using ANSI escape codes.
// @returns {void}
func PrintError(message string, err error) {
fmt.Printf("\033[1mERROR:\033[0m %s: %v\n", message, err)
}
// PrintToDo formats and prints a to-do message to standard output.
//
// @param {interface{}} message - An array of any type that forms the to-do message to display.
//
// The function checks each element in the message. If it is a time.Time type,
// it is formatted according to RFC3339. All elements are then converted into
// their string representations, concatenated into a single string,
// then formatted and printed the string to the console with a bold "TODO:" label
// using ANSI escape codes.
//
// @returns {void}
func PrintToDo(message ...interface{}) {
finalMessage := formatMessage(message...)
fmt.Printf("\033[1mTODO:\033[0m %s\n", finalMessage)
}
// Event is a simple event system that allows multiple listeners
// to subscribe and receive notifications when an event is emitted.
// @typedef {Object} Event
// @property {Array<Function>} listeners - An array of listener functions to be called when the event is emitted.
// @property {sync.Mutex} lock - A mutex used for ensuring thread-safety when modifying the listeners array or emitting events.
type Event struct {
listeners []func(interface{})
lock sync.Mutex
}
// Subscribe adds a listener function to the Event object.
// @method
// @memberof Event
// @param {Function} listener - A function to be called when the event is emitted. It takes a single argument, which is the data passed when emitting the event.
// @returns {void}
func (e *Event) Subscribe(listener func(interface{})) {
e.lock.Lock()
defer e.lock.Unlock()
e.listeners = append(e.listeners, listener)
}
// Emit triggers the event, notifying all subscribed listeners with the provided data.
// @method
// @memberof Event
// @param {interface{}} data - The data to be passed to each listener when the event is emitted.
// @returns {void}
func (e *Event) Emit(data interface{}) {
e.lock.Lock()
defer e.lock.Unlock()
for _, listener := range e.listeners {
listener(data)
}
}
func formatMessage(message ...interface{}) string {
finalMessage := ""
for i, word := range message {
if i > 0 {
finalMessage += " "
}
switch v := word.(type) {
case string:
finalMessage += v
case int:
finalMessage += strconv.Itoa(v)
case time.Time:
finalMessage += v.Format(time.RFC3339)
case []uint8:
finalMessage += string(v) // Converts the []uint8 slice to a string
case []uint16:
var numbers []string
for _, val := range v {
numbers = append(numbers, fmt.Sprintf("%d", val))
}
finalMessage += strings.Join(numbers, ", ")
case []uint32:
var numbers []string
for _, val := range v {
numbers = append(numbers, fmt.Sprintf("%d", val))
}
finalMessage += strings.Join(numbers, ", ")
case []uint64:
var numbers []string
for _, val := range v {
numbers = append(numbers, fmt.Sprintf("%d", val))
}
finalMessage += strings.Join(numbers, ", ")
case uint:
finalMessage += fmt.Sprintf("%d", v)
case uint8:
finalMessage += fmt.Sprintf("%d", v)
case uint16:
finalMessage += fmt.Sprintf("%d", v)
case uint32:
finalMessage += fmt.Sprintf("%d", v)
case uint64:
finalMessage += fmt.Sprintf("%d", v)
default:
finalMessage += fmt.Sprintf("Unknown type: %T", v)
}
}
return finalMessage
}
// ValidateEmailRegex checks if the provided email string matches the regex for a valid email address.
// @param {string} email - The email address to validate.
// @returns {bool} - True if the email is valid, false otherwise.
func ValidateEmailRegex(email string) bool {
emailValidationRegexSnippet := regexp.MustCompile(
`^[a-zA-Z0-9.!#$%&'*+/=?^_` + "`" + `{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$`,
)
return emailValidationRegexSnippet.MatchString(email)
}