Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,30 @@ Generates an array of numbers from 0 to n - 1.
```
<!-- prettier-ignore-end -->

#### removeAt

Removes an element at the given index from the given array.

##### Type signature

<!-- prettier-ignore-start -->
```typescript
(index: number) => (xs: any[]) => any[]
```
<!-- prettier-ignore-end -->

##### Examples

<!-- prettier-ignore-start -->
```javascript
removeAt(3)([1, 2, 3, 4, 5, 6]) // ⇒ [1, 2, 3, 5, 6]
```
<!-- prettier-ignore-end -->

##### Questions

- How to remove an item at a particular index?

#### repeat

Repeats the given element by given count of times.
Expand Down
24 changes: 24 additions & 0 deletions array/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,30 @@ Generates an array of numbers from 0 to n - 1.
```
<!-- prettier-ignore-end -->

# removeAt

Removes an element at the given index from the given array.

## Type signature

<!-- prettier-ignore-start -->
```typescript
(index: number) => (xs: any[]) => any[]
```
<!-- prettier-ignore-end -->

## Examples

<!-- prettier-ignore-start -->
```javascript
removeAt(3)([1, 2, 3, 4, 5, 6]) // ⇒ [1, 2, 3, 5, 6]
```
<!-- prettier-ignore-end -->

## Questions

- How to remove an item at a particular index?

# repeat

Repeats the given element by given count of times.
Expand Down
3 changes: 3 additions & 0 deletions array/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import multiple from "./multiple.js";
import none from "./none.js";
import partition from "./partition.js";
import range from "./range.js";
import removeAt from "./removeAt.js";
import repeat from "./repeat.js";
import reverse from "./reverse.js";
import reverseIf from "./reverseIf.js";
Expand Down Expand Up @@ -71,6 +72,7 @@ export {
none,
partition,
range,
removeAt,
repeat,
reverse,
reverseIf,
Expand Down Expand Up @@ -119,6 +121,7 @@ export default {
none,
partition,
range,
removeAt,
repeat,
reverse,
reverseIf,
Expand Down
3 changes: 3 additions & 0 deletions array/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import multiple from "./multiple";
import none from "./none";
import partition from "./partition";
import range from "./range";
import removeAt from "./removeAt";
import repeat from "./repeat";
import reverse from "./reverse";
import reverseIf from "./reverseIf";
Expand Down Expand Up @@ -71,6 +72,7 @@ export {
none,
partition,
range,
removeAt,
repeat,
reverse,
reverseIf,
Expand Down Expand Up @@ -119,6 +121,7 @@ export default {
none,
partition,
range,
removeAt,
repeat,
reverse,
reverseIf,
Expand Down
10 changes: 10 additions & 0 deletions array/removeAt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default index => xs => {
if (index >= xs.length || index < 0) {
return xs;
}

const ys = [...xs];
ys.splice(index, 1);

return ys;
};
12 changes: 12 additions & 0 deletions array/removeAt.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "removeAt",
"description": "Removes an element at the given index from the given array.",
"signature": "(index: number) => (xs: any[]) => any[]",
"examples": [
{
"language": "javascript",
"content": "removeAt(3)([1, 2, 3, 4, 5, 6]) // ⇒ [1, 2, 3, 5, 6]"
}
],
"questions": ["How to remove an item at a particular index?"]
}
23 changes: 23 additions & 0 deletions array/removeAt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# removeAt

Removes an element at the given index from the given array.

## Type signature

<!-- prettier-ignore-start -->
```typescript
(index: number) => (xs: any[]) => any[]
```
<!-- prettier-ignore-end -->

## Examples

<!-- prettier-ignore-start -->
```javascript
removeAt(3)([1, 2, 3, 4, 5, 6]) // ⇒ [1, 2, 3, 5, 6]
```
<!-- prettier-ignore-end -->

## Questions

- How to remove an item at a particular index?
16 changes: 16 additions & 0 deletions array/removeAt.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* eslint-env jest */
// @ts-ignore ambiguous import
import removeAt from "./removeAt.ts";

describe("removeAt", () => {
it("should remove an element at the specified index", () => {
expect(removeAt(3)([1, 2, 3, 4, 5, 6])).toEqual([1, 2, 3, 5, 6]);
});

it("should do nothing when given an index out of range", () => {
expect(removeAt(3)([])).toEqual([]);
expect(removeAt(-1)([])).toEqual([]);
expect(removeAt(3)([1, 2, 3])).toEqual([1, 2, 3]);
expect(removeAt(-1)([1, 2, 3])).toEqual([1, 2, 3]);
});
});
10 changes: 10 additions & 0 deletions array/removeAt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default (index: number) => (xs: any[]) => {
if (index >= xs.length || index < 0) {
return xs;
}

const ys = [...xs];
ys.splice(index, 1);

return ys;
};