diff --git a/types/klaw-sync/index.d.ts b/types/klaw-sync/index.d.ts index 1bdd409dfc..f222804222 100644 --- a/types/klaw-sync/index.d.ts +++ b/types/klaw-sync/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for klaw-sync 1.1 +// Type definitions for klaw-sync 2.0 // Project: https://github.com/manidlou/node-klaw-sync // Definitions by: Brendan Forster // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped @@ -6,35 +6,50 @@ /// import * as fs from 'fs' - declare namespace klawSync { - interface Item { - path: string - stats: fs.Stats - } + interface Item { + path: string + stats: fs.Stats + } - interface Options { - /** - * any paths or `micromatch` patterns to ignore. - * - * For more information on micromatch patterns: https://github.com/jonschlinkert/micromatch#features - */ - ignore?: string | string[] - /** - * True to only return files (ignore directories). - * - * Defaults to false if not specified. - */ - nodir?: boolean - /** - * True to only return directories (ignore files). - * - * Defaults to false if not specified. - */ - nofile?: boolean - } + type Filter = (item: Item) => boolean + + interface Options { + /** + * @description True to only return files (ignore directories). + * Defaults to false if not specified. + * @default false + */ + nodir?: boolean + + /** + * @description True to only return directories (ignore files). + * Defaults to false if not specified. + * @default false + */ + nofile?: boolean + + /** + * @description when filter function is used, the default behavior is to read all directories even + * if they don't pass the filter function (won't be included but still will be traversed). + * If you set true, there will be neither inclusion nor traversal for directories that + * don't pass the filter function + * @since v2.0.0 + */ + noRecurseOnFailedFilter?: boolean + + /** + * @description function that gets one argument fn({path: '', stats: {}}) and returns true to include + * or false to exclude the item + * @since v2.0.0 + */ + filter?: Filter + } } -declare function klawSync(root: string, options?: klawSync.Options): ReadonlyArray +declare function klawSync( + root: string, + options?: klawSync.Options, +): ReadonlyArray export = klawSync diff --git a/types/klaw-sync/klaw-sync-tests.ts b/types/klaw-sync/klaw-sync-tests.ts index 4808146882..5875c528fd 100644 --- a/types/klaw-sync/klaw-sync-tests.ts +++ b/types/klaw-sync/klaw-sync-tests.ts @@ -2,7 +2,7 @@ import * as path from 'path' import klawSync = require('klaw-sync') const outputMessage = (result: klawSync.Item) => { - console.log(`file: ${result.path} has size '${result.stats.size}'`) + console.log(`file: ${result.path} has size '${result.stats.size}'`) } klawSync('/some/dir').forEach(outputMessage) @@ -12,9 +12,12 @@ const defaultOptions = {} klawSync('/some/dir', defaultOptions).forEach(outputMessage) const options = { - ignore: ['.exe'], - nodir: true, - nofile: false, + nodir: true, + nofile: false, + noRecurseOnFailedFilter: false, + filter(item: klawSync.Item) { + return item.path.indexOf('node_modules') < 0 + }, } klawSync('/some/dir', options).forEach(outputMessage)