CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-lodash-result

The lodash method `_.result` exported as a module for getting values at object paths with function resolution and default value support.

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

lodash.result

lodash.result provides the _.result utility function from the lodash library as a standalone Node.js module. It enables deep property access with support for resolving functions by calling them if the retrieved value is a function, and providing a default value if the path doesn't exist or resolves to undefined.

Package Information

  • Package Name: lodash.result
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install lodash.result

Core Imports

var result = require('lodash.result');

Basic Usage

var result = require('lodash.result');

var object = {
  'user': {
    'name': 'Alice',
    'getAge': function() { return 30; },
    'details': {
      'address': '123 Main St'
    }
  }
};

// Get a simple property
result(object, 'user.name');
// => 'Alice'

// Get a nested property
result(object, 'user.details.address');
// => '123 Main St'

// Function resolution - automatically calls functions
result(object, 'user.getAge');
// => 30

// With default value for missing paths
result(object, 'user.email', 'no-email@example.com');
// => 'no-email@example.com'

// Array notation for paths
result(object, ['user', 'name']);
// => 'Alice'

Capabilities

Property Path Resolution

Gets the value at a specified path of an object, with support for resolving functions by calling them and providing default values for missing paths.

/**
 * Gets the value at path of object. If the path doesn't exist or resolves to undefined,
 * the defaultValue is returned. If the resolved value is a function, it's invoked with
 * the this binding of its parent object and its result is returned.
 * 
 * @param {Object} object - The object to query
 * @param {Array|string} path - The path of the property to resolve
 * @param {*} [defaultValue] - The value returned if the resolved value is undefined
 * @returns {*} Returns the resolved value
 */
function result(object, path, defaultValue);

Parameters:

  • object (Object): The object to query
  • path (Array|string): The path of the property to resolve. Can be:
    • String notation: 'a.b.c' or 'a[0].b.c'
    • Array notation: ['a', 'b', 'c'] or ['a', 0, 'b', 'c']
  • defaultValue (*, optional): The value returned if the resolved value is undefined

Returns:

  • (*): Returns the resolved value, or the result of calling a function if the value at path is a function, or the defaultValue if path doesn't exist

Key Behavior:

  1. Path Resolution: Supports both string ('a.b.c') and array (['a', 'b', 'c']) path notation
  2. Array Index Access: Handles array indices in both string ('arr[0].prop') and array (['arr', 0, 'prop']) formats
  3. Function Resolution: If the value at the specified path is a function, automatically calls it with the parent object as context (this)
  4. Default Value Handling: Returns the provided defaultValue if the path doesn't exist or resolves to undefined
  5. Null/Undefined Safety: Safely handles null or undefined objects without throwing errors

Usage Examples:

var result = require('lodash.result');

// Complex nested object with functions
var userData = {
  profile: {
    name: 'John Doe',
    getFullName: function() { 
      return this.name + ' (verified)'; 
    },
    settings: {
      theme: 'dark',
      notifications: {
        email: true,
        push: false
      }
    }
  },
  posts: [
    { title: 'First Post', views: 100 },
    { title: 'Second Post', views: 250 }
  ]
};

// String path notation
result(userData, 'profile.name');
// => 'John Doe'

// Function resolution with context
result(userData, 'profile.getFullName');
// => 'John Doe (verified)'

// Deep nested property access
result(userData, 'profile.settings.notifications.email');
// => true

// Array index access
result(userData, 'posts[0].title');
// => 'First Post'

// Array notation for complex paths
result(userData, ['posts', 1, 'views']);
// => 250

// Default value for missing paths
result(userData, 'profile.age', 'Unknown');
// => 'Unknown'

result(userData, 'nonexistent.deeply.nested.path', 'Not found');
// => 'Not found'

// Safe access on null/undefined objects
result(null, 'any.path', 'Safe default');
// => 'Safe default'

result(undefined, 'any.path', 'Safe default');
// => 'Safe default'

Common Use Cases:

  • Safe Property Access: Accessing nested object properties without risking TypeError on undefined intermediate values
  • Configuration Objects: Retrieving configuration values with fallback defaults
  • API Response Processing: Safely extracting data from potentially incomplete API responses
  • Dynamic Property Access: Using computed property paths stored as strings or arrays
  • Function Resolution: Automatically calling getter methods or computed properties
Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/lodash.result@4.5.x
Publish Source
CLI
Badge
tessl/npm-lodash-result badge