-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHas.ts
More file actions
52 lines (49 loc) · 1.45 KB
/
Has.ts
File metadata and controls
52 lines (49 loc) · 1.45 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
50
51
52
import { Token } from "./lexer"
import { Rule } from "./Rule"
import { Type } from "./Type"
import { Completor } from "./Type/Completor"
export class Has extends Rule {
readonly precedence = 85
readonly class = "Has"
constructor(readonly property: string) {
super()
}
is(value: any, property?: string): boolean {
return typeof value == "object" && value
? Object.entries(value).some(e => {
property = typeof property == "string" ? property : this.property
return e[0] == property
? true
: typeof e[1] == "object" && e[1]
? this.is(
e[1],
property.includes(".") && property.split(".")[0] == e[0] ? property.split(".")[1] : undefined
)
: false
})
: false
}
toString(): string {
return `has(${this.property})`
}
}
export function has(criteria: string): Has
export function has(criteria: string, value?: any): boolean
export function has(criteria: string, value?: any): Has | boolean {
const result = new Has(criteria)
return value ? result.is(value) : result
}
function complete(tokens: Token[], type?: Type.Object, baseObject?: Type.Object): Type.Completion[] | Type.Completion {
return baseObject && type?.class == "object"
? Completor.functions(
tokens,
(tokens?: Token[]) => type.completions.filter(c => c.value.startsWith(tokens ? tokens[0].value : "")),
{
value: "has()",
cursor: 4,
suggestion: { value: "has()" },
}
)
: []
}
Type.Object.add(complete)