-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAny.ts
More file actions
26 lines (25 loc) · 750 Bytes
/
Any.ts
File metadata and controls
26 lines (25 loc) · 750 Bytes
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
import { Criteria } from "./Criteria"
import { create, Rule } from "./Rule"
export class Any extends Rule {
readonly precedence = 50
readonly class = "Any"
constructor(readonly criteria: Rule) {
super()
}
is(value: any): boolean {
return (
value &&
typeof value == "object" &&
Object.getOwnPropertyNames(value).some(property => this.criteria.is(value[property]) || this.is(value[property]))
)
}
toString() {
return this.criteria.stringify(this.precedence)
}
}
export function any(criteria: Criteria): Rule
export function any(criteria: Criteria, value: any): boolean
export function any(criteria: Criteria, value?: any): Rule | boolean {
const result = new Any(create(criteria))
return value ? result.is(value) : result
}