-
Notifications
You must be signed in to change notification settings - Fork 280
/
value-parser.ts
48 lines (41 loc) · 1.41 KB
/
value-parser.ts
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
import { PrimitiveValueListNode } from '../operation-node/primitive-value-list-node.js'
import { ValueListNode } from '../operation-node/value-list-node.js'
import { ValueNode } from '../operation-node/value-node.js'
import { isReadonlyArray } from '../util/object-utils.js'
import {
parseExpression,
ExpressionOrFactory,
isExpressionOrFactory,
} from './expression-parser.js'
import { OperationNode } from '../operation-node/operation-node.js'
export type ValueExpression<DB, TB extends keyof DB, V> =
| V
| ExpressionOrFactory<DB, TB, V>
export type ValueExpressionOrList<DB, TB extends keyof DB, V> =
| ValueExpression<DB, TB, V>
| ReadonlyArray<ValueExpression<DB, TB, V>>
export function parseValueExpressionOrList(
arg: ValueExpressionOrList<any, any, unknown>
): OperationNode {
if (isReadonlyArray(arg)) {
return parseValueExpressionList(arg)
} else {
return parseValueExpression(arg)
}
}
export function parseValueExpression(
exp: ValueExpression<any, any, unknown>
): OperationNode {
if (isExpressionOrFactory(exp)) {
return parseExpression(exp)
}
return ValueNode.create(exp)
}
function parseValueExpressionList(
arg: ReadonlyArray<ValueExpression<any, any, unknown>>
): PrimitiveValueListNode | ValueListNode {
if (arg.some(isExpressionOrFactory)) {
return ValueListNode.create(arg.map((it) => parseValueExpression(it)))
}
return PrimitiveValueListNode.create(arg)
}