npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

object-path-generator

v1.0.9

Published

A lightweight library to generate object paths

Readme

object-path-generator ·

GitHub license npm version npm bundle size downloads

object-path-generator is a lightweight TypeScript utility library for dynamically generating paths for object properties at runtime. This tool simplifies the creation of data hooks, translation keys, and object paths for use with utilities like Lodash's get and set.


Usage example


Features

  • Dynamic Path Generation: Easily generate object paths in a type-safe way.
  • Flexible Usage: Ideal for creating data-hook strings, accessing translation keys, or using deep getters/setters of Lodash, and more.
  • TypeScript Support: Built with TypeScript, ensuring strong type safety and autocompletion in your IDE.

Installation

Install the library via npm or yarn:

# Using npm
npm install object-path-generator

# Using yarn
yarn add object-path-generator

Usage

Basic js Example

const { pathgen } = require('object-path-generator');
const gen = pathgen();
console.log(gen.blah.blah()); // Output: "blah.blah"
console.log(gen.blah.blah.blah()); // Output: "blah.blah.blah"
console.log(gen.testing.something[9][2].complex());
// Output: "testing.something.9.2.run"
console.log(gen.Down.The.Rabbit.Hole('Alice', { In: 'Wonderland' }));
// Output: "Down.The.Rabbit.Hole Alice In-Wonderland"

Basic Typescript Example

Generate type-safe object paths dynamically:

import { pathgen } from 'object-path-generator';
export const AlignmentDataHooks = pathgen<{
  Root: string;
  Label: string;
  HorizontalOption(alignment: ContentJustification): string;
  Item({ id: string }): string;
}>('AlignmentDataHooks');

console.log(AlignmentDataHooks.Root());
// Outputs: "AlignmentDataHooks.Root"
console.log(AlignmentDataHooks.Label());
// Outputs: "AlignmentDataHooks.Label"
console.log(AlignmentDataHooks.HorizontalOption(ContentJustification.Center));
// Outputs: "AlignmentDataHooks.HorizontalOption center"
console.log(AlignmentDataHooks.Item({ id: '123' }));
// Outputs: "AlignmentDataHooks.Item id-123"

Advanced Usage with Custom Function

You can customize the behavior of the generated paths by providing a custom function as the second argument.

Example 1: Using Lodash for Deep Object Access

import { pathgen } from 'object-path-generator';
import { get } from 'lodash';

interface MyObject {
  user: {
    name: string;
    details: {
      address: string;
    };
  };
}

const realObject: MyObject = {
  name: 'Ash Williams',
};

const objectProxy = pathgen<MyObject>('', (path, options) => {
  return get(realObject, path, 'default value');
});

console.log(objectProxy.name()); // Outputs: "Ash Williams"
console.log(objectProxy.details.address()); // Outputs: "default value"

Example 2: Using Translations

// messages_en.json
{
  "common": {
    "loggedIn": {
      "message": "Hey {{username}}, you have successfully logged in!"
    }
  },
  "readingWarning": "{{reader}} reads message from {{writer}}"
}
import { pathgen } from 'object-path-generator';

interface Translations {
  common: {
    loggedIn: {
      message: (data: Record<'username', unknown>) => string;
    };
  };
  readingWarning: (data: Record<'reader' | 'writer', unknown>) => string;
}

// useTransaltions.ts
const useTransaltions = () => {
  const { t } = useI18n();
  const translations = useMemo(
    pathgen<Translations>(undefined, (path, ...options) => {
      return t(path, ...options);
    }),
    [t],
  );

  return { translations };
};

// main.tsx
const { translations } = useTranslations();
console.log(translations.common.loggedIn.message({ username: 'Ash' }));
// Outputs: "Hey Ash, you have successfully logged in!"
console.log(translations.readingWarning({ reader: 'Sam', writer: 'Alex' }));
// Outputs: "Sam reads message from Alex"

API

pathgen

pathgen<T, R>(root?: string, customFn?: (path: string, ...options: any[]) => R)

Generic Types

  1. T:
    • Represents the structure of the object for which paths are generated. This is typically an interface or type defining the shape of your object.
  2. R (optional):
    • The return type of the generated functions. Defaults to string.

Parameters

  1. root (optional):

    • A string representing the root prefix for the generated paths. Typically, this is the name of the object you are generating paths for.
  2. customFn (optional):

    • A function called when a path is accessed. It receives:
      • path: The current path as a string.
      • ...options: Additional arguments passed when the path is accessed.

Returns

An object with the same structure as the input type T where:

  • Nested objects are recursively transformed into proxies.
  • Leaf properties that are primitive become functions (() => R(string by default)) that return their path or the result of customFn.

Why Use object-path-generator?

  • Simplify Path Handling: Avoid hardcoding object paths in your code.
  • Reusable Paths: Generate paths dynamically for use in multiple contexts (e.g., data-hook attributes, translations, and object access).
  • Type Safety: Leverage TypeScript to ensure type safety and autocompletion for object paths.
  • Customizable Behavior: Extend functionality with custom logic for dynamic path generation.
  • Caching for Performance: Reuses previously generated paths through an internal cache, reducing the overhead of creating and resolving paths repeatedly. This improves performance in scenarios where paths are accessed or generated frequently, such as rendering large UI components or processing deeply nested objects.

License

This library is licensed under the MIT License.


Contribution

We welcome contributions, issues, and feature requests! Feel free to create a pull request or submit an issue on GitHub.


Happy path generating! 🚀