Skip to content

Commit

Permalink
feat: introduce objectMode option
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmlnc committed Jun 9, 2019
1 parent 111dbf1 commit a505273
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 16 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,13 @@ An array of glob patterns to exclude matches.

Allow patterns to match filenames starting with a period (files & directories), even if the pattern does not explicitly have a period in that spot.

#### objectMode

* Type: `boolean`
* Default: `false`

Return an [`Entry`](#entry) object instead of filepath.

#### stats

* Type: `boolean`
Expand Down
15 changes: 13 additions & 2 deletions src/providers/transformers/entry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,26 @@ describe('Providers → Transformers → Entry', () => {
assert.strictEqual(actual, expected);
});

it('should return transformed entry as object when the `objectMode` option is enabled', () => {
const transformer = getTransformer({ objectMode: true });
const entry = tests.entry.builder().path('root/file.txt').file().build();

const expected = entry;

const actual = transformer(entry);

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

it('should return transformed entry as object when the `stats` option is enabled', () => {
const transformer = getTransformer({ stats: true });
const entry = tests.entry.builder().path('root/file.txt').file().build();
const entry = tests.entry.builder().path('root/file.txt').file().stats().build();

const expected = entry;

const actual = transformer(entry);

assert.deepEqual(actual, expected);
assert.deepStrictEqual(actual, expected);
});

it('should return entry with absolute filepath when the `absolute` option is enabled', () => {
Expand Down
6 changes: 5 additions & 1 deletion src/providers/transformers/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export default class EntryTransformer {

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

return this._settings.stats ? entry : entry.path;
if (this._settings.objectMode) {
return entry;
}

return entry.path;
}
}
10 changes: 10 additions & 0 deletions src/settings.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe('Settings', () => {
assert.ok(settings.deep);
assert.deepStrictEqual(settings.ignore, []);
assert.ok(!settings.dot);
assert.ok(!settings.objectMode);
assert.ok(!settings.stats);
assert.ok(settings.onlyFiles);
assert.ok(!settings.onlyDirectories);
Expand Down Expand Up @@ -45,6 +46,15 @@ describe('Settings', () => {
assert.ok(settings.onlyDirectories);
});

it('should set the "objectMode" option when the "stats" is enabled', () => {
const settings = new Settings({
stats: true
});

assert.ok(settings.objectMode);
assert.ok(settings.stats);
});

it('should set the "throwErrorOnBrokenSymbolicLink" option to "true" when the "stats" option is enabled', () => {
const settings = new Settings({
stats: true
Expand Down
34 changes: 21 additions & 13 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ export interface Options {
* even if the pattern does not explicitly have a period in that spot.
*/
dot?: boolean;
/**
* Return `Entry` object instead of filepath.
*/
objectMode?: boolean;
/**
* Return `fs.Stats` with `path` property instead of file path.
*/
Expand Down Expand Up @@ -100,31 +104,35 @@ export interface Options {
}

export default class Settings {
public readonly cwd: string = this._getValue(this._options.cwd, process.cwd());
public readonly absolute: boolean = this._getValue(this._options.absolute, false);
public readonly braceExpansion: boolean = this._getValue(this._options.braceExpansion, true);
public readonly caseSensitiveMatch: boolean = this._getValue(this._options.caseSensitiveMatch, true);
public readonly concurrency: number = this._getValue(this._options.concurrency, Infinity);
public readonly cwd: string = this._getValue(this._options.cwd, process.cwd());
public readonly deep: number | boolean = this._getValue(this._options.deep, true);
public readonly ignore: Pattern[] = this._getValue(this._options.ignore, [] as Pattern[]);
public readonly dot: boolean = this._getValue(this._options.dot, false);
public readonly stats: boolean = this._getValue(this._options.stats, false);
public readonly onlyFiles: boolean = this._getValue(this._options.onlyFiles, true);
public readonly onlyDirectories: boolean = this._getValue(this._options.onlyDirectories, false);
public readonly extglob: boolean = this._getValue(this._options.extglob, true);
public readonly followSymbolicLinks: boolean = this._getValue(this._options.followSymbolicLinks, true);
public readonly throwErrorOnBrokenSymbolicLink: boolean = this.stats && this._getValue(this._options.throwErrorOnBrokenSymbolicLink, true);
public readonly unique: boolean = this._getValue(this._options.unique, true);
public readonly markDirectories: boolean = this._getValue(this._options.markDirectories, false);
public readonly absolute: boolean = this._getValue(this._options.absolute, false);
public readonly braceExpansion: boolean = this._getValue(this._options.braceExpansion, true);
public readonly fs: FileSystemAdapter = this._getFileSystemMethods(this._options.fs);
public readonly globstar: boolean = this._getValue(this._options.globstar, true);
public readonly extglob: boolean = this._getValue(this._options.extglob, true);
public readonly caseSensitiveMatch: boolean = this._getValue(this._options.caseSensitiveMatch, true);
public readonly ignore: Pattern[] = this._getValue(this._options.ignore, [] as Pattern[]);
public readonly markDirectories: boolean = this._getValue(this._options.markDirectories, false);
public readonly matchBase: boolean = this._getValue(this._options.matchBase, false);
public readonly objectMode: boolean = this._getValue(this._options.objectMode, false);
public readonly onlyDirectories: boolean = this._getValue(this._options.onlyDirectories, false);
public readonly onlyFiles: boolean = this._getValue(this._options.onlyFiles, true);
public readonly stats: boolean = this._getValue(this._options.stats, false);
public readonly suppressErrors: boolean = this._getValue(this._options.suppressErrors, false);
public readonly fs: FileSystemAdapter = this._getFileSystemMethods(this._options.fs);
public readonly throwErrorOnBrokenSymbolicLink: boolean = this.stats && this._getValue(this._options.throwErrorOnBrokenSymbolicLink, true);
public readonly unique: boolean = this._getValue(this._options.unique, true);

constructor(private readonly _options: Options = {}) {
if (this.onlyDirectories) {
this.onlyFiles = false;
}
if (this.stats) {
this.objectMode = true;
}
}

private _getValue<T>(option: T | undefined, value: T): T {
Expand Down

0 comments on commit a505273

Please sign in to comment.