Giter Site home page Giter Site logo

sliceconv's Introduction

sliceconv

GoDoc Build Status Coverage Status Go Report Card

Sliceconv implements conversions to and from string representations of primitive types on entire slices. The package supports types int, float64, bool, and string. This package takes its cue from the Golang standard library's strconv.

Installation

If you do not have Go installed yet, you can find installation instructions here.

To pull the most recent version of sliceconv, use go get.

go get -u github.com/Henry-Sarabia/sliceconv

Then import the package into your project.

import "github.com/Henry-Sarabia/sliceconv"

Usage

Strings to Integers

As an example, assume you have a list of test scores as a slice of strings. You mean to find the highest score by passing the list into a findMax function. Unfortunately, the function has the signature findMax([]int) int. Using sliceconv, simply convert the slice of strings into a slice of ints to resolve the issue.

scores := []string{"98", "85", "100", "76", "92"}

ints, err := sliceconv.Atoi(scores)
if err != nil {
	// handle error
}

max := findMax(ints)
// output: 100

Be aware that the sliceconv.Atoi function fulfills the same contract as its counterpart strconv.Atoi. That is to say, the Atoi functions treat the strings as integers in base 10 and will return an error if any of the resulting integers cannot fit into the regular int type.

Integers to Strings

This time around, assume you still have a list of test scores but as a slice of ints. You mean to format a class report card by passing the list of scores into a formatReport function. With a streak of bad luck, the function has the signature formatReport([]string) string. With sliceconv, you can just convert the slice of ints into a slice of strings to satisfy the requirements.

scores := []int{98, 85, 100, 76, 92}

str := sliceconv.Itoa(scores)
report := formatReport(str)
// output: student_1: 98, student_2: 85, ..., student_5: 92

Similarly to sliceconv.Atoi, the sliceconv.Itoa function assumes the integers are in base 10.

Strings to Floats

Sticking to the test score example, assume you have a list of test scores as a slice of strings. You mean to find the average score by passing the list into an averaging function with the signature findAvg([]float64) float64. Taking advantage of sliceconv, you can convert the entire list of strings into a slice of floats.

scores := []string{"98.5", "85.1", "100", "76.9", "92.3"}

flts, err := sliceconv.Atof(scores)
if err != nil {
	// handle error
}

avg := findAvg(flts)
// output: 90.56

Be aware that the sliceconv.Atof function assumes the stringified floats have a bitsize of 64.

Floats to Strings

Again, assume you have a list of test scores but as a slice of float64s. You mean to format a class report card by passing the list of scores into a formatReport function. The function has the following signature: formatReport([]string) string. Using sliceconv, simply convert the slice of float64s into a slice of strings for the formatReport function.

scores := []float64{98.5, 85.1, 100, 76.9, 92.3}

str := sliceconv.Ftoa(scores)
report := formatReport(str)
// output: student_1: 98.5, student_2: 85.1, ..., student_5: 92.3

Please take note that the sliceconv.Ftoa function will fulfill the following contract: the string representation of the provided floats will have no exponent, the smallest precision needed to properly represent the number, and a bitsize of 64.

Strings to Bools

Let's assume a slightly more contrived example to get things going. Assume you have student's answer to a question asking them to list out the truth table for a logical conjuction. The answer is provided as a two dimensional slice. You have a function checkTable but it expects a 2D slice of bools, not strings. To resolve this, you can simply iterate through the slices and pass them each into one of the sliceconv functions.

str := []string{
	[]string{"T", "T", "T"},
    []string{"T", "F", "F"},
    []string{"F", "T", "F"},
    []string{"F", "F", "F"},
}

var bools []bool
for _, s := range str {
	b, err := sliceconv.Atob(s) // conversion happens here
	if err != nil {
		// handle error
	}
	
	bools = append(bools, b)
}

ok := checkTable(bools)
// output: true

As you can see from the example, the Atob function isn't limited to the straightfoward "true" and "false" strings. In fact, Atob is not case-sensitive and can accept the following: "1", "t", "true", "0", "f", and "false".

Bools to Strings

For this example, suppose you have the response to a true or false survey for a single student. You have the answers in the form of a slice of bools. You mean to display these answers in a report using a function with the signature formatReport([]string) string. Because this function only accepts a slice of strings, you will need to use the sliceconv package to convert your slice of bools.

bools := []bool{true, false, false, true, true}

str := sliceconv.Btoa(bools)
report := formatReport(str)
// output: Q1: true, Q2: false, Q3: false, Q4: true, Q5: true

Contributions

If you would like to contribute to this project, please adhere to the following guidelines.

  • Submit an issue describing the problem.
  • Fork the repo and add your contribution.
  • Add appropriate tests.
  • Run go fmt, go vet, and golint.
  • Prefer idiomatic Go over non-idiomatic code.
  • Follow the basic Go conventions found here.
  • If in doubt, try to match your code to the current codebase.
  • Create a pull request with a description of your changes.

I'll review pull requests as they come in and merge them if everything checks out.

Any and all contributions are greatly appreciated. Thank you!

sliceconv's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

sliceconv's Issues

Package should have tagged releases

Adopting semantic versioning using git tags will allow users to differentiate versions of the package and, more importantly, identify when the package has made a breaking change.

Reflect v1.0.1 in README

When issue #18 is resolved, the README needs to be updated to reflect the changes. One of the examples lists the current accepted boolean string representations.

Missing support for bools

The current package only supports slice conversion between strings and integers. Add support for bool as well.

README should reflect float support

As of now, the README only describes the usage of the package between integers and strings. It should be updated to reflect the newly added support for floats and strings as well.

Missing support for floats

The current package only supports slice conversion between strings and integers. Add support for float64 as well.

README should reflect bool support

As of now, the README only describes the usage of the package between integers, floats, and strings. It should be updated to reflect the newly added support for bools and strings as well.

Package is missing a README

There is no README for this package yet. Add a README file with information on what the package is used for, how to use it and how to contribute. Also add build status, test coverage, and Go report card score.

Change Atob to be case-insensitive

As of v1.0.0, only "true", "True", and "TRUE" are valid string representations of boolean value true. This is the same for the false boolean value. There is no reason to not accept any combination of upper and lowercase characters.

Variadic parameters are unwieldly in this context

Although variadic parameters seemed like a good idea at the time of creating the package, they actually lead to extra boilerplate when calling the package functions. The sliceconv package is meant for converting slices of primitive types to another slice of a primitive type; the use-case of converting a single variable is already covered under the regular strconv package and is out of scope for this package.

Package functions should simply take a slice of primitive types.

Atoi tests are missing error cases

The table driven tests for the Atoi function are missing the error cases; cases where one or more of the provided strings are not integers. These cases should be added for full test coverage.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.