Socket
Socket
Sign inDemoInstall

undefsafe

Package Overview
Dependencies
0
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

undefsafe

Undefined safe way of extracting object properties


Version published
Maintainers
1
Weekly downloads
5,436,121
decreased by-5.03%

Weekly downloads

Package description

What is undefsafe?

The undefsafe package is a utility for safely accessing deeply nested properties of an object. It allows developers to query properties without worrying about intermediate properties being undefined or null. This can be particularly useful in situations where the structure of an object might be unpredictable or when dealing with JSON objects returned from an API.

What are undefsafe's main functionalities?

Safe property access

This feature allows for the safe querying of nested properties. If any property in the path is undefined, undefsafe will return `undefined` instead of throwing an error.

const undefsafe = require('undefsafe');
const obj = { a: { b: { c: 'd' } } };
console.log(undefsafe(obj, 'a.b.c')); // 'd'
console.log(undefsafe(obj, 'a.x.c')); // undefined

Setting deeply nested values safely

Undefsafe can also be used to set a value at a deeply nested path within an object. If any part of the path does not exist, it will be created.

const undefsafe = require('undefsafe');
let obj = {};
undefsafe(obj, 'a.b.c', 'd');
console.log(obj.a.b.c); // 'd'

Other packages similar to undefsafe

Readme

Source

undefsafe

Simple function for retrieving deep object properties without getting "Cannot read property 'X' of undefined"

Can also be used to safely set deep values.

Usage

var object = {
  a: {
    b: {
      c: 1,
      d: [1,2,3],
      e: 'remy'
    }
  }
};

console.log(undefsafe(object, 'a.b.e')); // "remy"
console.log(undefsafe(object, 'a.b.not.found')); // undefined

Demo: https://jsbin.com/eroqame/3/edit?js,console

Setting

var object = {
  a: {
    b: [1,2,3]
  }
};

// modified object
var res = undefsafe(object, 'a.b.0', 10);

console.log(object); // { a: { b: [10, 2, 3] } }
console.log(res); // 1 - previous value

Star rules in paths

As of 1.2.0, undefsafe supports a * in the path if you want to search all of the properties (or array elements) for a particular element.

The function will only return a single result, either the 3rd argument validation value, or the first positive match. For example, the following github data:

const githubData = {
        commits: [{
          modified: [
            "one",
            "two"
          ]
        }, /* ... */ ]
      };

// first modified file found in the first commit
console.log(undefsafe(githubData, 'commits.*.modified.0'));

// returns `two` or undefined if not found
console.log(undefsafe(githubData, 'commits.*.modified.*', 'two'));

Keywords

FAQs

Last updated on 17 Oct 2021

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc