Giter Site home page Giter Site logo

resolve-options's Introduction

resolve-options

NPM version Downloads Build Status AppVeyor Build Status Coveralls Status Gitter chat

Resolve an options object based on configuration.

Usage

// This example assumes a Vinyl file

var createResolver = require('resolve-options');

var config = {
  cwd: {
    type: 'string',
    default: process.cwd
  },
  sourcemaps: {
    type: 'boolean',
    default: false
  },
  since: {
    type: ['date', 'number']
  },
  read: {
    type: 'boolean'
  }
};

var options = {
  sourcemaps: true,
  since: Date.now(),
  read: function(file) {
    return (file.extname !== '.mp4');
  }
};

var resolver = createResolver(config, options);

var cwd = resolver.resolve('cwd', file);
// cwd === process.cwd()

var sourcemaps = resolver.resolve('sourcemaps', file);
// sourcemaps === true

var read = resolver.resolve('read', file);
// Given .mp4, read === false
// Given .txt, read === true

API

createResolver([config,] [options])

Takes a config object that describes the options to accept/resolve and an options object (usually passed by a user) to resolve against the config. Returns a resolver that contains a resolve method for realtime resolution of options.

The config object takes the following structure:

config {
  [optionKey] {
    type // string, array or function
    default // any value or function
  }
}

Each option is represented by its optionKey in the config object. It must be an object with a type property.

The type property must be a string, array or function which will be passed to the value-or-function module (functions will be bound to the resolver to allow for dependent options).

A default property may also be specified as a fallback if the option isn't available or is invalid. The default value can be any value or a function (functions will be bound to the resolver to allow for dependent defaults). Note: default values are not type-validated by the value-or-function module.

resolver.resolve(optionKey, [...arguments])

Takes an optionKey string and any number of arguments to apply if an option is a function. Returns the resolved value for the optionKey.

License

MIT

resolve-options's People

Contributors

erikkemperman avatar phated avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.