# dash-get

> A tiny get function, similar to Lodash.get

Latest version **1.0.2** (published 2019-01-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install dash-get
pnpm add dash-get
yarn add dash-get
bun add dash-get
```

## 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.2 |
| Published | 2019-01-09 |
| First published | 2018-12-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 7.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Jon Quach |
| Maintainers | itsjonq |
| Keywords | get, dash, tiny, lodash, dash-get, object, value |

## Links

- npm: https://www.npmjs.com/package/dash-get
- Repository: https://github.com/itsjonq/dash-get
- Homepage: https://github.com/itsjonq/dash-get#readme
- Issues: https://github.com/itsjonq/dash-get/issues
- npm.io page: https://npm.io/package/dash-get

## Alternatives

- [lodash.assign](https://npm.io/package/lodash.assign.md) — 2.3M weekly downloads
- [lodash.chunk](https://npm.io/package/lodash.chunk.md) — 1.8M weekly downloads
- [react-native-ios-utilities](https://npm.io/package/react-native-ios-utilities.md) — 138.5K weekly downloads
- [@technically/lodash](https://npm.io/package/@technically/lodash.md) — 50.9K weekly downloads
- [@fluid-topics/ft-icon](https://npm.io/package/@fluid-topics/ft-icon.md) — 20.6K weekly downloads

## Recent versions

- 1.0.2 (latest) — 2019-01-09
- 1.0.1 — 2018-12-02
- 1.0.0 — 2018-12-01

## README

# ✊ (dash) get

[![Build Status](https://travis-ci.org/ItsJonQ/dash-get.svg?branch=master)](https://travis-ci.org/ItsJonQ/dash-get)
[![npm version](https://badge.fury.io/js/dash-get.svg)](https://badge.fury.io/js/dash-get)

> A tiny get function', similar to Lodash.get

## ✨ Features

- **Zero dependencies**!
- Super tiny, at ~200 bytes gzipped
- Works almost exactly like [Lodash.get](https://lodash.com/docs/4.17.11#get)
- Ultra speedy! Check out the [performance tests](https://jsperf.com/get-try-catch-vs-reduce-vs-lodash-get)

## 🔧 Installation

Add `dash-get` to your project via `npm install`:

```
npm install --save dash-get
```

## 🕹 Usage

You can easily retrieve a value from a (deeply) nested object with `dash-get`, like so:

```js
import get from 'dash-get'

const someObject = {...}

const deeplyNestedValue = get(someObject, 'the.path.to.the.nested.value')
// value
```

The path could also be an `Array`:

```js
const someObject = {...}

const deeplyNestedValue = get(someObject, ['the', 'path', 'to', 'the', 'nested', 'value'])
// value
```

## 🎬 API

#### `get(obj, path, fallback)`

| Argument | Type                     | Description                                                           |
| -------- | ------------------------ | --------------------------------------------------------------------- |
| obj      | `Object`                 | The object to get the value from.                                     |
| path     | `Array<string>`/`string` | The path to the value.                                                |
| fallback | `any`                    | The fallback value, in case the desired value could not be retrieved. |

## 👻 Unsupported feature

This module does not support this particular use case:

```
get(object, 'a[0].b.c')
```

## 🤔 Why an npm module tho?

You totally don't have to `npm install` this. This exists for convenience purposes 😊.

In fact, it's encouraged that you add the `get` code to your code base! One less depenency to install and manage.

Here it is!

```js
function get(obj, path, fallback) {
  if (!obj || !path) return fallback;
  const paths = Array.isArray(path) ? path : path.split(".");
  let results = obj;
  let i = 0;

  while (i < paths.length && results !== undefined && results !== null) {
    results = results[paths[i]];
    i++;
  }

  if (i === paths.length) {
    return results !== undefined ? results : fallback;
  }

  return results !== undefined && results !== null ? results : fallback;
}
```

## ❤️ Thanks

Thanks to [@knicklabs](https://github.com/knicklabs) for pairing with me on this one!

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