Skip to content

Commit 9c990c7

Browse files
author
Henry Sarabia
authored
Merge pull request #22 from Henry-Sarabia/dev
Minor Patch V1.0.1
2 parents 0ef79e7 + 40f7110 commit 9c990c7

2 files changed

Lines changed: 37 additions & 33 deletions

File tree

README.md

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
# sliceconv
22

3-
[![GoDoc](https://godoc.org/github.com/Henry-Sarabia/sliceconv?status.svg)](https://godoc.org/github.com/Henry-Sarabia/sliceconv) [![Build Status](https://travis-ci.com/Henry-Sarabia/sliceconv.svg?branch=master)](https://travis-ci.com/Henry-Sarabia/sliceconv) [![Coverage Status](https://coveralls.io/repos/github/Henry-Sarabia/sliceconv/badge.svg?branch=master)](https://coveralls.io/github/Henry-Sarabia/sliceconv?branch=master) [![Go Report Card](https://goreportcard.com/badge/github.com/Henry-Sarabia/sliceconv)](https://goreportcard.com/report/github.com/Henry-Sarabia/sliceconv)
3+
[![GoDoc](https://godoc.org/github.com/Henry-Sarabia/sliceconv?status.svg)](https://godoc.org/github.com/Henry-Sarabia/sliceconv)
4+
[![Build Status](https://travis-ci.com/Henry-Sarabia/sliceconv.svg?branch=master)](https://travis-ci.com/Henry-Sarabia/sliceconv)
5+
[![Coverage Status](https://coveralls.io/repos/github/Henry-Sarabia/sliceconv/badge.svg?branch=master)](https://coveralls.io/github/Henry-Sarabia/sliceconv?branch=master)
6+
[![Go Report Card](https://goreportcard.com/badge/github.com/Henry-Sarabia/sliceconv)](https://goreportcard.com/report/github.com/Henry-Sarabia/sliceconv)
47

58
Sliceconv implements conversions to and from string representations of primitive types on entire slices.
69
The package supports types int, float64, bool, and string.
710
This package takes its cue from the Golang standard library's [strconv](https://golang.org/pkg/strconv/).
811

912
## Installation
1013

11-
If you do not have Go installed yet, you can find installation instructions
12-
[here](https://golang.org/doc/install).
14+
If you do not have Go installed yet, you can find installation instructions [here](https://golang.org/doc/install).
1315

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

1618
```
17-
go get github.com/Henry-Sarabia/sliceconv
19+
go get -u github.com/Henry-Sarabia/sliceconv
1820
```
1921

2022
Then import the package into your project.
@@ -27,10 +29,10 @@ import "github.com/Henry-Sarabia/sliceconv"
2729

2830
### Strings to Integers
2931

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

3537
```go
3638
scores := []string{"98", "85", "100", "76", "92"}
@@ -44,16 +46,16 @@ max := findMax(ints)
4446
// output: 100
4547
```
4648

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

5153
### Integers to Strings
5254

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

5860
```go
5961
scores := []int{98, 85, 100, 76, 92}
@@ -68,9 +70,8 @@ Similarly to `sliceconv.Atoi`, the `sliceconv.Itoa` function assumes the integer
6870
### Strings to Floats
6971

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

7576
```go
7677
scores := []string{"98.5", "85.1", "100", "76.9", "92.3"}
@@ -88,10 +89,10 @@ Be aware that the `sliceconv.Atof` function assumes the stringified floats have
8889

8990
### Floats to Strings
9091

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

9697
```go
9798
scores := []float64{98.5, 85.1, 100, 76.9, 92.3}
@@ -107,11 +108,12 @@ represent the number, and a bitsize of 64.
107108

108109
### Strings to Bools
109110

110-
Let's assume a slightly more contrived example to get things going. Assume you have student's answer
111-
to a question asking them to list out the truth table for a logical conjuction. The answer is provided
112-
as a two dimensional slice. You have a function `checkTable` but it expects a 2D slice of bools, not
113-
strings. To resolve this, you can simply iterate through the slices and pass them each into one of
114-
the sliceconv functions.
111+
Let's assume a slightly more contrived example to get things going.
112+
Assume you have student's answer to a question asking them to list out the truth table for a logical conjuction.
113+
The answer is provided as a two dimensional slice.
114+
You have a function `checkTable` but it expects a 2D slice of bools, not strings.
115+
To resolve this, you can simply iterate through the slices and pass them each into one of the sliceconv functions.
116+
115117
```go
116118
str := []string{
117119
[]string{"T", "T", "T"},
@@ -135,8 +137,8 @@ ok := checkTable(bools)
135137
```
136138

137139
As you can see from the example, the `Atob` function isn't limited to the straightfoward "true"
138-
and "false" strings. In fact, `Atob` can accept the following: "1", "t", "T", "TRUE", "true",
139-
"True", 0, "f", "F", "FALSE", "false", and "False".
140+
and "false" strings. In fact, `Atob` is **not** case-sensitive and can accept the following: "1", "t", "true",
141+
"0", "f", and "false".
140142

141143
### Bools to Strings
142144

sliceconv.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package sliceconv
33
import (
44
"github.com/pkg/errors"
55
"strconv"
6+
"strings"
67
)
78

89
// Atoi converts the provided strings into their respective base 10 integer
@@ -63,14 +64,15 @@ func Ftoa(flts []float64) []string {
6364
}
6465

6566
// Atob converts the provided strings into their respective bool representations.
66-
// Valid strings include "1", "t", "T", "TRUE", "true", "True", 0, "f", "F",
67-
// "FALSE", "false", and "False". If any of the strings are not valid, an error
68-
// is returned.
67+
// Strings that evaluate to true include "1", "t", and "true".
68+
// Strings that evaluate to false include "0", "f", and "false".
69+
// The strings are NOT case-sensitive.
70+
// If any string is invalid, an error is returned.
6971
func Atob(str []string) ([]bool, error) {
7072
var bools []bool
7173

7274
for _, s := range str {
73-
b, err := strconv.ParseBool(s)
75+
b, err := strconv.ParseBool(strings.ToLower(s))
7476
if err != nil {
7577
return nil, errors.Wrap(err, "one or more strings could not be converted to type bool")
7678
}

0 commit comments

Comments
 (0)