Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

loader-cache

Package Overview
Dependencies
Maintainers
2
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

loader-cache - npm Package Compare versions

Comparing version 0.2.5 to 0.3.0

doc.md

305

index.js

@@ -11,2 +11,5 @@ /*!

var path = require('path');
var typeOf = require('kind-of');
var slice = require('array-slice');
var flatten = require('arr-flatten');

@@ -20,2 +23,8 @@ /**

/**
* requires cache
*/
var requires = {};
/**
* Create a new instance of `Loaders`

@@ -37,40 +46,13 @@ *

/**
* Array of supported loader types as a convenience for creating
* utility functions to dynamically choose loaders. Currently supported
* types are:
*
* - `sync`
* - `async`
* - `promise`
* - `stream`
*
* @type {Array}
* @api public
*/
Loaders.loaderTypes = [
'sync',
'async',
'promise',
'stream'
];
/**
* Base register method used by all other register method.
*
* @param {String} `ext`
* @param {Function|Array} `fn`
* @param {Function} `fn`
* @param {String} `type`
* @return {Object}
* @return {String}
* @api private
*/
Loaders.prototype._register = function(ext, fn, type) {
if (Array.isArray(fn)) {
return this.compose(formatExt(ext), fn, type);
}
this.cache[type] = this.cache[type] || {};
this.cache[type][formatExt(ext)] = [fn];
return this;
Loaders.prototype.register = function(/*ext, fns, arr, type*/) {
return this.compose.apply(this, arguments);
};

@@ -85,19 +67,2 @@

*
* **Examples**
*
* ```js
* // register a loader for parsing YAML
* loaders.register('yaml', function(fp) {
* return YAML.safeLoad(fp);
* });
*
* // register a loader to be used in other loaders
* loaders.register('read', function(fp) {
* return fs.readFileSync(fp, 'utf8');
* });
*
* // create a new loader from the `yaml` and `read` loaders.
* loaders.register('yml', ['read', 'yaml']);
* ```
*
* @param {String|Array} `ext` File extension or name of the loader.

@@ -109,4 +74,4 @@ * @param {Function|Array} `fn` A loader function, or create a loader from other others by passing an array of names.

Loaders.prototype.register = function(ext, fn, type) {
this._register(ext, fn, type || 'sync');
Loaders.prototype.registerSync = function(ext, stack, fn) {
this.register(ext, stack, fn, 'sync');
};

@@ -121,19 +86,2 @@

*
* **Examples**
*
* ```js
* // register an async loader for parsing YAML
* loaders.registerAsync('yaml', function(fp, next) {
* next(null, YAML.safeLoad(fp));
* });
*
* // register a loader to be used in other loaders
* loaders.registerAsync('read', function(fp, next) {
* fs.readFile(fp, 'utf8', next);
* });
*
* // create a new loader from the `yaml` and `read` loaders.
* loaders.registerAsync('yml', ['read', 'yaml']);
* ```
*
* @param {String|Array} `ext` File extension or name of the loader.

@@ -145,4 +93,4 @@ * @param {Function|Array} `fn` A loader function with a callback parameter, or create a loader from other others by passing an array of names.

Loaders.prototype.registerAsync = function(ext, fn) {
this._register(ext, fn, 'async');
Loaders.prototype.registerAsync = function(/*ext, stack, fn*/) {
this.register.apply(this, slice(arguments).concat('async'));
};

@@ -157,30 +105,2 @@

*
* **Examples**
*
* ```js
* var Promise = require('bluebird');
*
* // register an promise loader for parsing YAML
* loaders.registerPromise('yaml', function(fp) {
* var deferred = Promise.pending();
* process.nextTick(function () {
* deferred.fulfill(YAML.safeLoad(fp));
* });
* return deferred.promise;
* });
*
* // register a loader to be used in other loaders
* loaders.registerPromise('read', function(fp) {
* var Promise = require('bluebird');
* var deferred = Promise.pending();
* fs.readFile(fp, 'utf8', function (err, content) {
* deferred.fulfill(content);
* });
* return deferred.promise;
* });
*
* // create a new loader from the `yaml` and `read` loaders.
* loaders.registerPromise('yml', ['read', 'yaml']);
* ```
*
* @param {String|Array} `ext` File extension or name of the loader.

@@ -192,4 +112,4 @@ * @param {Function|Array} `fn` A loader function that returns a promise, or create a loader from other others by passing an array of names.

Loaders.prototype.registerPromise = function(ext, fn) {
this._register(ext, fn, 'promise');
Loaders.prototype.registerPromise = function(/*ext, stack, fn*/) {
this.register.apply(this, slice(arguments).concat('promise'));
};

@@ -204,21 +124,2 @@

*
* **Examples**
*
* ```js
* // register an stream loader for parsing YAML
* loaders.registerStream('yaml', es.through(function(fp) {
* this.emit('data', YAML.safeLoad(fp));
* });
*
* // register a loader to be used in other loaders
* loaders.registerStream('read', function(fp) {
* fs.readFile(fp, 'utf8', function (err, content) {
* this.emit('data', content);
* });
* });
*
* // create a new loader from the `yaml` and `read` loaders.
* loaders.registerStream('yml', ['read', 'yaml']);
* ```
*
* @param {String|Array} `ext` File extension or name of the loader.

@@ -230,4 +131,4 @@ * @param {Stream|Array} `fn` A stream loader, or create a loader from other others by passing an array of names.

Loaders.prototype.registerStream = function(ext, fn) {
this._register(ext, fn, 'stream');
Loaders.prototype.registerStream = function(/*ext, stack, fn*/) {
this.register.apply(this, slice(arguments).concat('stream'));
};

@@ -239,23 +140,3 @@

*
* **Example**
*
* ```js
* // arbitrary name, so it won't match file extensions. This
* // loader will be used in other loaders for reading files
* loaders.register('read', function(fp) {
* return fs.readFileSync(fp, 'utf8');
* });
*
* // Parse a string of YAML
* loaders.register('yaml', function(fp) {
* return YAML.safeLoad(fp);
* });
*
* // Compose a new loader that will read a file, then parse it as YAML
* loaders.compose('yml', ['read', 'yaml']);
*
* // you can alternatively do the same thing with the register method, e.g.
* loaders.register('yml', ['read', 'yaml']);
* ```
*
* @param {String} `ext` File extension to select the loader or loader stack to use.

@@ -267,10 +148,9 @@ * @param {String} `loaders` Array of loader names.

Loaders.prototype.compose = function(ext, loaders, type) {
type = type || 'sync';
Loaders.prototype.compose = function(ext/*, stack, fns*/, type) {
type = filter(arguments, 'string')[1] || 'sync';
var stack = filter(arguments, ['array', 'function', 'object']);
stack = this.buildStack(type, stack);
var cache = this.cache[type] || {};
var stack = this.createTypeStack(loaders, type);
cache[ext] = cache[ext] || [];
cache[ext] = cache[ext].concat(stack);
this.cache[type] = this.cache[type] || {};
this.cache[type][ext] = union(this.cache[type][ext] || [], stack);
return this;

@@ -280,36 +160,22 @@ };

/**
* Create a loader stack of the given `type` from an
* array of `loaders`.
* Build a stack of loader functions when given a mix of functions and names.
*
* @param {Array} `loaders` Names of stored loaders to add to the stack.
* @param {String} `type=sync`
* @return {Array} Array of loaders
* @api public
* @param {String} `type` Loader type to get loaders from.
* @param {Array} `stack` Stack of loader functions and names.
* @return {Array} Resolved loader functions
*/
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);
Loaders.prototype.buildStack = function(type, stack) {
return flatten(stack.map(function(name) {
if (typeOf(name) === 'string') {
return this.cache[type][name];
}
if (typeOf(name) === 'array') {
return this.buildStack(type, name);
}
return name;
}.bind(this)));
};
/**
* 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.

@@ -336,7 +202,9 @@ *

var fns = this.loadStack(fp, stack, options);
if (!fns || fns.length === 0) {
var loader = matchLoader(fp, options, this);
stack = this.buildStack('sync', stack);
var fns = union(this.cache.sync[loader] || [], stack);
if (!fns) {
return fp;
}
return fns.reduce(function (acc, fn) {

@@ -367,3 +235,10 @@ return fn(acc, options);

Loaders.prototype.loadAsync = function(fp, stack, options, cb) {
if (typeof options === 'function') {
var async = requires.async || (requires.async = require('async'));
if (typeOf(stack) === 'function') {
cb = stack;
stack = [];
options = {};
}
if (typeOf(options) === 'function') {
cb = options;

@@ -373,14 +248,15 @@ options = {};

if (typeof stack === 'function') {
cb = stack;
options = {};
if (!Array.isArray(stack)) {
options = stack;
stack = [];
}
var fns = this.loadStack(fp, stack, options, 'async');
if (!fns || fns.length === 0) {
stack = this.buildStack('async', stack);
var loader = matchLoader(fp, options, this);
var fns = union(this.cache.async[loader] || [], stack);
if (!fns) {
return fp;
}
var async = require('async');
async.reduce(fns, fp, function (acc, fn, next) {

@@ -411,2 +287,3 @@ fn(acc, options, next);

Loaders.prototype.loadPromise = function(fp, stack, options) {
var Promise = requires.promise || (requires.promise = require('bluebird'));
if (!Array.isArray(stack)) {

@@ -417,9 +294,15 @@ options = stack;

var current = Promise.resolve();
options = options || {};
var fns = this.loadStack(fp, stack, options, 'promise');
var Promise = require('bluebird');
var current = Promise.resolve();
var loader = matchLoader(fp, options, this);
stack = this.buildStack('promise', stack);
if (!fns || fns.length === 0) return current.then(function () { return fp; });
var fns = union(this.cache.promise[loader] || [], stack);
if (!fns) {
return current.then(function () {
return fp;
});
}
return Promise.reduce(fns, function (acc, fn) {

@@ -451,2 +334,3 @@ return fn(acc, options);

Loaders.prototype.loadStream = function(fp, stack, options) {
var es = requires.es || (requires.es = require('event-stream'));
if (!Array.isArray(stack)) {

@@ -457,7 +341,8 @@ options = stack;

var es = require('event-stream');
options = options || {};
var loader = matchLoader(fp, options, this);
stack = this.buildStack('stream', stack);
var fns = this.loadStack(fp, stack, options, 'stream');
if (!fns || fns.length === 0) {
var fns = union(this.cache.stream[loader] || [], stack);
if (!fns) {
var noop = es.through(function (fp) {

@@ -506,1 +391,37 @@ this.emit('data', fp);

}
/**
* Array union util.
*
* @api private
*/
function union() {
return [].concat.apply([], arguments)
.filter(Boolean);
}
/**
* Filter util
*
* @api private
*/
function filter(arr, types) {
types = !Array.isArray(types)
? [types]
: types;
var len = arr.length;
var res = [];
var i = 0;
while (len--) {
var ele = arr[i++];
if (types.indexOf(typeOf(ele)) !== -1) {
res = res.concat(ele);
}
}
return res;
}
{
"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.5",
"version": "0.3.0",
"homepage": "https://github.com/jonschlinkert/loader-cache",

@@ -30,7 +30,2 @@ "author": {

},
"dependencies": {
"async": "^0.9.0",
"bluebird": "^2.3.11",
"event-stream": "^3.1.7"
},
"devDependencies": {

@@ -52,3 +47,11 @@ "js-yaml": "^3.2.3",

"reader"
]
],
"dependencies": {
"arr-flatten": "^0.2.1",
"array-slice": "^0.2.2",
"async": "^0.9.0",
"bluebird": "^2.3.11",
"event-stream": "^3.1.7",
"kind-of": "^0.1.2"
}
}

@@ -10,38 +10,12 @@ /*!

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');
var loaders;
describe('register', function () {
beforeEach(function () {
loaders = new Loaders();
// sync read loader
loaders.register('read', function (fp) {
return fs.readFileSync(fp, 'utf8');
});
// async read loader
loaders.registerAsync('read', function read(fp, options, next) {
fs.readFile(fp, 'utf8', next);
});
// promise read loader
loaders.registerPromise('read', Promise.method(function read(fp) {
return fs.readFileSync(fp, 'utf8');
}));
// stream read loaders
loaders.registerStream('read', es.through(function read(fp) {
this.emit('data', fs.readFileSync(fp, 'utf8'));
}));
});
});
describe('loaders (sync)', function () {

@@ -77,58 +51,81 @@ beforeEach(function() {

describe('register', function () {
it('should register loaders:', function () {
loaders.cache.sync.should.have.properties('yaml', 'yml', 'json', 'read', 'hbs', 'data');
});
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 `.compose()` method:', function () {
loaders.compose('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 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 `.register()` method:', function () {
loaders.register('foo', ['read', 'yaml']);
loaders.cache.sync.should.have.property('foo');
});
it('should compose a loader from other loaders and functions with the `.compose()` method:', function () {
function bar (fp) { return fp; }
function baz (contents) { return contents; };
loaders.compose('foo', bar, ['read'], baz, ['yaml']);
loaders.cache.sync.should.have.property('foo');
loaders.cache.sync.foo.length.should.be.eql(4);
});
it('should compose a loader from other loaders and functions with the `.register()` method:', function () {
function bar (fp) { return fp; }
function baz (contents) { return contents; };
loaders.register('foo', bar, ['read'], baz, ['yaml']);
loaders.cache.sync.should.have.property('foo');
loaders.cache.sync.foo.length.should.be.eql(4);
});
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 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 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 pass the value returned from a loader to the next loader:', function () {
function foo (fp) { return fp; }
function baz (contents) { return contents; };
loaders.register('bar', foo, ['read'], baz, ['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 compose a loader from other loaders and the given function:', function () {
loaders.register('foo', ['read', 'yaml'], function foo(val) {
return val;
});
loaders.cache.sync.should.have.property('foo');
});
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 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 use a custom function for matching loaders:', function () {
loaders.compose('parse', ['read', 'yaml']);
loaders.compose('extend', ['data']);
loaders.compose('bar', ['parse', 'extend']);
it('should use loaders passed in at load time:', function () {
loaders.compose('bar', ['read', 'yaml']);
loaders.load('fixtures/a.bar', ['data']).should.eql({c: 'd', e: 'f'});
});
loaders.load('fixtures/a.bar', {
matchLoader: function(pattern) {
return path.extname(pattern).slice(1);
}
}).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 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() {

@@ -163,51 +160,74 @@ loaders = new Loaders();

describe('register', function () {
it('should register async loaders:', function () {
loaders.cache.async.should.have.properties('yaml', 'yml', 'json', 'read', 'hbs', 'data');
});
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 compose an async loader from other async loaders and functions with the `.compose()` method:', function () {
function bar (fp, options, next) { next(null, fp); }
function baz (contents, options, next) { next(null, contents); };
loaders.compose('foo', bar, ['read'], baz, ['yaml'], 'async');
loaders.cache.async.should.have.property('foo');
loaders.cache.async.foo.length.should.be.eql(4);
});
it('should compose an async loader from other async loaders and functions with the `.register()` method:', function () {
function bar (fp, options, next) { next(null, fp); }
function baz (contents, options, next) { next(null, contents); };
loaders.registerAsync('foo', bar, ['read'], baz, ['yaml']);
loaders.cache.async.should.have.property('foo');
loaders.cache.async.foo.length.should.be.eql(4);
});
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) {
function foo (fp, options, next) { next(null, fp); }
function baz (contents, options, next) { next(null, contents); };
loaders.registerAsync('bar', foo, ['read'], baz, ['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 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 use async loaders passed in at load time:', 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();
});
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();
});

@@ -217,3 +237,3 @@ });

describe('loaders (promise)', function () {
describe('loaders promise', function () {
beforeEach(function() {

@@ -248,51 +268,73 @@ loaders = new Loaders();

describe('register', function () {
it('should register promise loaders:', function () {
loaders.cache.promise.should.have.properties('yaml', 'yml', 'json', 'read', 'hbs', 'data');
});
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 compose a promise loader from other promise loaders and functions with the `.compose()` method:', function () {
var bar = Promise.method(function bar (fp) { return fp; });
var baz = Promise.method(function baz (contents) { return contents; });
loaders.compose('foo', bar, ['read'], baz, ['yaml'], 'promise');
loaders.cache.promise.should.have.property('foo');
loaders.cache.promise.foo.length.should.be.eql(4);
});
it('should compose a promise loader from other promise loaders and functions with the `.register()` method:', function () {
var bar = Promise.method(function bar (fp) { return fp; });
var baz = Promise.method(function baz (contents) { return contents; });
loaders.registerPromise('foo', bar, ['read'], baz, ['yaml']);
loaders.cache.promise.should.have.property('foo');
loaders.cache.promise.foo.length.should.be.eql(4);
});
it('should pass the value returned from a promise loader to the next promise loader:', function (done) {
var foo = Promise.method(function foo (fp) { return fp; });
var baz = Promise.method(function baz (contents) { return contents; });
loaders.registerPromise('bar', foo, ['read'], baz, ['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.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 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 use promise loaders passed in at load time:', 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();
});
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();
});

@@ -302,3 +344,4 @@ });

describe('loaders (stream)', function () {
describe('loaders stream', function () {
beforeEach(function() {

@@ -333,49 +376,70 @@ loaders = new Loaders();

describe('register', function () {
it('should register stream loaders:', function () {
loaders.cache.stream.should.have.properties('yaml', 'yml', 'json', 'read', 'hbs', 'data');
});
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');
});
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 compose a stream loader from other stream loaders and functions with the `.compose()` method:', function () {
var bar = es.through(function bar (fp) { this.emit('data', fp); });
var baz = es.through(function baz (contents) { this.emit('data', contents); });
loaders.compose('foo', bar, ['read'], baz, ['yaml'], 'stream');
loaders.cache.stream.should.have.property('foo');
loaders.cache.stream.foo.length.should.be.eql(4);
});
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 and functions with the `.register()` method:', function () {
var bar = es.through(function bar (fp) { this.emit('data', fp); });
var baz = es.through(function baz (contents) { this.emit('data', contents); });
loaders.registerStream('foo', bar, ['read'], baz, ['yaml']);
loaders.cache.stream.should.have.property('foo');
loaders.cache.stream.foo.length.should.be.eql(4);
});
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 pass the value returned from a stream loader to the next stream loader:', function (done) {
var foo = es.through(function foo (fp) { this.emit('data', fp); });
var baz = es.through(function baz (contents) { this.emit('data', contents); });
loaders.registerStream('bar', foo, ['read'], baz, ['yaml', 'data']);
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 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 use stream loaders passed in at load time:', 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);
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc