tiny-readdir 2.6.0 → 2.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Dirent, Options, Result } from './types';
1
+ import type { Dirent, Options, ResultDirectory, ResultDirectories, Result } from './types';
2
2
  declare const readdir: (rootPath: string, options?: Options) => Promise<Result>;
3
3
  export default readdir;
4
- export type { Dirent, Options, Result };
4
+ export type { Dirent, Options, ResultDirectory, ResultDirectories, Result };
package/dist/index.js CHANGED
@@ -2,15 +2,16 @@
2
2
  import fs from 'node:fs';
3
3
  import path from 'node:path';
4
4
  import { NOOP_PROMISE_LIKE } from './constants.js';
5
- import { isFunction, makeCounterPromise } from './utils.js';
5
+ import { castArray, isFunction, makeCounterPromise } from './utils.js';
6
6
  /* MAIN */
7
7
  //TODO: Streamline the type of dirnmaps
8
8
  const readdir = (rootPath, options) => {
9
9
  const followSymlinks = options?.followSymlinks ?? false;
10
10
  const maxDepth = options?.depth ?? Infinity;
11
11
  const maxPaths = options?.limit ?? Infinity;
12
- const ignore = options?.ignore ?? (() => false);
13
- const isIgnored = isFunction(ignore) ? ignore : (targetPath) => ignore.test(targetPath);
12
+ const ignore = options?.ignore ?? [];
13
+ const ignores = castArray(ignore).map(ignore => isFunction(ignore) ? ignore : (targetPath) => ignore.test(targetPath));
14
+ const isIgnored = (targetPath) => ignores.some(ignore => ignore(targetPath));
14
15
  const signal = options?.signal ?? { aborted: false };
15
16
  const onDirents = options?.onDirents || (() => { });
16
17
  const directories = [];
package/dist/types.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  type Callback = () => void;
2
+ type ArrayMaybe<T> = T[] | T;
2
3
  type PromiseMaybe<T> = Promise<T> | T;
3
4
  type Dirent = {
4
5
  isFile: () => boolean;
@@ -15,7 +16,7 @@ type Options = {
15
16
  depth?: number;
16
17
  limit?: number;
17
18
  followSymlinks?: boolean;
18
- ignore?: ((targetPath: string) => boolean) | RegExp;
19
+ ignore?: ArrayMaybe<((targetPath: string) => boolean) | RegExp>;
19
20
  signal?: {
20
21
  aborted: boolean;
21
22
  };
package/dist/utils.d.ts CHANGED
@@ -1,8 +1,9 @@
1
1
  import type { Callback } from './types';
2
+ declare const castArray: <T>(value: T | T[]) => T[];
2
3
  declare const isFunction: (value: unknown) => value is Function;
3
4
  declare const makeCounterPromise: () => {
4
5
  promise: Promise<void>;
5
6
  increment: Callback;
6
7
  decrement: Callback;
7
8
  };
8
- export { isFunction, makeCounterPromise };
9
+ export { castArray, isFunction, makeCounterPromise };
package/dist/utils.js CHANGED
@@ -1,6 +1,9 @@
1
1
  /* IMPORT */
2
2
  import makeNakedPromise from 'promise-make-naked';
3
3
  /* MAIN */
4
+ const castArray = (value) => {
5
+ return Array.isArray(value) ? value : [value];
6
+ };
4
7
  const isFunction = (value) => {
5
8
  return (typeof value === 'function');
6
9
  };
@@ -19,4 +22,4 @@ const makeCounterPromise = () => {
19
22
  return { promise, increment, decrement };
20
23
  };
21
24
  /* EXPORT */
22
- export { isFunction, makeCounterPromise };
25
+ export { castArray, isFunction, makeCounterPromise };
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "tiny-readdir",
3
3
  "repository": "github:fabiospampinato/tiny-readdir",
4
4
  "description": "A simple promisified recursive readdir function.",
5
- "version": "2.6.0",
5
+ "version": "2.7.1",
6
6
  "type": "module",
7
7
  "main": "dist/index.js",
8
8
  "exports": "./dist/index.js",
package/src/index.ts CHANGED
@@ -4,7 +4,7 @@
4
4
  import fs from 'node:fs';
5
5
  import path from 'node:path';
6
6
  import {NOOP_PROMISE_LIKE} from './constants';
7
- import {isFunction, makeCounterPromise} from './utils';
7
+ import {castArray, isFunction, makeCounterPromise} from './utils';
8
8
  import type {Dirent, Options, ResultDirectory, ResultDirectories, Result} from './types';
9
9
 
10
10
  /* MAIN */
@@ -16,8 +16,9 @@ const readdir = ( rootPath: string, options?: Options ): Promise<Result> => {
16
16
  const followSymlinks = options?.followSymlinks ?? false;
17
17
  const maxDepth = options?.depth ?? Infinity;
18
18
  const maxPaths = options?.limit ?? Infinity;
19
- const ignore = options?.ignore ?? (() => false);
20
- const isIgnored = isFunction ( ignore ) ? ignore : ( targetPath: string ) => ignore.test ( targetPath );
19
+ const ignore = options?.ignore ?? [];
20
+ const ignores = castArray ( ignore ).map ( ignore => isFunction ( ignore ) ? ignore : ( targetPath: string ) => ignore.test ( targetPath ) );
21
+ const isIgnored = ( targetPath: string ) => ignores.some ( ignore => ignore ( targetPath ) );
21
22
  const signal = options?.signal ?? { aborted: false };
22
23
  const onDirents = options?.onDirents || (() => {});
23
24
  const directories: string[] = [];
@@ -252,4 +253,4 @@ const readdir = ( rootPath: string, options?: Options ): Promise<Result> => {
252
253
  /* EXPORT */
253
254
 
254
255
  export default readdir;
255
- export type {Dirent, Options, Result};
256
+ export type {Dirent, Options, ResultDirectory, ResultDirectories, Result};
package/src/types.ts CHANGED
@@ -3,6 +3,8 @@
3
3
 
4
4
  type Callback = () => void;
5
5
 
6
+ type ArrayMaybe<T> = T[] | T;
7
+
6
8
  type PromiseMaybe<T> = Promise<T> | T;
7
9
 
8
10
  /* MAIN */
@@ -23,7 +25,7 @@ type Options = {
23
25
  depth?: number,
24
26
  limit?: number,
25
27
  followSymlinks?: boolean,
26
- ignore?: (( targetPath: string ) => boolean) | RegExp,
28
+ ignore?: ArrayMaybe<(( targetPath: string ) => boolean) | RegExp>,
27
29
  signal?: { aborted: boolean },
28
30
  onDirents?: ( dirents: Dirent[] ) => PromiseMaybe<undefined>
29
31
  };
package/src/utils.ts CHANGED
@@ -6,6 +6,12 @@ import type {Callback} from './types';
6
6
 
7
7
  /* MAIN */
8
8
 
9
+ const castArray = <T> ( value: T[] | T ): T[] => {
10
+
11
+ return Array.isArray ( value ) ? value : [value];
12
+
13
+ };
14
+
9
15
  const isFunction = ( value: unknown ): value is Function => {
10
16
 
11
17
  return ( typeof value === 'function' );
@@ -40,4 +46,4 @@ const makeCounterPromise = (): { promise: Promise<void>, increment: Callback, de
40
46
 
41
47
  /* EXPORT */
42
48
 
43
- export {isFunction, makeCounterPromise};
49
+ export {castArray, isFunction, makeCounterPromise};