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

@stone-js/config

v0.0.33

Published

Fluent and simple API with deep dot access to manage configurations in JavaScript any project

Downloads

263

Readme

Stone.js: Config

npm npm npm Maintenance Publish Package to npmjs Dependabot Status Conventional Commits

Fluent and simple API with deep dot access to manage configurations in any JavaScript project.


Synopsis

@stone-js/config is a versatile configuration management library that supports both vanilla JavaScript and TypeScript. It allows developers to easily manage application settings with features like nested configuration access, default value management, and proxy-based custom behavior for configuration properties.

Installation

The Config library is available from the npm registry and can be installed as follows:

npm i @stone-js/config

Yarn:

yarn add @stone-js/config

PNPM:

pnpm add @stone-js/config

[!NOTE] This package is Pure ESM. If you are unfamiliar with what that means or how to handle it in your project, please refer to this guide on Pure ESM packages.

Make sure your project setup is compatible with ESM. This might involve updating your package.json or using certain bundler configurations.

The Config module can only be imported via ESM import syntax:

import { Config } from '@stone-js/config';

Getting Started

The Config library is designed to simplify configuration management in JavaScript and TypeScript projects. The library provides a Config class that allows you to create, access, modify, and clear configuration values, while also providing utility methods for managing defaults and nested properties.

The library is compatible with both vanilla JavaScript and TypeScript, providing strong type safety when used in TypeScript projects.

Usage

Importing the Library

To use the Config library, import the Config class from the installed package:

import { Config } from '@stone-js/config';

Creating a Config Instance

You can create a Config instance with an initial set of configuration values using the Config.create() method:

import { Config } from '@stone-js/config';

const config = Config.create({
  appName: 'MyApp',
  settings: {
    theme: 'dark',
    notifications: true
  }
});

Accessing Configuration Values

You can access configuration values using the get method. The get method also allows you to specify a fallback value if the key does not exist:

console.log(config.get('appName')); // Outputs: 'MyApp'
console.log(config.get('settings.theme')); // Outputs: 'dark'
console.log(config.get('settings.language', 'en')); // Outputs: 'en' (fallback value)

Setting Configuration Values

You can add or update configuration values using the set method:

config.set('settings.theme', 'light');
console.log(config.get('settings.theme')); // Outputs: 'light'

You can also set multiple values at once by passing an object:

config.set({
  'settings.language': 'fr',
  'settings.notifications': false
});
console.log(config.get('settings.language')); // Outputs: 'fr'

Checking for Configuration Values

To check if a particular configuration value exists, use the has method:

console.log(config.has('settings.theme')); // Outputs: true
console.log(config.has('settings.nonExistentKey')); // Outputs: false

Adding Configuration Values with Merge

The add method allows you to add configuration values. If the key already exists and both values are objects, they will be merged:

config.add('settings', { notifications: false });
console.log(config.get('settings.notifications')); // Outputs: false

Getting the First Match Configuration Value

The firstMatch method allows you to get the value of the first existing key from an array of keys:

const value = config.firstMatch(['nonExistentKey', 'settings.theme'], 'defaultTheme');
console.log(value); // Outputs: 'light'

Getting Multiple Configuration Values

The getMany method allows you to get multiple configuration values at once:

const values = config.getMany(['appName', 'settings.theme']);
console.log(values); // Outputs: { appName: 'MyApp', 'settings.theme': 'light' }

Clearing Configuration

You can clear all configuration values using the clear method:

config.clear();
console.log(config.all()); // Outputs: {}

Working with Nested Properties

The Config class supports accessing and setting nested properties. You can use dot-notation strings to manage nested properties effectively:

console.log(config.get('settings.theme')); // Outputs: 'light'
config.set('settings.newFeature.enabled', true);
console.log(config.get('settings.newFeature.enabled')); // Outputs: true

Summary

The @stone-js/config library is a powerful and flexible solution for managing configuration in JavaScript and TypeScript applications. With support for nested properties, default value handling, and proxy-based custom behaviors, it provides a robust toolset for configuration management.

Key Features:

  • Simple API: Easy to use methods for creating, accessing, and modifying configuration values.
  • TypeScript Compatibility: Full TypeScript support for type safety and better development experience.
  • Nested Properties: Seamless handling of nested configuration values.
  • Default Value Management: Easily set and manage default values for configuration keys.

Start using @stone-js/config to simplify your application's configuration management and bring flexibility and robustness to your codebase.

API documentation

Contributing

See Contributing Guide.

Credits