Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

get-else-set

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

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.

  • 0.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

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 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!' 
                }
            ]
        */
        })
    })

})

`

FAQs

Package last updated on 02 Jun 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc