https://github.com/quickshiftin/js-isset
isset for javascript - inspired by php
https://github.com/quickshiftin/js-isset
Last synced: about 2 months ago
JSON representation
isset for javascript - inspired by php
- Host: GitHub
- URL: https://github.com/quickshiftin/js-isset
- Owner: quickshiftin
- License: mit
- Created: 2021-03-12T19:44:00.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2021-03-13T00:34:08.000Z (about 4 years ago)
- Last Synced: 2025-03-16T13:46:07.470Z (3 months ago)
- Language: JavaScript
- Size: 112 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README

# 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'
```