Skip to content

Commit

Permalink
Renamed the getPath option to map
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesMessinger committed Aug 19, 2019
1 parent 0c78dfe commit 0ca077c
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 26 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ Usage
### createFilter(options, criteria)
- **`options`** - An object with some or all of the following properties:
- `getPath` - A function that returns the file path from the given arguments
- `map` - A function that maps filtered items to file paths
- `sep` - A custom path separator, such as `\` or `/`
- **`criteria`** - The filter criteria. See the [`filePathFilter`](#filepathfiltercriteria) for details.
Expand All @@ -99,7 +99,7 @@ Usage
The `createFilter` function is an alternative to the `filePathFilter` function that allows you to customize the behavior to suit your needs.
#### Filtering objects
The `filePathFilter` function creates a function that filters arrays of strings, but what if you need to filter an array of objects instead? That's where the `getPath` option comes in handy. You can use it to tell File Path Filter where to find the file paths on your objects. Here's an example:
The `filePathFilter` function creates a function that filters arrays of strings, but what if you need to filter an array of objects instead? That's where the `map` option comes in handy. You can use it to map objects (or any other value) to file paths. Here's an example:
```javascript
const { createFilter } = require("file-path-filter");
Expand All @@ -112,13 +112,13 @@ const files = [
{ dir: "/my/website/blog", filename: "post-2.html" },
];

// A function to returns the full path of each file
function getPath(file) {
// A function to returns the path of each file
function map(file) {
return path.join(file.dir, file.filename);
}

// Filter the file objects - return all HTML files except the blog posts
files.filter(createFilter({ getPath }, "**/*.html", "!**/blog/*.html"));
files.filter(createFilter({ map }, "**/*.html", "!**/blog/*.html"));
```
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "file-path-filter",
"version": "2.2.0",
"version": "2.2.1",
"description": "Filters file paths using globs, regular expressions, or custom criteria",
"keywords": [
"filter",
Expand Down
11 changes: 6 additions & 5 deletions src/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ type NormalizedOptions = Required<Options>;
*/
function normalizeOptions(options: Options): NormalizedOptions {
return {
getPath: options.getPath || String,
// tslint:disable-next-line: no-any no-unsafe-any
map: options.map || (options as any).getPath || String,
sep: options.sep || path.sep,
};
}
Expand Down Expand Up @@ -113,10 +114,10 @@ criterion: FilterCriterion, options: NormalizedOptions, filter?: Filter): [Filte
}
else if (criterion instanceof RegExp) {
let pattern = criterion;
let { getPath } = options;
let { map } = options;

filterFunction = function regExpFilter(...args: unknown[]) {
let filePath = getPath(...args);
let filePath = map(...args);
return pattern.test(filePath);
};
}
Expand All @@ -131,10 +132,10 @@ criterion: FilterCriterion, options: NormalizedOptions, filter?: Filter): [Filte
* Creates a `FilterFunction` for filtering based on glob patterns
*/
function createGlobFilter(pattern: RegExp, options: NormalizedOptions, invert: boolean): FilterFunction {
let { getPath, sep } = options;
let { map, sep } = options;

return function globFilter(...args: unknown[]) {
let filePath = getPath(...args);
let filePath = map(...args);

if (sep !== "/") {
// Glob patterns always expect forward slashes, even on Windows
Expand Down
7 changes: 4 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ export type AnyFilter = FilterCriteria | Partial<Filters>;
*/
export interface Options {
/**
* A function taht returns the file path from the given arguments.
* A function that maps each filtered item to a file path. This allows you to filter things
* other than strings.
*
* Defaults to a function that returns the first argument as a string.
*/
getPath?: PathGetter;
map?: MapFunction;

/**
* The path separator. This allows you to filter paths from
Expand All @@ -54,4 +55,4 @@ export interface Options {
/**
* A function that returns the file path from the given arguments
*/
export type PathGetter = (...args: unknown[]) => string;
export type MapFunction = (...args: unknown[]) => string;
24 changes: 12 additions & 12 deletions test/specs/get-path.spec.js → test/specs/map.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ const { createFilter } = require("../../lib");
const paths = require("../fixtures/paths");
const { expect } = require("chai");

describe("options.getPath", () => {
describe("options.map", () => {

it("should filter custom file objects by a single glob pattern", () => {
let files = paths.map((path) => ({ path }));

function getPath (file) {
function map (file) {
return file.path;
}

let filter = createFilter({ getPath }, "**/*.html");
let filter = createFilter({ map }, "**/*.html");
let result = files.filter(filter);

expect(result).to.deep.equal([
Expand All @@ -28,13 +28,13 @@ describe("options.getPath", () => {

it("should filter custom file objects by multiple globs", () => {
let files = paths.map((path) => ({ deep: { path }}));
let filter = createFilter({ getPath },
let filter = createFilter({ map },
"**/*.txt",
"**/*.png",
);
let result = files.filter(filter);

function getPath (file) {
function map (file) {
return file.deep.path;
}

Expand All @@ -48,11 +48,11 @@ describe("options.getPath", () => {
it("should filter custom file objects with separate include/exclude criteria", () => {
let files = paths.map((path) => ({ path }));

function getPath (file) {
function map (file) {
return file.path;
}

let filter = createFilter({ getPath },
let filter = createFilter({ map },
{
include: [
/\.html$/,
Expand All @@ -76,18 +76,18 @@ describe("options.getPath", () => {
]);
});

it("should filter pass all arguments to the getPath() function", () => {
it("should filter pass all arguments to the map() function", () => {
let files = paths.map((path) => ({ path }));
let random = Math.random();
let now = new Date();

function getPath (num, date, file) {
function map (num, date, file) {
expect(num).to.equal(random);
expect(date.toISOString()).to.equal(now.toISOString());
return file.path;
}

let filter = createFilter({ getPath }, "**/*.html", "!**/blog/**");
let filter = createFilter({ map }, "**/*.html", "!**/blog/**");
let result = files.filter((file) => filter(random, now, file));

expect(result).to.deep.equal([
Expand All @@ -102,11 +102,11 @@ describe("options.getPath", () => {
let random = Math.random();
let now = new Date();

function getPath (file) {
function map (file) {
return file.path;
}

let filter = createFilter({ getPath },
let filter = createFilter({ map },
{
include (num, date, file) {
expect(num).to.equal(random);
Expand Down

0 comments on commit 0ca077c

Please sign in to comment.