-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEndsWith.ts
More file actions
38 lines (35 loc) · 1.19 KB
/
EndsWith.ts
File metadata and controls
38 lines (35 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { Token } from "./lexer"
import { Rule } from "./Rule"
import { Type } from "./Type"
import { Completor } from "./Type/Completor"
export class EndsWith extends Rule {
readonly precedence = Number.MAX_SAFE_INTEGER
readonly class = "EndsWith"
constructor(readonly needle: string) {
super()
}
is(value: any): boolean {
return typeof value == "string" && value.endsWith(this.needle)
}
toString() {
return `*${this.needle}`
}
}
export function endsWith(needle: string): EndsWith
export function endsWith(needle: string, value: any): boolean
export function endsWith(needle: string, value?: any): EndsWith | boolean {
const result = new EndsWith(needle)
return value ? result.is(value) : result
}
function complete(tokens: Token[], string: Type.String): Type.Completion[] | Type.Completion {
return Completor.expressions(
tokens,
(tokens?: Token[]) => {
return !tokens || (tokens[0].value == "*" && string.value && string.value.endsWith(tokens[1]?.value ?? ""))
? [Type.Completion.prepend("*", { value: string?.value ?? "", suggestion: { value: string?.value ?? "" } })]
: []
},
{ value: "*", suggestion: { value: "*", description: "endswith" } }
)
}
Type.String.add(complete)