1.0.0 • Published 7 years ago

require-maybe v1.0.0

Weekly downloads
11
License
MIT
Repository
github
Last release
7 years ago

require-maybe

build status code coverage code style styled with prettier made with lass license

Optionally require a module, optionally falling back to others.

  • returns undefined if the optional module(s) can't be found
  • without throwing an exception
  • can attempt to load alternative modules
  • works with all paths (unlike similar modules)

This can be useful for libraries that want to use certain features when the optional modules are installed, without pulling in additional dependencies.

Table of Contents

Install

npm:

npm install require-maybe

yarn:

yarn add require-maybe

Usage

const requireMaybe = require('require-maybe');

function noop(x) {
  return x
}

// use `rainbow` if it’s installed, or normal text if it isn't
const rainbow = requireMaybe('chalk-rainbow') || noop
console.log(rainbow('hello world!'))
// -> rainbowified or plain "hello world!"


// Use either implementation of `camelcase`, or none
const camelcase = requireMaybe(['lodash.camelcase', 'camelcase']) || noop
console.log(camelcase('hello world!'))
// -> "helloWorld!" or "hello world!"

Contributors

NameWebsite
Andreas Pizsahttps://github.com/AndreasPizsa

License

MIT © Andreas Pizsa