Socket
Socket
Sign inDemoInstall

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.3.2 to 0.4.0

175

index.js

@@ -12,4 +12,2 @@ /*!

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

@@ -51,3 +49,2 @@ /**

* @return {String}
* @api private
*/

@@ -90,3 +87,5 @@

Loaders.prototype.registerAsync = function(/*ext, stack, fn*/) {
this.register.apply(this, slice(arguments).concat('async'));
var i = arguments.length, args = new Array(i);
while (i--) args[i] = arguments[i];
this.register.apply(this, args.concat('async'));
};

@@ -108,3 +107,5 @@

Loaders.prototype.registerPromise = function(/*ext, stack, fn*/) {
this.register.apply(this, slice(arguments).concat('promise'));
var i = arguments.length, args = new Array(i);
while (i--) args[i] = arguments[i];
this.register.apply(this, args.concat('promise'));
};

@@ -126,3 +127,5 @@

Loaders.prototype.registerStream = function(/*ext, stack, fn*/) {
this.register.apply(this, slice(arguments).concat('stream'));
var i = arguments.length, args = new Array(i);
while (i--) args[i] = arguments[i];
this.register.apply(this, args.concat('stream'));
};

@@ -138,11 +141,24 @@

* @return {Object} `Loaders` to enable chaining
* @api private
*/
Loaders.prototype.compose = function(ext/*, stack, fns*/, type) {
type = filter(arguments, 'string')[1] || 'sync';
var stack = filter(arguments, ['array', 'function', 'object']);
Loaders.prototype.compose = function(ext/*, stack, fns*/) {
var len = arguments.length - 1, i = 1;
var stack = [], type;
while (len--) {
var arg = arguments[i++];
if (typeof arg === 'string') {
type = arg;
} else if (arg) {
stack.push(arg);
}
}
if (typeof type === 'undefined') {
type = 'sync';
}
this.cache[type] = this.cache[type] || {};
stack = this.buildStack(type, stack);
this.cache[type] = this.cache[type] || {};
this.cache[type][ext] = union(this.cache[type][ext] || [], stack);

@@ -153,2 +169,35 @@ return this;

/**
* Create a from other (previously cached) loaders.
*
* @param {String} `name` Name of the loader or loader stack to use, usually this is a file extension.
* @param {String} `loaders` Array of loader names.
* @return {Object} `Loaders` to enable chaining
*/
Loaders.prototype.composeStream = function() {
var fn = this._makeComposer('stream');
return fn.apply(fn, arguments);
};
/**
* Internal method for creating composers.
*
* @param {String} `type` The type of composer to create.
* @return {Function} Composer function for the given `type.
*/
Loaders.prototype._makeComposer = function() {
return function () {
// don't slice args (for v8 optimizations)
var len = arguments.length, i = 0;
var args = new Array(i);
while (len--) {
args[i] = arguments[i++];
}
args[i] = 'stream';
this.compose.apply(this, args);
}.bind(this);
};
/**
* Build a stack of loader functions when given a mix of functions and names.

@@ -162,11 +211,16 @@ *

Loaders.prototype.buildStack = function(type, stack) {
return flatten(stack.map(function(name) {
var len = stack && stack.length, i = 0;
var res = [];
while (i < len) {
var name = stack[i++];
if (typeOf(name) === 'string') {
return this.cache[type][name];
res = res.concat(this.cache[type][name]);
} else if (typeOf(name) === 'array') {
res = res.concat(this.buildStack(type, name));
} else {
res.push(name);
}
if (typeOf(name) === 'array') {
return this.buildStack(type, name);
}
return name;
}.bind(this)));
}
return res;
};

@@ -184,3 +238,3 @@

*
* @param {String} `fp` File path to load.
* @param {String} `val` Value to load, like a file path.
* @param {String} `options` Options to pass to whatever loaders are defined.

@@ -191,18 +245,27 @@ * @return {String}

Loaders.prototype.load = function(fp, stack, options) {
Loaders.prototype.load = function(val, stack, options) {
if (!Array.isArray(stack)) {
options = stack;
stack = [];
options = stack; stack = [];
}
var loader = matchLoader(fp, options, this);
var loader = matchLoader(val, options, this);
stack = this.buildStack('sync', stack);
var fns = union(this.cache.sync[loader] || [], stack);
if (!fns) {
return fp;
var fns = [];
if (this.cache.sync.hasOwnProperty(loader)) {
fns = fns.concat(this.cache.sync[loader]);
}
return fns.reduce(function (acc, fn) {
return fn(acc, options);
}, fp);
if (stack && stack.length) {
fns = fns.concat(stack);
}
if (!fns.length) return val;
var len = fns.length, i = 0;
while (len--) {
var fn = fns[i++];
val = fn(val, options);
}
return val;
};

@@ -232,15 +295,11 @@

if (typeOf(stack) === 'function') {
cb = stack;
stack = [];
options = {};
cb = stack; stack = []; options = {};
}
if (typeOf(options) === 'function') {
cb = options;
options = {};
cb = options; options = {};
}
if (!Array.isArray(stack)) {
options = stack;
stack = [];
options = stack; stack = [];
}

@@ -252,3 +311,3 @@

var fns = union(this.cache.async[loader] || [], stack);
if (!fns) {
if (!fns.length) {
return fp;

@@ -295,3 +354,3 @@ }

var fns = union(this.cache.promise[loader] || [], stack);
if (!fns) {
if (!fns.length) {
return current.then(function () {

@@ -339,3 +398,3 @@ return fp;

var fns = union(this.cache.stream[loader] || [], stack);
if (!fns) {
if (!fns.length) {
var noop = es.through(function (fp) {

@@ -361,3 +420,2 @@ this.emit('data', fp);

* @return {Object} Object
* @api private
*/

@@ -377,45 +435,14 @@

* @return {String}
* @api private
*/
function formatExt(ext) {
return (ext[0] === '.')
? ext.slice(1)
: ext;
return (ext[0] === '.') ? ext.slice(1) : ext;
}
/**
* Array union util.
*
* @api private
* Concat a list of arrays.
*/
function union() {
return [].concat.apply([], arguments)
.filter(Boolean);
return [].concat.apply([], arguments);
}
/**
* 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.3.2",
"version": "0.4.0",
"homepage": "https://github.com/jonschlinkert/loader-cache",

@@ -26,4 +26,2 @@ "author": {

"dependencies": {
"arr-flatten": "^0.2.1",
"array-slice": "^0.2.2",
"async": "^0.9.0",

@@ -36,3 +34,4 @@ "bluebird": "^2.3.11",

"js-yaml": "^3.2.3",
"mocha": "*"
"mocha": "*",
"should": "^5.0.1"
},

@@ -51,2 +50,2 @@ "keywords": [

]
}
}

@@ -1,6 +0,5 @@

# loader-cache [![NPM version](https://badge.fury.io/js/loader-cache.svg)](http://badge.fury.io/js/loader-cache)
# loader-cache [![NPM version](https://badge.fury.io/js/loader-cache.svg)](http://badge.fury.io/js/loader-cache) [![Build Status](https://travis-ci.org/jonschlinkert/loader-cache.svg)](https://travis-ci.org/jonschlinkert/loader-cache)
> 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.
## Install
## Install with [npm](npmjs.org)

@@ -33,8 +32,10 @@

## Run tests
## Running tests
Install dev dependencies.
```bash
npm test
npm i -d && npm test
```
## Usage

@@ -47,3 +48,3 @@

## API
### [Loaders](index.js#L30)
### [Loaders](./index.js#L37)

@@ -57,15 +58,4 @@ Create a new instance of `Loaders`

### [.loaderTypes](index.js#L48)
### [.registerSync](./index.js#L67)
Array of supported loader types as a convenience for creating utility functions to dynamically choose loaders. Currently supported types are:
- `sync`
- `async`
- `promise`
- `stream`
### [.register](index.js#L105)
Register the given 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.
* `ext` **{String|Array}**: File extension or name of the loader.

@@ -75,23 +65,10 @@ * `fn` **{Function|Array}**: A loader function, or create a loader from other others by passing an array of names.

**Examples**
Register the given 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
// register a loader for parsing YAML
loaders.register('yaml', function(fp) {
return YAML.safeLoad(fp);
});
### [.registerAsync](./index.js#L84)
// 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']);
```
### [.registerAsync](index.js#L139)
Register the given async 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.
* `ext` **{String|Array}**: File extension or name of the loader.

@@ -101,23 +78,10 @@ * `fn` **{Function|Array}**: A loader function with a callback parameter, or create a loader from other others by passing an array of names.

**Examples**
Register the given async 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
// register an async loader for parsing YAML
loaders.registerAsync('yaml', function(fp, next) {
next(null, YAML.safeLoad(fp));
});
### [.registerPromise](./index.js#L103)
// 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']);
```
### [.registerPromise](index.js#L184)
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.
* `ext` **{String|Array}**: File extension or name of the loader.

@@ -127,34 +91,10 @@ * `fn` **{Function|Array}**: A loader function that returns a promise, or create a loader from other others by passing an array of names.

**Examples**
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');
### [.registerStream](./index.js#L122)
// 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']);
```
### [.registerStream](index.js#L220)
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.
* `ext` **{String|Array}**: File extension or name of the loader.

@@ -164,35 +104,13 @@ * `fn` **{Stream|Array}**: A stream loader, or create a loader from other others by passing an array of names.

**Examples**
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.
```js
// register an stream loader for parsing YAML
loaders.registerStream('yaml', es.through(function(fp) {
this.emit('data', YAML.safeLoad(fp));
});
### [.load](./index.js#L236)
// 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']);
```
### [.createTypeStack](index.js#L276)
* `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#L316)
Run loaders associated with `ext` of the given filepath.
* `fp` **{String}**: File path to load.
* `val` **{String}**: Value to load, like a file path.
* `options` **{String}**: Options to pass to whatever loaders are defined.

@@ -208,3 +126,3 @@ * `returns`: {String}

### [.loadAsync](index.js#L351)
### [.loadAsync](./index.js#L282)

@@ -227,3 +145,3 @@ Run async loaders associated with `ext` of the given filepath.

### [.loadPromise](index.js#L393)
### [.loadPromise](./index.js#L328)

@@ -246,3 +164,3 @@ Run promise loaders associated with `ext` of the given filepath.

### [.loadStream](index.js#L431)
### [.loadStream](./index.js#L373)

@@ -277,4 +195,5 @@ Run stream loaders associated with `ext` of the given filepath.

## License
Copyright (c) 2014 Jon Schlinkert
Copyright (c) 2015 Jon Schlinkert
Released under the MIT license

@@ -284,2 +203,2 @@

_This file was generated by [verb](https://github.com/assemble/verb) on December 03, 2014._
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on March 10, 2015._
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