tinyglobby icon indicating copy to clipboard operation
tinyglobby copied to clipboard

patterns being sorted

Open userquin opened this issue 3 months ago • 2 comments

Check https://github.com/unplugin/unplugin-vue-components/issues/831, fast-glob seems to respect the patterns while tinyglobby applies a sort to the patterns.

Is there something we can do to respect patterns order?

userquin avatar Sep 25 '25 19:09 userquin

both tinyglobby and fast-glob return results in an arbitrary order, which is documented at https://superchupu.dev/tinyglobby/comparison#ordering, you shouldn't rely on the results being sorted in any particular way on either of the libraries. so the only thing that can be done is for a user to sort it themselves sadly

SuperchupuDev avatar Sep 25 '25 19:09 SuperchupuDev

@SuperchupuDev any chance to add some new option here to allow sort results by patterns? Maybe I can contribute, otherwise I need to add picomatch and some of the logic here at unplugin-vue-components to do the sort:

private sortPathsByGlobPrecedence(files: string[]) {
    const sortedResults = []
    const processedFiles = new Set()

    const { globs, ...rest } = this.options
    for (const glob of globs) {
      const isMatch = picomatch(glob, rest) // <=== here we need to include some normalization done at tinyglobby

      for (const file of files) {
        if (!processedFiles.has(file) && isMatch(file)) {
          sortedResults.push(file)
          processedFiles.add(file)
        }
      }
    }

    return sortedResults
  }

userquin avatar Sep 25 '25 20:09 userquin