utils

package module
Version: v1.14.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 19, 2021 License: MIT Imports: 38 Imported by: 2

README

Utils

tests

Various utility functions

Array Functions

  • ArrayContains(array interface{}, val interface{}) (exists bool, index int)
  • ArrayMerge(array ...[]interface{}) []interface{}
  • IsStringArrayEqual(a, b []string) bool

Email Functions

  • EmailSend(from string, to []string, subject string, htmlMessage string) (bool, error)

Environment Variables Functions

  • AppAddress() string - returns the URL addree the app is running under (APP_URL:APP:PORT)
  • AppEnv() string - returns the environment the app is running in (APP_ENV)
  • AppInDevelopment() bool - return whether app is in development
  • AppInLive() bool - return whether app is in production / live
  • AppInProduction() bool - return whether app is in production / live
  • AppInTesting() bool - return whether app is being tested
  • AppName() string - returns the name the app is running under (APP_NAME)
  • AppPort() string - returns the port the app is running under (APP_PORT)
  • AppURL() string - returns the URL the app is running under (APP_URL)
  • DbDriver() string - returns the database driver (DB_DRIVER)
  • DbHost() string - returns the database host (DB_HOST)
  • DbPort() string - returns the database port (DB_PORT)
  • DbDatabase() string - returns the database name driver (DB_DATABASE)
  • DbUsername() string - returns the database username (DB_USERNAME)
  • DbPassword() string - returns the database password (DB_PASSWORD)
  • EmailFromAddress() string - returns the mail from address (MAIL_FROM)
  • EmailFromName() string - returns the mail from name (MAIL_NAME)
  • Env(key string) string - returns an enviroment vaialble (i.e. Env("DB_DRIVER"))
  • EnvIntialize(key string) string - Intializes an .env file, if exists. Fails loudly if the file is invalid and cannot be parsed
utils.EnvIntialize()

Fiber Framework Functions

  • FiberAllIps(c *fiber.Ctx) []string
  • FiberGetValueArray2D(c *fiber.Ctx, key string, defaultValue ...[]string)
  • FiberReq(c *fiber.Ctx, key string, valueDefault string) string - returns the trimmed key from POST, or default value if empty

File Functions

  • FileExists(filePath string) bool
  • FileGetContents(filename string) (string, error)
  • FilePutContents(filename string, data string, mode os.FileMode) error - writes a string to file
  • FileToBase64(filePath string) string - converts a file to Base64 encoded string
  • ImgToBase64Url(filePath string) string - converts an image file to Base64 encoded URL string

HTML Functions

  • MinCSS(cssString string) (string, error) - Minifies a Css string
  • MinHTML(htmlString string) (string, error) - Minifies a HTML string
  • MinScript(sctiptString string) (string, error) - Minifies a JavaScript string
  • ScriptsHTML(str string) string - returns an HTML fragment of scripts tags, with embedded and minified local scripts (mainly suitable for serverless environment)
  • StylesHTML(str string) string - returns an HTML fragment of scripts tags, with embedded and minified local styles (mainly suitable for serverless environment)

HTTP Functions

  • IP(r *http.Request) string - Returns the IP address of the user
  • Req(r *http.Request, key string, defaultValue string) string - Returns a POST or GET value for a key, or default if not exists
  • RespondJSON(w http.ResponseWriter, response api.Response) - Respond returns an API response as JSON

Interface Functions

  • InterfaceToStringArray(v interface{}) []string
  • LinkWebsite() string

Map Functions

  • MapToColumn(inputMap []map[string]string, keyName string) []string - returns a column from map
  • MapToKeyValue(inputMap []map[string]string, keyName string, valueName string) map[string]string - returns a key-value array from an array of maps

String Functions

  • AddSlashes(str string) string - adds slashes
  • Base64Decode(src string) ([]byte, error) - decodes a string from Base64
  • Base64Encode(src []byte) string - encodes a string to Base64
  • RandStr(len int) string - generates a random string
  • RandStrFromGamma(length int, gamma string) string - generates random string of specified length with the characters specified in the gamma string
  • Slugify(s string, replaceWith rune) string - converts a string to slug
  • StrToInt(s string) (int, error) - converts a string to Int32
  • StrToInt64(s string) (int64, error) - converts a string to Int64
  • StrToMD5Hash(text string) string - StrToMD5Hash converts a string to MD5 hash
  • StrToSHA1Hash(text string) string - StrToSHA1Hash converts a string to SHA1 hash
  • StrToSHA256Hash(text string) string - converts a string to SHA256 hash
  • TemplateParseString(template string, data) string - parses a template file and returns as string
  • ToString(v interface{}) string - converts an interface to string

Time Functions

  • StrToTimeUnix(str string) (int64, error) - converts string to Unix time

Other Functions

  • IsNumeric(s string) bool - checks if a string is numeric

Change Log

2021-07-19 - Added functions AppInDevelopment, AppInLive, AppInProduction, AppInTesting

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddSlashes added in v1.4.5

func AddSlashes(str string) string

AddSlashes addslashes()

func AppAddress added in v1.2.1

func AppAddress() string

AppAddress return the full URL and PORT for the app

func AppEnv added in v1.2.1

func AppEnv() string

AppEnv returns the environment the app is running in

func AppInDevelopment added in v1.14.0

func AppInDevelopment() bool

AppInDevelopment return whether app is in development

func AppInLive added in v1.14.0

func AppInLive() bool

AppInProduction return whether app is in production / live

func AppInProduction added in v1.14.0

func AppInProduction() bool

AppInProduction return whether app is in production / live

func AppInTesting added in v1.14.0

func AppInTesting() bool

AppInTesting return whether app is being tested

func AppName added in v1.2.1

func AppName() string

AppName return the name for the app

func AppPort added in v1.2.1

func AppPort() string

AppPort return the port for the app

func AppURL added in v1.2.1

func AppURL() string

AppURL return the URL for the app

func ArrayContains added in v1.3.7

func ArrayContains(array interface{}, val interface{}) (exists bool, index int)

ArrayContains checks whether an array contains the specified value

func ArrayMerge added in v1.7.0

func ArrayMerge(array ...[]interface{}) []interface{}

ArrayMerge merges two arrays

func Base64Decode added in v1.13.0

func Base64Decode(src string) ([]byte, error)

Base64Encode decodes a string from Base64

func Base64Encode added in v1.13.0

func Base64Encode(src []byte) string

Base64Encode encodes a string to Base64

func DbDatabase added in v1.2.2

func DbDatabase() string

DbDatabase return the name of the database

func DbDriver added in v1.2.2

func DbDriver() string

DbDriver returns the driver of the database

func DbHost added in v1.2.2

func DbHost() string

DbHost return the host of the database

func DbPassword added in v1.2.2

func DbPassword() string

DbPassword return the password for the database

func DbPort added in v1.2.2

func DbPort() string

DbPort return the port of the database

func DbUsername added in v1.2.2

func DbUsername() string

DbUsername return the username of the database

func EmailFromAddress added in v1.2.1

func EmailFromAddress() string

EmailFromAddress return the URL for the app

func EmailFromName added in v1.2.1

func EmailFromName() string

EmailFromName return the URL for the app

func EmailSend added in v1.3.5

func EmailSend(from string, to []string, subject string, htmlMessage string) (bool, error)

EmailSend sends an email

func Env added in v1.2.1

func Env(key string) string

Env returns the value for an environment key

func EnvInitialize added in v1.4.7

func EnvInitialize()

EnvInitialize intializes the envorinment variables

func FiberAllIps added in v1.3.7

func FiberAllIps(c *fiber.Ctx) []string

FiberAllIps returns all the IPs in the chain

func FiberGetValueArray2D added in v1.1.2

func FiberGetValueArray2D(c *fiber.Ctx, key string, defaultValue ...[]string) [][]string

FiberGetValueArray2D return a 2D array of a post body Defaults to nil if the form key doesn't exist. If a default value is given, it will return that value if the form key does not exist. Returned value is only valid within the handler. Do not store any references. Make copies or use the Immutable setting instead.

func FiberReq added in v1.4.8

func FiberReq(c *fiber.Ctx, key string, valueDefault string) string

FiberReq return the key from the $_REQUEST, or default value if empty

func FileExists added in v1.2.4

func FileExists(filePath string) bool

FileExists checks if a file exists

func FileGetContents added in v1.2.4

func FileGetContents(filename string) (string, error)

FileGetContents reads entire file into a string

func FilePutContents added in v1.5.0

func FilePutContents(filename string, data string, mode os.FileMode) error

FilePutContents adds content to file

func FileToBase64 added in v1.4.12

func FileToBase64(filePath string) string

FileToBase64 converts a file to Base64 encoded string

func IP added in v1.8.0

func IP(r *http.Request) string

IP gets the IP address for the user

func ImgToBase64Url added in v1.4.12

func ImgToBase64Url(filePath string) string

ImgToBase64Url converts an image file to Base64 encoded URL string

func InterfaceToStringArray added in v1.4.1

func InterfaceToStringArray(v interface{}) []string

InterfaceToStringArray converts an interface to String array

func IsNumeric added in v1.3.5

func IsNumeric(s string) bool

IsNumeric checks if a string is numeric

func IsStringArrayEqual added in v1.4.1

func IsStringArrayEqual(a, b []string) bool

IsStringArrayEqual checks whether 2 string arrays are the same

func LinkWebsite added in v1.3.1

func LinkWebsite() string

LinkWebsite returns a URL to the current website

func MapToColumn added in v1.3.2

func MapToColumn(inputMap []map[string]string, keyName string) []string

MapToColumn Returns a column from map

func MapToKeyValue added in v1.3.2

func MapToKeyValue(inputMap []map[string]string, keyName string, valueName string) map[string]string

MapToKeyValue returns a key-value array an array of maps

func MinCSS added in v1.4.6

func MinCSS(cssString string) (string, error)

MinCSS minifies the CSS

func MinHTML added in v1.4.6

func MinHTML(htmlString string) (string, error)

MinHTML minifies the CSS

func MinScript added in v1.4.6

func MinScript(sctiptString string) (string, error)

MinScript minifies the script

func Query added in v1.7.0

func Query(db *sql.DB, query string) (result map[int]map[string]string, err error)

Query executes a SQL query and returns array of key value maps

func RandStr added in v1.3.4

func RandStr(length int) string

RandStr generates random string of specified length

func RandStrFromGamma added in v1.13.0

func RandStrFromGamma(length int, gamma string) string

RandStrFromGamma generates random string of specified length with the characters specified in the gamma string

func Req added in v1.3.5

func Req(r *http.Request, key string, defaultValue string) string

Req returns a POST or GET key, or default if not exists

func RespondJSON added in v1.4.10

func RespondJSON(w http.ResponseWriter, response api.Response)

RespondJSON returns an API response as JSON

func ScriptsHTML added in v1.4.6

func ScriptsHTML(str string) string

ScriptsHTML the HTML from scripts string

func Slugify added in v1.3.2

func Slugify(s string, replaceWith rune) string

Slugify replaces each run of characters which are not ASCII letters or numbers with the Replacement character, except for leading or trailing runs. Letters will be stripped of diacritical marks and lowercased. Letter or number codepoints that do not have combining marks or a lower-cased variant will be passed through unaltered.

func StrToInt added in v1.7.0

func StrToInt(s string) (int, error)

StrToInt converts a string to Int32

func StrToInt64 added in v1.7.0

func StrToInt64(s string) (int64, error)

StrToInt64 converts a string to Int64

func StrToMD5Hash added in v1.13.0

func StrToMD5Hash(text string) string

StrToMD5Hash converts a string to MD5 hash

func StrToSHA1Hash added in v1.13.0

func StrToSHA1Hash(text string) string

StrToSHA1Hash converts a string to SHA1 hash

func StrToSHA256Hash added in v1.13.0

func StrToSHA256Hash(text string) string

StrToSHA256Hash converts a string to SHA256 hash

func StrToTimeUnix added in v1.7.0

func StrToTimeUnix(str string) (int64, error)

StrToTimeUnix converts sting to Unix time

func StylesHTML added in v1.4.6

func StylesHTML(str string) string

StylesHTML the styles

func TemplateParseString

func TemplateParseString(templateString string, data interface{}) string

TemplateParseString parses a template string with the passed data

func ToString added in v1.6.0

func ToString(v interface{}) string

ToString converts an interface to string

func Unlink(filename string) error

Unlink deletes a file

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL