Last active
January 5, 2022 22:49
-
-
Save forivall/1c8a512a8dcaba123334ba9dcefb8a06 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* For vscode globs that can't just do `!`, but do have negative char classes | |
* | |
* turn `test` into `{[^t]*,?[^e]*,??[^s]*,???[^t]*}` | |
* `{[^t]*,?{,[^r]*,?{,[^a]*,?[^p]*}}}` | |
* | |
* two directories need to be excluded, the following would be needed: | |
* (ex. for negating 'test' or 'glob') | |
* | |
* `{[^tg]*,?[^el]*,??[^so]*,???[^tb]*,te{sb,ob,ot},t{ls,lo,eo}t,{tl,ge,gl}st,gl{ot,sb,st},g{es,ls,eo}b,{ge,tl,te}ob` | |
* | |
* @see https://github.com/microsoft/vscode/issues/869?ts=2#issuecomment-804615384 | |
*/ | |
function globNegate(s: string): string { | |
switch (s.length) { | |
case 0: | |
return ''; | |
case 1: | |
return `{[^${s}]*,??*}`; | |
case 2: | |
return `{[^${s[0]}]*,?[${s[1]}]*,???*}`; | |
} | |
return [...s] | |
.reduceRight((p, c) => `{,[^${c}]*,?${p}}`, `?*`) | |
.replace(/^\{,/, '{'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment