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

objconf

v1.3.2

Published

merge json and env vars into config object

Downloads

6

Readme

None of the 100 config libraries on npm did what I wanted... so i created my own. Works a little bit like the wonderful rust config crate. Goals of this projects include

  • no dependencies
  • treat the config like an object, not a key value store
  • be able to set default values
  • merge config from json file at a given path, this is optional
  • merge env var's with a namespace. Allow nesting object values with underscores. Do a best effort conversion of the data type ('false' => boolean)
  • validate the configuration based on a given schema
$ yarn add objconf

schema after loading

    const Config = require('objconf');
    let conf = new Config();
    conf.merge_defaults({
        a: {
            b: 'something'
        },
        c: 12345
    });

    // assume config.json = { c: 67890 }
    conf.merge_file('./config.json');

    // export PROJECT_A_B = 'something else'
    conf.merge_env('project');

    conf.validate({
        a: {
            b: 'string',
        },
        c: 'number',
    });

    let config = conf.get();
    console.log(config.a.b); // => 'something else'
    console.log(config.c); // => 67890

schema before loading

    const Config = require('objconf');
    let conf = new Config();
    conf.set_schema({
        a: {
            b: 'string'
        },
        c: 'number'
    });
    conf.merge_env() // only merges env vars 'A', 'B', and 'C'
    conf.validate()

API

constructor(options)

  • options.preserveCase: default `false, dont convert keys and prefix's to lowercase
set_schema(obj)

set the config objects schema

    conf.set_schema({
        a: {
            b: 'string'
        },
        c: 'number'
    });
merge_defaults(obj)

merge configuration with a given object

conf.merge_defaults({
    a: {
        b: 'something'
    },
    c: 12345
});
merge_file(filepath, required=false)

merge configuration from a json file given its path. When required is false, any error on reading the file will be suppressed. set required to true to raise those errors.

conf.merge_file('./config.json'); // suppresses errors, good for local dev config
conf.merge_file('./config.json', true); // raises errors
merge_env(prefix = '')

given environment variables with the given prefix. The env var names by default are converted to lowercase for comparrison and merging into the configuration. So for example the env var PREFIX_PARENT_CHILD=test will be merged into the config at { parent: { child: 'test' } }. Prefix is optional, if you do not give one its suggested to add a schema before running this method.

conf.merge_env(); // run set_schema() first!
conf.merge_env('project');
validate(schema = this.schema)

takes a schema which is an object with string values. These values can be 'string', 'boolean', 'null ', 'undefined', or 'number'. if the types of the current configuration do not match the schema, an error is thrown. If you have set the schema already you do not need to pass one in

conf.merge_defaults({
    a: {
        b: 'something',
    },
    c: true,
    d: 12345,
    e: null,
    f: undefined,
});

conf.validate({
    a: {
        b: 'string',
    },
    c: 'boolean',
    d: 'number',
    e: 'null',
    f: 'undefined',
});
    conf.set_schema({
        a: {
            b: 'string'
        },
        c: 'number'
    });
    ...
    conf.validate()
get()

return a copy of the configuration object

conf.merge_defaults({ a: 'test' });
console.log(cong.get().a, ' = test')

Development

Issues and PR's are always welcome, just follow the prettier.js style guides described below.

Testing

this project uses ava, just run yarn test (or npm test if your into that sort of thing). Test's are good examples on how to use this package

Styling

This project uses Prettier.js for code formating and linting. I would recomend installing it globally as described here and integrate it with your editor.

here is the configuration used

--no-semi: false
--single-quote: true
--tab-width: 4
--print-width: 100

check out .eslint.rc as well