import { globToQueryAndRegex } from "https://deno.land/x/earthstar@v10.2.0/src/entries/npm.ts";
Helper for querying Earthstar docs using a glob-style query string.
Given a glob string, return:
- an earthstar Query
- and a regular expression (as a plain string, not a RegExp instance).
Glob strings support '' and '' as wildcards. '' matches any sequence of characters at all including slashes. '' matches any sequence of characters except a forward slash -- it does not span directories in the path. Your glob string may have multiple asterisks in various positions, except they cannot be directly adjecent to each other (no '***' should ever occur -- this will cause a ValidationError to be thrown).
Note that no other wildcards are supported, unlike Bash globs.
To use this function, run the query yourself and apply the regex as a filter to the paths of the resulting documents, to get only the documents whose paths match the glob. The regex will be null if it's not needed (if the query is strong enough to get the job done by itself).
The returned query will use some subset of
the path
, pathStartsWith
, and pathEndswith
properties,
and no other properties.
Example glob strings:
"/posts/*ing/*.json" matches "/posts/sailing/12345.json"
"/posts/**.json" matches "/posts/sailing/12345.json"
and "/posts/a/b/c/d/e/f/g/12345.json"
and "/posts/.json"
"**" matches every possible path
To use it:
let queryByGlob = async (replica: IReplica, glob: string): Promise<Doc[]> => { let { query, regex } = globToQueryAndRegex(glob);
let docs = await replica.queryDocs(query);
if (regex != null) {
let re = new RegExp(regex);
docs = docs.filter(doc => re.test(doc.path));
}
return docs;
}
let posts = await queryByGlob(myReplica, '/posts/*.txt');