tiny-readdir 2.2.1 → 2.3.0

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.js CHANGED
@@ -11,22 +11,27 @@ const readdir = (rootPath, options) => {
11
11
  const isIgnored = isFunction(ignore) ? ignore : (targetPath) => ignore.test(targetPath);
12
12
  const signal = options?.signal ?? { aborted: false };
13
13
  const directories = [];
14
+ const directoriesNames = new Set();
14
15
  const files = [];
16
+ const filesNames = new Set();
15
17
  const symlinks = [];
18
+ const symlinksNames = new Set();
16
19
  const map = {};
17
20
  const visited = new Set();
18
- const resultEmpty = { directories: [], files: [], symlinks: [], map: {} };
19
- const result = { directories, files, symlinks, map };
21
+ const resultEmpty = { directories: [], directoriesNames: new Set(), files: [], filesNames: new Set(), symlinks: [], symlinksNames: new Set(), map: {} };
22
+ const result = { directories, directoriesNames, files, filesNames, symlinks, symlinksNames, map };
20
23
  const { promise, increment, decrement } = makeCounterPromise();
21
24
  let foundPaths = 0;
22
- const handleDirectory = (dirmap, subPath, depth) => {
25
+ const handleDirectory = (dirmap, subPath, name, depth) => {
23
26
  if (visited.has(subPath))
24
27
  return;
25
28
  if (foundPaths >= maxPaths)
26
29
  return;
27
30
  foundPaths += 1;
28
31
  dirmap.directories.push(subPath);
32
+ dirmap.directoriesNames.add(name);
29
33
  directories.push(subPath);
34
+ directoriesNames.add(name);
30
35
  visited.add(subPath);
31
36
  if (depth >= maxDepth)
32
37
  return;
@@ -34,24 +39,28 @@ const readdir = (rootPath, options) => {
34
39
  return;
35
40
  populateResultFromPath(subPath, depth + 1);
36
41
  };
37
- const handleFile = (dirmap, subPath) => {
42
+ const handleFile = (dirmap, subPath, name) => {
38
43
  if (visited.has(subPath))
39
44
  return;
40
45
  if (foundPaths >= maxPaths)
41
46
  return;
42
47
  foundPaths += 1;
43
48
  dirmap.files.push(subPath);
49
+ dirmap.filesNames.add(name);
44
50
  files.push(subPath);
51
+ filesNames.add(name);
45
52
  visited.add(subPath);
46
53
  };
47
- const handleSymlink = (dirmap, subPath, depth) => {
54
+ const handleSymlink = (dirmap, subPath, name, depth) => {
48
55
  if (visited.has(subPath))
49
56
  return;
50
57
  if (foundPaths >= maxPaths)
51
58
  return;
52
59
  foundPaths += 1;
53
60
  dirmap.symlinks.push(subPath);
61
+ dirmap.symlinksNames.add(name);
54
62
  symlinks.push(subPath);
63
+ symlinksNames.add(name);
55
64
  visited.add(subPath);
56
65
  if (!followSymlinks)
57
66
  return;
@@ -61,36 +70,37 @@ const readdir = (rootPath, options) => {
61
70
  return;
62
71
  populateResultFromSymlink(subPath, depth + 1);
63
72
  };
64
- const handleStat = (dirmap, rootPath, stat, depth) => {
73
+ const handleStat = (dirmap, rootPath, name, stat, depth) => {
65
74
  if (signal.aborted)
66
75
  return;
67
76
  if (isIgnored(rootPath))
68
77
  return;
69
78
  if (stat.isDirectory()) {
70
- handleDirectory(dirmap, rootPath, depth);
79
+ handleDirectory(dirmap, rootPath, name, depth);
71
80
  }
72
81
  else if (stat.isFile()) {
73
- handleFile(dirmap, rootPath);
82
+ handleFile(dirmap, rootPath, name);
74
83
  }
75
84
  else if (stat.isSymbolicLink()) {
76
- handleSymlink(dirmap, rootPath, depth);
85
+ handleSymlink(dirmap, rootPath, name, depth);
77
86
  }
78
87
  };
79
88
  const handleDirent = (dirmap, rootPath, dirent, depth) => {
80
89
  if (signal.aborted)
81
90
  return;
82
91
  const separator = (rootPath === path.sep) ? '' : path.sep;
83
- const subPath = `${rootPath}${separator}${dirent.name}`;
92
+ const name = dirent.name;
93
+ const subPath = `${rootPath}${separator}${name}`;
84
94
  if (isIgnored(subPath))
85
95
  return;
86
96
  if (dirent.isDirectory()) {
87
- handleDirectory(dirmap, subPath, depth);
97
+ handleDirectory(dirmap, subPath, name, depth);
88
98
  }
89
99
  else if (dirent.isFile()) {
90
- handleFile(dirmap, subPath);
100
+ handleFile(dirmap, subPath, name);
91
101
  }
92
102
  else if (dirent.isSymbolicLink()) {
93
- handleSymlink(dirmap, subPath, depth);
103
+ handleSymlink(dirmap, subPath, name, depth);
94
104
  }
95
105
  };
96
106
  const handleDirents = (dirmap, rootPath, dirents, depth) => {
@@ -113,7 +123,7 @@ const readdir = (rootPath, options) => {
113
123
  return decrement();
114
124
  if (!dirents.length)
115
125
  return decrement();
116
- const dirmap = map[rootPath] = { directories: [], files: [], symlinks: [] };
126
+ const dirmap = map[rootPath] = { directories: [], directoriesNames: new Set(), files: [], filesNames: new Set(), symlinks: [], symlinksNames: new Set() };
117
127
  handleDirents(dirmap, rootPath, dirents, depth);
118
128
  decrement();
119
129
  });
@@ -130,8 +140,9 @@ const readdir = (rootPath, options) => {
130
140
  return decrement();
131
141
  if (signal.aborted)
132
142
  return decrement();
133
- const dirmap = map[rootPath] = { directories: [], files: [], symlinks: [] };
134
- handleStat(dirmap, realPath, stat, depth);
143
+ const name = path.basename(realPath);
144
+ const dirmap = map[rootPath] = { directories: [], directoriesNames: new Set(), files: [], filesNames: new Set(), symlinks: [], symlinksNames: new Set() };
145
+ handleStat(dirmap, realPath, name, stat, depth);
135
146
  decrement();
136
147
  });
137
148
  });
package/dist/types.d.ts CHANGED
@@ -10,8 +10,11 @@ type Options = {
10
10
  };
11
11
  type ResultDirectory = {
12
12
  directories: string[];
13
+ directoriesNames: Set<string>;
13
14
  files: string[];
15
+ filesNames: Set<string>;
14
16
  symlinks: string[];
17
+ symlinksNames: Set<string>;
15
18
  };
16
19
  type ResultDirectories = {
17
20
  [path: string]: ResultDirectory;
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.2.1",
5
+ "version": "2.3.0",
6
6
  "type": "module",
7
7
  "main": "dist/index.js",
8
8
  "exports": "./dist/index.js",
package/readme.md CHANGED
@@ -27,6 +27,10 @@ console.log ( result.directories ); // => Array of absolute paths pointing to di
27
27
  console.log ( result.files ); // => Array of absolute paths pointing to files
28
28
  console.log ( result.symlinks ); // => Array of absolute paths pointing to symlinks
29
29
 
30
+ console.log ( result.directoriesNames ); // => Set of directories names found
31
+ console.log ( result.filesNames ); // => Set of files name found
32
+ console.log ( result.symlinksNames ); // => Set of symlinks names found
33
+
30
34
  setTimeout ( () => aborter.abort (), 10000 ); // Aborting if it's going to take longer than 10s
31
35
  ```
32
36
 
package/src/index.ts CHANGED
@@ -17,17 +17,20 @@ const readdir = ( rootPath: string, options?: Options ): Promise<Result> => {
17
17
  const isIgnored = isFunction ( ignore ) ? ignore : ( targetPath: string ) => ignore.test ( targetPath );
18
18
  const signal = options?.signal ?? { aborted: false };
19
19
  const directories: string[] = [];
20
+ const directoriesNames: Set<string> = new Set ();
20
21
  const files: string[] = [];
22
+ const filesNames: Set<string> = new Set ();
21
23
  const symlinks: string[] = [];
24
+ const symlinksNames: Set<string> = new Set ();
22
25
  const map: ResultDirectories = {};
23
26
  const visited = new Set<string> ();
24
- const resultEmpty: Result = { directories: [], files: [], symlinks: [], map: {} };
25
- const result: Result = { directories, files, symlinks, map };
27
+ const resultEmpty: Result = { directories: [], directoriesNames: new Set (), files: [], filesNames: new Set (), symlinks: [], symlinksNames: new Set (), map: {} };
28
+ const result: Result = { directories, directoriesNames, files, filesNames, symlinks, symlinksNames, map };
26
29
  const {promise, increment, decrement} = makeCounterPromise ();
27
30
 
28
31
  let foundPaths = 0;
29
32
 
30
- const handleDirectory = ( dirmap: ResultDirectory, subPath: string, depth: number ): void => {
33
+ const handleDirectory = ( dirmap: ResultDirectory, subPath: string, name: string, depth: number ): void => {
31
34
 
32
35
  if ( visited.has ( subPath ) ) return;
33
36
 
@@ -35,7 +38,9 @@ const readdir = ( rootPath: string, options?: Options ): Promise<Result> => {
35
38
 
36
39
  foundPaths += 1;
37
40
  dirmap.directories.push ( subPath );
41
+ dirmap.directoriesNames.add ( name );
38
42
  directories.push ( subPath );
43
+ directoriesNames.add ( name );
39
44
  visited.add ( subPath );
40
45
 
41
46
  if ( depth >= maxDepth ) return;
@@ -46,7 +51,7 @@ const readdir = ( rootPath: string, options?: Options ): Promise<Result> => {
46
51
 
47
52
  };
48
53
 
49
- const handleFile = ( dirmap: ResultDirectory, subPath: string ): void => {
54
+ const handleFile = ( dirmap: ResultDirectory, subPath: string, name: string ): void => {
50
55
 
51
56
  if ( visited.has ( subPath ) ) return;
52
57
 
@@ -54,12 +59,14 @@ const readdir = ( rootPath: string, options?: Options ): Promise<Result> => {
54
59
 
55
60
  foundPaths += 1;
56
61
  dirmap.files.push ( subPath );
62
+ dirmap.filesNames.add ( name );
57
63
  files.push ( subPath );
64
+ filesNames.add ( name );
58
65
  visited.add ( subPath );
59
66
 
60
67
  };
61
68
 
62
- const handleSymlink = ( dirmap: ResultDirectory, subPath: string, depth: number ): void => {
69
+ const handleSymlink = ( dirmap: ResultDirectory, subPath: string, name: string, depth: number ): void => {
63
70
 
64
71
  if ( visited.has ( subPath ) ) return;
65
72
 
@@ -67,7 +74,9 @@ const readdir = ( rootPath: string, options?: Options ): Promise<Result> => {
67
74
 
68
75
  foundPaths += 1;
69
76
  dirmap.symlinks.push ( subPath );
77
+ dirmap.symlinksNames.add ( name );
70
78
  symlinks.push ( subPath );
79
+ symlinksNames.add ( name );
71
80
  visited.add ( subPath );
72
81
 
73
82
  if ( !followSymlinks ) return;
@@ -80,7 +89,7 @@ const readdir = ( rootPath: string, options?: Options ): Promise<Result> => {
80
89
 
81
90
  };
82
91
 
83
- const handleStat = ( dirmap: ResultDirectory, rootPath: string, stat: fs.Stats, depth: number ): void => {
92
+ const handleStat = ( dirmap: ResultDirectory, rootPath: string, name: string, stat: fs.Stats, depth: number ): void => {
84
93
 
85
94
  if ( signal.aborted ) return;
86
95
 
@@ -88,15 +97,15 @@ const readdir = ( rootPath: string, options?: Options ): Promise<Result> => {
88
97
 
89
98
  if ( stat.isDirectory () ) {
90
99
 
91
- handleDirectory ( dirmap, rootPath, depth );
100
+ handleDirectory ( dirmap, rootPath, name, depth );
92
101
 
93
102
  } else if ( stat.isFile () ) {
94
103
 
95
- handleFile ( dirmap, rootPath );
104
+ handleFile ( dirmap, rootPath, name );
96
105
 
97
106
  } else if ( stat.isSymbolicLink () ) {
98
107
 
99
- handleSymlink ( dirmap, rootPath, depth );
108
+ handleSymlink ( dirmap, rootPath, name, depth );
100
109
 
101
110
  }
102
111
 
@@ -107,21 +116,22 @@ const readdir = ( rootPath: string, options?: Options ): Promise<Result> => {
107
116
  if ( signal.aborted ) return;
108
117
 
109
118
  const separator = ( rootPath === path.sep ) ? '' : path.sep;
110
- const subPath = `${rootPath}${separator}${dirent.name}`;
119
+ const name = dirent.name;
120
+ const subPath = `${rootPath}${separator}${name}`;
111
121
 
112
122
  if ( isIgnored ( subPath ) ) return;
113
123
 
114
124
  if ( dirent.isDirectory () ) {
115
125
 
116
- handleDirectory ( dirmap, subPath, depth );
126
+ handleDirectory ( dirmap, subPath, name, depth );
117
127
 
118
128
  } else if ( dirent.isFile () ) {
119
129
 
120
- handleFile ( dirmap, subPath );
130
+ handleFile ( dirmap, subPath, name );
121
131
 
122
132
  } else if ( dirent.isSymbolicLink () ) {
123
133
 
124
- handleSymlink ( dirmap, subPath, depth );
134
+ handleSymlink ( dirmap, subPath, name, depth );
125
135
 
126
136
  }
127
137
 
@@ -155,7 +165,7 @@ const readdir = ( rootPath: string, options?: Options ): Promise<Result> => {
155
165
 
156
166
  if ( !dirents.length ) return decrement ();
157
167
 
158
- const dirmap = map[rootPath] = { directories: [], files: [], symlinks: [] };
168
+ const dirmap = map[rootPath] = { directories: [], directoriesNames: new Set (), files: [], filesNames: new Set (), symlinks: [], symlinksNames: new Set () };
159
169
 
160
170
  handleDirents ( dirmap, rootPath, dirents, depth );
161
171
 
@@ -181,9 +191,10 @@ const readdir = ( rootPath: string, options?: Options ): Promise<Result> => {
181
191
 
182
192
  if ( signal.aborted ) return decrement ();
183
193
 
184
- const dirmap = map[rootPath] = { directories: [], files: [], symlinks: [] };
194
+ const name = path.basename ( realPath );
195
+ const dirmap = map[rootPath] = { directories: [], directoriesNames: new Set (), files: [], filesNames: new Set (), symlinks: [], symlinksNames: new Set () };
185
196
 
186
- handleStat ( dirmap, realPath, stat, depth );
197
+ handleStat ( dirmap, realPath, name, stat, depth );
187
198
 
188
199
  decrement ();
189
200
 
package/src/types.ts CHANGED
@@ -15,8 +15,11 @@ type Options = {
15
15
 
16
16
  type ResultDirectory = {
17
17
  directories: string[],
18
+ directoriesNames: Set<string>,
18
19
  files: string[],
19
- symlinks: string[]
20
+ filesNames: Set<string>,
21
+ symlinks: string[],
22
+ symlinksNames: Set<string>
20
23
  };
21
24
 
22
25
  type ResultDirectories = {
package/test/index.js CHANGED
@@ -6,6 +6,10 @@ import fs from 'node:fs';
6
6
  import path from 'node:path';
7
7
  import readdir from '../dist/index.js';
8
8
 
9
+ /* HELPERS */
10
+
11
+ const toBasename = filePath => path.basename ( filePath );
12
+
9
13
  /* MAIN */
10
14
 
11
15
  describe ( 'Tiny Readdir', it => {
@@ -41,43 +45,67 @@ describe ( 'Tiny Readdir', it => {
41
45
 
42
46
  const expected = {
43
47
  directories: [folder1Path, folder2Path, folder1DeepPath, root2Path],
48
+ directoriesNames: new Set ( [folder1Path, folder2Path, folder1DeepPath, root2Path].map ( toBasename ) ),
44
49
  files: [file1aPath, file1bPath, file2Path, fileDeep1Path],
50
+ filesNames: new Set ( [file1aPath, file1bPath, file2Path, fileDeep1Path].map ( toBasename ) ),
45
51
  symlinks: [symlink1FromPath, symlink2FromPath],
52
+ symlinksNames: new Set ( [symlink1FromPath, symlink2FromPath].map ( toBasename ) ),
46
53
  map: {
47
54
  [root1Path]: {
48
55
  directories: [folder1Path, folder2Path],
56
+ directoriesNames: new Set ( [folder1Path, folder2Path].map ( toBasename ) ),
49
57
  files: [],
50
- symlinks: [symlink1FromPath]
58
+ filesNames: new Set (),
59
+ symlinks: [symlink1FromPath],
60
+ symlinksNames: new Set ( [symlink1FromPath].map ( toBasename ) )
51
61
  },
52
62
  [root2Path]: {
53
63
  directories: [],
64
+ directoriesNames: new Set (),
54
65
  files: [],
55
- symlinks: [symlink2FromPath]
66
+ filesNames: new Set (),
67
+ symlinks: [symlink2FromPath],
68
+ symlinksNames: new Set ( [symlink2FromPath].map ( toBasename ) )
56
69
  },
57
70
  [folder1Path]: {
58
71
  directories: [folder1DeepPath],
72
+ directoriesNames: new Set ( [folder1DeepPath].map ( toBasename ) ),
59
73
  files: [file1aPath, file1bPath],
60
- symlinks: []
74
+ filesNames: new Set ( [file1aPath, file1bPath].map ( toBasename ) ),
75
+ symlinks: [],
76
+ symlinksNames: new Set ()
61
77
  },
62
78
  [folder2Path]: {
63
79
  directories: [],
80
+ directoriesNames: new Set (),
64
81
  files: [file2Path],
65
- symlinks: []
82
+ filesNames: new Set ( [file2Path].map ( toBasename ) ),
83
+ symlinks: [],
84
+ symlinksNames: new Set ()
66
85
  },
67
86
  [folder1DeepPath]: {
68
87
  directories: [],
88
+ directoriesNames: new Set (),
69
89
  files: [fileDeep1Path],
70
- symlinks: []
90
+ filesNames: new Set ( [fileDeep1Path].map ( toBasename ) ),
91
+ symlinks: [],
92
+ symlinksNames: new Set ()
71
93
  },
72
94
  [symlink1FromPath]: {
73
95
  directories: [root2Path],
96
+ directoriesNames: new Set ( [root2Path].map ( toBasename ) ),
74
97
  files: [],
75
- symlinks: []
98
+ filesNames: new Set (),
99
+ symlinks: [],
100
+ symlinksNames: new Set ()
76
101
  },
77
102
  [symlink2FromPath]: {
78
103
  directories: [],
104
+ directoriesNames: new Set (),
79
105
  files: [],
80
- symlinks: []
106
+ filesNames: new Set (),
107
+ symlinks: [],
108
+ symlinksNames: new Set ()
81
109
  }
82
110
  }
83
111
  };
@@ -128,23 +156,35 @@ describe ( 'Tiny Readdir', it => {
128
156
 
129
157
  const expected = {
130
158
  directories: [folder1Path, folder2Path],
159
+ directoriesNames: new Set ( [folder1Path, folder2Path].map ( toBasename ) ),
131
160
  files: [],
161
+ filesNames: new Set (),
132
162
  symlinks: [symlink1FromPath],
163
+ symlinksNames: new Set ( [symlink1FromPath].map ( toBasename ) ),
133
164
  map: {
134
165
  [root1Path]: {
135
166
  directories: [folder1Path, folder2Path],
167
+ directoriesNames: new Set ( [folder1Path, folder2Path].map ( toBasename ) ),
136
168
  files: [],
137
- symlinks: [symlink1FromPath]
169
+ filesNames: new Set (),
170
+ symlinks: [symlink1FromPath],
171
+ symlinksNames: new Set ( [symlink1FromPath].map ( toBasename ) )
138
172
  },
139
173
  [folder1Path]: {
140
174
  directories: [],
175
+ directoriesNames: new Set (),
141
176
  files: [],
142
- symlinks: []
177
+ filesNames: new Set (),
178
+ symlinks: [],
179
+ symlinksNames: new Set ()
143
180
  },
144
181
  [folder2Path]: {
145
182
  directories: [],
183
+ directoriesNames: new Set (),
146
184
  files: [],
147
- symlinks: []
185
+ filesNames: new Set (),
186
+ symlinks: [],
187
+ symlinksNames: new Set ()
148
188
  }
149
189
  }
150
190
  };