require-one
Advanced tools
Comparing version 0.1.2 to 0.2.0
{ | ||
"name": "require-one", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"description": "Require the first found package from an array.", | ||
"main": "require-one.js", | ||
"scripts": { | ||
"test": "node test" | ||
"test": "standard require-one.js test/index.js; node test" | ||
}, | ||
@@ -23,4 +23,5 @@ "repository": { | ||
"devDependencies": { | ||
"standard": "^4.3.1", | ||
"testit": "^2.0.2" | ||
} | ||
} |
@@ -1,7 +0,6 @@ | ||
# Require One | ||
# Require One [![NPM version](https://img.shields.io/npm/v/require-one.svg)](https://npmjs.org/package/require-one "View this project on NPM") | ||
[![Build Status](https://img.shields.io/travis/RobLoach/require-one/master.svg)](http://travis-ci.org/RobLoach/jquery-once "Check this project's build status on TravisCI") | ||
[![NPM version](https://img.shields.io/npm/v/require-one.svg)](https://npmjs.org/package/require-one "View this project on NPM") | ||
[![Build Status](https://img.shields.io/travis/RobLoach/require-one/master.svg)](http://travis-ci.org/RobLoach/require-one "Check this project's build status on TravisCI") | ||
[![NPM downloads](https://img.shields.io/npm/dm/require-one.svg)](https://npmjs.org/package/require-one "View this project on NPM") | ||
[![Dependency Status](https://img.shields.io/david/RobLoach/jquery-once.svg)](https://david-dm.org/RobLoach/jquery-once) | ||
[![Dependency Status](https://img.shields.io/david/RobLoach/require-one.svg)](https://david-dm.org/RobLoach/require-one) | ||
@@ -27,3 +26,3 @@ Load the first package found from the given array. | ||
``` javascript | ||
var $ = requireOne(['jquery', 'cheerio']); | ||
var $ = requireOne('jquery', 'zepto', 'cheerio'); | ||
// => jQuery or Cheerio, depending on which one is loaded first. | ||
@@ -38,3 +37,3 @@ ``` | ||
// Retrieve the first package that is available. | ||
var $ = requireOne(["jquery", "cheerio"]); | ||
var $ = requireOne('jquery', 'cheerio'); | ||
// => jQuery or Cheerio, depending on which one is loaded first. | ||
@@ -55,3 +54,3 @@ | ||
<script> | ||
var con = requireOne(['nope', 'nopers', 'nopes', 'console']); | ||
var con = requireOne('nope', 'nopers', 'nopes', 'console'); | ||
con.log('Hello World! Found the console?'); | ||
@@ -58,0 +57,0 @@ </script> |
@@ -13,39 +13,47 @@ /*! | ||
(function (root, factory) { | ||
/* global define */ | ||
if (typeof define === 'function' && define.amd) { | ||
// AMD | ||
define(function(require) { | ||
define(function (require) { | ||
// Use AMD's require() wrapper. | ||
return (root.requireOne = factory(require)); | ||
}); | ||
root.requireOne = factory(require) | ||
return root.requireOne | ||
}) | ||
} else if (typeof exports === 'object') { | ||
// CommonJS | ||
module.exports = factory(require); | ||
module.exports = factory(require) | ||
} else { | ||
// Browser globals | ||
root.requireOne = factory(function (packageName) { | ||
// Build our own require function, checking the global scope. | ||
/** | ||
* Our own require function for the global scope. | ||
*/ | ||
function globalRequire (packageName) { | ||
if (root[packageName]) { | ||
return root[packageName]; | ||
return root[packageName] | ||
} | ||
else { | ||
throw new Error("Package " + packageName + "not found"); | ||
} | ||
}); | ||
throw new Error('Package ' + packageName + 'not found') | ||
} | ||
root.requireOne = factory(globalRequire) | ||
} | ||
}(this, function (requireFunction) { | ||
/** | ||
* Iterate through each package, and return the first loadable one. | ||
* Iterate through each package, returning the first loadable one. | ||
* | ||
* @arg {(...string|string[])} packages - An array of strings representing | ||
* which packages to load, or an argument list of strings to load. | ||
* | ||
* @returns The first loaded package from the packages parameter. | ||
*/ | ||
return function(requires) { | ||
for (var i in requires) { | ||
var name = requires[i]; | ||
return function requireFromArray (packages) { | ||
// Retrieve the list of package names. | ||
var packagesNames = Array.isArray(packages) ? packages : arguments | ||
for (var i in packagesNames) { | ||
try { | ||
return requireFunction(name); | ||
return requireFunction(packagesNames[i]) | ||
} catch (e) { | ||
// Do nothing, but continue on to the next package. | ||
continue | ||
} | ||
catch (e) { | ||
// Do nothing. | ||
} | ||
} | ||
throw new Error('Could not found one of the expected packages: ' + JSON.stringify(requires)); | ||
}; | ||
})); | ||
throw new Error('Could not found one of the expected packages: ' + JSON.stringify(packages)) | ||
} | ||
})) |
@@ -1,14 +0,19 @@ | ||
var assert = require('assert'); | ||
var test = require('testit'); | ||
var requireOne = require('..'); | ||
var assert = require('assert') | ||
var test = require('testit') | ||
var requireOne = require('..') | ||
test('Load from an array', function () { | ||
var testFramework = requireOne(['tape', 'testit']); | ||
assert.deepEqual(testFramework, test); | ||
}); | ||
var testFramework = requireOne(['tape', 'testit']) | ||
assert.deepEqual(testFramework, test) | ||
}) | ||
test('Throws an error when not found', function() { | ||
assert.throws(function() { | ||
requireOne(['not-found', '404']); | ||
}); | ||
}); | ||
test('Load from string arguments', function () { | ||
var testFramework = requireOne('tape', 'testit') | ||
assert.deepEqual(testFramework, test) | ||
}) | ||
test('Throws an error when not found', function () { | ||
assert.throws(function () { | ||
requireOne(['not-found', '404']) | ||
}) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
6529
13
112
2
60