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

resolver-revolver

v0.4.10

Published

Resolves and validates configurations.

Downloads

218

Readme

resolver-revolver

Codeship Status for sytac/gulp-commonjs-tasks

revolver

Description

Resolves values from and validates by trying to find values in given contexts in a lazy way.

Installation

$ npm install --save resolver-revolver

Since that is pretty generic, here's an example which resolves NODE_ENV from either the command line, then from the system environment, and if all else fails it will set it to a default of development.

If you'd like to try these examples, please run gulp prepare-examples first

// ./examples/easy/index.js

'use strict';

var resolverRevolver = require('resolver-revolver');
var argv = require('yargs').argv;
var isString = require('lodash.isstring');

var resolved = resolverRevolver.parse({
  context: {
    argv: argv,
    env: process.env
  },
  resolvables: {
    'environment': {
      from: ['argv.NODE_ENV', 'env.NODE_ENV'],
      default: 'development'
    }
  }
});

var defaults = {
  environment: resolved.environment()
};

console.log(JSON.stringify(defaults, null, 4));

Default value

Since argv.NODE_ENV and env.NODE_ENV can not be resolved round-robin style, the default will be resolved.

$ node examples/easy
{
    "environment": "development"
}

Use a cli argument

Since the first

$ node examples/easy --NODE_ENV=production
{
    "environment": "production"
}

Export a system variable

$ export NODE_ENV=coocoo
$ node examples/easy                           
{
    "environment": "coocoo"
}

Provide a cli argument and export a system variable

Since argv.NODE_ENV has a higher priority than env.NODE_ENV, it will be resolved.

$ export NODE_ENV=coocoo
$ node examples/easy --NODE_ENV=production                          
{
    "environment": "production"
}

Savvy? Here's a more elaborate example.

// ./examples/elaborate/index.js

'use strict';

var resolverRevolver = require('resolver-revolver');
var argv = require('yargs').argv;
var negate = require('lodash.negate');
var isUndefined = require('lodash.isundefined');
var isDefined = negate(isUndefined);

var resolved = resolverRevolver.parse({
  // Add a console for logging
  // defaults to undefined
  console: console,

  // Our context
  // defaults to {}
  context: {
    argv: argv,
    env: process.env
  },

  // when set to true it returns the default without validating
  lenientDefaults: false,

  // Resolvables
  resolvables: {
    'environment': {
      // defaults to []
      from: ['argv.NODE_ENV', 'env.NODE_ENV'],
      // defaults to undefined
      default: 'development',

      // And preconditions. If you do not set this array, it will default to
      // [isDefined].
      //
      // A precondition can be a function or an object containing a function and
      // name like so:
      // {
      //  fn: isDefined,
      //  name: 'is defined'
      // }
      //
      // Let's add our own here
      // defaults to []
      preconditions: [{
        fn: isDefined,
        name: 'is defined'
      }, {
        fn: function (value) {
          return ['development', 'production', 'test'].indexOf(value) !== -1;
        },
        name: 'is an enviroment'
      }]
    }
  }
});

var defaults = {
  // call resolved.environment function
  environment: resolved.environment()
};

console.log(JSON.stringify(defaults, null, 4));
$ unset NODE_ENV
$ node examples/elaborate --NODE_ENV asdasd
Resolving: environment
    ✖ (1 of 3) - argv.NODE_ENV: asdasd
      ✔ (1 of 2) is defined
      ✖ (2 of 2) is an enviroment
    ✖ (2 of 3) - env.NODE_ENV: undefined
      ✖ (1 of 2) is defined
      ✖ (2 of 2) is an enviroment
  ✊✔ (3 of 3) - default value: development
      ✔ (1 of 2) is defined
      ✔ (2 of 2) is an enviroment

Resolved environment from default value to development

{
    "environment": "development"
}

Running tests

$ gulp test

Test reports are written to ./reports.

Contributing

  • Do pull requests.
  • Make sure there's tests and meaningful coverage.
  • Respect ./eslintrc.
  • Issues should go in issues.