|
| 1 | +/** |
| 2 | + * Matches `/`-separated paths without accessing the file system, using the |
| 3 | + * same pattern syntax as `FileSystem.glob`. |
| 4 | + * |
| 5 | + * @stability experimental |
| 6 | + * @since 4.0.0 |
| 7 | + */ |
| 8 | +import { format } from "./Formatter.ts" |
| 9 | +import { dual } from "./Function.ts" |
| 10 | +import { type Inspectable, NodeInspectSymbol } from "./Inspectable.ts" |
| 11 | +import * as internal from "./internal/glob.ts" |
| 12 | +import { type Pipeable, pipeArguments } from "./Pipeable.ts" |
| 13 | + |
| 14 | +const TypeId = "~effect/Glob" |
| 15 | + |
| 16 | +/** |
| 17 | + * A compiled glob pattern. |
| 18 | + * |
| 19 | + * **Details** |
| 20 | + * |
| 21 | + * `pattern` is the source text and `dot` records whether wildcards match names |
| 22 | + * that start with `.`. Match paths against it with {@link match}. |
| 23 | + * |
| 24 | + * @see {@link make} for compiling a pattern |
| 25 | + * |
| 26 | + * @stability experimental |
| 27 | + * @category models |
| 28 | + * @since 4.0.0 |
| 29 | + */ |
| 30 | +export interface Glob extends Pipeable, Inspectable { |
| 31 | + readonly [TypeId]: typeof TypeId |
| 32 | + readonly pattern: string |
| 33 | + readonly dot: boolean |
| 34 | +} |
| 35 | + |
| 36 | +interface GlobImpl extends Glob { |
| 37 | + readonly compiled: internal.Compiled |
| 38 | +} |
| 39 | + |
| 40 | +const GlobProto = { |
| 41 | + [TypeId]: TypeId, |
| 42 | + toJSON(this: Glob) { |
| 43 | + return { _id: "Glob", pattern: this.pattern, dot: this.dot } |
| 44 | + }, |
| 45 | + toString(this: Glob) { |
| 46 | + return `Glob(${format(this.toJSON())})` |
| 47 | + }, |
| 48 | + [NodeInspectSymbol](this: Glob) { |
| 49 | + return this.toJSON() |
| 50 | + }, |
| 51 | + pipe() { |
| 52 | + return pipeArguments(this, arguments) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * Compiles a glob pattern into a reusable `Glob`. |
| 58 | + * |
| 59 | + * **Details** |
| 60 | + * |
| 61 | + * Patterns use `/` as the separator. `*` matches any run of characters within |
| 62 | + * one path segment, `?` matches one character, and `[...]` matches one |
| 63 | + * character from a set, with `!` or `^` negating it and POSIX classes such as |
| 64 | + * `[[:digit:]]` allowed. `{a,b}` expands to each alternative and may nest. A |
| 65 | + * segment that is exactly `**` matches zero or more whole segments. A trailing |
| 66 | + * `/` matches only directories, and `\` makes the next character literal. |
| 67 | + * |
| 68 | + * **Gotchas** |
| 69 | + * |
| 70 | + * Wildcards and `**` skip names that start with `.` unless the pattern segment |
| 71 | + * itself starts with `.` or `dot` is `true`; they never match `.` or `..`. |
| 72 | + * `x/.` is the same as `x/`, and a `.` or `..` segment after `**` makes the |
| 73 | + * pattern match nothing, as does the empty pattern. UNC patterns and paths |
| 74 | + * such as `//server/share` are not supported and never match. Braces and |
| 75 | + * commas inside `[...]` are class members. Extended glob syntax and leading |
| 76 | + * `!` negation are not interpreted, but ordinary wildcards still apply within |
| 77 | + * them (for example, `?(a)` matches `x(a)`). Malformed syntax, such as an |
| 78 | + * unclosed `[`, matches literally. Braces also stay literal if expansion would |
| 79 | + * exceed 10,000 alternatives. |
| 80 | + * |
| 81 | + * **Example** (Matching source files) |
| 82 | + * |
| 83 | + * ```ts import.meta.vitest |
| 84 | + * import { Glob } from "effect" |
| 85 | + * |
| 86 | + * const sources = Glob.make("src/**\/*.{ts,tsx}") |
| 87 | + * |
| 88 | + * Glob.match(sources, "src/index.ts") // => true |
| 89 | + * Glob.match(sources, "src/ui/Button.tsx") // => true |
| 90 | + * Glob.match(sources, "src/.cache/index.ts") // => false |
| 91 | + * Glob.match(sources, "test/index.ts") // => false |
| 92 | + * ``` |
| 93 | + * |
| 94 | + * @stability experimental |
| 95 | + * @category constructors |
| 96 | + * @since 4.0.0 |
| 97 | + */ |
| 98 | +export const make = (pattern: string, options?: { |
| 99 | + /** Whether wildcards and `**` match names that start with `.`. */ |
| 100 | + readonly dot?: boolean | undefined |
| 101 | +}): Glob => { |
| 102 | + const dot = options?.dot ?? false |
| 103 | + const self = Object.create(GlobProto) |
| 104 | + self.pattern = pattern |
| 105 | + self.dot = dot |
| 106 | + self.compiled = internal.compile([pattern], dot) |
| 107 | + return self |
| 108 | +} |
| 109 | + |
| 110 | +/** |
| 111 | + * Tests whether a `/`-separated path matches a `Glob`. |
| 112 | + * |
| 113 | + * **Details** |
| 114 | + * |
| 115 | + * Empty segments and `./` segments in the path are ignored. A trailing `/` |
| 116 | + * marks the path as a directory, which a pattern ending in `/` requires. |
| 117 | + * |
| 118 | + * **Example** (Filtering paths) |
| 119 | + * |
| 120 | + * ```ts import.meta.vitest |
| 121 | + * import { Glob, pipe } from "effect" |
| 122 | + * |
| 123 | + * const tests = Glob.make("**\/*.test.ts") |
| 124 | + * const paths = ["src/a.ts", "src/a.test.ts", "test/b.test.ts"] |
| 125 | + * |
| 126 | + * paths.filter((path) => Glob.match(tests, path)) // => ["src/a.test.ts", "test/b.test.ts"] |
| 127 | + * pipe(tests, Glob.match("./src/a.test.ts")) // => true |
| 128 | + * ``` |
| 129 | + * |
| 130 | + * @stability experimental |
| 131 | + * @category predicates |
| 132 | + * @since 4.0.0 |
| 133 | + */ |
| 134 | +export const match: { |
| 135 | + (path: string): (self: Glob) => boolean |
| 136 | + (self: Glob, path: string): boolean |
| 137 | +} = dual(2, (self: Glob, path: string): boolean => internal.match((self as GlobImpl).compiled, path)) |
| 138 | + |
| 139 | +/** |
| 140 | + * Escapes glob syntax so a string matches only itself. |
| 141 | + * |
| 142 | + * **Example** (Matching a literal file name) |
| 143 | + * |
| 144 | + * ```ts import.meta.vitest |
| 145 | + * import { Glob } from "effect" |
| 146 | + * |
| 147 | + * const name = "report[1].txt" |
| 148 | + * |
| 149 | + * Glob.match(Glob.make(name), name) // => false |
| 150 | + * Glob.match(Glob.make(Glob.escape(name)), name) // => true |
| 151 | + * ``` |
| 152 | + * |
| 153 | + * @stability experimental |
| 154 | + * @category utils |
| 155 | + * @since 4.0.0 |
| 156 | + */ |
| 157 | +export const escape = (input: string): string => input.replace(/[\\*?[\]{}]/g, "\\$&") |
0 commit comments