From a50527374eda58976df86f264fc7550aabfeb15e Mon Sep 17 00:00:00 2001 From: mrmlnc Date: Tue, 4 Jun 2019 20:51:25 +0300 Subject: [PATCH] feat: introduce `objectMode` option --- README.md | 7 +++++ src/providers/transformers/entry.spec.ts | 15 +++++++++-- src/providers/transformers/entry.ts | 6 ++++- src/settings.spec.ts | 10 +++++++ src/settings.ts | 34 +++++++++++++++--------- 5 files changed, 56 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index c4ab4a6d..31429d78 100644 --- a/README.md +++ b/README.md @@ -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` diff --git a/src/providers/transformers/entry.spec.ts b/src/providers/transformers/entry.spec.ts index d6678327..1087cb32 100644 --- a/src/providers/transformers/entry.spec.ts +++ b/src/providers/transformers/entry.spec.ts @@ -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', () => { diff --git a/src/providers/transformers/entry.ts b/src/providers/transformers/entry.ts index 8ce4067d..02562707 100644 --- a/src/providers/transformers/entry.ts +++ b/src/providers/transformers/entry.ts @@ -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; } } diff --git a/src/settings.spec.ts b/src/settings.spec.ts index 13803c3e..acde8376 100644 --- a/src/settings.spec.ts +++ b/src/settings.spec.ts @@ -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); @@ -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 diff --git a/src/settings.ts b/src/settings.ts index f74cb78f..f7022cd1 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -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. */ @@ -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(option: T | undefined, value: T): T {