diff --git a/docs/functions.md b/docs/functions.md index 0d9e7c1..cc373a1 100644 --- a/docs/functions.md +++ b/docs/functions.md @@ -312,6 +312,28 @@ type last = (items: T[]) => T | undefined {% endtab %} {% endtabs %} +## rest + +Given an array, return every item except the first. + +{% tabs %} +{% tab title="Usage" %} + +```typescript +rest([1, 2, 3]) // [2, 3] +``` + +{% endtab %} + +{% tab title="Type Definition" %} + +```typescript +type rest = (items: T[]) => T[] +``` + +{% endtab %} +{% endtabs %} + ## greaterThan Create a predicate which checks whether a value is greater than a number. diff --git a/src/__tests__/rest.spec.ts b/src/__tests__/rest.spec.ts new file mode 100644 index 0000000..ff4a1e7 --- /dev/null +++ b/src/__tests__/rest.spec.ts @@ -0,0 +1,7 @@ +import { rest } from "../index" + +describe("rest", () => { + test("gets the rest item in an array", () => { + expect(rest([0, 1, 2, 3])).toEqual([1, 2, 3]) + }) +}) diff --git a/src/index.ts b/src/index.ts index c2141f2..77d2497 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,6 +7,8 @@ export const first = index(0) export const last = (items: T[]): T => items[items.length - 1] +export const rest = ([, ...remaining]: T[]) => remaining + export const constant = (value: T) => () => value export const identity = (value: T) => value