simple-folder-search 2.0.0 → 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/README.md CHANGED
@@ -1,14 +1,6 @@
1
1
  # Simple Folder Search
2
2
 
3
- This package provides a simple way to search for files within a folder. It is designed to be easy to use and integrate into your projects.
4
-
5
- This was mostly made for use with the `discord-player` package, but it can still be used almost everywhere.
6
-
7
- ## Features
8
-
9
- - Search for files by name
10
- - Filter files by extension
11
- - Recursive search through subdirectories
3
+ Simple Folder Search is a package that provides a simple way to search for files within a folder. It is designed to be easy to use and integrate into your projects.
12
4
 
13
5
  ## Installation
14
6
 
@@ -18,195 +10,35 @@ To install the package, use the following command:
18
10
  npm install simple-folder-search
19
11
  ```
20
12
 
21
- ## Usage
22
-
23
- Here is an example of how to use the package to search for files and play a file using `discord-player`'s search method:
13
+ You can also install it using yarn:
24
14
 
25
- ```javascript
26
- const { simpleFolderSearch } = require('simple-folder-search');
27
- const { Player, QueryType } = require('discord-player');
28
-
29
- // Initialize the player
30
- const player = new Player(client);
31
-
32
- // Search for music files in your music folder
33
- const musicFiles = simpleFolderSearch('./music', ['.mp3', '.wav'], 'my song', 0.6);
34
-
35
- if (musicFiles.length > 0) {
36
- // Get the first matching file
37
- const firstMatch = musicFiles[0];
38
-
39
- // Search the track using the proper engine
40
- const research = player.search(firstMatch, {
41
- searchEngine: QueryType.FILE,
42
- });
43
-
44
- // Play the file using discord-player
45
- await player.play(voiceChannel, research);
46
- } else {
47
- console.log('No matching files found');
48
- }
15
+ ```bash
16
+ yarn add simple-folder-search
49
17
  ```
50
18
 
51
- ### Searching by Name and Artist
19
+ ## Usage
52
20
 
53
- You can also search for files using both the file name and the artist (folder name) by providing the search query as an array. The first element of the array should be the file name, and the second element should be the artist (folder name).
21
+ Here is a basic example of how to use the package:
54
22
 
55
23
  ```javascript
56
- const results = await simpleFolderSearch("./music", [".mp3", ".wav"], ["my song", "artist name"], 0.6, 100);
57
- console.log(results);
58
- ```
59
-
60
- In this example, the search will look for files named "my song" within folders named "artist name".
61
-
62
- ### Normal behaviour
63
-
64
- Normally, simple-folder-search will go through files loading one batch at a time, which you can configure. A batch of 10, for example, consists of 10 fully loaded files, it will then push the file names to an array and then reset the batch and start again. This ensures that not all files are loaded at once, which could take a lot of memory.
65
-
66
- The issue with that is that files will always be loaded again each time the function is called.
67
-
68
- ### Custom caching method
69
-
70
- You can provide a custom caching method to the `simpleFolderSearch` function using a callback. This allows you to use different caching mechanisms, such as an in-memory cache or a database.
71
-
72
- #### In-Memory Cache Example
24
+ const { simpleFolderSearch } = require('simple-folder-search');
73
25
 
74
- ```javascript
75
- const cache = {};
76
-
77
- async function inMemoryCacheCallback(filePath, fileExtensions) {
78
- if (cache[filePath]) {
79
- return cache[filePath];
80
- }
81
-
82
- // Default file collection logic
83
- const files = [];
84
- function collectFiles(dir) {
85
- const entries = fs.readdirSync(dir, { withFileTypes: true });
86
- for (const entry of entries) {
87
- const fullPath = path.join(dir, entry.name);
88
- if (entry.isDirectory()) {
89
- collectFiles(fullPath);
90
- } else if (fileExtensions.length === 0 || fileExtensions.some(ext => entry.name.endsWith(ext))) {
91
- files.push(fullPath);
92
- }
93
- }
94
- }
95
-
96
- collectFiles(filePath);
97
- cache[filePath] = files;
98
- return files;
99
- }
100
-
101
- const results = await simpleFolderSearch("./music", [".mp3", ".wav"], "my song", 0.6, 100, inMemoryCacheCallback);
26
+ const results = await simpleFolderSearch('./music', ['.mp3', '.wav'], 'my song', { minimumScore: 0.6 });
102
27
  console.log(results);
103
28
  ```
104
29
 
105
- #### Database Cache Example
30
+ For more detailed usage and advanced options, please refer to the [Usage Documentation](docs/usage.md).
106
31
 
107
- You can use a lightweight database like SQLite for caching.
32
+ ## Documentation
108
33
 
109
- 1. **Install SQLite**:
110
- ```bash
111
- npm install sqlite3
112
- ```
34
+ For detailed documentation, please refer to the following files:
113
35
 
114
- 2. **Indexing Files**:
115
- Create a script to index files and store them in the database.
116
-
117
- ```javascript
118
- const sqlite3 = require('sqlite3').verbose();
119
- const db = new sqlite3.Database('files.db');
120
-
121
- db.serialize(() => {
122
- db.run("CREATE TABLE IF NOT EXISTS files (name TEXT, path TEXT, extension TEXT)");
123
-
124
- const indexFiles = (dir) => {
125
- const entries = fs.readdirSync(dir, { withFileTypes: true });
126
- for (const entry of entries) {
127
- const fullPath = path.join(dir, entry.name);
128
- if (entry.isDirectory()) {
129
- indexFiles(fullPath);
130
- } else {
131
- const ext = path.extname(entry.name);
132
- db.run("INSERT INTO files (name, path, extension) VALUES (?, ?, ?)", [entry.name, fullPath, ext]);
133
- }
134
- }
135
- };
136
-
137
- indexFiles("./your-directory");
138
- });
139
-
140
- db.close();
141
- ```
142
-
143
- 3. **Database Cache Callback**:
144
- Implement the cache callback to fetch files from the database.
145
-
146
- ```javascript
147
- const sqlite3 = require('sqlite3').verbose();
148
- const db = new sqlite3.Database('files.db');
149
-
150
- async function dbCacheCallback(filePath, fileExtensions) {
151
- return new Promise((resolve, reject) => {
152
- db.all("SELECT path FROM files WHERE extension IN (" + fileExtensions.map(() => '?').join(',') + ")", fileExtensions, (err, rows) => {
153
- if (err) {
154
- reject(err);
155
- } else {
156
- resolve(rows.map(row => row.path));
157
- }
158
- });
159
- });
160
- }
161
-
162
- const results = await simpleFolderSearch("./music", [".mp3", ".wav"], "my song", 0.6, 100, dbCacheCallback);
163
- console.log(results);
164
- ```
165
-
166
- This approach allows you to customize the caching mechanism according to your needs, whether it's an in-memory cache, a database, or any other storage solution.
167
-
168
- ## Types
169
-
170
- The `simpleFolderSearch` function uses the following types:
171
-
172
- ### `CacheCallback`
173
-
174
- A callback function type for custom caching logic.
175
-
176
- ```typescript
177
- type CacheCallback = (filePath: string, fileExtensions: string[]) => Promise<string[]>;
178
- ```
179
-
180
- ### `simpleFolderSearch`
181
-
182
- The main function to perform a simple folder search.
183
-
184
- ```typescript
185
- /**
186
- * Perform a simple folder search based on file names and optionally artist names.
187
- *
188
- * @param {string} filepath - The directory to search in.
189
- * @param {string[]} fileExtensions - An array of file extensions to filter by.
190
- * @param {string | string[]} search - The search query, either a string or an array of [name, artist].
191
- * @param {number} [minimumScore=0.6] - The minimum score for a match to be considered valid.
192
- * @param {number} [batchSize=10] - The number of files to process in each batch.
193
- * @param {CacheCallback} [cacheCallback] - Optional callback for custom caching logic.
194
- * @returns {Promise<string[]>} A promise that resolves to an array of file paths that match the search query.
195
- */
196
- export async function simpleFolderSearch(
197
- filepath: string,
198
- fileExtensions: string[],
199
- search: string | string[],
200
- minimumScore: number = 0.6,
201
- batchSize: number = 100,
202
- cacheCallback?: CacheCallback
203
- ): Promise<string[]>;
204
- ```
36
+ - [Installation](docs/installation.md)
37
+ - [Usage](docs/usage.md)
38
+ - [Options](docs/options.md)
39
+ - [Custom Caching](docs/caching.md)
40
+ - [Types](docs/types.md)
205
41
 
206
42
  ## License
207
43
 
208
- This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
209
-
210
- ## Contributing
211
-
212
- Contributions are welcome! Please open an issue or submit a pull request for any changes.
44
+ This project is licensed under the MIT License.
package/dist/index.cjs CHANGED
@@ -30,6 +30,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
30
30
  // src/index.ts
31
31
  var index_exports = {};
32
32
  __export(index_exports, {
33
+ searchCandidates: () => searchCandidates,
33
34
  simpleFolderSearch: () => simpleFolderSearch
34
35
  });
35
36
  module.exports = __toCommonJS(index_exports);
@@ -38,7 +39,12 @@ module.exports = __toCommonJS(index_exports);
38
39
  var import_fs = __toESM(require("fs"), 1);
39
40
  var import_path = __toESM(require("path"), 1);
40
41
  var import_fuse = __toESM(require("fuse.js"), 1);
41
- async function simpleFolderSearch(filepath, fileExtensions, search, minimumScore = 0.6, batchSize = 100, cacheCallback) {
42
+ async function simpleFolderSearch(filepath, fileExtensions, search, options = {}, cacheCallback) {
43
+ const {
44
+ minimumScore = 0.6,
45
+ batchSize = 100,
46
+ parallelSearches = 1
47
+ } = options;
42
48
  const absolutePath = import_path.default.resolve(process.cwd(), filepath);
43
49
  if (!import_fs.default.existsSync(absolutePath)) throw new Error("Filepath does not exist.");
44
50
  if (!Array.isArray(fileExtensions)) throw new Error("fileExtensions must be an array.");
@@ -46,14 +52,21 @@ async function simpleFolderSearch(filepath, fileExtensions, search, minimumScore
46
52
  if (typeof minimumScore !== "number") throw new Error("Minimum score must be a number.");
47
53
  if (minimumScore < 0 || minimumScore > 1) throw new Error("Minimum score must be between 0 and 1.");
48
54
  if (typeof batchSize !== "number" || batchSize <= 0) throw new Error("Batch size must be a positive number.");
49
- let filesForFuse = [];
55
+ if (typeof parallelSearches !== "number" || parallelSearches <= 0) throw new Error("Parallel searches must be a positive number.");
56
+ const filesForFuse = [];
57
+ const processFiles = async (files) => {
58
+ for (const file of files) {
59
+ const fileData = {
60
+ name: import_path.default.parse(file).name,
61
+ artist: import_path.default.basename(import_path.default.dirname(file)),
62
+ path: file
63
+ };
64
+ filesForFuse.push(fileData);
65
+ }
66
+ };
50
67
  if (cacheCallback) {
51
- const cachedFiles = await cacheCallback(absolutePath, fileExtensions);
52
- filesForFuse = cachedFiles.map((file) => ({
53
- name: import_path.default.parse(file).name,
54
- artist: import_path.default.basename(import_path.default.dirname(file)),
55
- path: file
56
- }));
68
+ const cachedFiles = await cacheCallback(absolutePath, fileExtensions, search);
69
+ await processFiles(cachedFiles);
57
70
  } else {
58
71
  function* collectFiles(dir) {
59
72
  const entries = import_fs.default.readdirSync(dir, { withFileTypes: true });
@@ -73,29 +86,29 @@ async function simpleFolderSearch(filepath, fileExtensions, search, minimumScore
73
86
  if (batch.length > 0)
74
87
  yield batch;
75
88
  }
76
- for (const batch of collectFiles(absolutePath)) {
77
- for (const file of batch) {
78
- filesForFuse.push({
79
- name: import_path.default.parse(file).name,
80
- artist: import_path.default.basename(import_path.default.dirname(file)),
81
- path: file
82
- });
83
- }
84
- }
89
+ for (const batch of collectFiles(absolutePath))
90
+ await processFiles(batch);
85
91
  }
86
92
  if (!search || Array.isArray(search) && search.length === 0)
87
93
  return filesForFuse.map((file) => file.path);
94
+ return await searchCandidates(filesForFuse, search, minimumScore);
95
+ }
96
+ async function searchCandidates(candidates, search, minimumScore) {
88
97
  const fuseOptions = {
89
98
  includeScore: true,
90
99
  threshold: 1 - minimumScore,
91
100
  // Convert our score to Fuse threshold
92
101
  keys: ["name", "artist"]
93
102
  };
94
- const fuse = new import_fuse.default(filesForFuse, fuseOptions);
95
- const results = Array.isArray(search) ? fuse.search({ name: search[0], artist: search[1] }) : fuse.search(search);
96
- return results.filter((result) => result.score ? result.score <= 1 - minimumScore : false).map((result) => result.item.path);
103
+ const fuse = new import_fuse.default(candidates, fuseOptions);
104
+ const searchQueries = Array.isArray(search) ? [{ name: search[0], artist: search[1] }] : [search];
105
+ const searchPromises = searchQueries.map((query) => fuse.search(query));
106
+ const results = await Promise.all(searchPromises);
107
+ const flattenedResults = results.flat();
108
+ return flattenedResults.filter((result) => result.score ? result.score <= 1 - minimumScore : false).map((result) => result.item.path);
97
109
  }
98
110
  // Annotate the CommonJS export names for ESM import in node:
99
111
  0 && (module.exports = {
112
+ searchCandidates,
100
113
  simpleFolderSearch
101
114
  });
package/dist/index.d.cts CHANGED
@@ -1,15 +1,32 @@
1
- type CacheCallback = (filePath: string, fileExtensions: string[]) => Promise<string[]>;
1
+ type CacheCallback = (filePath: string, fileExtensions: string[], search?: string[] | string) => Promise<string[]>;
2
+ interface SearchOptions {
3
+ minimumScore?: number;
4
+ batchSize?: number;
5
+ parallelSearches?: number;
6
+ }
2
7
  /**
3
8
  * Perform a simple folder search based on file names and optionally artist names.
4
9
  *
5
10
  * @param {string} filepath - The directory to search in.
6
11
  * @param {string[]} fileExtensions - An array of file extensions to filter by.
7
12
  * @param {string | string[]} search - The search query, either a string or an array of [name, artist].
8
- * @param {number} [minimumScore=0.6] - The minimum score for a match to be considered valid.
9
- * @param {number} [batchSize=10] - The number of files to process in each batch.
13
+ * @param {SearchOptions} [options] - Optional search options.
10
14
  * @param {CacheCallback} [cacheCallback] - Optional callback for custom caching logic.
11
15
  * @returns {Promise<string[]>} A promise that resolves to an array of file paths that match the search query.
12
16
  */
13
- declare function simpleFolderSearch(filepath: string, fileExtensions: string[], search: string | string[], minimumScore?: number, batchSize?: number, cacheCallback?: CacheCallback): Promise<string[]>;
17
+ declare function simpleFolderSearch(filepath: string, fileExtensions: string[], search: string | string[], options?: SearchOptions, cacheCallback?: CacheCallback): Promise<string[]>;
18
+ /**
19
+ * Search for candidates based on a search query and minimum score.
20
+ *
21
+ * @param {Array<{name: string; artist?: string; path: string;}>} candidates - An array of candidate objects to search through.
22
+ * @param {string | string[]} search - The search query, either a string or an array of [name, artist].
23
+ * @param {number} minimumScore - The minimum score required for a candidate to be considered a match.
24
+ * @returns {Promise<string[]>} A promise that resolves to an array of file paths that match the search query.
25
+ */
26
+ declare function searchCandidates(candidates: {
27
+ name: string;
28
+ artist?: string;
29
+ path: string;
30
+ }[], search: string | string[], minimumScore: number): Promise<string[]>;
14
31
 
15
- export { simpleFolderSearch };
32
+ export { searchCandidates, simpleFolderSearch };
package/dist/index.d.ts CHANGED
@@ -1,15 +1,32 @@
1
- type CacheCallback = (filePath: string, fileExtensions: string[]) => Promise<string[]>;
1
+ type CacheCallback = (filePath: string, fileExtensions: string[], search?: string[] | string) => Promise<string[]>;
2
+ interface SearchOptions {
3
+ minimumScore?: number;
4
+ batchSize?: number;
5
+ parallelSearches?: number;
6
+ }
2
7
  /**
3
8
  * Perform a simple folder search based on file names and optionally artist names.
4
9
  *
5
10
  * @param {string} filepath - The directory to search in.
6
11
  * @param {string[]} fileExtensions - An array of file extensions to filter by.
7
12
  * @param {string | string[]} search - The search query, either a string or an array of [name, artist].
8
- * @param {number} [minimumScore=0.6] - The minimum score for a match to be considered valid.
9
- * @param {number} [batchSize=10] - The number of files to process in each batch.
13
+ * @param {SearchOptions} [options] - Optional search options.
10
14
  * @param {CacheCallback} [cacheCallback] - Optional callback for custom caching logic.
11
15
  * @returns {Promise<string[]>} A promise that resolves to an array of file paths that match the search query.
12
16
  */
13
- declare function simpleFolderSearch(filepath: string, fileExtensions: string[], search: string | string[], minimumScore?: number, batchSize?: number, cacheCallback?: CacheCallback): Promise<string[]>;
17
+ declare function simpleFolderSearch(filepath: string, fileExtensions: string[], search: string | string[], options?: SearchOptions, cacheCallback?: CacheCallback): Promise<string[]>;
18
+ /**
19
+ * Search for candidates based on a search query and minimum score.
20
+ *
21
+ * @param {Array<{name: string; artist?: string; path: string;}>} candidates - An array of candidate objects to search through.
22
+ * @param {string | string[]} search - The search query, either a string or an array of [name, artist].
23
+ * @param {number} minimumScore - The minimum score required for a candidate to be considered a match.
24
+ * @returns {Promise<string[]>} A promise that resolves to an array of file paths that match the search query.
25
+ */
26
+ declare function searchCandidates(candidates: {
27
+ name: string;
28
+ artist?: string;
29
+ path: string;
30
+ }[], search: string | string[], minimumScore: number): Promise<string[]>;
14
31
 
15
- export { simpleFolderSearch };
32
+ export { searchCandidates, simpleFolderSearch };
package/dist/index.js CHANGED
@@ -2,7 +2,12 @@
2
2
  import fs from "fs";
3
3
  import path from "path";
4
4
  import Fuse from "fuse.js";
5
- async function simpleFolderSearch(filepath, fileExtensions, search, minimumScore = 0.6, batchSize = 100, cacheCallback) {
5
+ async function simpleFolderSearch(filepath, fileExtensions, search, options = {}, cacheCallback) {
6
+ const {
7
+ minimumScore = 0.6,
8
+ batchSize = 100,
9
+ parallelSearches = 1
10
+ } = options;
6
11
  const absolutePath = path.resolve(process.cwd(), filepath);
7
12
  if (!fs.existsSync(absolutePath)) throw new Error("Filepath does not exist.");
8
13
  if (!Array.isArray(fileExtensions)) throw new Error("fileExtensions must be an array.");
@@ -10,14 +15,21 @@ async function simpleFolderSearch(filepath, fileExtensions, search, minimumScore
10
15
  if (typeof minimumScore !== "number") throw new Error("Minimum score must be a number.");
11
16
  if (minimumScore < 0 || minimumScore > 1) throw new Error("Minimum score must be between 0 and 1.");
12
17
  if (typeof batchSize !== "number" || batchSize <= 0) throw new Error("Batch size must be a positive number.");
13
- let filesForFuse = [];
18
+ if (typeof parallelSearches !== "number" || parallelSearches <= 0) throw new Error("Parallel searches must be a positive number.");
19
+ const filesForFuse = [];
20
+ const processFiles = async (files) => {
21
+ for (const file of files) {
22
+ const fileData = {
23
+ name: path.parse(file).name,
24
+ artist: path.basename(path.dirname(file)),
25
+ path: file
26
+ };
27
+ filesForFuse.push(fileData);
28
+ }
29
+ };
14
30
  if (cacheCallback) {
15
- const cachedFiles = await cacheCallback(absolutePath, fileExtensions);
16
- filesForFuse = cachedFiles.map((file) => ({
17
- name: path.parse(file).name,
18
- artist: path.basename(path.dirname(file)),
19
- path: file
20
- }));
31
+ const cachedFiles = await cacheCallback(absolutePath, fileExtensions, search);
32
+ await processFiles(cachedFiles);
21
33
  } else {
22
34
  function* collectFiles(dir) {
23
35
  const entries = fs.readdirSync(dir, { withFileTypes: true });
@@ -37,28 +49,28 @@ async function simpleFolderSearch(filepath, fileExtensions, search, minimumScore
37
49
  if (batch.length > 0)
38
50
  yield batch;
39
51
  }
40
- for (const batch of collectFiles(absolutePath)) {
41
- for (const file of batch) {
42
- filesForFuse.push({
43
- name: path.parse(file).name,
44
- artist: path.basename(path.dirname(file)),
45
- path: file
46
- });
47
- }
48
- }
52
+ for (const batch of collectFiles(absolutePath))
53
+ await processFiles(batch);
49
54
  }
50
55
  if (!search || Array.isArray(search) && search.length === 0)
51
56
  return filesForFuse.map((file) => file.path);
57
+ return await searchCandidates(filesForFuse, search, minimumScore);
58
+ }
59
+ async function searchCandidates(candidates, search, minimumScore) {
52
60
  const fuseOptions = {
53
61
  includeScore: true,
54
62
  threshold: 1 - minimumScore,
55
63
  // Convert our score to Fuse threshold
56
64
  keys: ["name", "artist"]
57
65
  };
58
- const fuse = new Fuse(filesForFuse, fuseOptions);
59
- const results = Array.isArray(search) ? fuse.search({ name: search[0], artist: search[1] }) : fuse.search(search);
60
- return results.filter((result) => result.score ? result.score <= 1 - minimumScore : false).map((result) => result.item.path);
66
+ const fuse = new Fuse(candidates, fuseOptions);
67
+ const searchQueries = Array.isArray(search) ? [{ name: search[0], artist: search[1] }] : [search];
68
+ const searchPromises = searchQueries.map((query) => fuse.search(query));
69
+ const results = await Promise.all(searchPromises);
70
+ const flattenedResults = results.flat();
71
+ return flattenedResults.filter((result) => result.score ? result.score <= 1 - minimumScore : false).map((result) => result.item.path);
61
72
  }
62
73
  export {
74
+ searchCandidates,
63
75
  simpleFolderSearch
64
76
  };
@@ -0,0 +1,102 @@
1
+ # Custom Caching
2
+
3
+ The Simple Folder Search package supports custom caching to improve search performance. You can provide a custom caching method to the `simpleFolderSearch` function using a callback. This allows you to use different caching mechanisms, such as an in-memory cache or a database.
4
+
5
+ ## In-Memory Cache Example
6
+
7
+ ```javascript
8
+ const cache = {};
9
+
10
+ async function inMemoryCacheCallback(filePath, fileExtensions) {
11
+ if (cache[filePath]) {
12
+ return cache[filePath];
13
+ }
14
+
15
+ // Default file collection logic
16
+ const files = [];
17
+ function collectFiles(dir) {
18
+ const entries = fs.readdirSync(dir, { withFileTypes: true });
19
+ for (const entry of entries) {
20
+ const fullPath = path.join(dir, entry.name);
21
+ if (entry.isDirectory()) {
22
+ collectFiles(fullPath);
23
+ } else if (fileExtensions.length === 0 || fileExtensions.some(ext => entry.name.endsWith(ext))) {
24
+ files.push(fullPath);
25
+ }
26
+ }
27
+ }
28
+
29
+ collectFiles(filePath);
30
+ cache[filePath] = files;
31
+ return files;
32
+ }
33
+
34
+ const results = await simpleFolderSearch("./music", [".mp3", ".wav"], "my song", { minimumScore: 0.6, batchSize: 100 }, inMemoryCacheCallback);
35
+ console.log(results);
36
+ ```
37
+
38
+ ## Database Cache Example
39
+
40
+ You can use a lightweight database like SQLite for caching.
41
+
42
+ 1. Install SQLite:
43
+
44
+ ```bash
45
+ npm install sqlite3
46
+ ```
47
+
48
+ 2. Indexing Files: Create a script to index files and store them in the database.
49
+
50
+ ```js
51
+ const sqlite3 = require('sqlite3').verbose();
52
+ const db = new sqlite3.Database('files.db');
53
+
54
+ db.serialize(() => {
55
+ db.run("CREATE TABLE IF NOT EXISTS files (name TEXT, path TEXT, extension TEXT)");
56
+
57
+ const indexFiles = (dir) => {
58
+ const entries = fs.readdirSync(dir, { withFileTypes: true });
59
+ for (const entry of entries) {
60
+ const fullPath = path.join(dir, entry.name);
61
+ if (entry.isDirectory()) {
62
+ indexFiles(fullPath);
63
+ } else {
64
+ const ext = path.extname(entry.name);
65
+ db.run("INSERT INTO files (name, path, extension) VALUES (?, ?, ?)", [entry.name, fullPath, ext]);
66
+ }
67
+ }
68
+ };
69
+
70
+ indexFiles("./your-directory");
71
+ });
72
+
73
+ db.close();
74
+ ```
75
+
76
+ 3. Database Cache Callback: Implement the cache callback to fetch files from the database.
77
+
78
+ This method also pre-checks the search to filter words out of the database results, as the database searches much faster than fuzzy-matching
79
+
80
+ ```js
81
+ const sqlite3 = require('sqlite3').verbose();
82
+ const db = new sqlite3.Database('files.db');
83
+
84
+ async function dbCacheCallback(filePath, fileExtensions) {
85
+ return new Promise((resolve, reject) => {
86
+ const exts = fileExtensions.map(() => '?').join(',');
87
+ const tokens = (!search ? '1=1' : Array.isArray(search) ? search.join(' '): search).toLowerCase().split(' ').map(x => 'path LIKE "%'+x+'%"').join(' OR ');
88
+ db.all("SELECT path FROM files WHERE extension IN (" + exts + ") AND (" + tokens + ")", fileExtensions, (err, rows) => {
89
+ if (err) {
90
+ reject(err);
91
+ } else {
92
+ resolve(rows.map(row => row.path));
93
+ }
94
+ });
95
+ });
96
+ }
97
+
98
+ const results = await simpleFolderSearch("./music", [".mp3", ".wav"], "my song", { minimumScore: 0.6, batchSize: 100 }, dbCacheCallback);
99
+ console.log(results);
100
+ ```
101
+
102
+ This approach allows you to customize the caching mechanism according to your needs, whether it's an in-memory cache, a database, or any other storage solution.
package/docs/index.md ADDED
@@ -0,0 +1,11 @@
1
+ # Simple Folder Search Documentation
2
+
3
+ Welcome to the Simple Folder Search documentation. This package provides a simple way to search for files within a folder. It is designed to be easy to use and integrate into your projects.
4
+
5
+ ## Table of Contents
6
+
7
+ - [Installation](installation.md)
8
+ - [Usage](usage.md)
9
+ - [Options](options.md)
10
+ - [Custom Caching](caching.md)
11
+ - [Types](types.md)
@@ -0,0 +1,13 @@
1
+ # Installation
2
+
3
+ To install the package, use the following command:
4
+
5
+ ```bash
6
+ npm install simple-folder-search
7
+ ```
8
+
9
+ You can also install it using yarn:
10
+
11
+ ```bash
12
+ yarn add simple-folder-search
13
+ ```
@@ -0,0 +1,21 @@
1
+ # Options
2
+
3
+ The Simple Folder Search package provides several options to customize the search behavior. Here are the available options:
4
+
5
+ ## minimumScore
6
+
7
+ - **Type:** `number`
8
+ - **Default:** `0.6`
9
+ - **Description:** The minimum score required for a file to be considered a match.
10
+
11
+ ## batchSize
12
+
13
+ - **Type:** `number`
14
+ - **Default:** `100`
15
+ - **Description:** The number of files to process in each batch.
16
+
17
+ ## parallelSearches
18
+
19
+ - **Type:** `number`
20
+ - **Default:** `1`
21
+ - **Description:** The number of parallel searches to perform.
package/docs/types.md ADDED
@@ -0,0 +1,32 @@
1
+ # Types
2
+
3
+ This document describes the types used in the Simple Folder Search package.
4
+
5
+ ## SearchOptions
6
+
7
+ ```typescript
8
+ interface SearchOptions {
9
+ minimumScore?: number;
10
+ batchSize?: number;
11
+ parallelSearches?: number;
12
+ }
13
+ ```
14
+
15
+ ## CacheCallback
16
+
17
+ A callback function type for custom caching logic.
18
+
19
+ ```ts
20
+ type CacheCallback = (filePath: string, fileExtensions: string[], search?: string[] | string) => Promise<string[]>;
21
+ ```
22
+
23
+ ## SearchResult
24
+
25
+ ```ts
26
+ interface SearchResult {
27
+ filePath: string;
28
+ score: number;
29
+ }
30
+ ```
31
+
32
+ These types are used to define the options for the search and the structure of the search results.
package/docs/usage.md ADDED
@@ -0,0 +1,55 @@
1
+ # Usage
2
+
3
+ Here is an example of how to use the package to search for files and play a file using `discord-player`'s search method:
4
+
5
+ ```javascript
6
+ const { simpleFolderSearch } = require('simple-folder-search');
7
+ const { Player, QueryType } = require('discord-player');
8
+
9
+ // Initialize the player
10
+ const player = new Player(client);
11
+
12
+ // Search for music files in your music folder
13
+ const musicFiles = await simpleFolderSearch('./music', ['.mp3', '.wav'], 'my song', { minimumScore: 0.6 });
14
+
15
+ if (musicFiles.length > 0) {
16
+ // Get the first matching file
17
+ const firstMatch = musicFiles[0];
18
+
19
+ // Search the track using the proper engine
20
+ const research = player.search(firstMatch, {
21
+ searchEngine: QueryType.FILE,
22
+ });
23
+
24
+ // Play the file using discord-player
25
+ await player.play(voiceChannel, research);
26
+ } else {
27
+ console.log('No matching files found');
28
+ }
29
+ ```
30
+
31
+ ## Searching by Name and Artist
32
+
33
+ You can also search for files using both the file name and the artist (folder name) by providing the search query as an array. The first element of the array should be the file name, and the second element should be the artist (folder name).
34
+
35
+ ```js
36
+ const results = await simpleFolderSearch("./music", [".mp3", ".wav"], ["my song", "artist name"], { minimumScore: 0.6, batchSize: 100 });
37
+ console.log(results);
38
+ ```
39
+
40
+ In this example, the search will look for files named "my song" within folders named "artist name".
41
+
42
+ ## Advanced Usage
43
+
44
+ ```js
45
+ const options = {
46
+ minimumScore: 0.7,
47
+ batchSize: 50,
48
+ parallelSearches: 5,
49
+ };
50
+
51
+ const advancedResults = await simpleFolderSearch("./music", [".mp3", ".wav"], "advanced search", options);
52
+ console.log(advancedResults);
53
+ ```
54
+
55
+ In this example, the search will look for files with a minimum score of 0.7, process 50 files at a time and perform 5 parallel searches.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "simple-folder-search",
3
- "version": "2.0.0",
3
+ "version": "2.3.0",
4
4
  "description": "A simple folder search package",
5
5
  "main": "dist/index.cjs",
6
6
  "exports": {
@@ -2,7 +2,13 @@ import fs from "fs";
2
2
  import path from "path";
3
3
  import Fuse from "fuse.js";
4
4
 
5
- type CacheCallback = (filePath: string, fileExtensions: string[]) => Promise<string[]>;
5
+ type CacheCallback = (filePath: string, fileExtensions: string[], search?: string[] | string) => Promise<string[]>;
6
+
7
+ interface SearchOptions {
8
+ minimumScore?: number;
9
+ batchSize?: number;
10
+ parallelSearches?: number;
11
+ }
6
12
 
7
13
  /**
8
14
  * Perform a simple folder search based on file names and optionally artist names.
@@ -10,8 +16,7 @@ type CacheCallback = (filePath: string, fileExtensions: string[]) => Promise<str
10
16
  * @param {string} filepath - The directory to search in.
11
17
  * @param {string[]} fileExtensions - An array of file extensions to filter by.
12
18
  * @param {string | string[]} search - The search query, either a string or an array of [name, artist].
13
- * @param {number} [minimumScore=0.6] - The minimum score for a match to be considered valid.
14
- * @param {number} [batchSize=10] - The number of files to process in each batch.
19
+ * @param {SearchOptions} [options] - Optional search options.
15
20
  * @param {CacheCallback} [cacheCallback] - Optional callback for custom caching logic.
16
21
  * @returns {Promise<string[]>} A promise that resolves to an array of file paths that match the search query.
17
22
  */
@@ -19,10 +24,15 @@ export async function simpleFolderSearch(
19
24
  filepath: string,
20
25
  fileExtensions: string[],
21
26
  search: string | string[],
22
- minimumScore: number = 0.6,
23
- batchSize: number = 100,
27
+ options: SearchOptions = {},
24
28
  cacheCallback?: CacheCallback,
25
29
  ): Promise<string[]> {
30
+ const {
31
+ minimumScore = 0.6,
32
+ batchSize = 100,
33
+ parallelSearches = 1,
34
+ } = options;
35
+
26
36
  const absolutePath = path.resolve(process.cwd(), filepath);
27
37
 
28
38
  if (!fs.existsSync(absolutePath)) throw new Error("Filepath does not exist.");
@@ -31,16 +41,25 @@ export async function simpleFolderSearch(
31
41
  if (typeof minimumScore !== "number") throw new Error("Minimum score must be a number.");
32
42
  if (minimumScore < 0 || minimumScore > 1) throw new Error("Minimum score must be between 0 and 1.");
33
43
  if (typeof batchSize !== "number" || batchSize <= 0) throw new Error("Batch size must be a positive number.");
44
+ if (typeof parallelSearches !== "number" || parallelSearches <= 0) throw new Error("Parallel searches must be a positive number.");
45
+
46
+ const filesForFuse: { name: string; artist?: string; path: string}[] = [];
47
+
48
+ const processFiles = async (files: string[]) => {
49
+ for (const file of files) {
50
+ const fileData: { name: string; artist?: string; path: string} = {
51
+ name: path.parse(file).name,
52
+ artist: path.basename(path.dirname(file)),
53
+ path: file,
54
+ };
34
55
 
35
- let filesForFuse: { name: string; artist?: string; path: string }[] = [];
56
+ filesForFuse.push(fileData);
57
+ }
58
+ };
36
59
 
37
60
  if (cacheCallback) {
38
- const cachedFiles = await cacheCallback(absolutePath, fileExtensions);
39
- filesForFuse = cachedFiles.map(file => ({
40
- name: path.parse(file).name,
41
- artist: path.basename(path.dirname(file)),
42
- path: file,
43
- }));
61
+ const cachedFiles = await cacheCallback(absolutePath, fileExtensions, search);
62
+ await processFiles(cachedFiles);
44
63
  } else {
45
64
  function* collectFiles(dir: string): Generator<string[]> {
46
65
  const entries = fs.readdirSync(dir, { withFileTypes: true });
@@ -63,33 +82,43 @@ export async function simpleFolderSearch(
63
82
  yield batch;
64
83
  }
65
84
 
66
- for (const batch of collectFiles(absolutePath)) {
67
- for (const file of batch) {
68
- filesForFuse.push({
69
- name: path.parse(file).name,
70
- artist: path.basename(path.dirname(file)),
71
- path: file,
72
- });
73
- }
74
- }
85
+ for (const batch of collectFiles(absolutePath))
86
+ await processFiles(batch);
87
+
75
88
  }
76
89
 
77
90
  // If search is empty, return all files with matching extensions
78
91
  if (!search || (Array.isArray(search) && search.length === 0))
79
92
  return filesForFuse.map(file => file.path);
80
93
 
94
+ return await searchCandidates(filesForFuse, search, minimumScore);
95
+ }
96
+
97
+ /**
98
+ * Search for candidates based on a search query and minimum score.
99
+ *
100
+ * @param {Array<{name: string; artist?: string; path: string;}>} candidates - An array of candidate objects to search through.
101
+ * @param {string | string[]} search - The search query, either a string or an array of [name, artist].
102
+ * @param {number} minimumScore - The minimum score required for a candidate to be considered a match.
103
+ * @returns {Promise<string[]>} A promise that resolves to an array of file paths that match the search query.
104
+ */
105
+ export async function searchCandidates(candidates: {name: string; artist?: string; path: string;}[], search: string|string[], minimumScore: number): Promise<string[]> {
81
106
  // Setup Fuse with options
82
107
  const fuseOptions = {
83
108
  includeScore: true,
84
109
  threshold: 1 - minimumScore, // Convert our score to Fuse threshold
85
110
  keys: ["name", "artist"],
86
111
  };
112
+ const fuse = new Fuse(candidates, fuseOptions);
87
113
 
88
- const fuse = new Fuse(filesForFuse, fuseOptions);
89
-
90
114
  // Perform search and filter results
91
- const results = Array.isArray(search) ? fuse.search({ name: search[0], artist: search[1] }) : fuse.search(search);
92
- return results
115
+ const searchQueries = Array.isArray(search) ? [{ name: search[0], artist: search[1] }] : [search];
116
+ const searchPromises = searchQueries.map(query => fuse.search(query));
117
+
118
+ const results = await Promise.all(searchPromises);
119
+ const flattenedResults = results.flat();
120
+
121
+ return flattenedResults
93
122
  .filter(result => (result.score ? result.score <= (1 - minimumScore) : false))
94
123
  .map(result => result.item.path);
95
124
  }