-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNot.ts
More file actions
49 lines (46 loc) · 1.35 KB
/
Not.ts
File metadata and controls
49 lines (46 loc) · 1.35 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
39
40
41
42
43
44
45
46
47
48
49
import { Criteria } from "./Criteria"
import { Token } from "./lexer"
import { create, Rule } from "./Rule"
import { Type } from "./Type"
import { Completor } from "./Type/Completor"
export class Not extends Rule {
static readonly precedence = 90
readonly precedence = Not.precedence
readonly class = "Not"
constructor(readonly criteria: Rule) {
super()
}
is(value: any): boolean {
return !this.criteria.is(value)
}
toString(): string {
return `!${this.criteria.stringify(this.precedence)}`
}
}
export function not(criteria: Criteria): Not
export function not(criteria: Criteria, value: any): boolean
export function not(criteria: Criteria, value?: any): Not | boolean {
const result = new Not(create(criteria))
return value ? result.is(value) : result
}
function complete(
tokens: Token[],
type?: Type.String | Type.Number | Type.Boolean | Type.Object,
baseObject?: Type.Object
): Type.Completion[] {
return type && type.class != "object" && baseObject
? Completor.expressions(
tokens,
(tokens: Token[]) => {
return tokens[0].value != "!"
? []
: Type.Completion.prepend("!", type.complete(tokens.slice(1), baseObject, type))
},
{ value: "!", cursor: 1, suggestion: { value: "!", description: "not" } }
)
: []
}
Type.String.add(complete)
Type.Number.add(complete)
Type.Boolean.add(complete)
Type.Object.add(complete)