# first-of-type

> A utility function that returns the first value in an array or object which matches the specified type.

Latest version **1.0.0** (published 2022-02-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install first-of-type
pnpm add first-of-type
yarn add first-of-type
bun add first-of-type
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2022-02-06 |
| First published | 2022-02-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 3.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Tom Southall |
| Maintainers | tomsouthall |

## Links

- npm: https://www.npmjs.com/package/first-of-type
- npm.io page: https://npm.io/package/first-of-type

## Recent versions

- 1.0.0 (latest) — 2022-02-06

## README

# first-of-type

## Introduction

A utility function that returns the first value in an array or object which matches the specified type.

This can be useful in situations where the keys of an object or order of array values is unknown but you know that a field of a certain type is likely to be present.

## Installation

```shell
$ npm install first-of-type
```

## Usage

```js
import firstOfType from 'first-of-type'

firstOfType(obj, 'string')
```

## Examples

```js
// Return the first string element in an array
firstOfType([1, 2, 3, 'foobar', 4, 5], 'string')
=> 'foobar'
```

```js
// Return the first string element in an array
// containing multiple string elements
firstOfType([1, 2, 3, 'foo', 'bar', 4, 5], 'string')
=> 'foo'
```

```js
// When no element with a type of string exists
firstOfType([1, 2, 3, 4, 5], 'string')
=> undefined
```

```js
// Return the first string property found in an object
firstOfType({a: 1, b: 2, c: 'foobar', d: 3}, 'string')
=> 'foobar'
```

```js
// Invalid type specified
firstOfType({a: 1, b: 2, c: 'foobar', d: 3}, 'not-a-type')
=> undefined
```

## To test

```shell
$ npm run test
```

## To build

```shell
$ npm run build
```

---
_Source: https://npm.io/package/first-of-type · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
