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

Commit a43067b

Browse files
committed
feat(core): add index, nth and last helpers
1 parent 36bdcce commit a43067b

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

src/__tests__/index.spec.ts

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

src/__tests__/last.spec.ts

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

src/__tests__/nth.spec.ts

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

src/index.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
2-
export const first = <T>(items: T[]): T => items[0]
2+
export const index = (i: number) => <T>(items: T[]): T => items[i]
3+
4+
export const nth = (n: number) => index(n - 1)
5+
6+
export const first = index(0)
7+
8+
export const last = <T>(items: T[]): T => items[items.length - 1]
39

410
export const constant = <T>(value: T) => () => value
511

0 commit comments

Comments
 (0)