Repository: elliotchance/pie
Pull request #160 (2020-02-06 20:58:53)

Insert new function
ref #43

<!-- 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/160)
<!-- Reviewable:end -->


codecov-io: # [Codecov](https://codecov.io/gh/elliotchance/pie/pull/160?src=pr&el=h1) Report
> :exclamation: No coverage uploaded for pull request base (`master@dc775bf`). [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/160/graphs/tree.svg?width=650&token=OvugwUQGM3&height=150&src=pr)](https://codecov.io/gh/elliotchance/pie/pull/160?src=pr&el=tree)

```diff
@@            Coverage Diff            @@
##             master     #160   +/-   ##
=========================================
  Coverage          ?   98.65%           
=========================================
  Files             ?        8           
  Lines             ?     1782           
  Branches          ?        0           
=========================================
  Hits              ?     1758           
  Misses            ?       12           
  Partials          ?       12
```


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

------

[Continue to review full report at Codecov](https://codecov.io/gh/elliotchance/pie/pull/160?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/160?src=pr&el=footer). Last update [dc775bf...5624655](https://codecov.io/gh/elliotchance/pie/pull/160?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/README.md b/README.md
index daa8251..f3db4a8 100644
--- a/README.md
+++ b/README.md
@@ -196,6 +196,7 @@ The letters in brackets indicate:
 | [`Float64s`](#float64s) (S)             | ✓      | ✓      | ✓      |      | n        | Float64s transforms each element to a float64.  |
 | [`Group`](#group)                       | ✓      | ✓      |        |      | n        | Group returns a map of the value with an individual count.  |
 | [`Intersect`](#intersect)               | ✓      | ✓      |        |      | n        | Intersect returns items that exist in all lists.  |
+| [`Insert`](#insert)                     | ✓      | ✓      | ✓      |      | n        | Insert a value at an index  |
 | [`Ints`](#ints) (S)                     | ✓      | ✓      | ✓      |      | n        | Ints transforms each element to an integer.  |
 | [`Join`](#join) (S)                     | ✓      | ✓      | ✓      |      | n        | Join returns a string from joining each of the elements.  |
 | [`JSONBytes`](#jsonbytes)               | ✓      | ✓      | ✓      |      | n        | JSONBytes returns the JSON encoded array as bytes.  |
@@ -428,6 +429,11 @@ It returns slice without any duplicates.
 If zero slice arguments are provided, then nil is returned.
 
 
+## Insert
+
+Insert a value at an index
+
+
 ## Ints
 
 Ints transforms each element to an integer.
diff --git a/functions/insert.go b/functions/insert.go
new file mode 100644
index 0000000..a58a300
--- /dev/null
+++ b/functions/insert.go
@@ -0,0 +1,10 @@
+package functions
+
+// Insert a value at an index
+func (ss SliceType) Insert(index int, values ...ElementType) SliceType {
+	if index >= ss.Len() {
+		return SliceType.Extend(ss, SliceType(values))
+	}
+
+	return SliceType.Extend(ss[:index], SliceType(values), ss[index:])
+}
diff --git a/functions/main.go b/functions/main.go
index 752be4f..5ca0cec 100644
--- a/functions/main.go
+++ b/functions/main.go
@@ -43,6 +43,7 @@ var Functions = []struct {
 	{"Float64s", "float64s.go", ForAll, "n"},
 	{"Group", "group.go", ForNumbersAndStrings, "n"},
 	{"Intersect", "intersect.go", ForNumbersAndStrings, "n"},
+	{"Insert", "insert.go", ForAll, "n"},
 	{"Ints", "ints.go", ForAll, "n"},
 	{"Join", "join.go", ForAll, "n"},
 	{"JSONBytes", "json_bytes.go", ForAll, "n"},
diff --git a/pie/carpointers_pie.go b/pie/carpointers_pie.go
index 0f5755a..97202a0 100755
--- a/pie/carpointers_pie.go
+++ b/pie/carpointers_pie.go
@@ -266,6 +266,15 @@ func (ss carPointers) Float64s() Float64s {
 	return result
 }
 
+// Insert a value at an index
+func (ss carPointers) Insert(index int, values ...*car) carPointers {
+	if index >= ss.Len() {
+		return carPointers.Extend(ss, carPointers(values))
+	}
+
+	return carPointers.Extend(ss[:index], carPointers(values), ss[index:])
+}
+
 // Ints transforms each element to an integer.
 func (ss carPointers) Ints() Ints {
 	l := len(ss)
diff --git a/pie/cars_pie.go b/pie/cars_pie.go
index 9f6ef4f..5b18f75 100755
--- a/pie/cars_pie.go
+++ b/pie/cars_pie.go
@@ -266,6 +266,15 @@ func (ss cars) Float64s() Float64s {
 	return result
 }
 
+// Insert a value at an index
+func (ss cars) Insert(index int, values ...car) cars {
+	if index >= ss.Len() {
+		return cars.Extend(ss, cars(values))
+	}
+
+	return cars.Extend(ss[:index], cars(values), ss[index:])
+}
+
 // Ints transforms each element to an integer.
 func (ss cars) Ints() Ints {
 	l := len(ss)
diff --git a/pie/float64s_pie.go b/pie/float64s_pie.go
index a2e7803..ef1ce85 100755
--- a/pie/float64s_pie.go
+++ b/pie/float64s_pie.go
@@ -349,6 +349,15 @@ func (ss Float64s) Intersect(slices ...Float64s) (ss2 Float64s) {
 	return
 }
 
+// Insert a value at an index
+func (ss Float64s) Insert(index int, values ...float64) Float64s {
+	if index >= ss.Len() {
+		return Float64s.Extend(ss, Float64s(values))
+	}
+
+	return Float64s.Extend(ss[:index], Float64s(values), ss[index:])
+}
+
 // Ints transforms each element to an integer.
 func (ss Float64s) Ints() Ints {
 	l := len(ss)
diff --git a/pie/ints_pie.go b/pie/ints_pie.go
index 2a02d30..90425fd 100755
--- a/pie/ints_pie.go
+++ b/pie/ints_pie.go
@@ -349,6 +349,15 @@ func (ss Ints) Intersect(slices ...Ints) (ss2 Ints) {
 	return
 }
 
+// Insert a value at an index
+func (ss Ints) Insert(index int, values ...int) Ints {
+	if index >= ss.Len() {
+		return Ints.Extend(ss, Ints(values))
+	}
+
+	return Ints.Extend(ss[:index], Ints(values), ss[index:])
+}
+
 // Ints transforms each element to an integer.
 func (ss Ints) Ints() Ints {
 	l := len(ss)
diff --git a/pie/strings_pie.go b/pie/strings_pie.go
index 297cd04..5ff32b7 100755
--- a/pie/strings_pie.go
+++ b/pie/strings_pie.go
@@ -325,6 +325,15 @@ func (ss Strings) Intersect(slices ...Strings) (ss2 Strings) {
 	return
 }
 
+// Insert a value at an index
+func (ss Strings) Insert(index int, values ...string) Strings {
+	if index >= ss.Len() {
+		return Strings.Extend(ss, Strings(values))
+	}
+
+	return Strings.Extend(ss[:index], Strings(values), ss[index:])
+}
+
 // Ints transforms each element to an integer.
 func (ss Strings) Ints() Ints {
 	l := len(ss)
diff --git a/template.go b/template.go
index 65f7db3..3d1feef 100644
--- a/template.go
+++ b/template.go
@@ -355,6 +355,17 @@ func (ss SliceType) Group() map[ElementType]int {
 	}
 	return group
 }
+`,
+	"Insert": `package functions
+
+// Insert a value at an index
+func (ss SliceType) Insert(index int, values ...ElementType) SliceType {
+	if index >= ss.Len() {
+		return SliceType.Extend(ss, SliceType(values))
+	}
+
+	return SliceType.Extend(ss[:index], SliceType(values), ss[index:])
+}
 `,
 	"Intersect": `package functions
 


Test patch:
diff --git a/pie/carpointers_test.go b/pie/carpointers_test.go
index fe34f4a..ec5d450 100644
--- a/pie/carpointers_test.go
+++ b/pie/carpointers_test.go
@@ -1359,3 +1359,14 @@ func TestCarPointers_Pop(t *testing.T) {
 	assert.Equal(t, carPointers{}, cars)
 
 }
+
+func TestCarPointers_Insert(t *testing.T) {
+	car1 := &car{Name: "maverick", Color: "red"}
+	car2 := &car{Name: "ferrari", Color: "red"}
+	car3 := &car{Name: "panda", Color: "white"}
+
+	assert.Equal(t, carPointers{}, carPointers(nil).Insert(0))
+	assert.Equal(t, carPointers{car2, car1}, carPointers{car1}.Insert(0, car2))
+	assert.Equal(t, carPointers{car1, car2}, carPointers{car1}.Insert(1, car2))
+	assert.Equal(t, carPointers{car1, car2, car3}, carPointers{car1, car3}.Insert(1, car2))
+}
diff --git a/pie/cars_test.go b/pie/cars_test.go
index bcb2cd0..d10a088 100644
--- a/pie/cars_test.go
+++ b/pie/cars_test.go
@@ -1332,3 +1332,14 @@ func TestCars_Pop(t *testing.T) {
 	assert.Equal(t, cars{}, garage)
 
 }
+
+func TestCars_Insert(t *testing.T) {
+	car1 := car{Name: "maverick", Color: "red"}
+	car2 := car{Name: "ferrari", Color: "red"}
+	car3 := car{Name: "panda", Color: "white"}
+
+	assert.Equal(t, cars{}, cars(nil).Insert(0))
+	assert.Equal(t, cars{car2, car1}, cars{car1}.Insert(0, car2))
+	assert.Equal(t, cars{car1, car2}, cars{car1}.Insert(1, car2))
+	assert.Equal(t, cars{car1, car2, car3}, cars{car1, car3}.Insert(1, car2))
+}
diff --git a/pie/float64s_test.go b/pie/float64s_test.go
index 10447d1..51091b4 100644
--- a/pie/float64s_test.go
+++ b/pie/float64s_test.go
@@ -1513,3 +1513,11 @@ func TestFloat64s_Group(t *testing.T) {
 		3: 3,
 	}, Float64s{1, 2, 2, 3, 3, 3}.Group())
 }
+
+func TestFloat64s_Insert(t *testing.T) {
+
+	assert.Equal(t, Float64s{}, Float64s(nil).Insert(0))
+	assert.Equal(t, Float64s{2.0, 1.0}, Float64s{1.0}.Insert(0, 2.0))
+	assert.Equal(t, Float64s{1.0, 2.0}, Float64s{1.0}.Insert(1, 2.0))
+	assert.Equal(t, Float64s{1.0, 2.0, 3.3}, Float64s{1.0, 3.3}.Insert(1, 2.0))
+}
diff --git a/pie/ints_test.go b/pie/ints_test.go
index 9ccdc44..471ffd7 100644
--- a/pie/ints_test.go
+++ b/pie/ints_test.go
@@ -1502,3 +1502,11 @@ func TestInts_Group(t *testing.T) {
 		3: 3,
 	}, Ints{1, 2, 2, 3, 3, 3}.Group())
 }
+
+func TestInts_Insert(t *testing.T) {
+
+	assert.Equal(t, Ints{}, Ints(nil).Insert(0))
+	assert.Equal(t, Ints{2, 1}, Ints{1}.Insert(0, 2))
+	assert.Equal(t, Ints{1, 2}, Ints{1}.Insert(1, 2))
+	assert.Equal(t, Ints{1, 2, 3}, Ints{1, 3}.Insert(1, 2))
+}
diff --git a/pie/strings_test.go b/pie/strings_test.go
index 4be41b7..9fab296 100644
--- a/pie/strings_test.go
+++ b/pie/strings_test.go
@@ -1526,3 +1526,11 @@ func TestStrings_Group(t *testing.T) {
 		"foobar": 3,
 	}, Strings{"foo", "bar", "bar", "foobar", "foobar", "foobar"}.Group())
 }
+
+func TestStrings_Insert(t *testing.T) {
+
+	assert.Equal(t, Strings{}, Strings(nil).Insert(0))
+	assert.Equal(t, Strings{"bar", "foo"}, Strings{"foo"}.Insert(0, "bar"))
+	assert.Equal(t, Strings{"foo", "bar"}, Strings{"foo"}.Insert(1, "bar"))
+	assert.Equal(t, Strings{"foo", "bar", "zap"}, Strings{"foo", "zap"}.Insert(1, "bar"))
+}