Latest Threat Research:Malicious dYdX Packages Published to npm and PyPI After Maintainer Compromise.Details
Socket
Book a DemoInstallSign in
Socket

get-or-else

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

get-or-else

Get Or Else in Javascript

Source
npmnpm
Version
1.1.4
Version published
Maintainers
1
Created
Source

get-or-else

Build status

Simple Get Or Else module written in JavaScript ES5.

Request an object property at a given namespace with a backup value, incase the desired namespace does not yield a result.

Useful if you have an untrustworthy data source. It will probably save you a bit of if, else-ery.

Example ES5

var getOrElse = require("get-or-else");

window.a = {x:4};
getOrElse({ get: [window,'a.b.c'], else: {} });
// {} - does not exist, so `else` is used
getOrElse({ get: [window,'a'], else: {} })
// {x:4} - does exist, so expected value is returned

Example ES6 Redux

import getOrElse from 'get-or-else';

export const name = (state = {}, action = {}) => {

  switch(action.type) {

  case 'SET_NAME':
    return {
      ...state,
      ...getOrElse({ get: [action,'payload.name'], else: {} });
    };

  default:
    return state;

  }
};

Example ES6 React

see this repo get-or-else-demo

import React from 'react';
import ReactDOM from 'react-dom';
import getOrElse from 'get-or-else';

const nameObj = {
  salutation: 'Mr',
  name: {
    first: 'James'
  }
};

const salutation = getOrElse({  get: [nameObj,'salutation'], else: false });

const NameComponent = () => {
  return (
    <h1>
      We have been expecting you

      {salutation && <span> {salutation} </span>}

      <span> {getOrElse({ get: [nameObj,'name.first'], else: '' })}</span>

      <span> {getOrElse({ get: [nameObj,'name.last'], else: '' })}</span>
    </h1>
  );
};

ReactDOM.render(<NameComponent />, document.getElementById('root'));

/* NameComponent Renders `
<h1>
  We have been expecting you
  <span> Mr </span>
  <span>James</span>
  <span></span>
</h1>`

nameObj.name.last does not exist so it does not display
*/

NPM Package

get-or-else

Browser compatibility

IE 9 or greater - Array.every on Mozilla's compatibility chart

The getOrElse Module

/**
 * @param {Object} getOrElseObj
 * @param {Array} getOrElseObj.get
 * @param {Object} getOrElseObj.get[0] - the root object to your namespace.
 * e.g. this, window, someObj (Must exist).
 * @param {String} getOrElseObj.get[1] - a string representation of the namespace you
 * are targeting to get it's property. e.g. 'a.b.c.d'
 * @returns {Object} the item you hope for `get`, or a backup item `else` if it does not exist.
 * @example
 * GIVEN
 * window.a = {x:4}
 *
 * getOrElse({ get: [window,'a.b.c'], else: {} }) // {} - does not exist, so `else` is used
 * getOrElse({ get: [window,'a'], else: {} }) // {x:4} - does exist, so expected value is returned
 */

var getOrElse = function( getOrElseObj ) {
  if (!getOrElseObj.get[0]) return getOrElseObj.else;
  var contextObj = getOrElseObj.get[0];
  var namespace = getOrElseObj.get[1].split('.');
  var value = contextObj; // reassigns to obj[key] on each array.every iteration
  return (
    namespace.every( function( key ) {
      return (value = value[key]) != undefined
    })
  ) ? value : getOrElseObj.else;
};

module.exports = getOrElse;

Run the tests

Given you have Node installed, cd into this folder and:

npm install
npm test

Keywords

get

FAQs

Package last updated on 13 Aug 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