diff --git a/package-lock.json b/package-lock.json index 77f2a81..103ca91 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "tk-functional", - "version": "0.1.5", + "version": "0.2.3", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 0323adc..fedb722 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "tk-functional", - "version": "0.2.2", + "version": "0.2.3", "description": "Typescript/Javascript helpers for functional-style programming", "source": "src/index.ts", "main": "dist/tk-functional.umd.js", diff --git a/src/index.test.ts b/src/index.test.ts index 9ff64d9..4f54e89 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -1,4 +1,4 @@ -import { always, and, at, attr, bool, cmp, first, fmap, identity, is, isinstance, last, never, nn, nnu, nop, not, nu, or, partial, pipe } from "./index"; +import { always, and, at, attr, bool, cmp, first, fmap, identity, is, isinstance, last, never, nn, nnu, nop, not, nu, or, partial, pipe, second, third } from "./index"; describe(nop, () => { it("does nothing", () => { @@ -158,6 +158,22 @@ describe(first, () => { }); }); +describe(second, () => { + it("gets the second item in an array", () => { + expect(second([1, 4, 8])).toBe(4); + expect(second([1])).toBeUndefined(); + expect(second([])).toBeUndefined(); + }); +}); + +describe(third, () => { + it("gets the third item in an array", () => { + expect(third([1, 4, 8])).toBe(8); + expect(third([1, 4])).toBeUndefined(); + expect(third([])).toBeUndefined(); + }); +}); + describe(last, () => { it("gets the last item in an array", () => { expect(last([1, 4, 8])).toBe(8); diff --git a/src/index.ts b/src/index.ts index f880dd4..171f217 100644 --- a/src/index.ts +++ b/src/index.ts @@ -125,7 +125,17 @@ export function at(idx: number): (array: ReadonlyArray) => T | undefined { /** * Array first item getter */ -export const first: (array: ReadonlyArray) => T | undefined = at(0); +export const first: >(array: T) => T[0] | undefined = at(0); + +/** + * Array second item getter + */ +export const second: >(array: T) => T[1] | undefined = at(1); + +/** + * Array third item getter + */ +export const third: >(array: T) => T[2] | undefined = at(2); /** * Array last item getter