Skip to content

Commit

Permalink
refactor: drop transform option
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmlnc committed Jun 9, 2019
1 parent 2af2623 commit 111dbf1
Show file tree
Hide file tree
Showing 11 changed files with 7 additions and 124 deletions.
32 changes: 0 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,38 +291,6 @@ Allow glob patterns without slashes to match a file path based on its basename.
Suppress any errors from reader. Works only with Node.js 10.10+.
Can be useful when the directory has entries with a special level of access.

#### transform

* Type: `Function`
* Default: `null`

Allows you to transform a path or `fs.Stats` object before sending to the array.

```js
const fg = require('fast-glob');

const entries1 = fg.sync(['**/*.scss']);
const entries2 = fg.sync(['**/*.scss'], { transform: (entry) => '_' + entry });

console.log(entries1); // ['a.scss', 'b.scss']
console.log(entries2); // ['_a.scss', '_b.scss']
```

If you are using **TypeScript**, you probably want to specify your own type of the returned array.

```ts
import * as fg from 'fast-glob';

interface IMyOwnEntry {
path: string;
}

const entries: IMyOwnEntry[] = fg.sync<IMyOwnEntry>(['*.md'], {
transform: (entry) => typeof entry === 'string' ? { path: entry } : { path: entry.path }
// Will throw compilation error for non-IMyOwnEntry types (boolean, for example)
});
```

#### fs

* Type: `FileSystemAdapter`
Expand Down
3 changes: 1 addition & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import ProviderAsync from './providers/async';
import Provider from './providers/provider';
import ProviderStream from './providers/stream';
import ProviderSync from './providers/sync';
import Settings, { Options, TransformFunction } from './settings';
import Settings, { Options } from './settings';
import { EntryItem, Pattern } from './types/index';
import * as utils from './utils/index';

Expand Down Expand Up @@ -78,7 +78,6 @@ export {

Options,
Settings,
TransformFunction,
Task,
EntryItem
};
11 changes: 0 additions & 11 deletions src/providers/async.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,6 @@ describe('Providers → ProviderAsync', () => {
assert.deepStrictEqual(actual, expected);
});

it('should call the transform function when it is presented', async () => {
const transform = sinon.stub();
const provider = getProvider({ transform });
const task = tests.task.builder().base('.').positive('*').build();
const entry = tests.entry.builder().path('root/file.txt').file().build();

await getEntries(provider, task, entry);

assert.strictEqual(transform.callCount, 1);
});

it('should throw error', async () => {
const provider = getProvider();
const task = tests.task.builder().base('.').positive('*').build();
Expand Down
11 changes: 0 additions & 11 deletions src/providers/stream.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,6 @@ describe('Providers → ProviderStream', () => {
assert.deepStrictEqual(actual, expected);
});

it('should call the transform function when it is presented', async () => {
const transform = sinon.stub();
const provider = getProvider({ transform });
const task = tests.task.builder().base('.').positive('*').build();
const entry = tests.entry.builder().path('root/file.txt').file().build();

await getEntries(provider, task, entry);

assert.strictEqual(transform.callCount, 1);
});

it('should emit error to the transform stream', (done) => {
const provider = getProvider();
const task = tests.task.builder().base('.').positive('*').build();
Expand Down
13 changes: 0 additions & 13 deletions src/providers/sync.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,5 @@ describe('Providers → ProviderSync', () => {
assert.strictEqual(provider.reader.static.callCount, 1);
assert.deepStrictEqual(actual, expected);
});

it('should call the transform function when it is presented', () => {
const transform = sinon.stub();
const provider = getProvider({ transform });
const task = tests.task.builder().base('.').positive('*').build();
const entry = tests.entry.builder().path('root/file.txt').file().build();

provider.reader.dynamic.returns([entry]);

provider.read(task);

assert.strictEqual(transform.callCount, 1);
});
});
});
11 changes: 0 additions & 11 deletions src/providers/transformers/entry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,6 @@ describe('Providers → Transformers → Entry', () => {
assert.deepEqual(actual, expected);
});

it('should apply custom transform function', () => {
const transformer = getTransformer({ transform: () => 'cake' });
const entry = tests.entry.builder().path('root/file.txt').file().build();

const expected = 'cake';

const actual = transformer(entry);

assert.strictEqual(actual, expected);
});

it('should return entry with absolute filepath when the `absolute` option is enabled', () => {
const transformer = getTransformer({ absolute: true });
const entry = tests.entry.builder().path('root/file.txt').file().build();
Expand Down
8 changes: 1 addition & 7 deletions src/providers/transformers/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@ export default class EntryTransformer {

entry.path = utils.path.unixify(entry.path);

const item = this._settings.stats ? entry : entry.path;

if (this._settings.transform === null) {
return item;
}

return this._settings.transform(item);
return this._settings.stats ? entry : entry.path;
}
}
1 change: 0 additions & 1 deletion src/settings.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ describe('Settings', () => {
assert.ok(settings.extglob);
assert.ok(settings.caseSensitiveMatch);
assert.ok(!settings.matchBase);
assert.strictEqual(settings.transform, null);
assert.ok(!settings.suppressErrors);
assert.deepStrictEqual(settings.fs, DEFAULT_FILE_SYSTEM_ADAPTER);
});
Expand Down
11 changes: 2 additions & 9 deletions src/settings.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import * as fs from 'fs';

import { EntryItem, FileSystemAdapter, Pattern } from './types/index';

export type TransformFunction<T> = (entry: EntryItem) => T;
import { FileSystemAdapter, Pattern } from './types/index';

export const DEFAULT_FILE_SYSTEM_ADAPTER: FileSystemAdapter = {
lstat: fs.lstat,
Expand All @@ -13,7 +11,7 @@ export const DEFAULT_FILE_SYSTEM_ADAPTER: FileSystemAdapter = {
readdirSync: fs.readdirSync
};

export interface Options<T = EntryItem> {
export interface Options {
/**
* The maximum number of concurrent calls to `fs.readdir`.
*/
Expand Down Expand Up @@ -90,10 +88,6 @@ export interface Options<T = EntryItem> {
* For example, `a?b` would match the path `/xyz/123/acb`, but not `/xyz/acb/123`.
*/
matchBase?: boolean;
/**
* Allows you to transform a path or `fs.Stats` object before sending to the array.
*/
transform?: TransformFunction<T> | null;
/**
* Suppress any errors from reader.
* Can be useful when the directory has entries with a special level of access.
Expand Down Expand Up @@ -124,7 +118,6 @@ export default class Settings {
public readonly extglob: boolean = this._getValue(this._options.extglob, true);
public readonly caseSensitiveMatch: boolean = this._getValue(this._options.caseSensitiveMatch, true);
public readonly matchBase: boolean = this._getValue(this._options.matchBase, false);
public readonly transform: TransformFunction<EntryItem> | null = this._getValue(this._options.transform, null);
public readonly suppressErrors: boolean = this._getValue(this._options.suppressErrors, false);
public readonly fs: FileSystemAdapter = this._getFileSystemMethods(this._options.fs);

Expand Down
6 changes: 3 additions & 3 deletions src/tests/smoke/smoke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface SmokeTest {
ignore?: Pattern;
cwd?: string;
globOptions?: glob.IOptions;
fgOptions?: Options<string>;
fgOptions?: Options;
/**
* Allow to run only one test case with debug information.
*/
Expand Down Expand Up @@ -152,8 +152,8 @@ function getNodeGlobEntries(pattern: Pattern, ignore?: Pattern, cwd?: string, op
/**
* Return entries from the `fast-glob` package with sorting.
*/
function getFastGlobEntries(pattern: Pattern, ignore?: Pattern, cwd?: string, opts?: Options<string>): string[] {
const options: Options<string> = {
function getFastGlobEntries(pattern: Pattern, ignore?: Pattern, cwd?: string, opts?: Options): string[] {
const options: Options = {
cwd: cwd || process.cwd(),
ignore: ignore ? [ignore] : [],
onlyFiles: false,
Expand Down
24 changes: 0 additions & 24 deletions src/tests/smoke/static.smoke.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import * as assert from 'assert';

import { Entry } from '../../types/index';
import * as smoke from './smoke';

smoke.suite('Smoke → Static', [
Expand Down Expand Up @@ -66,27 +63,6 @@ smoke.suite('Smoke → Static (relative)', [
{ pattern: '../../file.md', cwd: 'fixtures/first/nested' }
]);

smoke.suite('Smoke → Static (stats)', [
{
issue: 144,
pattern: 'fixtures/file.md',
fgOptions: {
stats: true,
transform: (entry) => {
const obj = entry as Entry;

if (obj.stats === undefined) {
assert.fail('the `stats` property is not defined');
} else {
assert.ok(obj.stats.isFile());
}

return obj.path;
}
}
}
]);

smoke.suite('Smoke → Static (error)', [
{
pattern: 'non-exist.txt'
Expand Down

0 comments on commit 111dbf1

Please sign in to comment.