Skip to content

Commit 2d21f7f

Browse files
kborucinskikrystianborucinski
authored andcommitted
Add array/insert method (#37)
* Add insert method * Update after code review Co-authored-by: krystianborucinski <krystian.borucinski@sandstream.pl>
1 parent edb5c50 commit 2d21f7f

File tree

9 files changed

+130
-0
lines changed

9 files changed

+130
-0
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,32 @@ Flattens the nested arrays by a single level.
203203
```
204204
<!-- prettier-ignore-end -->
205205

206+
#### insert
207+
208+
Inserts the given item to the array at a specific index.
209+
210+
##### Type signature
211+
212+
<!-- prettier-ignore-start -->
213+
```typescript
214+
(
215+
index: number
216+
) => (item: any) => ([...xs]: any[]) => any[]
217+
```
218+
<!-- prettier-ignore-end -->
219+
220+
##### Examples
221+
222+
<!-- prettier-ignore-start -->
223+
```javascript
224+
insert(0)('d')(['a', 'b', 'c']); // ⇒ ['d', 'a', 'b', 'c']
225+
```
226+
227+
```javascript
228+
insert(1)('d')(['a', 'b', 'c']); // ⇒ ['a', 'd', 'b', 'c']
229+
```
230+
<!-- prettier-ignore-end -->
231+
206232
#### intersection
207233

208234
Finds common items between both arrays.

array/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,32 @@ Flattens the nested arrays by a single level.
186186
```
187187
<!-- prettier-ignore-end -->
188188

189+
# insert
190+
191+
Inserts the given item to the array at a specific index.
192+
193+
## Type signature
194+
195+
<!-- prettier-ignore-start -->
196+
```typescript
197+
(
198+
index: number
199+
) => (item: any) => ([...xs]: any[]) => any[]
200+
```
201+
<!-- prettier-ignore-end -->
202+
203+
## Examples
204+
205+
<!-- prettier-ignore-start -->
206+
```javascript
207+
insert(0)('d')(['a', 'b', 'c']); // ⇒ ['d', 'a', 'b', 'c']
208+
```
209+
210+
```javascript
211+
insert(1)('d')(['a', 'b', 'c']); // ⇒ ['a', 'd', 'b', 'c']
212+
```
213+
<!-- prettier-ignore-end -->
214+
189215
# intersection
190216

191217
Finds common items between both arrays.

array/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import find from "./find.js";
1111
import first from "./first.js";
1212
import flatMap from "./flatMap.js";
1313
import flatten from "./flatten.js";
14+
import insert from "./insert.js";
1415
import intersection from "./intersection.js";
1516
import is from "./is.js";
1617
import last from "./last.js";
@@ -53,6 +54,7 @@ export {
5354
first,
5455
flatMap,
5556
flatten,
57+
insert,
5658
intersection,
5759
is,
5860
last,
@@ -96,6 +98,7 @@ export default {
9698
first,
9799
flatMap,
98100
flatten,
101+
insert,
99102
intersection,
100103
is,
101104
last,

array/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import find from "./find";
1111
import first from "./first";
1212
import flatMap from "./flatMap";
1313
import flatten from "./flatten";
14+
import insert from "./insert";
1415
import intersection from "./intersection";
1516
import is from "./is";
1617
import last from "./last";
@@ -53,6 +54,7 @@ export {
5354
first,
5455
flatMap,
5556
flatten,
57+
insert,
5658
intersection,
5759
is,
5860
last,
@@ -96,6 +98,7 @@ export default {
9698
first,
9799
flatMap,
98100
flatten,
101+
insert,
99102
intersection,
100103
is,
101104
last,

array/insert.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default index => item => ([...xs]) => {
2+
xs.splice(index, 0, item);
3+
4+
return xs;
5+
};

array/insert.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "insert",
3+
"description": "Inserts the given item to the array at a specific index.",
4+
"signature": "(\n index: number\n) => (item: any) => ([...xs]: any[]) => any[]",
5+
"examples": [
6+
{
7+
"language": "javascript",
8+
"content": "insert(0)('d')(['a', 'b', 'c']); // ⇒ ['d', 'a', 'b', 'c']"
9+
},
10+
{
11+
"language": "javascript",
12+
"content": "insert(1)('d')(['a', 'b', 'c']); // ⇒ ['a', 'd', 'b', 'c']"
13+
}
14+
],
15+
"questions": ["TODO: List questions that may this function answer."]
16+
}

array/insert.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# insert
2+
3+
Inserts the given item to the array at a specific index.
4+
5+
## Type signature
6+
7+
<!-- prettier-ignore-start -->
8+
```typescript
9+
(
10+
index: number
11+
) => (item: any) => ([...xs]: any[]) => any[]
12+
```
13+
<!-- prettier-ignore-end -->
14+
15+
## Examples
16+
17+
<!-- prettier-ignore-start -->
18+
```javascript
19+
insert(0)('d')(['a', 'b', 'c']); // ⇒ ['d', 'a', 'b', 'c']
20+
```
21+
22+
```javascript
23+
insert(1)('d')(['a', 'b', 'c']); // ⇒ ['a', 'd', 'b', 'c']
24+
```
25+
<!-- prettier-ignore-end -->

array/insert.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/* eslint-env jest */
2+
// @ts-ignore ambiguous import
3+
import insert from "./insert.ts";
4+
5+
describe("insert", () => {
6+
it("should insert item to the array on the second position", () => {
7+
expect(insert(1)("d")(["a", "b", "c"])).toEqual(["a", "d", "b", "c"]);
8+
expect(insert(-2)("d")(["a", "b", "c"])).toEqual(["a", "d", "b", "c"]);
9+
});
10+
11+
it("should insert item to the array on the first position", () => {
12+
expect(insert(0)("b")(["a"])).toEqual(["b", "a"]);
13+
expect(insert(-1)("b")(["a"])).toEqual(["b", "a"]);
14+
});
15+
16+
it("should insert item to empty array on the first position", () => {
17+
expect(insert(10)("a")([])).toEqual(["a"]);
18+
expect(insert(0)("a")([])).toEqual(["a"]);
19+
expect(insert(-10)("a")([])).toEqual(["a"]);
20+
});
21+
});

array/insert.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default (index: number) => (item: any) => ([...xs]: any[]) => {
2+
xs.splice(index, 0, item);
3+
4+
return xs;
5+
};

0 commit comments

Comments
 (0)