0.1.0 • Published 8 years ago

get-else-set v0.1.0

Weekly downloads
2
License
MIT
Repository
github
Last release
8 years ago

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!

const getElseSet = require('get-else-set');

// Synchronous  
getElseSet.sync(checkPropertyArray, object, setValue[Optional, default is null])

// Asynchronous  
getElseSet.async(checkPropertyArray, object, setValue[Required], callback)

Example Script

Example Data Here
Example Script Here 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!' 
                }
            ]
        */
        })
    })

})