init project
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func ArrayToString(a []int64, delim string) string {
|
||||
return strings.Trim(strings.Replace(fmt.Sprint(a), " ", delim, -1), "[]")
|
||||
}
|
||||
|
||||
func Contains(slice []string, item string) bool {
|
||||
set := make(map[string]struct{}, len(slice))
|
||||
for _, s := range slice {
|
||||
set[s] = struct{}{}
|
||||
}
|
||||
|
||||
_, ok := set[item]
|
||||
return ok
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func FormatCurrency(value float64) string {
|
||||
currencyStr := strconv.FormatFloat(value, 'f', 2, 64)
|
||||
split := strings.Split(currencyStr, ".")
|
||||
n := len(split[0])
|
||||
if n <= 3 {
|
||||
return fmt.Sprintf("Rp %s,%s", split[0], split[1])
|
||||
}
|
||||
var result []string
|
||||
for i := 0; i < n; i += 3 {
|
||||
end := n - i
|
||||
start := end - 3
|
||||
if start < 0 {
|
||||
start = 0
|
||||
}
|
||||
result = append([]string{split[0][start:end]}, result...)
|
||||
}
|
||||
currencyStr = strings.Join(result, ".")
|
||||
return fmt.Sprintf("Rp %s,%s", currencyStr, split[1])
|
||||
}
|
||||
|
||||
const charset = "abcdefghijklmnopqrstuvwxyz" +
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
||||
|
||||
var seededRand *rand.Rand = rand.New(
|
||||
rand.NewSource(time.Now().UnixNano()))
|
||||
|
||||
func GenerateRandomString(length int) string {
|
||||
b := make([]byte, length)
|
||||
for i := range b {
|
||||
b[i] = charset[seededRand.Intn(len(charset))]
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package utils
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestFormatCurrency(t *testing.T) {
|
||||
type args struct {
|
||||
value float64
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "hundred",
|
||||
args: args{
|
||||
value: 626000,
|
||||
},
|
||||
want: "Rp 626.000,00",
|
||||
},
|
||||
{
|
||||
name: "million",
|
||||
args: args{
|
||||
value: 62603300,
|
||||
},
|
||||
want: "Rp 62.603.300,00",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := FormatCurrency(tt.args.value); got != tt.want {
|
||||
t.Errorf("FormatCurrency() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package generator
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func MedicalRecordNumberRand() string {
|
||||
return RandStringRunes(10)
|
||||
}
|
||||
|
||||
func RandStringRunes(n int) string {
|
||||
rand.Seed(time.Now().UnixNano()) // seed the random number generator
|
||||
|
||||
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
b := make([]byte, n)
|
||||
for i := range b {
|
||||
b[i] = letterBytes[rand.Intn(len(letterBytes))]
|
||||
}
|
||||
|
||||
return string(b)
|
||||
}
|
||||
|
||||
// format -> <uuid>-<time unix>
|
||||
func GenerateFileName() string {
|
||||
return fmt.Sprintf("%v-%v", GenerateUUID(), strconv.Itoa(int(time.Now().Unix())))
|
||||
}
|
||||
|
||||
func GenerateUUID() string {
|
||||
id := uuid.New()
|
||||
return id.String()
|
||||
}
|
||||
Reference in New Issue
Block a user