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

@n7e/configuration

v0.3.1

Published

A lightweight configuration library.

Downloads

151

Readme

Configuration

A lightweight configuration library.

Whether to be used as configuration for an entire application or just for small objects with convenience methods, this library provides a configuration solution that is easy to use and extend.

For further insights, read on.

Installation

To install this library use your favorite package manager. No additional steps are required to start using the library.

npm install @n7e/configuration

This library is implemented in TypeScript but can be used with JavaScript without any additional steps.

Configuration Object

The protagonist of this library is the configuration object. Configuration objects lets you retrieve and manipulate configuration values. All configuration objects implement the Configuration interface. To reference it simply import it and use it as a type.

import { Configuration } from "@n7e/configuration";

function doSomethingWith(configuration: Configuration): void {
    // ...
}

Configuration is just an interface describing the functionality of a configuration object. To create configuration objects use a configuration builder.

Key Path

Key paths are used to reference values in configuration objects. To reference leaf values in nested objects the key in each subsequent object is separated with a ..

The key path for the value "value" in the following structure looks like: nested.key.

{
    nested: {
        key: "value"
    }
}

Check Value Presence

To check whether a configuration value is present in a configuration object use the has method:

if (configuration.has("key")) {
    // ...
}

Even if the value at the given key path is null or undefined the value is considered to be present.

Retrieve Value

To retrieve configuration values from a configuration object use the get method:

const value = configuration.get("key");

The value will be of any type found at the given key path. So if the key path does not reference a leaf the type will be that of an object.

If the key path does not reference a value undefined will be returned. You can supply a desired value to use in case of an absent value:

const value = configuration.get("key", "default value");

If the key path does not reference a value "default value" will be returned.

Set Configuration Value

To set a configuration value use the set method. Values can be of any type:

configuration.set("key", "value");

The key path will reference the given value. If the key path is nested the necessary intermediary objects will be created in the configuration object. To illustrate this suppose we start with an empty configuration object and add a value to a nested key path:

configuration.set("nested.key", "value");

The value referenced by the key path "nested" will be {key: "value"}.

Remove Value

To remove a value from a configuration source use the remove method:

configuration.remove("key");

Clear Values

To clear all values from a configuration object use the clear method.

configuration.clear();

Configuration Builder

To create configuration objects you should use a ConfigurationBuilder.

To define what configuration values makes up the final configuration object you can add any number of configuration sources.

configurationBuilder
    .addConfigurationSource(jsonFileConfigurationSource)
    .addConfigurationSource(restConfigurationSource)
    .addConfigurationSource(someOtherConfigurationSource);

A configuration builder can be passed around and incrementally augmented to eventually produce a desired configuration object.

const configuration = await configurationBuilder.build();

ConfigurationBuilder is just an interface describing the functionality of a configuration builder. To create a configuration builder instance you need to reference a specific implementation.

Configuration Sources

Configuration sources provide configuration builders with the values to build configuration objects from. The asynchronous nature of configuration sources allows for configuration values to be retrieved from any number of external resources.

ConfigurationSource is just an interface describing the functionality of a configuration source. To create a configuration source instances you need to reference a specific implementation.

Object Configuration Source

There's a provided implementation of ConfigurationSource that provides configuration values from a plain JavaScript object. The final configuration object will contain the values of any object provided to the constructor.

import { ObjectConfigurationSource } from "@n7e/configuration";

configurationBuilder.addConfigurationSource(new ObjectConfigurationSource({key: "value"}));

Default Configuration Builder

There's a provided implementation of ConfigurationBuilder ready to use.

import { DefaultConfigurationBuilder } from "@n7e/configuration";

const configurationBuilder = new DefaultConfigurationBuilder();

// Add any desired configuration sources...

const configuration = await configurationBuilder.build();

Configuration Section

Configuration sections are essentially configuration objects operating in isolation on a specific key path. Any and all operations on the configuration section will only affect the specified key path.

This can be useful when you want to enable some procedure to modify configuration but don't want to give the procedure access to the entire configuration object.

setRelevantConfigurationValues(configurationSectionFactory.createConfigurationSection("nested", configuration));

function setRelevantConfigurationValues(configuration: Configuration): void {
    configuration.set("key", "value");
}

Calling all() on the provided configuration object after the procedure call will yield the following value:

{
    nested: {
        key: "value"
    }
}

To create a configuration section use a configuration section factory.

Configuration Section Factory

Configuration section factories has the ability to create configuration sections.

ConfigurationSectionFactory is just an interface describing the functionality of a configuration section factory. To create a configuration section factory instance you need to reference a specific implementation.

Default Configuration Section Factory

There's a provided default implementation of ConfigurationSectionFactory.

import { DefaultConfigurationSectionFactory } from "@n7e/configuration";

const configurationSectionFactory = new DefaultConfigurationSectionFactory();

/*
 * The created configuration section will operate on the "section" key path of
 * the provided configuration object.
 */
const configurationSection = configurationSectionFactory.createConfigurationSection("section", configuration);