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

nested-navigator

v1.2.2

Published

A lightweight, type-safe utility with automatic type inference to effortlessly navigate and query deeply nested objects and arrays in JavaScript and TypeScript.

Downloads

56

Readme

nested-navigator

npm version License: MIT

A lightweight, type-safe utility for effortlessly navigating and querying deeply nested objects and arrays in JavaScript and TypeScript.

Table of Contents

Installation

You can install nested-navigator using npm:

npm install nested-navigator

Or using yarn:

yarn add nested-navigator

Usage

Basic Usage

import { navigator } from "nested-navigator";

const data = {
  user: {
    name: "John Doe",
    address: {
      city: "New York",
    },
    hobbies: ["reading", "coding"],
  },
};

const city = navigator(data).navigateTo("user.address.city").value();

console.log(city); // Outputs: 'New York'

const hobbiesCount = navigator(data).navigateTo("user.hobbies").getLength();

console.log(hobbiesCount); // Outputs: 2

Advanced Usage

const users = [
  { id: 1, name: "Alice", age: 30, skills: ["JavaScript", "React"] },
  { id: 2, name: "Bob", age: 25, skills: ["Python", "Django"] },
];

const olderThan25 = navigator(users)
  .filter("age", 25, "greater_than")
  .value();

console.log(olderThan25); // Outputs: [{ id: 1, name: "Alice", age: 30, skills: ["JavaScript", "React"] }]

const bobsFirstSkill = navigator(users)
  .find("name", "Bob")
  .navigateTo("skills.0")
  .value();

console.log(bobsFirstSkill); // Outputs: 'Python'

const aliceIndex = navigator(users).getIndex("name", "Alice");

console.log(aliceIndex); // Outputs: 0

API

navigator(obj)

Creates a new NestedNavigator instance.

  • obj: The object to navigate.

Returns: A new NestedNavigator instance.

navigateTo(path)

Navigates to a nested property specified by the given key path.

  • path: A string representing the path to the desired property, using dot notation for nested properties.

Returns: A new NestedNavigator instance positioned at the specified nested location.

find(key, value, operation?)

Finds an element in the current array based on a key-value pair and an optional comparison operation.

  • key: The key to search on.
  • value: The value to search for.
  • operation: (Optional) The comparison operation to use. Defaults to "equals".

Returns: A new NestedNavigator instance with the found array element.

filter(key, value, operation?)

Filters elements in the current array based on a key-value pair and an optional comparison operation.

  • key: The key to filter on.
  • value: The value to filter for.
  • operation: (Optional) The comparison operation to use. Defaults to "equals".

Returns: A new NestedNavigator instance with an array of matching elements.

getIndex(keyOrValue, value?, operation?)

Finds the index of an element in the current array based on a key-value pair (for arrays of objects) or a direct value (for arrays of primitives).

  • keyOrValue: For arrays of objects, this is the key to search on. For arrays of primitives, this is the value to search for.
  • value: (Optional) For arrays of objects, this is the value to search for. This parameter is not used for arrays of primitives.
  • operation: (Optional) The comparison operation to use. Defaults to "equals".

Returns: The index of the found element, or -1 if not found.

getLength()

Returns the length of the current array if it's an array, or undefined otherwise.

Returns: The length of the array (number) if the current value is an array, or undefined if it's not an array.

value()

Returns the current value in the navigation.

Returns: The current value, or undefined if the navigation led to an invalid path.

Comparison Operations

The following comparison operations are available for find, filter, and getIndex methods:

  • "equals" (default): Checks if values are strictly equal.
  • "not_equals": Checks if values are not strictly equal.
  • "greater_than": Checks if the first value is greater than the second.
  • "less_than": Checks if the first value is less than the second.
  • "greater_than_or_equal": Checks if the first value is greater than or equal to the second.
  • "less_than_or_equal": Checks if the first value is less than or equal to the second.

Note: Numerical operations ("greater_than", "less_than", "greater_than_or_equal", "less_than_or_equal") are only performed if both values are numbers.

Examples

Navigating through nested objects

const data = {
  user: {
    profile: {
      name: "John Doe",
      age: 30,
    },
  },
};

const age = navigator(data).navigateTo("user.profile.age").value();

console.log(age); // Outputs: 30

Working with arrays and comparison operations

const data = {
  users: [
    { id: 1, name: "Alice", age: 28 },
    { id: 2, name: "Bob", age: 32 },
    { id: 3, name: "Charlie", age: 22 },
  ],
};

// Finding users older than 25
const olderThan25 = navigator(data)
  .navigateTo("users")
  .filter("age", 25, "greater_than")
  .value();

console.log(olderThan25);
// Outputs: [
//   { id: 1, name: "Alice", age: 28 },
//   { id: 2, name: "Bob", age: 32 }
// ]

// Finding the first user not named "Bob"
const notBob = navigator(data)
  .navigateTo("users")
  .find("name", "Bob", "not_equals")
  .value();

console.log(notBob); // Outputs: { id: 1, name: "Alice", age: 28 }

// Getting the index of the youngest user
const youngestIndex = navigator(data)
  .navigateTo("users")
  .getIndex("age", undefined, "less_than_or_equal");

console.log(youngestIndex); // Outputs: 2 (index of Charlie)

TypeScript Support

nested-navigator is written in TypeScript and provides full type safety and autocompletion when used in TypeScript projects.

import { navigator, NestedKeyOf } from "nested-navigator";

interface User {
  name: string;
  age: number;
  address: {
    city: string;
    country: string;
  };
}

const user: User = {
  name: "John Doe",
  age: 30,
  address: {
    city: "New York",
    country: "USA",
  },
};

const path: NestedKeyOf<User> = "address.city";
const city = navigator(user).navigateTo(path).value();

console.log(city); // Outputs: 'New York'

Contributing

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

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

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