thecodrr / fdir

⚡ The fastest directory crawler & globbing library for NodeJS. Crawls 1m files in < 1s
https://thecodrr.github.io/fdir/
MIT License
1.46k stars 51 forks source link

[Feature] middleware / custom transforms support #107

Open SuperchupuDev opened 1 month ago

SuperchupuDev commented 1 month ago

currently, what is returned after crawling and what is used in filters is always what fdir has processed. i propose a function that allows users to transform those paths with custom logic.

why? because if a user wants to use their custom logic to transform fdir paths they currently have to duplicate it if using filters/excludes/globs (which also means the transform logic is ran twice, which is bad for performance):

const files = await new fdir()
  .withRelativePaths()
  .filter(p => {
    // whatever custom logic they might need
    const path = `../${p}`;

    return someFilteringFunction(path);
  })
  .crawl(root)
  .withPromise()

// the exact same custom logic again
return files.map(p => `../${p}`)

with support for custom transforms, it could look something more like this:

return new fdir()
  .withRelativePaths()
  .transform(p => `../${p}`)
  .filter(someFilteringFunction)
  .crawl(root)
  .withPromise()

// it returns paths with a leading `../`, without duplicating the transform logic. woohoo!

see a real world example of the problem this proposal aims to solve in https://github.com/SuperchupuDev/tinyglobby/blob/1b51f7b90fb0b7db09d6d656b6f80341a0eaeffd/src/index.ts#L72-L73 and https://github.com/SuperchupuDev/tinyglobby/pull/18/files' processPath function