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

Commit

Permalink
feat(core): add index, nth and last helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
tdreyno committed Mar 4, 2020
1 parent 36bdcce commit a43067b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { index } from "../index"

describe("index", () => {
test("gets the index item in an array", () => {
expect(index(0)([0, 1])).toBe(0)
})

test("gets the index item in an empty array", () => {
expect(index(0)([])).toBe(undefined)
})
})
11 changes: 11 additions & 0 deletions src/__tests__/last.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { last } from "../index"

describe("last", () => {
test("gets the last item in an array", () => {
expect(last([0, 1])).toBe(1)
})

test("gets the last item in an empty array", () => {
expect(last([])).toBe(undefined)
})
})
11 changes: 11 additions & 0 deletions src/__tests__/nth.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { nth } from "../index"

describe("nth", () => {
test("gets the nth item in an array", () => {
expect(nth(1)([0, 1])).toBe(0)
})

test("gets the nth item in an empty array", () => {
expect(nth(1)([])).toBe(undefined)
})
})
8 changes: 7 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
export const first = <T>(items: T[]): T => items[0]
export const index = (i: number) => <T>(items: T[]): T => items[i]

export const nth = (n: number) => index(n - 1)

export const first = index(0)

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

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

Expand Down

0 comments on commit a43067b

Please sign in to comment.