# js-isset

> isset for javascript - inspired by PHP

Latest version **1.0.4** (published 2021-03-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install js-isset
pnpm add js-isset
yarn add js-isset
bun add js-isset
```

## 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.4 |
| Published | 2021-03-13 |
| First published | 2021-03-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 37.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Nathan Nobbe |
| Maintainers | quickshiftin |
| Keywords | javascript, isset |

## Links

- npm: https://www.npmjs.com/package/js-isset
- Repository: https://github.com/quickshiftin/js-isset
- Homepage: https://github.com/quickshiftin/js-isset#readme
- Issues: https://github.com/quickshiftin/js-isset/issues
- npm.io page: https://npm.io/package/js-isset

## Recent versions

- 1.0.4 (latest) — 2021-03-13
- 1.0.3 — 2021-03-13
- 1.0.2 — 2021-03-13
- 1.0.1 — 2021-03-13
- 1.0.0 — 2021-03-12

## README

![build](https://github.com/quickshiftin/js-isset/actions/workflows/node.js.yml/badge.svg)

# js-isset
`isset` for javascript - inspired by [php](https://www.php.net/manual/en/function.isset.php)

In Javascript, when you want to check for existance, or try to retrieve a deeply nested object property,
tests must be performed all the way down the object structure. For example, if there is an object variable
`dataLayerObject`, and a value is expected at `dataLayerObject.config.checklist`, you might use code like this to retrieve it

```javascript
if(dataLayerObject && dataLayerObject.config && dataLayerObject.config.checklist) {
    return dataLayerObject.config.checklist;
}
```

Sometimes naive programmers resort to exceptions for flow control, an anti-pattern, as in

```javascript
try {
    return dataLayerObject.config.checklist;
} catch (e) {
    console.error('dataLayer missing config or checklist...', e);
}
```

`isset` allows you to provide an object, and a 'path' to search for. Its companion function `valAt` let's you retrieve the value
at that path with the same notation

```javascript
if(isset(dataLayerObject, '.config.checklist')) {
    return valAt(dataLayerObject, '.config.checklist');
}
```

Since the default return value of `valAt` is `null`, you might be able to simply use

```javascript
return valAt(dataLayerObject, '.config.checklist');
```
depending on your use case.

If a node doesn't exist along the desired path, `isset` will return false. By default `valAt` will return `null`, but you can specifiy a `defaultValue` of your choosing.

## Installation
```bash
npm install --save js-isset
```

## API
```javascript
/**
 * Check the provided object for existence of the required path, and that the value at
 * that path is not null. If no path is provided, the object proper is inspected to ensure
 * it is not null.
 * 
 * @param object [object] - desired object to inspect
 * @param path [string] - path to search for
 * @returns [boolean]
 * @throws Error
 */
function isset(object, path=null)
```

```javascript
/**
 * Retrieve the deeply nested value at the desired path within object, and if the path is
 * not present, or the value at said path is null, return the defaultValue.
 *
 * @param object [object] - desired object to inspect
 * @param path [string] - path to search for
 * @param defaultValue [string] - default return value
 * @returns [any]
 * @throws Error
 */
function valAt(object, path, defaultValue=null)
```

## Example usage

```javascript
import { isset, valAt } from 'js-isset';

var dataLayerObject = {
    config: {
        checklist: 'ready, set, go'
    }
};

isset(dataLayerObject, '.config.checklist')            // true
valAt(dataLayerObject, '.config.checklist')            // 'ready, set, go
isset(dataLayerObject, '.config.broken')               // false
valAt(dataLayerObject, '.config.broken')               // null
valAt(dataLayerObject, '.config.broken', 'my default') // 'my default'
```

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