fabricator
Advanced tools
Comparing version 0.4.0 to 0.4.1
101
index.js
@@ -9,9 +9,9 @@ 'use strict'; | ||
* | ||
* @param {Mixed} stack String, array or object that contains constructible entities | ||
* @param {Object} options | ||
* Possible options: | ||
* | ||
* Possible options | ||
* - source: {String} Absolute path to be used to resolve filepaths | ||
* - recursive: {Boolean} Should file paths be recursively discovered | ||
* - source: {String} Absolute path to be used to resolve file paths. | ||
* - recursive: {Boolean} Should file paths be recursively discovered. | ||
* | ||
* @param {Mixed} stack String, array or object that contains constructible entities | ||
* @param {Object} options Optional options. | ||
* @returns {Array} collection of constructors. | ||
@@ -21,19 +21,5 @@ * @api public | ||
module.exports = function fabricator(stack, options) { | ||
var type = typeof stack; | ||
if ('object' === type && Array.isArray(stack)) type = 'array'; | ||
options = options || {}; | ||
return fabricate(type, stack, options || {}); | ||
}; | ||
/** | ||
* Discover constructible entities. | ||
* | ||
* @param {String} type Typeof stack. | ||
* @param {Mixed} stack | ||
* @param {Object} options | ||
* @return {Array} filtered collection of constructible entities. | ||
* @api private | ||
*/ | ||
function fabricate(type, stack, options) { | ||
switch (type) { | ||
switch (is(stack)) { | ||
case 'string': | ||
@@ -50,13 +36,16 @@ stack = read(stack, options); | ||
break; | ||
default: | ||
throw new Error('Unsupported type, cannot fabricate an: '+ is(stack)); | ||
} | ||
return stack.filter(Boolean); | ||
} | ||
return (stack || []).filter(Boolean); | ||
}; | ||
/** | ||
* Read directory and initialize javascript files. | ||
* Read directory and initialize JavaScript files. | ||
* | ||
* @param {String} filepath Full directory path. | ||
* @param {Object} options | ||
* @return {Array} collection of constructors | ||
* @param {Object} options Additional configuration. | ||
* @return {Array} collection of constructor | ||
* @api private | ||
@@ -68,5 +57,5 @@ */ | ||
// | ||
// Check if the provided string is a JS file. | ||
// Check if the provided string is a JS file or when recursion is not allowed. | ||
// | ||
if (js(filepath)) return [ | ||
if (js(filepath) || options.recursive === false) return [ | ||
init(filepath, path.basename(filepath, '.js')) | ||
@@ -76,9 +65,2 @@ ]; | ||
// | ||
// Recursion on directories not allowed, initialize the index.js file. | ||
// | ||
if (options.recursive === false) return [ | ||
init(filepath, path.basename(filepath, '.js')) | ||
]; | ||
// | ||
// Read the directory, only process files. | ||
@@ -89,6 +71,16 @@ // | ||
var stat = fs.statSync(file); | ||
if (stat.isDirectory() && fs.existsSync(path.join(file, 'index.js'))) { | ||
// | ||
// Use the directory name instead of `index` for name as it probably has | ||
// more meaning then just `index` as a name. | ||
// | ||
return init(path.join(file, 'index.js'), path.basename(file, '.js')); | ||
} | ||
// | ||
// Only allow JS files, init determines if it is a constructible instance. | ||
// | ||
if (!fs.statSync(file).isFile() || !js(file)) return; | ||
if (!stat.isFile() || !js(file)) return; | ||
return init(file, path.basename(file, '.js')); | ||
@@ -109,4 +101,3 @@ }); | ||
return function reduce(stack, entity) { | ||
var base = obj ? obj[entity] : entity | ||
, nojs = !js(base); | ||
var base = obj ? obj[entity] : entity; | ||
@@ -116,4 +107,4 @@ // | ||
// | ||
if (nojs) return stack.concat(traverse(base, options)); | ||
return stack.concat(init(base, entity)); | ||
if (js(base)) return stack.concat(init(base, entity)); | ||
return stack.concat(traverse(base, options)); | ||
}; | ||
@@ -131,9 +122,22 @@ } | ||
function js(file) { | ||
var extname = path.extname(file) | ||
, type = typeof file; | ||
var type = is(file); | ||
return 'function' === type || 'string' === type && extname === '.js'; | ||
return 'function' === type | ||
|| 'string' === type && path.extname(file) === '.js'; | ||
} | ||
/** | ||
* A better alternative to `typeof` checks by trying to figure out the root | ||
* class of things. This eliminates the needs for Array.is checks when the type | ||
* is an object etc. | ||
* | ||
* @param {Mixed} obj Unknown thing we need to know. | ||
* @returns {String} | ||
* @api private | ||
*/ | ||
function is(obj) { | ||
return Object.prototype.toString.call(obj).slice(8, -1).toLowerCase(); | ||
} | ||
/** | ||
* It's not required to supply resolve with instances, we can just | ||
@@ -150,6 +154,11 @@ * automatically require them if they are using the: | ||
function init(constructor, name) { | ||
constructor = ('string' === typeof constructor) ? require(constructor) : constructor; | ||
if (!constructor.prototype) return false; | ||
constructor = ('string' === is(constructor)) ? require(constructor) : constructor; | ||
// | ||
// We really want to have a function/class here. Make sure that we can | ||
// construct it using `new constructor` | ||
// | ||
if (!constructor.prototype) return; | ||
// | ||
// Sets the lowercase name on the prototype if required. | ||
@@ -164,2 +173,2 @@ // | ||
return constructor; | ||
} | ||
} |
{ | ||
"name": "fabricator", | ||
"version": "0.4.0", | ||
"version": "0.4.1", | ||
"description": "Discover collections of constructible instances from strings (filepaths), arrays or objects", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "NODE_ENV=test ./node_modules/.bin/mocha $(find test -name '*.test.js')", | ||
"test": "illuminati", | ||
"coverage": "NODE_ENV=test ./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha $(find test -name '*.test.js') --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js || true && rm -rf ./coverage" | ||
@@ -28,3 +28,2 @@ }, | ||
"devDependencies": { | ||
"assume": "0.0.x", | ||
"coveralls": "2.10.x", | ||
@@ -34,4 +33,4 @@ "istanbul": "0.2.x", | ||
"mocha-lcov-reporter": "0.0.x", | ||
"pre-commit": "0.0.x" | ||
"illuminati": "0.0.x" | ||
} | ||
} |
@@ -12,2 +12,7 @@ // | ||
// | ||
// Constructors in a nested/sub directory. | ||
// | ||
exports.nested = __dirname + '/nested'; | ||
// | ||
// Relative file path with can be resolved. | ||
@@ -32,2 +37,2 @@ // | ||
latest: exports.string | ||
}; | ||
}; |
@@ -33,2 +33,7 @@ describe('Fabricator', function () { | ||
it('throws an error when we receive an invalid type', function (next) { | ||
try { fabricator(new Date()); } | ||
catch (e) { next(); } | ||
}); | ||
it('can be provided with a absolute source path to resolve filepaths', function () { | ||
@@ -50,2 +55,10 @@ var path = __dirname + '/fixtures' | ||
it('can read out sub directories', function () { | ||
var path = __dirname + '/fixtures' | ||
, result = fabricator(fixtures.nested); | ||
assume(result).to.be.an('array'); | ||
assume(result.length).to.equal(2); | ||
}); | ||
it('will discover constructors from objects', function () { | ||
@@ -74,2 +87,2 @@ var result = fabricator(fixtures.object); | ||
}); | ||
}); | ||
}); |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
12150
5
15
246
1