Skip to content
This repository was archived by the owner on Apr 2, 2023. It is now read-only.

Commit cd04181

Browse files
committed
feat(rest): add rest helper which is the opposite of first
1 parent fec3e49 commit cd04181

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

docs/functions.md

+22
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,28 @@ type last = <T>(items: T[]) => T | undefined
312312
{% endtab %}
313313
{% endtabs %}
314314
315+
## rest
316+
317+
Given an array, return every item except the first.
318+
319+
{% tabs %}
320+
{% tab title="Usage" %}
321+
322+
```typescript
323+
rest([1, 2, 3]) // [2, 3]
324+
```
325+
326+
{% endtab %}
327+
328+
{% tab title="Type Definition" %}
329+
330+
```typescript
331+
type rest = <T>(items: T[]) => T[]
332+
```
333+
334+
{% endtab %}
335+
{% endtabs %}
336+
315337
## greaterThan
316338
317339
Create a predicate which checks whether a value is greater than a number.

src/__tests__/rest.spec.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { rest } from "../index"
2+
3+
describe("rest", () => {
4+
test("gets the rest item in an array", () => {
5+
expect(rest([0, 1, 2, 3])).toEqual([1, 2, 3])
6+
})
7+
})

src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ export const first = index(0)
77

88
export const last = <T>(items: T[]): T => items[items.length - 1]
99

10+
export const rest = <T>([, ...remaining]: T[]) => remaining
11+
1012
export const constant = <T>(value: T) => () => value
1113

1214
export const identity = <T>(value: T) => value

0 commit comments

Comments
 (0)