gelpers

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2020 License: MIT Imports: 13 Imported by: 0

README

gelpers

Go GoDoc Go Report Card Go Version codecov

Description

This is a collection of miscellaneous helper functions that I've found myself re-writing for different projects.

Documentation

Overview

Package gelpers provides miscellaneous helper functions.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func IntMatrixTranspose added in v0.2.0

func IntMatrixTranspose(m [][]int) [][]int

IntMatrixTranspose returns a 2d slice such that input[r][c] = output[c][r]. It expects a non-nil, non-empty 2d slice where each row has the same length.

Example
package main

import (
	"fmt"

	"github.com/ecshreve/gelpers"
)

func main() {
	m := [][]int{{1, 2, 3}, {1, 2, 3}, {1, 2, 3}}
	fmt.Println(gelpers.IntMatrixTranspose(m))
}
Output:

[[1 1 1] [2 2 2] [3 3 3]]

func IntSliceContains

func IntSliceContains(s []int, v int) bool

IntSliceContains checks if the given slice contains the given value.

Example
package main

import (
	"fmt"

	"github.com/ecshreve/gelpers"
)

func main() {
	s := []int{1, 2, 3}
	fmt.Println(gelpers.IntSliceContains(s, 2))
}
Output:

true

func IntSliceMax added in v0.2.0

func IntSliceMax(s []int) int

IntSliceMax returns the largest value in the given slice of ints, or -1 if the slice is empty or nil.

Example
package main

import (
	"fmt"

	"github.com/ecshreve/gelpers"
)

func main() {
	s := []int{1, 2, 3}
	fmt.Println(gelpers.IntSliceMax(s))
}
Output:

3

func IntSliceMin added in v0.2.0

func IntSliceMin(s []int) int

IntSliceMin returns the smallest value in the given slice of ints, or -1 if the slice is empty or nil.

Example
package main

import (
	"fmt"

	"github.com/ecshreve/gelpers"
)

func main() {
	s := []int{1, 2, 3}
	fmt.Println(gelpers.IntSliceMin(s))
}
Output:

1

func IntSliceSum added in v0.2.0

func IntSliceSum(s []int) int

IntSliceSum returns the sum of all items in the given slice of ints.

Example
package main

import (
	"fmt"

	"github.com/ecshreve/gelpers"
)

func main() {
	s := []int{1, 2, 3}
	fmt.Println(gelpers.IntSliceSum(s))
}
Output:

6

func ReadJSONFile added in v0.3.0

func ReadJSONFile(path string) (*map[string]interface{}, error)

ReadJSONFile returns a pointer to a map representation of the JSON file at the given path, or an error if unsuccessful.

Example
package main

import (
	"fmt"
	"log"

	"github.com/ecshreve/gelpers"
)

func main() {
	// The example JSON file contains the following:
	//	{
	//	  "data": {
	//	    "outer_key": {
	//	      "inner_key": {
	//	        "v1": "here's a string",
	//	        "v2": 123
	//	       }
	//	     }
	//	   }
	// 	}

	dataMap, err := gelpers.ReadJSONFile("testdata/example.json")
	if err != nil {
		log.Fatal(err)
	}

	// Now we can do whatever you want with the data map.
	fmt.Println(dataMap)
}
Output:

&map[data:map[outer_key:map[inner_key:map[v1:here's a string v2:123]]]]

func SnakeToCamel

func SnakeToCamel(s string) string

SnakeToCamel converts a snake_case_string to a CamelCaseString. If the input is not valid then it returns the input string.

A string is considered valid if it satisfies the following:

  • non-empty
  • begins with a letter
  • only contains letters, numbers, and the underscores
Example
package main

import (
	"fmt"

	"github.com/ecshreve/gelpers"
)

func main() {
	fmt.Println(gelpers.SnakeToCamel("foo_bar_baz"))
}
Output:

FooBarBaz

func SpaceSepToCamel

func SpaceSepToCamel(s string) string

SpaceSepToCamel converts a space separated string to a CamelCaseString. If the input is not valid then it returns the input string.

A string is considered valid if it satisfies the following:

  • non-empty
  • begins with a letter
  • only contains letters, numbers, and spaces
Example
package main

import (
	"fmt"

	"github.com/ecshreve/gelpers"
)

func main() {
	fmt.Println(gelpers.SpaceSepToCamel("foo bar baz"))
}
Output:

FooBarBaz

func StringPtr

func StringPtr(str string) *string

StringPtr conveniently converts a string literal into a pointer to string.

Example
package main

import (
	"fmt"

	"github.com/ecshreve/gelpers"
)

func main() {
	sPtr := gelpers.StringPtr("hello")
	fmt.Printf("%T", sPtr)
}
Output:

*string

func StringVal

func StringVal(strPtr *string) string

StringVal conveniently converts a pointer to string to a string literal, if the given pointer is nil it returns an empty string.

Example
package main

import (
	"fmt"

	"github.com/ecshreve/gelpers"
)

func main() {
	s := "hello"
	sPtr := &s
	sVal := gelpers.StringVal(sPtr)
	fmt.Printf("%T", sVal)
}
Output:

string
Example (Second)
package main

import (
	"fmt"

	"github.com/ecshreve/gelpers"
)

func main() {
	var sPtr *string
	sVal := gelpers.StringVal(sPtr)
	fmt.Println(sVal == "")
}
Output:

true

func WriteCSVFile added in v0.3.0

func WriteCSVFile(data [][]string, path *string) (*os.File, error)

WriteCSVFile writes the given 2d slice of strings to a CSV file at the given path and returns a pointer to the output file, or an error if unsuccessful.

This function treats the first row in the data argument as the headers for the CSV file.

If no path is provided, then a default filename is generated for the output. The default directory for the output file is the root directory of the module. The filename is of the form `data_<ms_epoch>.output.csv`.

If a path is provided it must be for a CSV file.

Example
package main

import (
	"fmt"
	"log"
	"os"

	"github.com/ecshreve/gelpers"
)

func main() {
	data := [][]string{
		{"one", "two"},
		{"one_one", "two_two"},
	}

	outfilePath := "testdata/example.csv"
	dataFile, err := gelpers.WriteCSVFile(data, &outfilePath)
	if err != nil {
		log.Fatal(err)
	}
	defer os.Remove(dataFile.Name())

	// Now we can do whatever we want with the output file.
	fileInfo, _ := os.Stat(dataFile.Name())
	fmt.Println(fileInfo.Size())
}
Output:

24

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL