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

code-config

v1.0.55

Published

type friendly json configuration file management

Downloads

723

Readme

code-config

A type friendly JSON configuration file manager for NodeJS.

Why code-config?

code-config makes dealing with dynamic/static configuration files in TypeScript so much easier since it implements a system that allows you to define the JSON definition and it can also infer the types by the default value you pass through parameters.

Installation

Using yarn:

$ yarn add code-config

Using npm:

$ npm i code-config

Note: add --save if you are using npm < 5.0.0

Examples

Getting a JSON file from a path:

method init() will automatically create the file if it doesn't exists.

import { ConfigFactory } from 'code-config';

interface Definition {
  hello: string
}

export const config = ConfigFactory.getConfig<Definition>('path/to/config.json').init();

console.log(config.hello); // Should work perfectly.

console.log(config.test); // Should throw a type error.

Getting JSON file and place a default value if it doesn't exist:

import { ConfigFactory } from 'code-config';

interface Definition {
  hello: string
}

const defaultValue: Definition = {
  hello: 'World'
}

export const config = ConfigFactory.getConfig<Definition>('path/to/config.json', defaultValue).init();

console.log(config.hello); // Output: World

Saving a JSON file:

import { ConfigFactory } from 'code-config';

const defaultValue = {
  hello: 'World'
}

export const config = ConfigFactory.getConfig<Definition>('path/to/config.json', defaultValue).init();

console.log(config.hello); // Output: World

config.hello = 'Test';

config.save();

console.log(config.hello); // Output: Test

Clear a JSON file:

import { ConfigFactory } from 'code-config';

const defaultValue = {
  hello: 'World'
}

export const config = ConfigFactory.getConfig('path/to/config.json', defaultValue).init();

console.log(config.hello); // Output: World

config.clear();

config.save();

console.log(config.hello); // Output: undefined

Getters:

import { ConfigFactory } from 'code-config';

const defaultValue = {
  hello: 'World',
  test: {
    value: 'Result'
  }
}

export const config = ConfigFactory.getConfig('path/to/config.json', defaultValue).init();

console.log(config.hello); // Output: World
console.log(config.test.value); // Output: Result

console.log(config.get('hello')); // Output: World
console.log(config.get('test.value')); // Output: Result

Setters:

import { ConfigFactory } from 'code-config';

const defaultValue = {
  hello: 'World',
  test: {
    value: 'Result'
  }
}

export const config = ConfigFactory.getConfig('path/to/config.json', defaultValue).init();

console.log(config.hello); // Output: Hello

config.hello = 'Test';

console.log(config.hello); // Output: Test

config.set('hello', 'Set');

console.log(config.hello); // Output: Set