1. resolve-options
Resolve an options object based on configuration.
resolve-options
Package: resolve-options
Created by: gulpjs
Last modified: Sun, 26 Jun 2022 11:52:56 GMT
Version: 2.0.0
License: MIT
Downloads: 7,520,876
Repository: https://github.com/gulpjs/resolve-options

Install

npm install resolve-options
yarn add resolve-options

resolve-options

NPM version Downloads Build Status Coveralls Status

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.

resolver.resolveConstant(optionKey)

Like resolve, but only returns a value if the option is constant (not a function).

License

MIT

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code