New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

config-dot-get

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

config-dot-get

A simple JSON configuration reader with dot notation support

latest
Source
npmnpm
Version
1.0.1
Version published
Maintainers
1
Created
Source

config-dot-get

config-dot-get is a lightweight JSON configuration reader that allows developers to retrieve and modify settings using dot notation. It efficiently handles nested configuration values.

🚀 Installation

npm install config-dot-get

📖 Usage

const ConfigReader = require("config-dot-get");

const config = new ConfigReader("config.json");

// Get a value using dot notation
console.log(config.get("transpile.debug", true)); // Returns the value or default (true)

// Set a value
config.set("logging.level", "info");
console.log(config.get("logging.level")); // "info"

✨ Features

  • Supports dot notation for accessing nested keys.
  • Allows modifying settings dynamically with .set().
  • Automatically saves changes to the JSON file.
  • Provides default values if a key is missing.

📌 Examples

🔹 Basic Usage

const ConfigDotGet = require("config-dot-get");

const config = new ConfigReader("config.json");

// Retrieve a top-level value
console.log(config.get("app.name")); // Example output: "MyApp"

// Retrieve a nested value using dot notation
console.log(config.get("database.host")); // Example output: "localhost"

// Provide a default value if the key is missing
console.log(config.get("server.port", 3000)); // Example output: 3000 (default)

🔹 Setting Values

// Modify a value dynamically
config.set("logging.level", "debug");

// Retrieve the updated value
console.log(config.get("logging.level")); // Example output: "debug"

🔹 Handling missing values

console.log(config.get("feature.enable", false)); // Returns default (false)

🔹 Working with Nested Configuration

// Config structure (config.json)
// {
//   "database": {
//     "host": "localhost",
//     "port": 5432
//   }
// }

// Access nested values
console.log(config.get("database.port")); // Example output: 5432

🏆 Best Practices

  • Use default values: Always provide a default value when retrieving configuration settings to avoid unexpected undefined values.

    const port = config.get("server.port", 3000);
    
  • Keep configuration organized: Structure JSON files logically to prevent deep nesting that may be difficult to maintain.

  • Validate input: Ensure the retrieved values match expected data types to prevent runtime issues.

    const logLevel = config.get("logging.level", "info");
    if (typeof logLevel !== "string") {
     throw new Error("Invalid log level type");
    }
    
  • Avoid hardcoding paths: Store key paths in constants for better maintainability.

    const SERVER_PORT = "server.port";
    const port = config.get(SERVER_PORT, 3000);
    
  • Use .set() responsibly: Modify configuration settings only when necessary to prevent unintended overrides.

    config.set("feature.enabled", true);
    

🛠 License

This package is licensed under MIT License.

Keywords

json

FAQs

Package last updated on 14 Jun 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts