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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@trantoanvan/js-nested-traversal

v1.0.7

Published

Simplify the handling of deeply nested data structures in JavaScript

Downloads

182

Readme

nested-traversal

A powerful and flexible library designed to simplify the handling of deeply nested data structures in JavaScript

Introduction

NestedTraversal is a powerful and flexible library designed to simplify the handling of deeply nested data structures in JavaScript. Whether you're working with complex configuration objects, intricate JSON data, or any deeply nested structures, NestedTraversal provides an intuitive API to traverse, retrieve, and modify data with ease.

Features

  • Easy Access: Use dot notation to easily access deeply nested properties.
  • Safe Operations: Avoid "cannot read property of undefined" errors with safe traversal.
  • Flexible Modification: Modify nested structures without manually creating intermediate objects.
  • Merge Capability: Deeply merge multiple nested objects.
  • Array Handling: Seamlessly work with arrays in nested structures.

Installation

npm install nested-traversal

Basic Usage

import NestedTraversal from 'nested-traversal';

const data = {
  user: {
    profile: {
      name: 'John Doe',
      address: {
        city: 'New York'
      }
    },
    hobbies: ['reading', 'cycling', 'photography']
  }
};

const nt = new NestedTraversal(data);

// Get a deeply nested value
const city = nt.get('user.profile.address.city');
console.log(city); // Output: 'New York'

// Set a deeply nested value
nt.set('user.profile.address.zipCode', '10001');

// Access array element
const secondHobby = nt.get('user.hobbies.1');
console.log(secondHobby); // Output: 'cycling'

// Add to array
nt.set('user.hobbies.3', 'painting');

// Merge objects
nt.merge({
  user: {
    profile: {
      age: 30
    },
    hobbies: ['cooking']
  }
});

console.log(nt.get('user.profile.age')); // Output: 30
console.log(nt.get('user.hobbies').toJSON()); // Output: ['reading', 'cycling', 'photography', 'painting', 'cooking']

Advanced Usage: Array Operations

NestedTraversal seamlessly integrates with JavaScript's array methods for powerful data manipulation:

const nt = new NestedTraversal({
  company: {
    name: 'Tech Innovators',
    departments: [
        { id: 1, name: 'Engineering', employees: [
          { id: 101, name: 'Alice', role: 'Software Engineer' },
          { id: 102, name: 'Bob', role: 'DevOps Specialist' }
        ]},

        { id: 2, name: 'Marketing', employees: [
          { id: 201, name: 'Charlie', role: 'Marketing Manager' },
          { id: 202, name: 'Diana', role: 'Content Strategist' }
        ]}
    ]
  }
});

// Looping through departments
nt.get('company.departments').forEach((dept, index) => {
  console.log(`Department ${index + 1}: ${dept.get('name')}`);
});

// Nested loop: departments and employees
nt.get('company.departments').forEach(dept => {
  console.log(`\nEmployees in ${dept.get('name')}:`);
  dept.get('employees').forEach(emp => {
    console.log(`- ${emp.get('name')}: ${emp.get('role')}`);
  });
});

Real-World Applications

1. Configuration Management

NestedTraversal excels in managing complex application configurations:

const config = new NestedTraversal({
  app: {
    server: {
      port: 3000,
      host: 'localhost'
    },
    database: {
      url: 'mongodb://localhost:27017',
      name: 'myapp'
    },
    features: {
      auth: {
        enabled: true,
        provider: 'oauth'
      }
    }
  }
});

// Easily access configuration values
const serverPort = config.get('app.server.port');

// Modify configurations on the fly
config.set('app.features.auth.provider', 'jwt');

// Merge new configurations
config.merge({
  app: {
    features: {
      newFeature: {
        enabled: true
      }
    }
  }
});

console.log(config.get('app.features.newFeature.enabled')); // Output: true

2. State Management in Complex UIs

NestedTraversal can significantly simplify state management in complex user interfaces, such as dashboards or content management systems:

const uiState = new NestedTraversal({
  dashboard: {
    widgets: {
      weather: {
        visible: true,
        data: {
          temperature: 72,
          condition: 'Sunny'
        }
      },
      stocks: {
        visible: false,
        data: {
          indices: []
        }
      }
    },
    userPreferences: {
      theme: 'light',
      notifications: {
        email: true,
        push: false
      }
    }
  }
});

// Easily access and update UI state
const isWeatherWidgetVisible = uiState.get('dashboard.widgets.weather.visible');

// Update nested state without worrying about undefined properties
uiState.set('dashboard.widgets.stocks.data.indices', ['NASDAQ', 'DOW']);

// Update user preferences
uiState.merge({
  dashboard: {
    userPreferences: {
      notifications: {
        push: true
      }
    }
  }
});

console.log(uiState.get('dashboard.userPreferences.notifications.push')); // Output: true

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE.md file for details.