# get-else-set

> getElseSet is a utility function to check if a property (of any nested level) exists. If the property does not exist, the value is set to 'null' or a value of your choice.

Latest version **0.1.0** (published 2016-06-02) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install get-else-set
pnpm add get-else-set
yarn add get-else-set
bun add get-else-set
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2016-06-02 |
| First published | 2016-06-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Elias Hussary |
| Maintainers | eliashussary |

## Links

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

## Dependencies (1)

- [lodash](https://npm.io/package/lodash.md) ^4.13.1

## Recent versions

- 0.1.0 (latest) — 2016-06-02

## README

# getElseSet
## What is getElseSet?
getElseSet is a utility function to check if a property (of any nested level) exists. 
If the property does not exist, the value is set to 'null' or a value of your choice.

## How is it useful?
This is useful when mapping over and restructuring JSON objects that have near similar properties, 
but some may be undefined.

For example, you may have a NoSQL collection where a few documents in the collection are missing a property. 
Using getElseSet you can restructure your documents, and when met by an undefined property you can set it to 'null' or a value of your choice.

I developed this to assist with migrating from a NoSQL DB to SQL DB in which the NoSQL documents were not all the same.

## How to Use
Use it synchronously or asynchronously!  

`

    // Synchronous  
    getElseSet.sync(checkPropertyArray, object, setValue[Optional, default is null])

    // Asynchronous  
    getElseSet.async(checkPropertyArray, object, setValue[Required], callback)
`

## Example Script
[Example Data Here](https://github.com/eliash91/get-else-set/blob/master/example/example.data.json)
[Example Script Here](https://github.com/eliash91/get-else-set/blob/master/example/exampleScript.js) or below.

`

    'use strict'
    const getElseSet = require('../getElseSet');
    const fs = require('fs');

    // The properties you want to check for. In our example, we use nested properties.
    const myCheckArray = [
        'data.type',
        'data.attributes.title',
        'data.attributes.body'
    ]

    // Get data function. You can use anything to pull in your JSON data.
    fs.readFile('./example.data.json', 'utf8', (err, data) => {
        
        let jsonRes = JSON.parse(data);

        jsonRes.map(o => {

            
    // #### Synchronous Example!
            getElseSet.sync(myCheckArray, o, 'Sync Function: Replaced value!')
            /*
                Output:
                [
                    { 
                        data_type: 'articles',
                        data_attributes_title: 'JSON API paints my bikeshed!',
                        data_attributes_body: 'The shortest article. Ever.' 
                    },
                    { 
                        data_type: 'articles',
                        data_attributes_title: 'JSON API paints my bikeshed!',
    Replaced Value ->   data_attributes_body: 'Sync Function: Replaced value!' 
                    }
                ]
            */

            
    // #### Asynchronous Example!
            getElseSet.async(myCheckArray, o, 'Async Function: Replaced value!', result => {
                /*
                Output:
                [
                    { 
                        data_type: 'articles',
                        data_attributes_title: 'JSON API paints my bikeshed!',
                        data_attributes_body: 'The shortest article. Ever.' 
                    },
                    { 
                        data_type: 'articles',
                        data_attributes_title: 'JSON API paints my bikeshed!',
    Replaced Value ->   data_attributes_body: 'Async Function: Replaced value!' 
                    }
                ]
            */
            })
        })

    })


`

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