loader-cache
Advanced tools
Comparing version 0.1.2 to 0.2.0
97
index.js
@@ -206,2 +206,37 @@ /*! | ||
/** | ||
* 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 | ||
@@ -240,12 +275,11 @@ * example, you might create a loader like the following: | ||
loaders.reduce(function(stack, loader) { | ||
stack[type] = stack[type] || {}; | ||
stack[type][ext] = stack[type][ext] || []; | ||
stack[type][ext] = stack[type][ext].concat(this.cache[type][loader]); | ||
return stack; | ||
}.bind(this), this.cache); | ||
var cache = this.cache[type] || {}; | ||
var stack = this.createStack(loaders, type); | ||
cache[ext] = cache[ext] || []; | ||
cache[ext] = cache[ext].concat(stack); | ||
return this; | ||
}; | ||
/** | ||
@@ -267,6 +301,13 @@ * Run loaders associated with `ext` of the given filepath. | ||
Loaders.prototype.load = function(fp, options) { | ||
var fns = this.cache.sync[matchLoader(fp, options, this)]; | ||
if (!fns) return fp; | ||
Loaders.prototype.load = function(fp, options, stack) { | ||
if (Array.isArray(options)) { | ||
stack = options; | ||
options = {}; | ||
} | ||
var fns = this.loadStack(fp, options, stack); | ||
if (!fns) { | ||
return fp; | ||
} | ||
return fns.reduce(function (acc, fn) { | ||
@@ -296,4 +337,9 @@ return fn(acc, options); | ||
Loaders.prototype.loadAsync = function(fp, options, done) { | ||
var async = require('async'); | ||
Loaders.prototype.loadAsync = function(fp, options, stack, done) { | ||
if (Array.isArray(options)) { | ||
done = stack; | ||
stack = options; | ||
options = {}; | ||
} | ||
if (typeof options === 'function') { | ||
@@ -304,5 +350,8 @@ done = options; | ||
var fns = this.cache.async[matchLoader(fp, options, this)]; | ||
if (!fns) return fp; | ||
var fns = this.loadStack(fp, options, stack, 'async'); | ||
if (!fns) { | ||
return fp; | ||
} | ||
var async = require('async'); | ||
async.reduce(fns, fp, function (acc, fn, next) { | ||
@@ -332,10 +381,15 @@ fn(acc, options, next); | ||
Loaders.prototype.loadPromise = function(fp, options) { | ||
Loaders.prototype.loadPromise = function(fp, options, stack) { | ||
if (Array.isArray(options)) { | ||
stack = options; | ||
options = {}; | ||
} | ||
options = options || {}; | ||
var fns = this.loadStack(fp, options, stack, 'promise'); | ||
var Promise = require('bluebird'); | ||
var current = Promise.resolve(); | ||
options = options || {}; | ||
var fns = this.cache.promise[matchLoader(fp, options, this)]; | ||
if (!fns) return current.then(function () { return fp; }); | ||
return Promise.reduce(fns, function (acc, fn) { | ||
@@ -366,7 +420,12 @@ return fn(acc, options); | ||
Loaders.prototype.loadStream = function(fp, options) { | ||
Loaders.prototype.loadStream = function(fp, options, stack) { | ||
if (Array.isArray(options)) { | ||
stack = options; | ||
options = {}; | ||
} | ||
var es = require('event-stream'); | ||
options = options || {}; | ||
var fns = this.cache.stream[matchLoader(fp, options, this)]; | ||
var fns = this.loadStack(fp, options, stack, 'stream'); | ||
if (!fns) { | ||
@@ -373,0 +432,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.1.2", | ||
"version": "0.2.0", | ||
"homepage": "https://github.com/jonschlinkert/loader-cache", | ||
@@ -6,0 +6,0 @@ "author": { |
@@ -167,4 +167,13 @@ # loader-cache [![NPM version](https://badge.fury.io/js/loader-cache.svg)](http://badge.fury.io/js/loader-cache) | ||
### [.load](index.js#L265) | ||
### [.createStack](index.js#L215) | ||
* `loaders` **{Array}**: Names of stored loaders to add to the stack. | ||
* `type=sync` **{String}** | ||
* `returns` **{Array}**: Array of loaders | ||
Create a loader stack of the given `type` from an | ||
array of `loaders`. | ||
### [.load](index.js#L299) | ||
Run loaders associated with `ext` of the given filepath. | ||
@@ -183,3 +192,3 @@ | ||
### [.loadAsync](index.js#L294) | ||
### [.loadAsync](index.js#L334) | ||
@@ -202,3 +211,3 @@ Run async loaders associated with `ext` of the given filepath. | ||
### [.loadPromise](index.js#L328) | ||
### [.loadPromise](index.js#L376) | ||
@@ -221,3 +230,3 @@ Run promise loaders associated with `ext` of the given filepath. | ||
### [.loadStream](index.js#L362) | ||
### [.loadStream](index.js#L414) | ||
@@ -258,2 +267,2 @@ Run stream loaders associated with `ext` of the given filepath. | ||
_This file was generated by [verb](https://github.com/assemble/verb) on November 27, 2014._ | ||
_This file was generated by [verb](https://github.com/assemble/verb) on December 02, 2014._ |
297
test.js
@@ -10,9 +10,9 @@ /*! | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var should = require('should'); | ||
var Promise = require('bluebird'); | ||
var es = require('event-stream'); | ||
var should = require('should'); | ||
var YAML = require('js-yaml'); | ||
var Loaders = require('./'); | ||
var fs = require('fs'); | ||
@@ -51,46 +51,58 @@ var loaders; | ||
it('should register loaders:', function () { | ||
loaders.cache.sync.should.have.properties('yaml', 'yml', 'json', 'read', 'hbs', 'data'); | ||
}); | ||
describe('register', function () { | ||
it('should compose a loader from other loaders with the `.compose()` method:', function () { | ||
loaders.compose('foo', ['read', 'yaml']); | ||
loaders.cache.sync.should.have.property('foo'); | ||
}); | ||
it('should register loaders:', function () { | ||
loaders.cache.sync.should.have.properties('yaml', 'yml', 'json', 'read', 'hbs', 'data'); | ||
}); | ||
it('should compose a loader from other loaders with the `.register()` method:', function () { | ||
loaders.register('foo', ['read', 'yaml']); | ||
loaders.cache.sync.should.have.property('foo'); | ||
}); | ||
it('should compose a loader from other loaders with the `.compose()` method:', function () { | ||
loaders.compose('foo', ['read', 'yaml']); | ||
loaders.cache.sync.should.have.property('foo'); | ||
}); | ||
it('should pass the value returned from a loader to the next loader:', function () { | ||
loaders.register('bar', ['read', 'yaml', 'data']); | ||
loaders.load('fixtures/a.bar').should.eql({c: 'd', e: 'f'}); | ||
}); | ||
it('should compose a loader from other loaders with the `.register()` method:', function () { | ||
loaders.register('foo', ['read', 'yaml']); | ||
loaders.cache.sync.should.have.property('foo'); | ||
}); | ||
it('should pass the value returned from a loader to the next loader:', function () { | ||
loaders.compose('bar', ['read', 'yaml', 'data']); | ||
loaders.load('fixtures/a.bar').should.eql({c: 'd', e: 'f'}); | ||
}); | ||
it('should compose a loader from other loaders:', function () { | ||
loaders.compose('parse', ['read', 'yaml']); | ||
loaders.compose('extend', ['data']); | ||
loaders.compose('bar', ['parse', 'extend']); | ||
loaders.load('fixtures/a.bar').should.eql({c: 'd', e: 'f'}); | ||
}); | ||
describe('load', function () { | ||
it('should pass the value returned from a loader to the next loader:', function () { | ||
loaders.register('bar', ['read', 'yaml', 'data']); | ||
loaders.load('fixtures/a.bar').should.eql({c: 'd', e: 'f'}); | ||
}); | ||
it('should use a custom function for matching loaders:', function () { | ||
loaders.compose('parse', ['read', 'yaml']); | ||
loaders.compose('extend', ['data']); | ||
loaders.compose('bar', ['parse', 'extend']); | ||
loaders.load('fixtures/a.bar', { | ||
matchLoader: function(pattern) { | ||
return path.extname(pattern).slice(1); | ||
} | ||
}).should.eql({c: 'd', e: 'f'}); | ||
it('should pass the value returned from a loader to the next loader:', function () { | ||
loaders.compose('bar', ['read', 'yaml', 'data']); | ||
loaders.load('fixtures/a.bar').should.eql({c: 'd', e: 'f'}); | ||
}); | ||
it('should compose a loader from other loaders:', function () { | ||
loaders.compose('parse', ['read', 'yaml']); | ||
loaders.compose('extend', ['data']); | ||
loaders.compose('bar', ['parse', 'extend']); | ||
loaders.load('fixtures/a.bar').should.eql({c: 'd', e: 'f'}); | ||
}); | ||
it('should extend a loader stack with loaders defined on the load method:', function () { | ||
loaders.register('bar', ['read', 'yaml']); | ||
loaders.load('fixtures/a.bar', ['data']).should.eql({c: 'd', e: 'f'}); | ||
}); | ||
it('should use a custom function for matching loaders:', function () { | ||
loaders.compose('parse', ['read', 'yaml']); | ||
loaders.compose('extend', ['data']); | ||
loaders.compose('bar', ['parse', 'extend']); | ||
loaders.load('fixtures/a.bar', { | ||
matchLoader: function(pattern) { | ||
return path.extname(pattern).slice(1); | ||
} | ||
}).should.eql({c: 'd', e: 'f'}); | ||
}); | ||
}); | ||
}); | ||
describe('loaders async', function () { | ||
describe('loaders (async)', function () { | ||
beforeEach(function() { | ||
@@ -125,44 +137,56 @@ loaders = new Loaders(); | ||
it('should register async loaders:', function () { | ||
loaders.cache.async.should.have.properties('yaml', 'yml', 'json', 'read', 'hbs', 'data'); | ||
}); | ||
describe('register', function () { | ||
it('should register async loaders:', function () { | ||
loaders.cache.async.should.have.properties('yaml', 'yml', 'json', 'read', 'hbs', 'data'); | ||
}); | ||
it('should compose an async loader from other async loaders with the `.compose()` method:', function () { | ||
loaders.compose('foo', ['read', 'yaml'], 'async'); | ||
loaders.cache.async.should.have.property('foo'); | ||
}); | ||
it('should compose an async loader from other async loaders with the `.compose()` method:', function () { | ||
loaders.compose('foo', ['read', 'yaml'], 'async'); | ||
loaders.cache.async.should.have.property('foo'); | ||
}); | ||
it('should compose an async loader from other async loaders with the `.register()` method:', function () { | ||
loaders.registerAsync('foo', ['read', 'yaml']); | ||
loaders.cache.async.should.have.property('foo'); | ||
it('should compose an async loader from other async loaders with the `.register()` method:', function () { | ||
loaders.registerAsync('foo', ['read', 'yaml']); | ||
loaders.cache.async.should.have.property('foo'); | ||
}); | ||
}); | ||
it('should pass the value returned from an async loader to the next async loader:', function (done) { | ||
loaders.registerAsync('bar', ['read', 'yaml', 'data']); | ||
loaders.loadAsync('fixtures/a.bar', function (err, obj) { | ||
obj.should.eql({c: 'd', e: 'f'}); | ||
done(); | ||
describe('load', function () { | ||
it('should pass the value returned from an async loader to the next async loader:', function (done) { | ||
loaders.registerAsync('bar', ['read', 'yaml', 'data']); | ||
loaders.loadAsync('fixtures/a.bar', function (err, obj) { | ||
obj.should.eql({c: 'd', e: 'f'}); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('should pass the value returned from an async loader to the next async loader:', function (done) { | ||
loaders.compose('bar', ['read', 'yaml', 'data'], 'async'); | ||
loaders.loadAsync('fixtures/a.bar', function (err, obj) { | ||
obj.should.eql({c: 'd', e: 'f'}); | ||
done(); | ||
it('should pass the value returned from an async loader to the next async loader:', function (done) { | ||
loaders.compose('bar', ['read', 'yaml', 'data'], 'async'); | ||
loaders.loadAsync('fixtures/a.bar', function (err, obj) { | ||
obj.should.eql({c: 'd', e: 'f'}); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('should compose an async loader from other async loaders:', function (done) { | ||
loaders.compose('parse', ['read', 'yaml'], 'async'); | ||
loaders.compose('extend', ['data'], 'async'); | ||
loaders.compose('bar', ['parse', 'extend'], 'async'); | ||
loaders.loadAsync('fixtures/a.bar', function (err, obj) { | ||
obj.should.eql({c: 'd', e: 'f'}); | ||
done(); | ||
it('should extend a loader stack with loaders defined on the loadAsync method:', function (done) { | ||
loaders.compose('bar', ['read', 'yaml'], 'async'); | ||
loaders.loadAsync('fixtures/a.bar', ['data'], function (err, obj) { | ||
obj.should.eql({c: 'd', e: 'f'}); | ||
done(); | ||
}); | ||
}); | ||
it('should compose an async loader from other async loaders:', function (done) { | ||
loaders.compose('parse', ['read', 'yaml'], 'async'); | ||
loaders.compose('extend', ['data'], 'async'); | ||
loaders.compose('bar', ['parse', 'extend'], 'async'); | ||
loaders.loadAsync('fixtures/a.bar', function (err, obj) { | ||
obj.should.eql({c: 'd', e: 'f'}); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
describe('loaders promise', function () { | ||
describe('loaders (promise)', function () { | ||
beforeEach(function() { | ||
@@ -197,40 +221,52 @@ loaders = new Loaders(); | ||
it('should register promise loaders:', function () { | ||
loaders.cache.promise.should.have.properties('yaml', 'yml', 'json', 'read', 'hbs', 'data'); | ||
}); | ||
describe('register', function () { | ||
it('should register promise loaders:', function () { | ||
loaders.cache.promise.should.have.properties('yaml', 'yml', 'json', 'read', 'hbs', 'data'); | ||
}); | ||
it('should compose a promise loader from other promise loaders with the `.compose()` method:', function () { | ||
loaders.compose('foo', ['read', 'yaml'], 'promise'); | ||
loaders.cache.promise.should.have.property('foo'); | ||
}); | ||
it('should compose a promise loader from other promise loaders with the `.compose()` method:', function () { | ||
loaders.compose('foo', ['read', 'yaml'], 'promise'); | ||
loaders.cache.promise.should.have.property('foo'); | ||
}); | ||
it('should compose a promise loader from other promise loaders with the `.register()` method:', function () { | ||
loaders.registerPromise('foo', ['read', 'yaml']); | ||
loaders.cache.promise.should.have.property('foo'); | ||
it('should compose a promise loader from other promise loaders with the `.register()` method:', function () { | ||
loaders.registerPromise('foo', ['read', 'yaml']); | ||
loaders.cache.promise.should.have.property('foo'); | ||
}); | ||
}); | ||
it('should pass the value returned from a promise loader to the next promise loader:', function (done) { | ||
loaders.registerPromise('bar', ['read', 'yaml', 'data']); | ||
loaders.loadPromise('fixtures/a.bar').then(function (results) { | ||
results.should.eql({c: 'd', e: 'f'}); | ||
done(); | ||
describe('load', function () { | ||
it('should pass the value returned from a promise loader to the next promise loader:', function (done) { | ||
loaders.registerPromise('bar', ['read', 'yaml', 'data']); | ||
loaders.loadPromise('fixtures/a.bar').then(function (results) { | ||
results.should.eql({c: 'd', e: 'f'}); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('should pass the value returned from a promise loader to the next promise loader:', function (done) { | ||
loaders.compose('bar', ['read', 'yaml', 'data'], 'promise'); | ||
loaders.loadPromise('fixtures/a.bar').then(function (results) { | ||
results.should.eql({c: 'd', e: 'f'}); | ||
done(); | ||
it('should pass the value returned from a promise loader to the next promise loader:', function (done) { | ||
loaders.compose('bar', ['read', 'yaml', 'data'], 'promise'); | ||
loaders.loadPromise('fixtures/a.bar').then(function (results) { | ||
results.should.eql({c: 'd', e: 'f'}); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('should compose a promise loader from other promise loaders:', function (done) { | ||
loaders.compose('parse', ['read', 'yaml'], 'promise'); | ||
loaders.compose('extend', ['data'], 'promise'); | ||
loaders.compose('bar', ['parse', 'extend'], 'promise'); | ||
loaders.loadPromise('fixtures/a.bar').then(function (results) { | ||
results.should.eql({c: 'd', e: 'f'}); | ||
done(); | ||
it('should extend a loader stack with loaders defined on the loadPromise method:', function (done) { | ||
loaders.compose('bar', ['read', 'yaml'], 'promise'); | ||
loaders.loadPromise('fixtures/a.bar', ['data']).then(function (results) { | ||
results.should.eql({c: 'd', e: 'f'}); | ||
done(); | ||
}); | ||
}); | ||
it('should compose a promise loader from other promise loaders:', function (done) { | ||
loaders.compose('parse', ['read', 'yaml'], 'promise'); | ||
loaders.compose('extend', ['data'], 'promise'); | ||
loaders.compose('bar', ['parse', 'extend'], 'promise'); | ||
loaders.loadPromise('fixtures/a.bar').then(function (results) { | ||
results.should.eql({c: 'd', e: 'f'}); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
@@ -240,3 +276,3 @@ }); | ||
describe('loaders stream', function () { | ||
describe('loaders (stream)', function () { | ||
beforeEach(function() { | ||
@@ -271,38 +307,49 @@ loaders = new Loaders(); | ||
it('should register stream loaders:', function () { | ||
loaders.cache.stream.should.have.properties('yaml', 'yml', 'json', 'read', 'hbs', 'data'); | ||
}); | ||
describe('register', function () { | ||
it('should register stream loaders:', function () { | ||
loaders.cache.stream.should.have.properties('yaml', 'yml', 'json', 'read', 'hbs', 'data'); | ||
}); | ||
it('should compose a stream loader from other stream loaders with the `.compose()` method:', function () { | ||
loaders.compose('foo', ['read', 'yaml'], 'stream'); | ||
loaders.cache.stream.should.have.property('foo'); | ||
}); | ||
it('should compose a stream loader from other stream loaders with the `.compose()` method:', function () { | ||
loaders.compose('foo', ['read', 'yaml'], 'stream'); | ||
loaders.cache.stream.should.have.property('foo'); | ||
}); | ||
it('should compose a stream loader from other stream loaders with the `.register()` method:', function () { | ||
loaders.registerStream('foo', ['read', 'yaml']); | ||
loaders.cache.stream.should.have.property('foo'); | ||
it('should compose a stream loader from other stream loaders with the `.register()` method:', function () { | ||
loaders.registerStream('foo', ['read', 'yaml']); | ||
loaders.cache.stream.should.have.property('foo'); | ||
}); | ||
}); | ||
it('should pass the value returned from a stream loader to the next stream loader:', function (done) { | ||
loaders.registerStream('bar', ['read', 'yaml', 'data']); | ||
loaders.loadStream('fixtures/a.bar').on('data', function (results) { | ||
results.should.eql({c: 'd', e: 'f'}); | ||
}).on('end', done); | ||
}); | ||
describe('load', function () { | ||
it('should pass the value returned from a stream loader to the next stream loader:', function (done) { | ||
loaders.registerStream('bar', ['read', 'yaml', 'data']); | ||
loaders.loadStream('fixtures/a.bar').on('data', function (results) { | ||
results.should.eql({c: 'd', e: 'f'}); | ||
}).on('end', done); | ||
}); | ||
it('should pass the value returned from a stream loader to the next stream loader:', function (done) { | ||
loaders.compose('bar', ['read', 'yaml', 'data'], 'stream'); | ||
loaders.loadStream('fixtures/a.bar').on('data', function (results) { | ||
results.should.eql({c: 'd', e: 'f'}); | ||
}).on('end', done); | ||
}); | ||
it('should pass the value returned from a stream loader to the next stream loader:', function (done) { | ||
loaders.compose('bar', ['read', 'yaml', 'data'], 'stream'); | ||
loaders.loadStream('fixtures/a.bar').on('data', function (results) { | ||
results.should.eql({c: 'd', e: 'f'}); | ||
}).on('end', done); | ||
}); | ||
it('should compose a stream loader from other stream loaders:', function (done) { | ||
loaders.compose('parse', ['read', 'yaml'], 'stream'); | ||
loaders.compose('extend', ['data'], 'stream'); | ||
loaders.compose('bar', ['parse', 'extend'], 'stream'); | ||
loaders.loadStream('fixtures/a.bar').on('data', function (results) { | ||
results.should.eql({c: 'd', e: 'f'}); | ||
}).on('end', done); | ||
it('should extend a loader stack with loaders defined on the loadStream method:', function (done) { | ||
loaders.compose('bar', ['read', 'yaml'], 'stream'); | ||
loaders.loadStream('fixtures/a.bar', ['data']).on('data', function (results) { | ||
results.should.eql({c: 'd', e: 'f'}); | ||
}).on('end', done); | ||
}); | ||
it('should compose a stream loader from other stream loaders:', function (done) { | ||
loaders.compose('parse', ['read', 'yaml'], 'stream'); | ||
loaders.compose('extend', ['data'], 'stream'); | ||
loaders.compose('bar', ['parse', 'extend'], 'stream'); | ||
loaders.loadStream('fixtures/a.bar').on('data', function (results) { | ||
results.should.eql({c: 'd', e: 'f'}); | ||
}).on('end', done); | ||
}); | ||
}); | ||
}); |
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
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
36389
699
263
0