forked from miguelpragier/handy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandy.go
646 lines (521 loc) · 17 KB
/
handy.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
// Package handy is a toolbelt with utilities and helpers like validators, sanitizers and string formatters.
// There are routines to filter strings, convert between types, validate passwords with custom rules, easily format dates and much more.
package handy
import (
"crypto/sha256"
"fmt"
"math/rand"
"regexp"
"strconv"
"strings"
"time"
"unicode"
"unicode/utf8"
)
// CheckEmail returns true if the given sequence is a valid email address
// See https://tools.ietf.org/html/rfc2822#section-3.4.1 for details about email address anatomy
func CheckEmail(email string) bool {
if email == "" {
return false
}
re := 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 re.MatchString(email)
}
const (
// CheckNewPassword() Possible results
// CheckNewPasswordResultOK Means the checking ran alright
CheckNewPasswordResultOK = 0
// CheckNewPasswordResultDivergent Password is different from confirmation
CheckNewPasswordResultDivergent = 1
// CheckNewPasswordResultTooShort Password is too short
CheckNewPasswordResultTooShort = 2
// CheckNewPasswordResultTooSimple Given string doesn't satisfy complexity rules
CheckNewPasswordResultTooSimple = 3
// CheckNewPassword() Complexity Rules
// CheckNewPasswordComplexityLowest There's no rules besides the minimum length
// >>> This flag turns all others off <<<
CheckNewPasswordComplexityLowest = 1
// CheckNewPasswordComplexityRequireLetter At least one letter is required in order to aprove password
CheckNewPasswordComplexityRequireLetter = 2
// CheckNewPasswordComplexityRequireUpperCase At least one uppercase letter is required in order to aprove password.
// Only works if CheckNewPasswordComplexityRequireLetter is included/activated
CheckNewPasswordComplexityRequireUpperCase = 4
// CheckNewPasswordComplexityRequireNumber At least one number is required in order to aprove password
CheckNewPasswordComplexityRequireNumber = 8
// CheckNewPasswordComplexityRequireSpace The password must contain at least one space
CheckNewPasswordComplexityRequireSpace = 16
// CheckNewPasswordComplexityRequireSymbol User have to include at least one special character, like # or -
CheckNewPasswordComplexityRequireSymbol = 32
)
// CheckNewPassword Run some basic checks on new password strings, based on given options
// This routine requires at least 4 (four) characters
// Example requiring only basic minimum length: CheckNewPassword("lalala", "lalala", 10, CheckNewPasswordComplexityLowest)
// Example requiring number and symbol: CheckNewPassword("lalala", "lalala", 10, CheckNewPasswordComplexityRequireNumber|CheckNewPasswordComplexityRequireSymbol)
func CheckNewPassword(password, passwordConfirmation string, minimumlength uint, flagComplexity uint8) uint8 {
const minPasswordLengthDefault = 4
if minimumlength < minPasswordLengthDefault {
minimumlength = 4
}
if utf8.RuneCountInString(strings.TrimSpace(password)) < int(minimumlength) {
return CheckNewPasswordResultTooShort
}
if password != passwordConfirmation {
return CheckNewPasswordResultDivergent
}
letterFound := false
numberFound := false
symbolFound := false
spaceFound := false
upperCaseFound := false
if flagComplexity&CheckNewPasswordComplexityLowest != CheckNewPasswordComplexityLowest {
s := []rune(password)
for _, r := range s {
if unicode.IsLetter(r) {
letterFound = true
if unicode.IsUpper(r) {
upperCaseFound = true
}
}
if unicode.IsNumber(r) {
numberFound = true
}
if RuneHasSymbol(r) {
symbolFound = true
}
if r == ' ' {
spaceFound = true
}
}
}
if flagComplexity&CheckNewPasswordComplexityRequireLetter == CheckNewPasswordComplexityRequireLetter {
if !letterFound {
return CheckNewPasswordResultTooSimple
}
// Only checks uppercase if letter is required
if flagComplexity&CheckNewPasswordComplexityRequireUpperCase == CheckNewPasswordComplexityRequireUpperCase {
if !upperCaseFound {
return CheckNewPasswordResultTooSimple
}
}
}
if flagComplexity&CheckNewPasswordComplexityRequireNumber == CheckNewPasswordComplexityRequireNumber {
if !numberFound {
return CheckNewPasswordResultTooSimple
}
}
if flagComplexity&CheckNewPasswordComplexityRequireSymbol == CheckNewPasswordComplexityRequireSymbol {
if !symbolFound {
return CheckNewPasswordResultTooSimple
}
}
if flagComplexity&CheckNewPasswordComplexityRequireSpace == CheckNewPasswordComplexityRequireSpace {
if !spaceFound {
return CheckNewPasswordResultTooSimple
}
}
return CheckNewPasswordResultOK
}
//RuneHasSymbol returns true if the given rune contains a symbol
func RuneHasSymbol(ru rune) bool {
allowedSymbols := []rune("!\"#$%&'()*+´-./:;<=>?@[\\]^_`{|}~")
for _, r := range allowedSymbols {
if ru == r {
return true
}
}
return false
}
// StringHash simply generates a SHA256 hash from the given string
func StringHash(s string) string {
h := sha256.New()
h.Write([]byte(s))
sum := h.Sum(nil)
return fmt.Sprintf("%x", sum)
}
// OnlyLetters returns only the letters from the given string, after strip all the rest ( numbers, spaces, etc. )
func OnlyLetters(sequence string) string {
if utf8.RuneCountInString(sequence) == 0 {
return ""
}
var letters []rune
for _, r := range []rune(sequence) {
if unicode.IsLetter(r) {
letters = append(letters, r)
}
}
return string(letters)
}
// OnlyDigits returns only the numbers from the given string, after strip all the rest ( letters, spaces, etc. )
func OnlyDigits(sequence string) string {
if utf8.RuneCountInString(sequence) > 0 {
re, _ := regexp.Compile("[\\D]")
sequence = re.ReplaceAllString(sequence, "")
}
return sequence
}
// OnlyLettersAndNumbers returns only the letters and numbers from the given string, after strip all the rest, like spaces and special symbols.
func OnlyLettersAndNumbers(sequence string) string {
if utf8.RuneCountInString(sequence) == 0 {
return ""
}
var aplhanumeric []rune
for _, r := range []rune(sequence) {
if unicode.IsLetter(r) || unicode.IsDigit(r) {
aplhanumeric = append(aplhanumeric, r)
}
}
return string(aplhanumeric)
}
// RandomInt returns a rondom integer within the given (inclusive) range
func RandomInt(min, max int) int {
rand.Seed(time.Now().UTC().UnixNano())
return rand.Intn(max-min) + min
}
// CheckPhone returns true if a given sequence has between 9 and 14 digits
func CheckPhone(phone string, acceptEmpty bool) bool {
phone = OnlyDigits(phone)
return (acceptEmpty && (phone == "")) || ((len([]rune(phone)) >= 9) && (len([]rune(phone)) <= 14))
}
// StringAsFloat tries to convert a string to float, and if it can't, just returns zero
// It's limited to one billion
func StringAsFloat(s string, decimalSeparator, thousandsSeparator rune) float64 {
if s == "" {
return 0.0
}
const maxLength = 20
if len([]rune(s)) > maxLength {
s = s[0:maxLength]
}
s = strings.Replace(s, string(thousandsSeparator), "", -1)
s = strings.Replace(s, string(decimalSeparator), ".", -1)
if f, err := strconv.ParseFloat(s, 64); err == nil {
return f
}
return 0.0
}
// StringAsInteger returns the integer value extracted from string, or zero
func StringAsInteger(s string) int {
if s == "" {
return 0
}
if i, err := strconv.ParseInt(s, 10, 32); err == nil {
return int(i)
}
return 0
}
// Between checks if param n in between low and high integer params
func Between(n, low, high int) bool {
return n >= low && n <= high
}
// Tif is a simple implementation of the dear ternary IF operator
func Tif(condition bool, tifThen, tifElse interface{}) interface{} {
if condition {
return tifThen
}
return tifElse
}
// Truncate limits the length of a given string, trimming or not, according parameters
func Truncate(s string, maxLen int, trim bool) string {
if s == "" {
return s
}
if len(s) > maxLen {
s = s[0:maxLen]
}
if trim {
s = strings.TrimSpace(s)
}
return s
}
const (
// TransformNone No transformations are ordered. Only constraints maximum length
// TransformNone turns all other flags OFF.
TransformNone = uint8(1)
// TransformFlagTrim Trim spaces before and after process the input
// TransformFlagTrim Trims the string, removing leading and trailing spaces
TransformFlagTrim = uint8(2)
// TransformFlagLowerCase Makes the string lowercase
// If it's combined with TransformFlagUpperCase, only uppercase remains, once is executed later.
TransformFlagLowerCase = uint8(4)
// TransformFlagUpperCase Makes the string uppercase
// If it's combined with TransformFlagLowerCase, only uppercase remains, once it's executed later.
TransformFlagUpperCase = uint8(8)
// TransformFlagOnlyDigits Removes all non-numeric characters
TransformFlagOnlyDigits = uint8(16)
// TransformFlagOnlyLetters Removes all non-letter characters
TransformFlagOnlyLetters = uint8(32)
// TransformFlagOnlyLettersAndDigits Leaves only letters and numbers
TransformFlagOnlyLettersAndDigits = uint8(64)
// TransformFlagHash After process all other flags, applies SHA256 hashing on string for output
// The routine applies handy.StringHash() on given string
TransformFlagHash = uint8(128)
)
// Transform handles a string according given flags/parametrization, as follows:
// The transformations are made in arbitrary order, what can result in unexpected output. It the sequence matters, use TransformSerially instead.
// If maxLen==0, truncation is skipped
// The last operations are, by order, truncation and trimming.
func Transform(s string, maxLen int, transformFlags uint8) string {
if s == "" {
return s
}
if transformFlags&TransformNone == TransformNone {
if maxLen > 0 && utf8.RuneCountInString(s) > maxLen {
s = string([]rune(s)[:maxLen])
}
return s
}
if (transformFlags & TransformFlagOnlyLettersAndDigits) == TransformFlagOnlyLettersAndDigits {
s = OnlyLettersAndNumbers(s)
}
if (transformFlags & TransformFlagOnlyDigits) == TransformFlagOnlyDigits {
s = OnlyDigits(s)
}
if (transformFlags & TransformFlagOnlyLetters) == TransformFlagOnlyLetters {
s = OnlyLetters(s)
}
// Have to trim before and after, to avoid issues with string truncation and new leading/trailing spaces
if (transformFlags & TransformFlagTrim) == TransformFlagTrim {
s = strings.TrimSpace(s)
}
if (transformFlags & TransformFlagLowerCase) == TransformFlagLowerCase {
s = strings.ToLower(s)
}
if (transformFlags & TransformFlagUpperCase) == TransformFlagUpperCase {
s = strings.ToUpper(s)
}
if (transformFlags & TransformFlagHash) == TransformFlagHash {
s = StringHash(s)
}
if s == "" {
return s
}
if maxLen > 0 && utf8.RuneCountInString(s) > maxLen {
s = string([]rune(s)[:maxLen])
}
// Have to trim before and after, to avoid issues with string truncation and new leading/trailing spaces
if (transformFlags & TransformFlagTrim) == TransformFlagTrim {
s = strings.TrimSpace(s)
}
return s
}
// TransformSerially reformat given string according parameters, in the order these params were sent
// Example: TransformSerially("uh lalah 123", 4, TransformFlagOnlyDigits,TransformFlagHash,TransformFlagUpperCase)
// First remove non-digits, then hashes string and after make it all uppercase.
// If maxLen==0, truncation is skipped
// Truncation is the last operation
func TransformSerially(s string, maxLen int, transformFlags ...uint8) string {
if s == "" {
return s
}
for _, flag := range transformFlags {
switch flag {
case TransformFlagOnlyLettersAndDigits:
s = OnlyLettersAndNumbers(s)
case TransformFlagOnlyDigits:
s = OnlyDigits(s)
case TransformFlagOnlyLetters:
s = OnlyLetters(s)
case TransformFlagTrim:
s = strings.TrimSpace(s)
case TransformFlagLowerCase:
s = strings.ToLower(s)
case TransformFlagUpperCase:
s = strings.ToUpper(s)
case TransformFlagHash:
s = StringHash(s)
}
}
if maxLen > 0 && utf8.RuneCountInString(s) > maxLen {
s = string([]rune(s)[:maxLen])
}
return s
}
// MatchesAny returns true if any of the given items matches ( equals ) the subject ( search parameter )
func MatchesAny(search interface{}, items ...interface{}) bool {
for _, v := range items {
if fmt.Sprintf("%T", search) == fmt.Sprintf("%T", v) {
if search == v {
return true
}
}
}
return false
}
// HasOnlyNumbers returns true if the sequence is entirely numeric
func HasOnlyNumbers(sequence string) bool {
if utf8.RuneCountInString(sequence) == 0 {
return false
}
for _, r := range []rune(sequence) {
if !unicode.IsDigit(r) {
return false
}
}
return true
}
// HasOnlyLetters returns true if the sequence is entirely composed by letters
func HasOnlyLetters(sequence string) bool {
if utf8.RuneCountInString(sequence) == 0 {
return false
}
for _, r := range []rune(sequence) {
if !unicode.IsLetter(r) {
return false
}
}
return true
}
// TrimLen returns the runes count after trim the spaces
func TrimLen(text string) int {
if text == "" {
return 0
}
text = strings.TrimSpace(text)
if text == "" {
return 0
}
return utf8.RuneCountInString(text)
}
// CheckMinLen verifies if the rune-count is greater then or equal the given minimum
// It returns true if the given string has length greater than or equal than minLength parameter
func CheckMinLen(value string, minLength int) bool {
value = strings.TrimSpace(value)
return TrimLen(value) >= minLength
}
// IsNumericType checks if an interface's concrete type corresponds to some of golang native numeric types
func IsNumericType(x interface{}) bool {
switch x.(type) {
case uint:
return true
case uint8: // Or byte
return true
case uint16:
return true
case uint32:
return true
case uint64:
return true
case int:
return true
case int8:
return true
case int16:
return true
case int32:
return true
case float32:
return true
case float64:
return true
case complex64:
return true
case complex128:
return true
default:
return false
}
}
// Bit returns only uint8(0) or uint8(1).
// It receives an interface, and when it's a number, and when this number is 0 (zero) it returns 0. Otherwise it returns 1 (one)
// If the interface is not a number, it returns 0 (zero)
func Bit(x interface{}) uint8 {
if IsNumericType(x) && x != 0 {
return 1
}
return 0
}
// Boolean returns the bool version/interpretation of some value;
// It receives an interface, and when this is a number, Boolean() returns flase of zero and true for different from zero.
// If it's a string, try to find "1", "T", "TRUE" to return true.
// Any other case returns false
func Boolean(x interface{}) bool {
if IsNumericType(x) {
return x != 0
}
if s, ok := x.(string); ok {
s = Transform(s, 4, TransformFlagLowerCase|TransformFlagTrim)
return MatchesAny(s, "1", "true", "t")
}
return false
}
// Reverse returns the given string written backwards, with letters reversed.
func Reverse(s string) string {
if utf8.RuneCountInString(s) < 2 {
return s
}
r := []rune(s)
buffer := make([]rune, len(r))
for i, j := len(r)-1, 0; i >= 0; i-- {
buffer[j] = r[i]
j++
}
return string(buffer)
}
const (
// CheckPersonNameResultOK means the name was validated
CheckPersonNameResultOK = 0
// CheckPersonNameResultPolluted The routine only accepts letters, single quotes and spaces
CheckPersonNameResultPolluted = 1
// CheckPersonNameResultTooFewWords The funcion requires at least 2 words
CheckPersonNameResultTooFewWords = 2
// CheckPersonNameResultTooShort the sum of all characters must be >= 6
CheckPersonNameResultTooShort = 3
// CheckPersonNameResultTooSimple The name rule requires that at least one word
CheckPersonNameResultTooSimple = 4
)
// CheckPersonName returns true if the name contains at least two words, one >= 3 chars and one >=2 chars.
// I understand that this is a particular criteria, but this is the OpenSourceMagic, where you can change and adapt to your own specs.
func CheckPersonName(name string, acceptEmpty bool) uint8 {
name = strings.TrimSpace(name)
// If name is empty, AND it's accepted, return ok. Else, cry!
if name == "" {
if !acceptEmpty {
return CheckPersonNameResultTooShort
}
return CheckPersonNameResultOK
}
// Person names doesn't accept other than letters, spaces and single quotes
for _, r := range []rune(name) {
if !unicode.IsLetter(r) && r != ' ' && r != '\'' && r != '-' {
return CheckPersonNameResultPolluted
}
}
// A complete name has to be at least 2 words.
a := strings.Fields(name)
if len(a) < 2 {
return CheckPersonNameResultTooFewWords
}
// At least two words, one with 3 chars and other with 2
found2 := false
found3 := false
for _, s := range a {
if !found3 && utf8.RuneCountInString(s) >= 3 {
found3 = true
continue
}
if !found2 && utf8.RuneCountInString(s) >= 2 {
found2 = true
continue
}
}
if !found2 || !found3 {
return CheckPersonNameResultTooSimple
}
return CheckPersonNameResultOK
}
// StringReplaceAll keeps replacing until there's no more ocurrences to replace.
func StringReplaceAll(original string, replacementPairs ...string) string {
if original == "" {
return original
}
r := strings.NewReplacer(replacementPairs...)
for {
result := r.Replace(original)
if original != result {
original = result
} else {
break
}
}
return original
}