caching-transform
Advanced tools
Comparing version 0.0.1 to 0.0.2
33
index.js
@@ -31,4 +31,26 @@ 'use strict'; | ||
var salt = opts.salt || ''; | ||
var shouldTransform = opts.shouldTransform; | ||
var disableCache = opts.disableCache; | ||
function transform(input, additionalData, hash) { | ||
if (!created) { | ||
if (!cacheDirCreated && !disableCache) { | ||
mkdirp.sync(cacheDir); | ||
} | ||
if (!transformFn) { | ||
transformFn = factory(cacheDir); | ||
} | ||
created = true; | ||
} | ||
return transformFn(input, additionalData, hash); | ||
} | ||
return function (input, additionalData) { | ||
if (shouldTransform && !shouldTransform(input, additionalData)) { | ||
return input; | ||
} | ||
if (disableCache) { | ||
return transform(input, additionalData); | ||
} | ||
var hash = getHash(input, salt); | ||
@@ -40,12 +62,3 @@ var cachedPath = path.join(cacheDir, hash + ext); | ||
} catch (e) { | ||
if (!created) { | ||
if (!cacheDirCreated) { | ||
mkdirp.sync(cacheDir); | ||
} | ||
if (!transformFn) { | ||
transformFn = factory(cacheDir); | ||
} | ||
created = true; | ||
} | ||
var result = transformFn(input, additionalData, hash); | ||
var result = transform(input, additionalData, hash); | ||
fs.writeFileSync(cachedPath, result); | ||
@@ -52,0 +65,0 @@ return result; |
{ | ||
"name": "caching-transform", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Wraps a transform and provides caching", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -65,3 +65,3 @@ # caching-transform [![Build Status](https://travis-ci.org/jamestalmage/caching-transform.svg?branch=master)](https://travis-ci.org/jamestalmage/caching-transform) [![Coverage Status](https://coveralls.io/repos/jamestalmage/caching-transform/badge.svg?branch=master&service=github)](https://coveralls.io/github/jamestalmage/caching-transform?branch=master) | ||
- `additionalData`: An arbitrary data object passed through from the wrapper. A typical value might be a string filename. | ||
- `hash`: The salted hash of `input`. You will rarely need to use this, unless you intend to create multiple cache entries per transform invocation. | ||
- `hash`: The salted hash of `input`. Useful if you intend to create additional cache entries beyond the transform result (i.e. `nyc` also creates cache entries for source-map data). This value is not available if the cache is disabled, if you still need it, it can be computed via `cachingTransform.getHash(input, salt)`. | ||
@@ -74,3 +74,3 @@ The transform function should return a `string` containing the result of transforming `input`. | ||
If the `transform` function is expensive to create, and it is reasonable to expect that it may never be called during the life of the process, you can may supply a `factory` function that will be used to create the `transform` function the first time it is needed. | ||
If the `transform` function is expensive to create, and it is reasonable to expect that it may never be called during the life of the process, you may supply a `factory` function that will be used to create the `transform` function the first time it is needed. | ||
@@ -90,4 +90,18 @@ ##### cacheDir | ||
##### shouldTransform | ||
Type: `Function(input: string, additonalData: *)` | ||
Default: `always transform` | ||
A function that examines `input` and `additionalData` to determine whether the transform should be applied. Returning `false` means the transform will not be applied and `input` will be returned unmodified. | ||
##### disableCache | ||
Type: `boolean` | ||
Default: `false` | ||
If `true`, the cache is ignored and the transform is used every time regardless of cache contents. | ||
## License | ||
MIT © [James Talmage](http://github.com/jamestalmage) |
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
7939
60
105