fabricator
Advanced tools
Comparing version 0.2.1 to 0.3.0
52
index.js
@@ -11,12 +11,24 @@ 'use strict'; | ||
* @param {Mixed} stack String, array or object that contains constructible entities | ||
* @param {Function} done Completion callback. | ||
* @param {String} source Optional absolute path to be used to resolve filepaths | ||
* @param {Function} done Optional completion callback. | ||
* @return {Array} collection of constructors, if called synchronously. | ||
* @api public | ||
*/ | ||
module.exports = function fabricator(stack, done) { | ||
module.exports = function fabricator(stack, source, done) { | ||
var type = typeof stack; | ||
if ('object' === type && Array.isArray(stack)) type = 'array'; | ||
if ('function' !== typeof done) return fabricateSync(type, stack); | ||
fabricate(type, stack, done); | ||
// | ||
// No source was provided, check if the call was asynchronous. | ||
// | ||
if ('function' === typeof source) { | ||
done = source; | ||
source = null; | ||
} | ||
// | ||
// Call the fabricate function (a)synchronously. | ||
// | ||
if ('function' !== typeof done) return fabricateSync(type, stack, source); | ||
fabricate(type, stack, source, done); | ||
}; | ||
@@ -29,17 +41,18 @@ | ||
* @param {Mixed} stack | ||
* @param {String} source Optional absolute path to be used to resolve filepaths | ||
* @return {Array} filtered collection of constructible entities. | ||
* @api private | ||
*/ | ||
function fabricateSync(type, stack) { | ||
function fabricateSync(type, stack, source) { | ||
switch (type) { | ||
case 'string': | ||
stack = readSync(stack); | ||
stack = readSync(stack, source); | ||
break; | ||
case 'object': | ||
stack = Object.keys(stack).reduce(iterator(readSync, stack), []); | ||
stack = Object.keys(stack).reduce(iterator(readSync, stack, source), []); | ||
break; | ||
case 'array': | ||
stack = stack.reduce(iterator(readSync), []); | ||
stack = stack.reduce(iterator(readSync, source), []); | ||
break; | ||
@@ -56,6 +69,7 @@ } | ||
* @param {Mixed} stack | ||
* @param {String} source Optional absolute path to be used to resolve filepaths | ||
* @param {Function} done Completion callback. | ||
* @api private | ||
*/ | ||
function fabricate(type, stack, done) { | ||
function fabricate(type, stack, source, done) { | ||
var result = []; | ||
@@ -65,3 +79,3 @@ | ||
case 'string': | ||
read(stack, done); | ||
read(stack, source, done); | ||
break; | ||
@@ -73,2 +87,3 @@ | ||
stack, | ||
source, | ||
recur(Object.keys(stack).length, result, done) | ||
@@ -82,2 +97,3 @@ ), result); | ||
null, | ||
source, | ||
recur(stack.length, result, done) | ||
@@ -96,3 +112,5 @@ ), result); | ||
*/ | ||
function readSync(filepath) { | ||
function readSync(filepath, source) { | ||
if (source) filepath = path.resolve(source, filepath); | ||
// | ||
@@ -123,8 +141,11 @@ // Check if the provided string is a JS file. | ||
* @param {String} filepath Full directory path. | ||
* @param {String} source Optional absolute path to be used to resolve filepaths | ||
* @param {done} | ||
* @api privat | ||
*/ | ||
function read(filepath, done) { | ||
function read(filepath, source, done) { | ||
var iterate; | ||
if (source) filepath = path.resolve(source, filepath); | ||
// | ||
@@ -161,2 +182,3 @@ // Check if the provided string is a JS file. | ||
* @param {Object} obj Original object, if set values are fetched by entity. | ||
* @param {String} source Optional absolute path to be used to resolve filepaths | ||
* @param {Function} done Optional completion callback. | ||
@@ -166,3 +188,3 @@ * @return {Function} iterator | ||
*/ | ||
function iterator(traverse, obj, done) { | ||
function iterator(traverse, obj, source, done) { | ||
return function reduce(stack, entity) { | ||
@@ -176,3 +198,3 @@ var base = obj ? obj[entity] : entity | ||
if ('function' === typeof done) { | ||
if (nojs) return traverse(base, done); | ||
if (nojs) return traverse(base, source, done); | ||
return done(null, init(base, entity)); | ||
@@ -184,3 +206,3 @@ } | ||
// | ||
if (nojs) return stack.concat(traverse(base)); | ||
if (nojs) return stack.concat(traverse(base, source)); | ||
return stack.concat(init(base, entity)); | ||
@@ -187,0 +209,0 @@ }; |
{ | ||
"name": "fabricator", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"description": "Discover collections of constructible instances from strings (filepaths), arrays or objects", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -12,2 +12,7 @@ // | ||
// | ||
// Relative file path with can be resolved. | ||
// | ||
exports.relative = 'sub'; | ||
// | ||
// Mix of types on array. | ||
@@ -14,0 +19,0 @@ // |
@@ -57,2 +57,18 @@ describe('Fabricator', function () { | ||
it('can be provided with a absolute source path to resolve filepaths', function (done) { | ||
var path = __dirname + '/fixtures' | ||
, result = fabricator(fixtures.relative, path); | ||
assume(result).to.be.an('array'); | ||
assume(result.length).to.equal(1); | ||
fabricator(fixtures.relative, path, function (error, result) { | ||
assume(error).to.equal(null); | ||
assume(result).to.be.an('array'); | ||
assume(result.length).to.equal(1); | ||
done(); | ||
}); | ||
}); | ||
it('will discover constructors from objects', function (done) { | ||
@@ -59,0 +75,0 @@ var result = fabricator(fixtures.object); |
15448
342