common-utility-functions
    Preparing search index...

    Function getProp

    • Creates a function that safely retrieves a nested property from an object using dot notation. Returns undefined if any part of the path is undefined or null, preventing runtime errors when accessing deeply nested properties.

      Type Parameters

      • T extends Record<string, undefined | T>

      Parameters

      • keyPath: string

        A dot-separated string representing the path to the desired property (e.g., 'user.profile.name')

      Returns (obj: undefined | T) => undefined | T

      A function that takes an object and returns the value at the specified path, or undefined if not found

      const user = {
      profile: {
      name: 'John',
      settings: {
      theme: 'dark'
      }
      }
      };

      const getName = getProp('profile.name');
      const getTheme = getProp('profile.settings.theme');
      const getInvalid = getProp('profile.invalid.path');

      console.log(getName(user)); // 'John'
      console.log(getTheme(user)); // 'dark'
      console.log(getInvalid(user)); // undefined

      // Safe with undefined objects
      console.log(getName(undefined)); // undefined

      // Useful with array methods
      const users = [user, { profile: { name: 'Jane' } }];
      const names = users.map(getName); // ['John', 'Jane']

      1.0.0