Skip to content

A lightweight and safe utility for retrieving values from deeply nested JavaScript objects using dot and array notation, ensuring undefined safety.

License

Notifications You must be signed in to change notification settings

mitsuki31/deepget

Repository files navigation

DeepGet

DeepGet is a lightweight and safe utility for retrieving values from deeply nested JavaScript objects using dot and array notation. It ensures undefined safety, making object traversal hassle-free. Say goodbye to complex conditional checks and errors — DeepGet simplifies object traversal with ease.

Features

  • Dot notation support – Access deep properties easily ("a.b.c").
  • Array index notation – Retrieve array elements ("a.b.c[1]").
  • Custom Separator Support - Use a custom separator instead of a single dot (".").
  • Safe access – Prevents errors and returns undefined if the key either is invalid or missing.
  • Lightweight – No third-party dependencies, minimal footprint.

Installation

npm install @mitsuki31/deepget

Usage

Import

CommonJS

const { DeepGet } = require('@mitsuki31/deepget');

ES Module

import DeepGet from '@mitsuki31/deepget';
// Or: import { DeepGet } from 'deepget';

TIP: For more simplicity naming purpose, you can do, for example:

import DeepGet as DG from '@mitsuki31/deepget';

Basic Usage

const obj = { a: { b: { c: 42, $1: 1 } } };

console.log(DeepGet(obj, "a.b.c"));   // 42
console.log(DeepGet(obj, "a.b.$1"));  // 1
console.log(DeepGet(obj, "a.x.y"));   // undefined

Array Index Notation

const obj = { a: { b: { c: [10, 20, 30, [ 'foo' ]] } } };

console.log(DeepGet(obj, "a.b.c[1]"));     // 20
console.log(DeepGet(obj, "a.b.c[5]"));     // undefined
console.log(DeepGet(obj, "a.b.c[3][0]"));  // foo

Using a Custom Separator

const obj = { a: { b: { c: 42 } } };
console.log(DeepGet(obj, "a::b::c", { sep: "::" })); // 42

API

Refer to the Homepage for detailed APIs information.

Why Use DeepGet?

  • Prevents runtime errors: No need for manual if checks.
  • Handles missing values gracefully: Avoids TypeError: Cannot read property ... of undefined.
  • Intuitive dot and array notation: Access nested data effortlessly, also support nested arrays.

License

Licensed under the MIT License.

About

A lightweight and safe utility for retrieving values from deeply nested JavaScript objects using dot and array notation, ensuring undefined safety.

Topics

Resources

License

Stars

Watchers

Forks