Skip to content
This repository has been archived by the owner on Jan 4, 2020. It is now read-only.

Commit

Permalink
Fixed error in engine option operation and fixed unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RedKenrok committed Sep 12, 2018
1 parent 5fc5518 commit 36bae36
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 18 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 0.2.2 (2018-09-12)
For hoast version 0.1.0.

## Fixed
- `engine` parameter had critical error in asynchronous operation.

## 0.2.1 (2018-09-12)
For hoast version 0.1.0.

Expand Down
11 changes: 6 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,32 +39,33 @@ module.exports = function(options) {
return async function(hoast, files) {
debug(`Running module.`);

let filtered;
if (options.engine) {
debug(`Using engine filter method.`);

Promise.all(files.map(function(file) {
await Promise.all(files.map(async function(file) {
// First check nanomatch filter whether it should be filtered.
if (options.patterns && nanomatch.any(file.path, options.patterns) === options.invert) {
return true;
}
// Else use the specified engine function.
return options.engine(file);
return await options.engine(file);
})).then(function(result) {
files = files.filter(function(file, index) {
filtered = files.filter(function(file, index) {
return result[index];
});
});
} else {
debug(`Using standard filter method.`);

// Filter using nanomatch based of patterns.
files = files.filter(function(file) {
filtered = files.filter(function(file) {
debug(`Filtering file '${file.path}'.`);
return nanomatch.any(file.path, options.patterns) ? !options.invert : options.invert;
});
}
debug(`Finished filtering files.`);

return files;
return filtered;
};
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"author": "Ron Dekker",
"name": "hoast-filter",
"version": "0.2.1",
"version": "0.2.2",
"description": "Hoast module used to filter out files from further processing.",
"license": "ISC",
"keywords": [
Expand Down
24 changes: 12 additions & 12 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const test = require(`ava`);
const Filter = require(`.`);

// Test patterns option with a string.
test(`pattern`, function(t) {
test(`pattern`, async function(t) {
// Create dummy files.
let files = [{
path: `a.txt`
Expand All @@ -18,7 +18,7 @@ test(`pattern`, function(t) {
}];

// Test module.
files = Filter({
files = await Filter({
patterns: `**/*.txt`
})({}, files);

Expand All @@ -27,7 +27,7 @@ test(`pattern`, function(t) {
});

// Test patterns option with an array.
test(`patterns`, function(t) {
test(`patterns`, async function(t) {
// Create dummy files.
let files = [{
path: `a.txt`
Expand All @@ -45,7 +45,7 @@ test(`patterns`, function(t) {
}];

// Test module.
files = Filter({
files = await Filter({
patterns: [
`**/*.txt`,
`**/*.md`
Expand All @@ -57,7 +57,7 @@ test(`patterns`, function(t) {
});

// Test invert option.
test(`invert`, function(t) {
test(`invert`, async function(t) {
// Create dummy files.
let files = [{
path: `a.txt`
Expand All @@ -73,7 +73,7 @@ test(`invert`, function(t) {
}];

// Test module.
files = Filter({
files = await Filter({
invert: true,
patterns: [
`**/*.txt`,
Expand All @@ -86,7 +86,7 @@ test(`invert`, function(t) {
});

// Test engine option.
test(`engine`, function(t) {
test(`engine`, async function(t) {
// Create dummy files.
let files = [{
path: `a.txt`
Expand All @@ -102,7 +102,7 @@ test(`engine`, function(t) {
}];

// Test module.
files = Filter({
files = await Filter({
engine: function(file) {
return file.path === `a.txt`;
}
Expand All @@ -113,7 +113,7 @@ test(`engine`, function(t) {
});

// Test engine option in combination with a pattern.
test(`engine-pattern`, function(t) {
test(`engine-pattern`, async function(t) {
// Create dummy files.
let files = [{
path: `a.txt`
Expand All @@ -131,7 +131,7 @@ test(`engine-pattern`, function(t) {
}];

// Test module.
files = Filter({
files = await Filter({
engine: function(file) {
return file.path === `a.txt`;
},
Expand All @@ -143,7 +143,7 @@ test(`engine-pattern`, function(t) {
});

// Test engine option in combination with an inverted pattern.
test(`engine-invert`, function(t) {
test(`engine-invert`, async function(t) {
// Create dummy files.
let files = [{
path: `a.txt`
Expand All @@ -161,7 +161,7 @@ test(`engine-invert`, function(t) {
}];

// Test module.
files = Filter({
files = await Filter({
engine: function(file) {
return file.path === `b.md`;
},
Expand Down

0 comments on commit 36bae36

Please sign in to comment.