Skip to content

Commit eb24cde

Browse files
kborucinskikrystianborucinski
authored andcommitted
Add array/removeAt (#47)
* Add removeAt method Co-authored-by: krystianborucinski <krystian.borucinski@sandstream.pl>
1 parent 95f618b commit eb24cde

File tree

9 files changed

+125
-0
lines changed

9 files changed

+125
-0
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,30 @@ Generates an array of numbers from 0 to n - 1.
387387
```
388388
<!-- prettier-ignore-end -->
389389

390+
#### removeAt
391+
392+
Removes an element at the given index from the given array.
393+
394+
##### Type signature
395+
396+
<!-- prettier-ignore-start -->
397+
```typescript
398+
(index: number) => (xs: any[]) => any[]
399+
```
400+
<!-- prettier-ignore-end -->
401+
402+
##### Examples
403+
404+
<!-- prettier-ignore-start -->
405+
```javascript
406+
removeAt(3)([1, 2, 3, 4, 5, 6]) // ⇒ [1, 2, 3, 5, 6]
407+
```
408+
<!-- prettier-ignore-end -->
409+
410+
##### Questions
411+
412+
- How to remove an item at a particular index?
413+
390414
#### repeat
391415

392416
Repeats the given element by given count of times.

array/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,30 @@ Generates an array of numbers from 0 to n - 1.
370370
```
371371
<!-- prettier-ignore-end -->
372372

373+
# removeAt
374+
375+
Removes an element at the given index from the given array.
376+
377+
## Type signature
378+
379+
<!-- prettier-ignore-start -->
380+
```typescript
381+
(index: number) => (xs: any[]) => any[]
382+
```
383+
<!-- prettier-ignore-end -->
384+
385+
## Examples
386+
387+
<!-- prettier-ignore-start -->
388+
```javascript
389+
removeAt(3)([1, 2, 3, 4, 5, 6]) // ⇒ [1, 2, 3, 5, 6]
390+
```
391+
<!-- prettier-ignore-end -->
392+
393+
## Questions
394+
395+
- How to remove an item at a particular index?
396+
373397
# repeat
374398

375399
Repeats the given element by given count of times.

array/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import multiple from "./multiple.js";
2424
import none from "./none.js";
2525
import partition from "./partition.js";
2626
import range from "./range.js";
27+
import removeAt from "./removeAt.js";
2728
import repeat from "./repeat.js";
2829
import reverse from "./reverse.js";
2930
import reverseIf from "./reverseIf.js";
@@ -71,6 +72,7 @@ export {
7172
none,
7273
partition,
7374
range,
75+
removeAt,
7476
repeat,
7577
reverse,
7678
reverseIf,
@@ -119,6 +121,7 @@ export default {
119121
none,
120122
partition,
121123
range,
124+
removeAt,
122125
repeat,
123126
reverse,
124127
reverseIf,

array/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import multiple from "./multiple";
2424
import none from "./none";
2525
import partition from "./partition";
2626
import range from "./range";
27+
import removeAt from "./removeAt";
2728
import repeat from "./repeat";
2829
import reverse from "./reverse";
2930
import reverseIf from "./reverseIf";
@@ -71,6 +72,7 @@ export {
7172
none,
7273
partition,
7374
range,
75+
removeAt,
7476
repeat,
7577
reverse,
7678
reverseIf,
@@ -119,6 +121,7 @@ export default {
119121
none,
120122
partition,
121123
range,
124+
removeAt,
122125
repeat,
123126
reverse,
124127
reverseIf,

array/removeAt.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export default index => xs => {
2+
if (index >= xs.length || index < 0) {
3+
return xs;
4+
}
5+
6+
const ys = [...xs];
7+
ys.splice(index, 1);
8+
9+
return ys;
10+
};

array/removeAt.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "removeAt",
3+
"description": "Removes an element at the given index from the given array.",
4+
"signature": "(index: number) => (xs: any[]) => any[]",
5+
"examples": [
6+
{
7+
"language": "javascript",
8+
"content": "removeAt(3)([1, 2, 3, 4, 5, 6]) // ⇒ [1, 2, 3, 5, 6]"
9+
}
10+
],
11+
"questions": ["How to remove an item at a particular index?"]
12+
}

array/removeAt.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# removeAt
2+
3+
Removes an element at the given index from the given array.
4+
5+
## Type signature
6+
7+
<!-- prettier-ignore-start -->
8+
```typescript
9+
(index: number) => (xs: any[]) => any[]
10+
```
11+
<!-- prettier-ignore-end -->
12+
13+
## Examples
14+
15+
<!-- prettier-ignore-start -->
16+
```javascript
17+
removeAt(3)([1, 2, 3, 4, 5, 6]) // ⇒ [1, 2, 3, 5, 6]
18+
```
19+
<!-- prettier-ignore-end -->
20+
21+
## Questions
22+
23+
- How to remove an item at a particular index?

array/removeAt.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* eslint-env jest */
2+
// @ts-ignore ambiguous import
3+
import removeAt from "./removeAt.ts";
4+
5+
describe("removeAt", () => {
6+
it("should remove an element at the specified index", () => {
7+
expect(removeAt(3)([1, 2, 3, 4, 5, 6])).toEqual([1, 2, 3, 5, 6]);
8+
});
9+
10+
it("should do nothing when given an index out of range", () => {
11+
expect(removeAt(3)([])).toEqual([]);
12+
expect(removeAt(-1)([])).toEqual([]);
13+
expect(removeAt(3)([1, 2, 3])).toEqual([1, 2, 3]);
14+
expect(removeAt(-1)([1, 2, 3])).toEqual([1, 2, 3]);
15+
});
16+
});

array/removeAt.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export default (index: number) => (xs: any[]) => {
2+
if (index >= xs.length || index < 0) {
3+
return xs;
4+
}
5+
6+
const ys = [...xs];
7+
ys.splice(index, 1);
8+
9+
return ys;
10+
};

0 commit comments

Comments
 (0)