如何使用 Node.js 检索内容中不包含特定字符串的文件
How retrieve files that don't contain a certain string in their content using Node.js
我的 directory
中有一个 strings
的 array
代表文件 paths
。我创建了一个名为 readFileContent
的 function
来读取文件内容,而另一个 function
用于在文件内容中查找某个 string
。现在我想创建一个新的 array
文件路径,其中包含所述 string
。但是,我没有让它工作。这是我的代码:
const allFilePaths = [
'/path/to/file1.js',
'/slighty/longer/path/to/file2.js',
'/way/slighty/longer/path/to/file3.js',
'/path/to/file4.js',
];
// utility function to check if str param doesn't contains "https" string
const getUrl = str => !str.match(/https:\s*([^;]+)\n/);
// utility function to read a file - the file param represent a file path
const readFileContent = file => {
return new Promise((resolve, reject) => {
fs.readFile(file, 'utf8', (err, data) => {
if (err) return reject(err);
return resolve(data);
});
});
};
(async () => {
// functions to return all file that don't contain a url, I get error: str.match is not a function
const allFileWithoutUrls = await str.match is not a function.map(file =>
getUrl(readFileContent(file)) ? file : null
})();
有什么问题?
尝试 .filter
方法而不是 .map
。 .filter
方法将 return 一个新数组,其中每个项目取决于您 return 回调函数中的真值或假值。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
const allFilePaths = [
"/path/to/file1.js",
"/slighty/longer/path/to/file2.js",
"/way/slighty/longer/path/to/file3.js",
"/path/to/file4.js",
];
// utility function to check if str param doesn't contains "https" string
const getUrl = (str) => !str.match(/https:\s*([^;]+)\n/);
// utility function to read a file - the file param represent a file path
const readFileContent = (file) => {
return new Promise((resolve, reject) => {
fs.readFile(file, "utf8", (err, data) => {
if (err) return reject(err);
return resolve(data);
});
});
};
(async () => {
// functions to return all file that don't contain a url, I get error: str.match is not a function
const promises = allFilePaths.map(async (filePath) => {
const contents = await readFileContent(filePath);
return { contents, filePath };
});
const contents = await Promise.all(promises);
const allFileWithoutUrls = contents
.filter(({ contents }) => getUrl(contents))
.map(({ filePath }) => filePath);
console.log(allFileWithoutUrls);
})();
我的 directory
中有一个 strings
的 array
代表文件 paths
。我创建了一个名为 readFileContent
的 function
来读取文件内容,而另一个 function
用于在文件内容中查找某个 string
。现在我想创建一个新的 array
文件路径,其中包含所述 string
。但是,我没有让它工作。这是我的代码:
const allFilePaths = [
'/path/to/file1.js',
'/slighty/longer/path/to/file2.js',
'/way/slighty/longer/path/to/file3.js',
'/path/to/file4.js',
];
// utility function to check if str param doesn't contains "https" string
const getUrl = str => !str.match(/https:\s*([^;]+)\n/);
// utility function to read a file - the file param represent a file path
const readFileContent = file => {
return new Promise((resolve, reject) => {
fs.readFile(file, 'utf8', (err, data) => {
if (err) return reject(err);
return resolve(data);
});
});
};
(async () => {
// functions to return all file that don't contain a url, I get error: str.match is not a function
const allFileWithoutUrls = await str.match is not a function.map(file =>
getUrl(readFileContent(file)) ? file : null
})();
有什么问题?
尝试 .filter
方法而不是 .map
。 .filter
方法将 return 一个新数组,其中每个项目取决于您 return 回调函数中的真值或假值。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
const allFilePaths = [
"/path/to/file1.js",
"/slighty/longer/path/to/file2.js",
"/way/slighty/longer/path/to/file3.js",
"/path/to/file4.js",
];
// utility function to check if str param doesn't contains "https" string
const getUrl = (str) => !str.match(/https:\s*([^;]+)\n/);
// utility function to read a file - the file param represent a file path
const readFileContent = (file) => {
return new Promise((resolve, reject) => {
fs.readFile(file, "utf8", (err, data) => {
if (err) return reject(err);
return resolve(data);
});
});
};
(async () => {
// functions to return all file that don't contain a url, I get error: str.match is not a function
const promises = allFilePaths.map(async (filePath) => {
const contents = await readFileContent(filePath);
return { contents, filePath };
});
const contents = await Promise.all(promises);
const allFileWithoutUrls = contents
.filter(({ contents }) => getUrl(contents))
.map(({ filePath }) => filePath);
console.log(allFileWithoutUrls);
})();