Glob patterns


I want to propose Glob to match glob patterns. The idea is to have the same API of RegExp:

const path = 'example/file.js'
const glob = new Glob('**/*.js')

console.log(glob.test(path)) // true

This will be useful for many JS libs that provide their configuration in Glob because is easier to read than regex. Right now all these libs have to do all the conversions from glob to regex adding extra size in their bundles.

There are a lot of languages that support Glob natively, however, JavaScript is not one of them.

That repo link is a 404.

JS has no concept of “file”, and browsers don’t have access to the file system. This might be a better proposal for node?

It will be interesting to also have in the browser (IMO). For example for a router to manage the pathnames. It is just a string matcher, so it does not require any access to the file system.

About the repo, sorry, I got confused about how to create the proposal. I'm going to try it again.

Globs are isomorphic to regexps. The star and globstar patterns are not that hard to translate to actual RegExps (and many routers do exactly that or a variation thereof under the hood).

I'll give it to you, they come optionally with extensions that are less trivial to polyfill, but once again, I don't know how useful they would be in practice vs plain RegExps.

What more, on the server, going through every file in a tree and doing a pattern.test(fullPath) is not at all the most efficient way to proceed.

Suppose you want to match ./foo/bar/**/baz/*.js, you'll first want to limit your search to ./foo/bar/, then look for directories that contain a bar subdirectory, and only then loof for *.js files.

A lib that translates strings with ** and * to actual RegExp, without supporting [] and {} patterns would take a few hundreds of bytes, and there are probably several out there already...

Edit: case in point, after minification, is 760 bytes long, ~440 after gzip compression. OTOH, that lib has 5.5M weekly downloads, so there is demand for such a functionality.