loader-cache
Advanced tools
Comparing version 0.2.4 to 0.2.5
122
index.js
@@ -153,5 +153,6 @@ /*! | ||
* ```js | ||
* var Promise = require('bluebird'); | ||
* | ||
* // register an promise loader for parsing YAML | ||
* loaders.registerPromise('yaml', function(fp) { | ||
* var Promise = require('bluebird'); | ||
* var deferred = Promise.pending(); | ||
@@ -225,36 +226,2 @@ * process.nextTick(function () { | ||
/** | ||
* Create a loader stack of the given `type` from an | ||
* array of `loaders`. | ||
* | ||
* @param {Array} `loaders` Names of stored loaders to add to the stack. | ||
* @param {String} `type=sync` | ||
* @return {Array} Array of loaders | ||
* @api public | ||
*/ | ||
Loaders.prototype.createStack = function(loaders, type) { | ||
var cache = this.cache[type || 'sync']; | ||
return (loaders || []).reduce(function(stack, name) { | ||
return stack.concat(cache[name]); | ||
}.bind(this), []).filter(Boolean); | ||
}; | ||
/** | ||
* Private method for loading a stack of loaders that is | ||
* a combination of both stored loaders and locally defined | ||
* loaders. | ||
* | ||
* @param {Array} `loaders` Names of stored loaders to add to the stack. | ||
* @param {String} `type=sync` | ||
* @return {Array} Array of loaders | ||
* @api private | ||
*/ | ||
Loaders.prototype.loadStack = function(fp, opts, stack, type) { | ||
var loader = matchLoader(fp, opts, this); | ||
stack = [loader].concat(stack || []); | ||
return this.createStack(stack, type); | ||
}; | ||
/** | ||
* Create a loader from other (previously cached) loaders. For | ||
@@ -294,3 +261,3 @@ * example, you might create a loader like the following: | ||
var cache = this.cache[type] || {}; | ||
var stack = this.createStack(loaders, type); | ||
var stack = this.createTypeStack(loaders, type); | ||
@@ -302,4 +269,37 @@ cache[ext] = cache[ext] || []; | ||
/** | ||
* Create a loader stack of the given `type` from an | ||
* array of `loaders`. | ||
* | ||
* @param {Array} `loaders` Names of stored loaders to add to the stack. | ||
* @param {String} `type=sync` | ||
* @return {Array} Array of loaders | ||
* @api public | ||
*/ | ||
Loaders.prototype.createTypeStack = function(loaders, type) { | ||
var cache = this.cache[type || 'sync']; | ||
return (loaders || []).reduce(function(stack, name) { | ||
return stack.concat(cache[name]); | ||
}.bind(this), []).filter(Boolean); | ||
}; | ||
/** | ||
* Private method for loading a stack of loaders that is | ||
* a combination of both stored loaders and locally defined | ||
* loaders. | ||
* | ||
* @param {Array} `loaders` Names of stored loaders to add to the stack. | ||
* @param {String} `type=sync` | ||
* @return {Array} Array of loaders | ||
* @api private | ||
*/ | ||
Loaders.prototype.loadStack = function(fp, stack, opts, type) { | ||
var loader = matchLoader(fp, opts, this); | ||
stack = [loader].concat(stack || []); | ||
return this.createTypeStack(stack, type); | ||
}; | ||
/** | ||
* Run loaders associated with `ext` of the given filepath. | ||
@@ -320,9 +320,9 @@ * | ||
Loaders.prototype.load = function(fp, options, stack) { | ||
if (Array.isArray(options)) { | ||
stack = options; | ||
options = {}; | ||
Loaders.prototype.load = function(fp, stack, options) { | ||
if (!Array.isArray(stack)) { | ||
options = stack; | ||
stack = []; | ||
} | ||
var fns = this.loadStack(fp, options, stack); | ||
var fns = this.loadStack(fp, stack, options); | ||
if (!fns || fns.length === 0) { | ||
@@ -351,3 +351,3 @@ return fp; | ||
* @param {Object} `options` Options to pass to whatever loaders are defined. | ||
* @param {Function} `done` Callback to indicate loading has finished | ||
* @param {Function} `cb` Callback to indicate loading has finished | ||
* @return {String} | ||
@@ -357,6 +357,5 @@ * @api public | ||
Loaders.prototype.loadAsync = function(fp, options, stack, done) { | ||
if (Array.isArray(options)) { | ||
done = stack; | ||
stack = options; | ||
Loaders.prototype.loadAsync = function(fp, stack, options, cb) { | ||
if (typeof options === 'function') { | ||
cb = options; | ||
options = {}; | ||
@@ -366,8 +365,3 @@ } | ||
if (typeof stack === 'function') { | ||
done = stack; | ||
stack = []; | ||
} | ||
if (typeof options === 'function') { | ||
done = options; | ||
cb = stack; | ||
options = {}; | ||
@@ -377,3 +371,3 @@ stack = []; | ||
var fns = this.loadStack(fp, options, stack, 'async'); | ||
var fns = this.loadStack(fp, stack, options, 'async'); | ||
if (!fns || fns.length === 0) { | ||
@@ -386,3 +380,3 @@ return fp; | ||
fn(acc, options, next); | ||
}, done); | ||
}, cb); | ||
}; | ||
@@ -409,6 +403,6 @@ | ||
Loaders.prototype.loadPromise = function(fp, options, stack) { | ||
if (Array.isArray(options)) { | ||
stack = options; | ||
options = {}; | ||
Loaders.prototype.loadPromise = function(fp, stack, options) { | ||
if (!Array.isArray(stack)) { | ||
options = stack; | ||
stack = []; | ||
} | ||
@@ -418,3 +412,3 @@ | ||
var fns = this.loadStack(fp, options, stack, 'promise'); | ||
var fns = this.loadStack(fp, stack, options, 'promise'); | ||
var Promise = require('bluebird'); | ||
@@ -449,6 +443,6 @@ var current = Promise.resolve(); | ||
Loaders.prototype.loadStream = function(fp, options, stack) { | ||
if (Array.isArray(options)) { | ||
stack = options; | ||
options = {}; | ||
Loaders.prototype.loadStream = function(fp, stack, options) { | ||
if (!Array.isArray(stack)) { | ||
options = stack; | ||
stack = []; | ||
} | ||
@@ -459,3 +453,3 @@ | ||
var fns = this.loadStack(fp, options, stack, 'stream'); | ||
var fns = this.loadStack(fp, stack, options, 'stream'); | ||
if (!fns || fns.length === 0) { | ||
@@ -462,0 +456,0 @@ var noop = es.through(function (fp) { |
{ | ||
"name": "loader-cache", | ||
"description": "Register loader functions that dynamically read, parse or otherwise transform file contents when the name of the loader matches a file extension. You can also compose loaders from other loaders.", | ||
"version": "0.2.4", | ||
"version": "0.2.5", | ||
"homepage": "https://github.com/jonschlinkert/loader-cache", | ||
@@ -6,0 +6,0 @@ "author": { |
@@ -114,3 +114,3 @@ # loader-cache [![NPM version](https://badge.fury.io/js/loader-cache.svg)](http://badge.fury.io/js/loader-cache) | ||
### [.registerPromise](index.js#L183) | ||
### [.registerPromise](index.js#L184) | ||
@@ -126,5 +126,6 @@ Register the given promise loader callback `fn` as `ext`. Any arbitrary name can be assigned to a loader, however, the loader will only be called when either: a. `ext` matches the file extension of a path passed to the `.load()` method, or b. `ext` is an arbitrary name passed on the loader stack of another loader. Example below. | ||
```js | ||
var Promise = require('bluebird'); | ||
// register an promise loader for parsing YAML | ||
loaders.registerPromise('yaml', function(fp) { | ||
var Promise = require('bluebird'); | ||
var deferred = Promise.pending(); | ||
@@ -151,3 +152,3 @@ process.nextTick(function () { | ||
### [.registerStream](index.js#L219) | ||
### [.registerStream](index.js#L220) | ||
@@ -179,3 +180,3 @@ Register the given stream loader callback `fn` as `ext`. Any arbitrary name can be assigned to a loader, however, the loader will only be called when either: a. `ext` matches the file extension of a path passed to the `.load()` method, or b. `ext` is an arbitrary name passed on the loader stack of another loader. Example below. | ||
### [.createStack](index.js#L233) | ||
### [.createTypeStack](index.js#L276) | ||
@@ -210,3 +211,3 @@ * `loaders` **{Array}**: Names of stored loaders to add to the stack. | ||
* `options` **{Object}**: Options to pass to whatever loaders are defined. | ||
* `done` **{Function}**: Callback to indicate loading has finished | ||
* `cb` **{Function}**: Callback to indicate loading has finished | ||
* `returns`: {String} | ||
@@ -223,3 +224,3 @@ | ||
### [.loadPromise](index.js#L399) | ||
### [.loadPromise](index.js#L393) | ||
@@ -242,3 +243,3 @@ Run promise loaders associated with `ext` of the given filepath. | ||
### [.loadStream](index.js#L437) | ||
### [.loadStream](index.js#L431) | ||
@@ -245,0 +246,0 @@ Run stream loaders associated with `ext` of the given filepath. |
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
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
273
37682
739