Skip to content

Commit

Permalink
Merge pull request #1 from julyskies/1.0.3
Browse files Browse the repository at this point in the history
1.0.3
  • Loading branch information
peterdee authored Apr 24, 2024
2 parents c71779c + 867e8c7 commit 8268431
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 4 deletions.
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright 2021, Peter Dyumin
Copyright © 2021 - 2024, Peter Dyumin

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
26 changes: 23 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## gohelpers

This repository contains helper functions for Golang applications.
This package contains helper functions for Golang applications.

Minimal required Golang version: **1.16**

### Install

Expand All @@ -20,7 +22,11 @@ go get github.com/julyskies/gohelpers

- `MakeTimestamp() int64`

This helper function returns a UNIX timestamp.
This helper function returns a UNIX timestamp in milliseconds.

- `MakeTimestampSeconds() int64`

This helper function returns a UNIX timestamp in seconds.

- `ObjectValues(object interface{}) []string`

Expand Down Expand Up @@ -51,15 +57,29 @@ func example() {
}

includesInt := gohelpers.IncludesInt(arrayOfInts, 8) // false

includesString := gohelpers.IncludesString(arrayOfStrings, "a") // true

values := gohelpers.ObjectValues(animals) // ["elephant", "hippo", "lion"]

randomString := gohelpers.RandomString(8) // A9is5Try

timestamp := gohelpers.MakeTimestamp() // 1627987461201
timestampMS := gohelpers.MakeTimestamp() // 1627987461201

timestampSeconds := gohelpers.MakeTimestampSeconds() // 1713957122
}
```

### Testing

Tests are located in [`helpers_test.go`](./helpers_test.go)

Run tests:

```shell script
go test
```

### License

[MIT](./LICENSE.md)
10 changes: 10 additions & 0 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"
)

// Check if provided int array includes specified int value
func IncludesInt(array []int, value int) bool {
for _, element := range array {
if element == value {
Expand All @@ -16,6 +17,7 @@ func IncludesInt(array []int, value int) bool {
return false
}

// Check if provided string array includes specified string value
func IncludesString(array []string, value string) bool {
for _, element := range array {
if element == value {
Expand All @@ -25,10 +27,17 @@ func IncludesString(array []string, value string) bool {
return false
}

// Create a UNIX timestamp in milliseconds (13 digits)
func MakeTimestamp() int64 {
return time.Now().UnixNano() / int64(time.Millisecond)
}

// Create a UNIX timestamp in seconds (10 digits)
func MakeTimestampSeconds() int64 {
return time.Now().Unix()
}

// Get an array of string values from struct keys (similar to Object.values() in JS)
func ObjectValues(object interface{}) []string {
var list []string
elements := reflect.ValueOf(object)
Expand All @@ -44,6 +53,7 @@ func ObjectValues(object interface{}) []string {
return list
}

// Create a random alphanumeric string with specified length
func RandomString(length int) string {
if length <= 0 {
return ""
Expand Down
100 changes: 100 additions & 0 deletions helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package gohelpers

import (
"fmt"
"testing"
"time"
)

func TestIncludesInt(t *testing.T) {
arrayOfInts := []int{1, 2, 3, 4, 9}

if ok := IncludesInt(arrayOfInts, 9); !ok {
t.Error("value is included")
}

if ok := IncludesInt(arrayOfInts, 5); ok {
t.Error("value is not included")
}
}

func TestIncludesString(t *testing.T) {
arrayOfStrings := []string{"a", "b", "c"}

if ok := IncludesString(arrayOfStrings, "a"); !ok {
t.Error("value is included")
}

if ok := IncludesString(arrayOfStrings, "x"); ok {
t.Error("value is not included")
}
}

func TestMakeTimestamp(t *testing.T) {
timestampControl := time.Now().UnixNano() / int64(time.Millisecond)

timestamp := MakeTimestamp()

if timestamp < timestampControl {
t.Error("timestamp is invalid")
}

timestampString := fmt.Sprint(timestamp)
if len(timestampString) != 13 {
t.Error("timestamp is invalid")
}
}

func TestMakeTimestampSeconds(t *testing.T) {
timestampControl := time.Now().Unix()

timestamp := MakeTimestampSeconds()
if timestamp < timestampControl {
t.Error("timestamp is invalid")
}

timestampString := fmt.Sprint(timestamp)
if len(timestampString) != 10 {
t.Error("timestamp is invalid")
}
}

func TestObjectValues(t *testing.T) {
type Animals struct {
Elephant string
Hippo string
Lion string
}

animals := Animals{
Elephant: "elephant",
Hippo: "hippo",
Lion: "lion",
}

values := ObjectValues(animals)
if len(values) != 3 {
t.Error("invalid resulting slice length")
}

if !IncludesString(values, "lion") {
t.Error("invalid values are included in resulting slice")
}
}

func TestRandomString(t *testing.T) {
emptyString1 := RandomString(-5)
if len(emptyString1) > 0 {
t.Error("negative string length should result in empty string")
}

emptyString2 := RandomString(0)
if len(emptyString2) > 0 {
t.Error("zero string length should result in empty string")
}

randomString := RandomString(32)
if len(randomString) != 32 {
t.Error("invalid string length")
}
}

0 comments on commit 8268431

Please sign in to comment.