Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regex support for filter function ? #847

Closed
pstevovski opened this issue Nov 15, 2020 · 1 comment
Closed

Regex support for filter function ? #847

pstevovski opened this issue Nov 15, 2020 · 1 comment
Labels

Comments

@pstevovski
Copy link

  • Operating System: Windows 10
  • Node.js version: 12.16.3
  • fs-extra version: 9.0.1

Hi,

If I try to use a regular expression to control which files I'd like to be copied and which files not, I end up with the function exiting on the first false result, thus not proceeding to actually check the files that are located in the folders, that need to be copied over. And the first false result is I presume the first folder that it tries to match against the regular expression which obviously fails.

Are regular expressions not supported and can't be used like this? The files do end up matching when I log them to the console, if I just put return true at the end of the filter function, but then again, that copies all files, and I don't really need that :)

Example:

await fse.copy(dbFiles, directory, {
       overwrite: true,
       filter: (file: string): boolean => {
            const regex: RegExp = template.toLowerCase() === "javascript" ? RegExp(/(\.js|\.md)$/) : RegExp(/(\.ts|\.md)$/);

            return regex.test(file);
       }}
);

I had to end up with something like this to make it work:

await fse.copy(dbFiles, directory, {
  overwrite: true,
  filter: async(file: string): Promise<boolean> => {
      if (template.toLowerCase() === "javascript") {
         return (await fse.stat(file)).isDirectory() || file.endsWith(".js") || file.endsWith(".md");
      } else {
        return (await fse.stat(file)).isDirectory() || file.endsWith(".ts") || file.endsWith(".md");
     }
  }}
);

I think there's an issue with a premature exit of the function in case it gets to a false result while filtering, even though it should continue cycling trough each folder / file. Or am I wrong and I've completely wrongly tried to use the filtering function?

P.S. It would be really great if the examples, more in particular the fse.copy() example is better put together, I couldn't realize what to do with the parameters and how to actually get the filtering done and most likely wouldn't have unless I stumbled upon another issue that was just recently opened mentioning the isDirectory() check, so props to that guy 😃

@RyanZim
Copy link
Collaborator

RyanZim commented Nov 16, 2020

The filter function is called for both files & directories; and if you return false for a directory, files inside it are not recursed. Currently, isDirectory is the correct way to make it work; it should work with your regexes then. #844 is a proposal to pass the stats directly to the filter function, so you don't have to make the stat call yourself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants