Repository: elliotchance/pie
Pull request #135 (2019-05-12 16:34:59)

added: JSONBytes
JSONBytes returns the JSON encoded array as bytes.
One important thing to note is that it will treat a nil slice as an empty
slice to ensure that the JSON value return is always an array.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/elliotchance/pie/135)
<!-- Reviewable:end -->


kochurovro: Hi. I am realy sorry for reopen PR
kochurovro: @elliotchance done
codecov-io: # [Codecov](https://codecov.io/gh/elliotchance/pie/pull/135?src=pr&el=h1) Report
> :exclamation: No coverage uploaded for pull request base (`master@0b41640`). [Click here to learn what that means](https://docs.codecov.io/docs/error-reference#section-missing-base-commit).
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/elliotchance/pie/pull/135/graphs/tree.svg?width=650&token=OvugwUQGM3&height=150&src=pr)](https://codecov.io/gh/elliotchance/pie/pull/135?src=pr&el=tree)

```diff
@@           Coverage Diff           @@
##             master   #135   +/-   ##
=======================================
  Coverage          ?   100%           
=======================================
  Files             ?      8           
  Lines             ?   1323           
  Branches          ?      0           
=======================================
  Hits              ?   1323           
  Misses            ?      0           
  Partials          ?      0
```


| [Impacted Files](https://codecov.io/gh/elliotchance/pie/pull/135?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pie/float64s\_pie.go](https://codecov.io/gh/elliotchance/pie/pull/135/diff?src=pr&el=tree#diff-cGllL2Zsb2F0NjRzX3BpZS5nbw==) | `100% <100%> (ø)` | |
| [pie/carpointers\_pie.go](https://codecov.io/gh/elliotchance/pie/pull/135/diff?src=pr&el=tree#diff-cGllL2NhcnBvaW50ZXJzX3BpZS5nbw==) | `100% <100%> (ø)` | |
| [pie/cars\_pie.go](https://codecov.io/gh/elliotchance/pie/pull/135/diff?src=pr&el=tree#diff-cGllL2NhcnNfcGllLmdv) | `100% <100%> (ø)` | |
| [pie/ints\_pie.go](https://codecov.io/gh/elliotchance/pie/pull/135/diff?src=pr&el=tree#diff-cGllL2ludHNfcGllLmdv) | `100% <100%> (ø)` | |
| [pie/strings\_pie.go](https://codecov.io/gh/elliotchance/pie/pull/135/diff?src=pr&el=tree#diff-cGllL3N0cmluZ3NfcGllLmdv) | `100% <100%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/elliotchance/pie/pull/135?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/elliotchance/pie/pull/135?src=pr&el=footer). Last update [0b41640...47d0448](https://codecov.io/gh/elliotchance/pie/pull/135?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

kochurovro: @elliotchance I resolved merge conflict locally. Travis was passed

Patch:
diff --git a/README.md b/README.md
index 55b7535..93ce1de 100644
--- a/README.md
+++ b/README.md
@@ -194,6 +194,7 @@ The letters in brackets indicate:
 | `Intersect`       | ✓      | ✓      |        |      | n        | Intersect returns elements which exists in all slices. |
 | `Ints` (S)        | ✓      | ✓      | ✓      |      | n        | Transforms each element into an int. |
 | `Join`            | ✓      |        |        |      | n        | A string from joining each of the elements. |
+| `JSONBytes `      | ✓      | ✓      | ✓      |      | n        | The JSON encoded bytes. |
 | `JSONString`      | ✓      | ✓      | ✓      |      | n        | The JSON encoded string. |
 | `Keys`            |        |        |        | ✓    | n        | Returns all keys in the map (in random order). |
 | `Last`            | ✓      | ✓      | ✓      |      | 1        | The last element, or a zeroed value. |
diff --git a/functions/json_bytes.go b/functions/json_bytes.go
new file mode 100644
index 0000000..20b532e
--- /dev/null
+++ b/functions/json_bytes.go
@@ -0,0 +1,20 @@
+package functions
+
+import (
+	"encoding/json"
+)
+
+// JSONBytes returns the JSON encoded array as bytes.
+//
+// One important thing to note is that it will treat a nil slice as an empty
+// slice to ensure that the JSON value return is always an array.
+func (ss SliceType) JSONBytes() []byte {
+	if ss == nil {
+		return []byte("[]")
+	}
+
+	// An error should not be possible.
+	data, _ := json.Marshal(ss)
+
+	return data
+}
diff --git a/functions/main.go b/functions/main.go
index a86c1a7..74ad92a 100644
--- a/functions/main.go
+++ b/functions/main.go
@@ -41,6 +41,7 @@ var Functions = []struct {
 	{"Intersect", "intersect.go", ForNumbersAndStrings},
 	{"Ints", "ints.go", ForAll},
 	{"Join", "join.go", ForStrings},
+	{"JSONBytes", "json_bytes.go", ForAll},
 	{"JSONString", "json_string.go", ForAll},
 	{"Keys", "keys.go", ForMaps},
 	{"Last", "last.go", ForAll},
diff --git a/pie/carpointers_pie.go b/pie/carpointers_pie.go
index 3661f54..8c3d74f 100755
--- a/pie/carpointers_pie.go
+++ b/pie/carpointers_pie.go
@@ -247,6 +247,21 @@ func (ss carPointers) Ints() Ints {
 	return result
 }
 
+// JSONBytes returns the JSON encoded array as bytes.
+//
+// One important thing to note is that it will treat a nil slice as an empty
+// slice to ensure that the JSON value return is always an array.
+func (ss carPointers) JSONBytes() []byte {
+	if ss == nil {
+		return []byte("[]")
+	}
+
+	// An error should not be possible.
+	data, _ := json.Marshal(ss)
+
+	return data
+}
+
 // JSONString returns the JSON encoded array as a string.
 //
 // One important thing to note is that it will treat a nil slice as an empty
diff --git a/pie/cars_pie.go b/pie/cars_pie.go
index c49f5c6..7a364a6 100755
--- a/pie/cars_pie.go
+++ b/pie/cars_pie.go
@@ -247,6 +247,21 @@ func (ss cars) Ints() Ints {
 	return result
 }
 
+// JSONBytes returns the JSON encoded array as bytes.
+//
+// One important thing to note is that it will treat a nil slice as an empty
+// slice to ensure that the JSON value return is always an array.
+func (ss cars) JSONBytes() []byte {
+	if ss == nil {
+		return []byte("[]")
+	}
+
+	// An error should not be possible.
+	data, _ := json.Marshal(ss)
+
+	return data
+}
+
 // JSONString returns the JSON encoded array as a string.
 //
 // One important thing to note is that it will treat a nil slice as an empty
diff --git a/pie/float64s_pie.go b/pie/float64s_pie.go
index a452c29..3b48f59 100755
--- a/pie/float64s_pie.go
+++ b/pie/float64s_pie.go
@@ -320,6 +320,21 @@ func (ss Float64s) Ints() Ints {
 	return result
 }
 
+// JSONBytes returns the JSON encoded array as bytes.
+//
+// One important thing to note is that it will treat a nil slice as an empty
+// slice to ensure that the JSON value return is always an array.
+func (ss Float64s) JSONBytes() []byte {
+	if ss == nil {
+		return []byte("[]")
+	}
+
+	// An error should not be possible.
+	data, _ := json.Marshal(ss)
+
+	return data
+}
+
 // JSONString returns the JSON encoded array as a string.
 //
 // One important thing to note is that it will treat a nil slice as an empty
diff --git a/pie/ints_pie.go b/pie/ints_pie.go
index 49b58f9..974a126 100755
--- a/pie/ints_pie.go
+++ b/pie/ints_pie.go
@@ -320,6 +320,21 @@ func (ss Ints) Ints() Ints {
 	return result
 }
 
+// JSONBytes returns the JSON encoded array as bytes.
+//
+// One important thing to note is that it will treat a nil slice as an empty
+// slice to ensure that the JSON value return is always an array.
+func (ss Ints) JSONBytes() []byte {
+	if ss == nil {
+		return []byte("[]")
+	}
+
+	// An error should not be possible.
+	data, _ := json.Marshal(ss)
+
+	return data
+}
+
 // JSONString returns the JSON encoded array as a string.
 //
 // One important thing to note is that it will treat a nil slice as an empty
diff --git a/pie/strings_pie.go b/pie/strings_pie.go
index a97e825..bf73a7f 100755
--- a/pie/strings_pie.go
+++ b/pie/strings_pie.go
@@ -309,6 +309,21 @@ func (ss Strings) Join(glue string) (s string) {
 	return s
 }
 
+// JSONBytes returns the JSON encoded array as bytes.
+//
+// One important thing to note is that it will treat a nil slice as an empty
+// slice to ensure that the JSON value return is always an array.
+func (ss Strings) JSONBytes() []byte {
+	if ss == nil {
+		return []byte("[]")
+	}
+
+	// An error should not be possible.
+	data, _ := json.Marshal(ss)
+
+	return data
+}
+
 // JSONString returns the JSON encoded array as a string.
 //
 // One important thing to note is that it will treat a nil slice as an empty
diff --git a/template.go b/template.go
index df9a6ad..bf33199 100644
--- a/template.go
+++ b/template.go
@@ -365,6 +365,27 @@ func (ss SliceType) Ints() pie.Ints {
 
 	return result
 }
+`,
+	"JSONBytes": `package functions
+
+import (
+	"encoding/json"
+)
+
+// JSONBytes returns the JSON encoded array as bytes.
+//
+// One important thing to note is that it will treat a nil slice as an empty
+// slice to ensure that the JSON value return is always an array.
+func (ss SliceType) JSONBytes() []byte {
+	if ss == nil {
+		return []byte("[]")
+	}
+
+	// An error should not be possible.
+	data, _ := json.Marshal(ss)
+
+	return data
+}
 `,
 	"JSONString": `package functions
 


Test patch:
diff --git a/pie/carpointers_test.go b/pie/carpointers_test.go
index 6601cd2..b5f2009 100644
--- a/pie/carpointers_test.go
+++ b/pie/carpointers_test.go
@@ -240,6 +240,15 @@ var carPointersJSONTests = []struct {
 	},
 }
 
+func TestCarPointers_JSONBytes(t *testing.T) {
+	for _, test := range carPointersJSONTests {
+		t.Run("", func(t *testing.T) {
+			defer assertImmutableCarPointers(t, &test.ss)()
+			assert.Equal(t, []byte(test.jsonString), test.ss.JSONBytes())
+		})
+	}
+}
+
 func TestCarPointers_JSONString(t *testing.T) {
 	for _, test := range carPointersJSONTests {
 		t.Run("", func(t *testing.T) {
diff --git a/pie/cars_test.go b/pie/cars_test.go
index bfcecb3..65181d9 100644
--- a/pie/cars_test.go
+++ b/pie/cars_test.go
@@ -227,6 +227,14 @@ var carsJSONTests = []struct {
 	},
 }
 
+func TestCars_JSONBytes(t *testing.T) {
+	for _, test := range carsJSONTests {
+		t.Run("", func(t *testing.T) {
+			defer assertImmutableCars(t, &test.ss)()
+			assert.Equal(t, []byte(test.jsonString), test.ss.JSONBytes())
+		})
+	}
+}
 func TestCars_JSONString(t *testing.T) {
 	for _, test := range carsJSONTests {
 		t.Run("", func(t *testing.T) {
diff --git a/pie/float64s_test.go b/pie/float64s_test.go
index fb49044..25cf224 100644
--- a/pie/float64s_test.go
+++ b/pie/float64s_test.go
@@ -280,6 +280,14 @@ var float64sJSONTests = []struct {
 	},
 }
 
+func TestFloat64s_JSONBytes(t *testing.T) {
+	for _, test := range float64sJSONTests {
+		t.Run("", func(t *testing.T) {
+			defer assertImmutableFloat64s(t, &test.ss)()
+			assert.Equal(t, []byte(test.jsonString), test.ss.JSONBytes())
+		})
+	}
+}
 func TestFloat64s_JSONString(t *testing.T) {
 	for _, test := range float64sJSONTests {
 		t.Run("", func(t *testing.T) {
diff --git a/pie/ints_test.go b/pie/ints_test.go
index 8121c21..cc72c50 100644
--- a/pie/ints_test.go
+++ b/pie/ints_test.go
@@ -274,6 +274,15 @@ func TestInts_JSONString(t *testing.T) {
 	}
 }
 
+func TestInts_JSONBytes(t *testing.T) {
+	for _, test := range intsJSONTests {
+		t.Run("", func(t *testing.T) {
+			defer assertImmutableInts(t, &test.ss)()
+			assert.Equal(t, []byte(test.jsonString), test.ss.JSONBytes())
+		})
+	}
+}
+
 var intsSortTests = []struct {
 	ss        Ints
 	sorted    Ints
diff --git a/pie/strings_test.go b/pie/strings_test.go
index 8a549a9..3ab24e6 100644
--- a/pie/strings_test.go
+++ b/pie/strings_test.go
@@ -241,6 +241,14 @@ var stringsJSONTests = []struct {
 	},
 }
 
+func TestStrings_JSONBytes(t *testing.T) {
+	for _, test := range stringsJSONTests {
+		t.Run("", func(t *testing.T) {
+			defer assertImmutableStrings(t, &test.ss)()
+			assert.Equal(t, []byte(test.jsonString), test.ss.JSONBytes())
+		})
+	}
+}
 func TestStrings_JSONString(t *testing.T) {
 	for _, test := range stringsJSONTests {
 		t.Run("", func(t *testing.T) {