@react-querybuilder/datetime 8.8.4 → 8.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/date-fns.d.ts +1 -0
- package/dist/cjs/dayjs.d.ts +1 -0
- package/dist/cjs/getDatetimeRuleProcessorSQL-D95jgDK6.js +2 -0
- package/dist/cjs/getDatetimeRuleProcessorSQL-D95jgDK6.js.map +1 -0
- package/dist/cjs/index.d.ts +1 -0
- package/dist/cjs/jsdate.d.ts +1 -0
- package/dist/cjs/luxon.d.ts +1 -0
- package/dist/cjs/react-querybuilder_datetime.cjs.development.d.ts +108 -0
- package/dist/cjs/react-querybuilder_datetime.cjs.development.date-fns.d.ts +53 -0
- package/dist/cjs/react-querybuilder_datetime.cjs.development.date-fns.js +12 -11
- package/dist/cjs/react-querybuilder_datetime.cjs.development.date-fns.js.map +1 -1
- package/dist/cjs/react-querybuilder_datetime.cjs.development.dayjs.d.ts +53 -0
- package/dist/cjs/react-querybuilder_datetime.cjs.development.dayjs.js +12 -11
- package/dist/cjs/react-querybuilder_datetime.cjs.development.dayjs.js.map +1 -1
- package/dist/cjs/react-querybuilder_datetime.cjs.development.js +5 -4
- package/dist/cjs/react-querybuilder_datetime.cjs.development.js.map +1 -1
- package/dist/cjs/react-querybuilder_datetime.cjs.development.jsdate.d.ts +56 -0
- package/dist/cjs/react-querybuilder_datetime.cjs.development.jsdate.js +10 -10
- package/dist/cjs/react-querybuilder_datetime.cjs.development.jsdate.js.map +1 -1
- package/dist/cjs/react-querybuilder_datetime.cjs.development.luxon.d.ts +53 -0
- package/dist/cjs/react-querybuilder_datetime.cjs.development.luxon.js +12 -11
- package/dist/cjs/react-querybuilder_datetime.cjs.development.luxon.js.map +1 -1
- package/dist/cjs/react-querybuilder_datetime.cjs.production.d.ts +108 -0
- package/dist/cjs/react-querybuilder_datetime.cjs.production.date-fns.d.ts +53 -0
- package/dist/cjs/react-querybuilder_datetime.cjs.production.date-fns.js +1 -1
- package/dist/cjs/react-querybuilder_datetime.cjs.production.date-fns.js.map +1 -1
- package/dist/cjs/react-querybuilder_datetime.cjs.production.dayjs.d.ts +53 -0
- package/dist/cjs/react-querybuilder_datetime.cjs.production.dayjs.js +1 -1
- package/dist/cjs/react-querybuilder_datetime.cjs.production.dayjs.js.map +1 -1
- package/dist/cjs/react-querybuilder_datetime.cjs.production.js +1 -1
- package/dist/cjs/react-querybuilder_datetime.cjs.production.js.map +1 -1
- package/dist/cjs/react-querybuilder_datetime.cjs.production.jsdate.d.ts +56 -0
- package/dist/cjs/react-querybuilder_datetime.cjs.production.jsdate.js +1 -1
- package/dist/cjs/react-querybuilder_datetime.cjs.production.jsdate.js.map +1 -1
- package/dist/cjs/react-querybuilder_datetime.cjs.production.luxon.d.ts +53 -0
- package/dist/cjs/react-querybuilder_datetime.cjs.production.luxon.js +1 -1
- package/dist/cjs/react-querybuilder_datetime.cjs.production.luxon.js.map +1 -1
- package/dist/cjs/types-QCQUGVZa.d.ts +1694 -0
- package/dist/{getDatetimeRuleProcessorSQL-CutYTD23.js → getDatetimeRuleProcessorSQL-CN80oEFK.js} +23 -29
- package/dist/getDatetimeRuleProcessorSQL-CN80oEFK.js.map +1 -0
- package/dist/{getDatetimeRuleProcessorSQL-CocyaiI5.mjs → getDatetimeRuleProcessorSQL-KYRPP9qd.mjs} +17 -23
- package/dist/getDatetimeRuleProcessorSQL-KYRPP9qd.mjs.map +1 -0
- package/dist/react-querybuilder_datetime.d.mts +108 -0
- package/dist/react-querybuilder_datetime.date-fns.d.mts +53 -0
- package/dist/react-querybuilder_datetime.date-fns.mjs +10 -10
- package/dist/react-querybuilder_datetime.date-fns.mjs.map +1 -1
- package/dist/react-querybuilder_datetime.dayjs.d.mts +53 -0
- package/dist/react-querybuilder_datetime.dayjs.mjs +10 -10
- package/dist/react-querybuilder_datetime.dayjs.mjs.map +1 -1
- package/dist/react-querybuilder_datetime.jsdate.d.mts +56 -0
- package/dist/react-querybuilder_datetime.jsdate.mjs +10 -10
- package/dist/react-querybuilder_datetime.jsdate.mjs.map +1 -1
- package/dist/react-querybuilder_datetime.legacy-esm.d.ts +108 -0
- package/dist/react-querybuilder_datetime.legacy-esm.date-fns.d.ts +53 -0
- package/dist/react-querybuilder_datetime.legacy-esm.date-fns.js +10 -10
- package/dist/react-querybuilder_datetime.legacy-esm.date-fns.js.map +1 -1
- package/dist/react-querybuilder_datetime.legacy-esm.dayjs.d.ts +53 -0
- package/dist/react-querybuilder_datetime.legacy-esm.dayjs.js +10 -10
- package/dist/react-querybuilder_datetime.legacy-esm.dayjs.js.map +1 -1
- package/dist/react-querybuilder_datetime.legacy-esm.js +4 -4
- package/dist/react-querybuilder_datetime.legacy-esm.js.map +1 -1
- package/dist/react-querybuilder_datetime.legacy-esm.jsdate.d.ts +56 -0
- package/dist/react-querybuilder_datetime.legacy-esm.jsdate.js +11 -11
- package/dist/react-querybuilder_datetime.legacy-esm.jsdate.js.map +1 -1
- package/dist/react-querybuilder_datetime.legacy-esm.luxon.d.ts +53 -0
- package/dist/react-querybuilder_datetime.legacy-esm.luxon.js +10 -10
- package/dist/react-querybuilder_datetime.legacy-esm.luxon.js.map +1 -1
- package/dist/react-querybuilder_datetime.luxon.d.mts +53 -0
- package/dist/react-querybuilder_datetime.luxon.mjs +10 -10
- package/dist/react-querybuilder_datetime.luxon.mjs.map +1 -1
- package/dist/react-querybuilder_datetime.mjs +3 -3
- package/dist/react-querybuilder_datetime.mjs.map +1 -1
- package/dist/react-querybuilder_datetime.production.d.mts +108 -0
- package/dist/react-querybuilder_datetime.production.date-fns.d.mts +53 -0
- package/dist/react-querybuilder_datetime.production.date-fns.mjs +1 -1
- package/dist/react-querybuilder_datetime.production.date-fns.mjs.map +1 -1
- package/dist/react-querybuilder_datetime.production.dayjs.d.mts +53 -0
- package/dist/react-querybuilder_datetime.production.dayjs.mjs +1 -1
- package/dist/react-querybuilder_datetime.production.dayjs.mjs.map +1 -1
- package/dist/react-querybuilder_datetime.production.jsdate.d.mts +56 -0
- package/dist/react-querybuilder_datetime.production.jsdate.mjs +1 -1
- package/dist/react-querybuilder_datetime.production.jsdate.mjs.map +1 -1
- package/dist/react-querybuilder_datetime.production.luxon.d.mts +53 -0
- package/dist/react-querybuilder_datetime.production.luxon.mjs +1 -1
- package/dist/react-querybuilder_datetime.production.luxon.mjs.map +1 -1
- package/dist/react-querybuilder_datetime.production.mjs +1 -1
- package/dist/react-querybuilder_datetime.production.mjs.map +1 -1
- package/dist/types-CDEkL2M_.d.mts +1694 -0
- package/dist/types-EgfSemtR.d.ts +1694 -0
- package/package.json +23 -23
- package/dist/cjs/getDatetimeRuleProcessorSQL-npwUe9aM.js +0 -2
- package/dist/cjs/getDatetimeRuleProcessorSQL-npwUe9aM.js.map +0 -1
- package/dist/getDatetimeRuleProcessorSQL-CocyaiI5.mjs.map +0 -1
- package/dist/getDatetimeRuleProcessorSQL-CutYTD23.js.map +0 -1
- package/dist/types/QueryBuilderDateTime.d.ts +0 -4
- package/dist/types/datetimeRuleProcessorJsonLogic.d.ts +0 -10
- package/dist/types/getDatetimeJsonLogicOperations.d.ts +0 -6
- package/dist/types/getDatetimeRuleProcessorCEL.d.ts +0 -7
- package/dist/types/getDatetimeRuleProcessorJSONata.d.ts +0 -7
- package/dist/types/getDatetimeRuleProcessorMongoDBQuery.d.ts +0 -7
- package/dist/types/getDatetimeRuleProcessorNL.d.ts +0 -13
- package/dist/types/getDatetimeRuleProcessorSQL.d.ts +0 -32
- package/dist/types/index.d.ts +0 -8
- package/dist/types/index.date-fns.d.ts +0 -48
- package/dist/types/index.dayjs.d.ts +0 -48
- package/dist/types/index.jsdate.d.ts +0 -48
- package/dist/types/index.luxon.d.ts +0 -48
- package/dist/types/rqbDateTimeLibraryAPI.date-fns.d.ts +0 -5
- package/dist/types/rqbDateTimeLibraryAPI.dayjs.d.ts +0 -5
- package/dist/types/rqbDateTimeLibraryAPI.jsdate.d.ts +0 -8
- package/dist/types/rqbDateTimeLibraryAPI.luxon.d.ts +0 -5
- package/dist/types/types.d.ts +0 -111
- package/dist/types/utils.d.ts +0 -5
- package/dist/types-esm/QueryBuilderDateTime.d.mts +0 -4
- package/dist/types-esm/datetimeRuleProcessorJsonLogic.d.mts +0 -10
- package/dist/types-esm/getDatetimeJsonLogicOperations.d.mts +0 -6
- package/dist/types-esm/getDatetimeRuleProcessorCEL.d.mts +0 -7
- package/dist/types-esm/getDatetimeRuleProcessorJSONata.d.mts +0 -7
- package/dist/types-esm/getDatetimeRuleProcessorMongoDBQuery.d.mts +0 -7
- package/dist/types-esm/getDatetimeRuleProcessorNL.d.mts +0 -13
- package/dist/types-esm/getDatetimeRuleProcessorSQL.d.mts +0 -32
- package/dist/types-esm/index.d.mts +0 -8
- package/dist/types-esm/index.date-fns.d.mts +0 -48
- package/dist/types-esm/index.dayjs.d.mts +0 -48
- package/dist/types-esm/index.jsdate.d.mts +0 -48
- package/dist/types-esm/index.luxon.d.mts +0 -48
- package/dist/types-esm/rqbDateTimeLibraryAPI.date-fns.d.mts +0 -5
- package/dist/types-esm/rqbDateTimeLibraryAPI.dayjs.d.mts +0 -5
- package/dist/types-esm/rqbDateTimeLibraryAPI.jsdate.d.mts +0 -8
- package/dist/types-esm/rqbDateTimeLibraryAPI.luxon.d.mts +0 -5
- package/dist/types-esm/types.d.mts +0 -111
- package/dist/types-esm/utils.d.mts +0 -5
|
@@ -0,0 +1,1694 @@
|
|
|
1
|
+
import { RulesLogic } from "json-logic-js";
|
|
2
|
+
|
|
3
|
+
//#region ../core/src/types/type-fest/is-equal.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
Returns a boolean for whether the two given types are equal.
|
|
7
|
+
|
|
8
|
+
@link https://github.com/microsoft/TypeScript/issues/27024#issuecomment-421529650
|
|
9
|
+
@link https://stackoverflow.com/questions/68961864/how-does-the-equals-work-in-typescript/68963796#68963796
|
|
10
|
+
|
|
11
|
+
Use-cases:
|
|
12
|
+
- If you want to make a conditional branch based on the result of a comparison of two types.
|
|
13
|
+
|
|
14
|
+
@example
|
|
15
|
+
```
|
|
16
|
+
import type {IsEqual} from 'type-fest';
|
|
17
|
+
|
|
18
|
+
// This type returns a boolean for whether the given array includes the given item.
|
|
19
|
+
// `IsEqual` is used to compare the given array at position 0 and the given item and then return true if they are equal.
|
|
20
|
+
type Includes<Value extends readonly any[], Item> =
|
|
21
|
+
Value extends readonly [Value[0], ...infer rest]
|
|
22
|
+
? IsEqual<Value[0], Item> extends true
|
|
23
|
+
? true
|
|
24
|
+
: Includes<rest, Item>
|
|
25
|
+
: false;
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
@group type-fest
|
|
29
|
+
*/
|
|
30
|
+
type IsEqual<A, B> = (<G>() => G extends A & G | G ? 1 : 2) extends (<G>() => G extends B & G | G ? 1 : 2) ? true : false;
|
|
31
|
+
//#endregion
|
|
32
|
+
//#region ../core/src/types/type-fest/is-never.d.ts
|
|
33
|
+
/**
|
|
34
|
+
Returns a boolean for whether the given type is `never`.
|
|
35
|
+
|
|
36
|
+
@link https://github.com/microsoft/TypeScript/issues/31751#issuecomment-498526919
|
|
37
|
+
@link https://stackoverflow.com/a/53984913/10292952
|
|
38
|
+
@link https://www.zhenghao.io/posts/ts-never
|
|
39
|
+
|
|
40
|
+
Useful in type utilities, such as checking if something does not occur.
|
|
41
|
+
|
|
42
|
+
@example
|
|
43
|
+
```
|
|
44
|
+
import type {IsNever, And} from 'type-fest';
|
|
45
|
+
|
|
46
|
+
// https://github.com/andnp/SimplyTyped/blob/master/src/types/strings.ts
|
|
47
|
+
type AreStringsEqual<A extends string, B extends string> =
|
|
48
|
+
And<
|
|
49
|
+
IsNever<Exclude<A, B>> extends true ? true : false,
|
|
50
|
+
IsNever<Exclude<B, A>> extends true ? true : false
|
|
51
|
+
>;
|
|
52
|
+
|
|
53
|
+
type EndIfEqual<I extends string, O extends string> =
|
|
54
|
+
AreStringsEqual<I, O> extends true
|
|
55
|
+
? never
|
|
56
|
+
: void;
|
|
57
|
+
|
|
58
|
+
function endIfEqual<I extends string, O extends string>(input: I, output: O): EndIfEqual<I, O> {
|
|
59
|
+
if (input === output) {
|
|
60
|
+
process.exit(0);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
endIfEqual('abc', 'abc');
|
|
65
|
+
//=> never
|
|
66
|
+
|
|
67
|
+
endIfEqual('abc', '123');
|
|
68
|
+
//=> void
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
@group type-fest
|
|
72
|
+
*/
|
|
73
|
+
type IsNever<T> = [T] extends [never] ? true : false;
|
|
74
|
+
//#endregion
|
|
75
|
+
//#region ../core/src/types/type-fest/if-never.d.ts
|
|
76
|
+
/**
|
|
77
|
+
An if-else-like type that resolves depending on whether the given type is `never`.
|
|
78
|
+
|
|
79
|
+
@see {@link IsNever}
|
|
80
|
+
|
|
81
|
+
@example
|
|
82
|
+
```
|
|
83
|
+
import type {IfNever} from 'type-fest';
|
|
84
|
+
|
|
85
|
+
type ShouldBeTrue = IfNever<never>;
|
|
86
|
+
//=> true
|
|
87
|
+
|
|
88
|
+
type ShouldBeBar = IfNever<'not never', 'foo', 'bar'>;
|
|
89
|
+
//=> 'bar'
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
@group type-fest
|
|
93
|
+
*/
|
|
94
|
+
type IfNever$1<T, TypeIfNever = true, TypeIfNotNever = false> = (IsNever<T> extends true ? TypeIfNever : TypeIfNotNever);
|
|
95
|
+
//#endregion
|
|
96
|
+
//#region ../core/src/types/type-fest/unknown-array.d.ts
|
|
97
|
+
/**
|
|
98
|
+
Represents an array with `unknown` value.
|
|
99
|
+
|
|
100
|
+
Use case: You want a type that all arrays can be assigned to, but you don't care about the value.
|
|
101
|
+
|
|
102
|
+
@example
|
|
103
|
+
```
|
|
104
|
+
import type {UnknownArray} from 'type-fest';
|
|
105
|
+
|
|
106
|
+
type IsArray<T> = T extends UnknownArray ? true : false;
|
|
107
|
+
|
|
108
|
+
type A = IsArray<['foo']>;
|
|
109
|
+
//=> true
|
|
110
|
+
|
|
111
|
+
type B = IsArray<readonly number[]>;
|
|
112
|
+
//=> true
|
|
113
|
+
|
|
114
|
+
type C = IsArray<string>;
|
|
115
|
+
//=> false
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
@group type-fest
|
|
119
|
+
*/
|
|
120
|
+
type UnknownArray = readonly unknown[];
|
|
121
|
+
//#endregion
|
|
122
|
+
//#region ../core/src/types/type-fest/internal/array.d.ts
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
Returns whether the given array `T` is readonly.
|
|
126
|
+
|
|
127
|
+
@group type-fest
|
|
128
|
+
*/
|
|
129
|
+
type IsArrayReadonly<T extends UnknownArray> = IfNever$1<T, false, T extends unknown[] ? false : true>;
|
|
130
|
+
/**
|
|
131
|
+
An if-else-like type that resolves depending on whether the given array is readonly.
|
|
132
|
+
|
|
133
|
+
@see {@link IsArrayReadonly}
|
|
134
|
+
|
|
135
|
+
@example
|
|
136
|
+
```
|
|
137
|
+
import type {ArrayTail} from 'type-fest';
|
|
138
|
+
|
|
139
|
+
type ReadonlyPreservingArrayTail<TArray extends readonly unknown[]> =
|
|
140
|
+
ArrayTail<TArray> extends infer Tail
|
|
141
|
+
? IfArrayReadonly<TArray, Readonly<Tail>, Tail>
|
|
142
|
+
: never;
|
|
143
|
+
|
|
144
|
+
type ReadonlyTail = ReadonlyPreservingArrayTail<readonly [string, number, boolean]>;
|
|
145
|
+
//=> readonly [number, boolean]
|
|
146
|
+
|
|
147
|
+
type NonReadonlyTail = ReadonlyPreservingArrayTail<[string, number, boolean]>;
|
|
148
|
+
//=> [number, boolean]
|
|
149
|
+
|
|
150
|
+
type ShouldBeTrue = IfArrayReadonly<readonly unknown[]>;
|
|
151
|
+
//=> true
|
|
152
|
+
|
|
153
|
+
type ShouldBeBar = IfArrayReadonly<unknown[], 'foo', 'bar'>;
|
|
154
|
+
//=> 'bar'
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
@group type-fest
|
|
158
|
+
*/
|
|
159
|
+
type IfArrayReadonly<T extends UnknownArray, TypeIfArrayReadonly = true, TypeIfNotArrayReadonly = false> = IsArrayReadonly<T> extends infer Result ? Result extends true ? TypeIfArrayReadonly : TypeIfNotArrayReadonly : never;
|
|
160
|
+
//#endregion
|
|
161
|
+
//#region ../core/src/types/type-fest/is-any.d.ts
|
|
162
|
+
type NoInfer<T> = T extends infer U ? U : never;
|
|
163
|
+
/**
|
|
164
|
+
Returns a boolean for whether the given type is `any`.
|
|
165
|
+
|
|
166
|
+
@link https://stackoverflow.com/a/49928360/1490091
|
|
167
|
+
|
|
168
|
+
Useful in type utilities, such as disallowing `any`s to be passed to a function.
|
|
169
|
+
|
|
170
|
+
@example
|
|
171
|
+
```
|
|
172
|
+
import type {IsAny} from 'type-fest';
|
|
173
|
+
|
|
174
|
+
const typedObject = {a: 1, b: 2} as const;
|
|
175
|
+
const anyObject: any = {a: 1, b: 2};
|
|
176
|
+
|
|
177
|
+
function get<O extends (IsAny<O> extends true ? {} : Record<string, number>), K extends keyof O = keyof O>(obj: O, key: K) {
|
|
178
|
+
return obj[key];
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const typedA = get(typedObject, 'a');
|
|
182
|
+
//=> 1
|
|
183
|
+
|
|
184
|
+
const anyA = get(anyObject, 'a');
|
|
185
|
+
//=> any
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
@group type-fest
|
|
189
|
+
*/
|
|
190
|
+
type IsAny<T> = 0 extends 1 & NoInfer<T> ? true : false;
|
|
191
|
+
//#endregion
|
|
192
|
+
//#region ../core/src/types/type-fest/simplify.d.ts
|
|
193
|
+
/**
|
|
194
|
+
Useful to flatten the type output to improve type hints shown in editors. And also to transform an interface into a type to aide with assignability.
|
|
195
|
+
|
|
196
|
+
@example
|
|
197
|
+
```
|
|
198
|
+
import type {Simplify} from 'type-fest';
|
|
199
|
+
|
|
200
|
+
type PositionProps = {
|
|
201
|
+
top: number;
|
|
202
|
+
left: number;
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
type SizeProps = {
|
|
206
|
+
width: number;
|
|
207
|
+
height: number;
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
// In your editor, hovering over `Props` will show a flattened object with all the properties.
|
|
211
|
+
type Props = Simplify<PositionProps & SizeProps>;
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
Sometimes it is desired to pass a value as a function argument that has a different type. At first inspection it may seem assignable, and then you discover it is not because the `value`'s type definition was defined as an interface. In the following example, `fn` requires an argument of type `Record<string, unknown>`. If the value is defined as a literal, then it is assignable. And if the `value` is defined as type using the `Simplify` utility the value is assignable. But if the `value` is defined as an interface, it is not assignable because the interface is not sealed and elsewhere a non-string property could be added to the interface.
|
|
215
|
+
|
|
216
|
+
If the type definition must be an interface (perhaps it was defined in a third-party npm package), then the `value` can be defined as `const value: Simplify<SomeInterface> = ...`. Then `value` will be assignable to the `fn` argument. Or the `value` can be cast as `Simplify<SomeInterface>` if you can't re-declare the `value`.
|
|
217
|
+
|
|
218
|
+
@example
|
|
219
|
+
```
|
|
220
|
+
import type {Simplify} from 'type-fest';
|
|
221
|
+
|
|
222
|
+
interface SomeInterface {
|
|
223
|
+
foo: number;
|
|
224
|
+
bar?: string;
|
|
225
|
+
baz: number | undefined;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
type SomeType = {
|
|
229
|
+
foo: number;
|
|
230
|
+
bar?: string;
|
|
231
|
+
baz: number | undefined;
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
const literal = {foo: 123, bar: 'hello', baz: 456};
|
|
235
|
+
const someType: SomeType = literal;
|
|
236
|
+
const someInterface: SomeInterface = literal;
|
|
237
|
+
|
|
238
|
+
function fn(object: Record<string, unknown>): void {}
|
|
239
|
+
|
|
240
|
+
fn(literal); // Good: literal object type is sealed
|
|
241
|
+
fn(someType); // Good: type is sealed
|
|
242
|
+
fn(someInterface); // Error: Index signature for type 'string' is missing in type 'someInterface'. Because `interface` can be re-opened
|
|
243
|
+
fn(someInterface as Simplify<SomeInterface>); // Good: transform an `interface` into a `type`
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
@link https://github.com/microsoft/TypeScript/issues/15300
|
|
247
|
+
@see SimplifyDeep
|
|
248
|
+
|
|
249
|
+
@group type-fest
|
|
250
|
+
*/
|
|
251
|
+
type Simplify<T> = { [KeyType in keyof T]: T[KeyType] } & {};
|
|
252
|
+
//#endregion
|
|
253
|
+
//#region ../core/src/types/type-fest/union-to-intersection.d.ts
|
|
254
|
+
/**
|
|
255
|
+
Convert a union type to an intersection type using [distributive conditional types](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types).
|
|
256
|
+
|
|
257
|
+
Inspired by [this Stack Overflow answer](https://stackoverflow.com/a/50375286/2172153).
|
|
258
|
+
|
|
259
|
+
@example
|
|
260
|
+
```
|
|
261
|
+
import type {UnionToIntersection} from 'type-fest';
|
|
262
|
+
|
|
263
|
+
type Union = {the(): void} | {great(arg: string): void} | {escape: boolean};
|
|
264
|
+
|
|
265
|
+
type Intersection = UnionToIntersection<Union>;
|
|
266
|
+
//=> {the(): void; great(arg: string): void; escape: boolean};
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
A more applicable example which could make its way into your library code follows.
|
|
270
|
+
|
|
271
|
+
@example
|
|
272
|
+
```
|
|
273
|
+
import type {UnionToIntersection} from 'type-fest';
|
|
274
|
+
|
|
275
|
+
class CommandOne {
|
|
276
|
+
commands: {
|
|
277
|
+
a1: () => undefined,
|
|
278
|
+
b1: () => undefined,
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
class CommandTwo {
|
|
283
|
+
commands: {
|
|
284
|
+
a2: (argA: string) => undefined,
|
|
285
|
+
b2: (argB: string) => undefined,
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
const union = [new CommandOne(), new CommandTwo()].map(instance => instance.commands);
|
|
290
|
+
type Union = typeof union;
|
|
291
|
+
//=> {a1(): void; b1(): void} | {a2(argA: string): void; b2(argB: string): void}
|
|
292
|
+
|
|
293
|
+
type Intersection = UnionToIntersection<Union>;
|
|
294
|
+
//=> {a1(): void; b1(): void; a2(argA: string): void; b2(argB: string): void}
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
@group type-fest
|
|
298
|
+
*/
|
|
299
|
+
type UnionToIntersection<Union> = (Union extends unknown ? (distributedUnion: Union) => void : never) extends ((mergedIntersection: infer Intersection) => void) ? Intersection & Union : never;
|
|
300
|
+
//#endregion
|
|
301
|
+
//#region ../core/src/types/type-fest/keys-of-union.d.ts
|
|
302
|
+
/**
|
|
303
|
+
Create a union of all keys from a given type, even those exclusive to specific union members.
|
|
304
|
+
|
|
305
|
+
Unlike the native `keyof` keyword, which returns keys present in **all** union members, this type returns keys from **any** member.
|
|
306
|
+
|
|
307
|
+
@link https://stackoverflow.com/a/49402091
|
|
308
|
+
|
|
309
|
+
@example
|
|
310
|
+
```
|
|
311
|
+
import type {KeysOfUnion} from 'type-fest';
|
|
312
|
+
|
|
313
|
+
type A = {
|
|
314
|
+
common: string;
|
|
315
|
+
a: number;
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
type B = {
|
|
319
|
+
common: string;
|
|
320
|
+
b: string;
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
type C = {
|
|
324
|
+
common: string;
|
|
325
|
+
c: boolean;
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
type Union = A | B | C;
|
|
329
|
+
|
|
330
|
+
type CommonKeys = keyof Union;
|
|
331
|
+
//=> 'common'
|
|
332
|
+
|
|
333
|
+
type AllKeys = KeysOfUnion<Union>;
|
|
334
|
+
//=> 'common' | 'a' | 'b' | 'c'
|
|
335
|
+
```
|
|
336
|
+
|
|
337
|
+
@group type-fest
|
|
338
|
+
*/
|
|
339
|
+
type KeysOfUnion<ObjectType> = keyof UnionToIntersection<ObjectType extends unknown ? Record<keyof ObjectType, never> : never>;
|
|
340
|
+
//#endregion
|
|
341
|
+
//#region ../core/src/types/type-fest/optional-keys-of.d.ts
|
|
342
|
+
/**
|
|
343
|
+
Extract all optional keys from the given type.
|
|
344
|
+
|
|
345
|
+
This is useful when you want to create a new type that contains different type values for the optional keys only.
|
|
346
|
+
|
|
347
|
+
@example
|
|
348
|
+
```
|
|
349
|
+
import type {OptionalKeysOf, Except} from 'type-fest';
|
|
350
|
+
|
|
351
|
+
interface User {
|
|
352
|
+
name: string;
|
|
353
|
+
surname: string;
|
|
354
|
+
|
|
355
|
+
luckyNumber?: number;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
const REMOVE_FIELD = Symbol('remove field symbol');
|
|
359
|
+
type UpdateOperation<Entity extends object> = Except<Partial<Entity>, OptionalKeysOf<Entity>> & {
|
|
360
|
+
[Key in OptionalKeysOf<Entity>]?: Entity[Key] | typeof REMOVE_FIELD;
|
|
361
|
+
};
|
|
362
|
+
|
|
363
|
+
const update1: UpdateOperation<User> = {
|
|
364
|
+
name: 'Alice'
|
|
365
|
+
};
|
|
366
|
+
|
|
367
|
+
const update2: UpdateOperation<User> = {
|
|
368
|
+
name: 'Bob',
|
|
369
|
+
luckyNumber: REMOVE_FIELD
|
|
370
|
+
};
|
|
371
|
+
```
|
|
372
|
+
|
|
373
|
+
@group type-fest
|
|
374
|
+
*/
|
|
375
|
+
type OptionalKeysOf<BaseType extends object> = BaseType extends unknown ? (keyof { [Key in keyof BaseType as BaseType extends Record<Key, BaseType[Key]> ? never : Key]: never }) & (keyof BaseType) : never;
|
|
376
|
+
//#endregion
|
|
377
|
+
//#region ../core/src/types/type-fest/required-keys-of.d.ts
|
|
378
|
+
/**
|
|
379
|
+
Extract all required keys from the given type.
|
|
380
|
+
|
|
381
|
+
This is useful when you want to create a new type that contains different type values for the required keys only or use the list of keys for validation purposes, etc...
|
|
382
|
+
|
|
383
|
+
@example
|
|
384
|
+
```
|
|
385
|
+
import type {RequiredKeysOf} from 'type-fest';
|
|
386
|
+
|
|
387
|
+
declare function createValidation<Entity extends object, Key extends RequiredKeysOf<Entity> = RequiredKeysOf<Entity>>(field: Key, validator: (value: Entity[Key]) => boolean): ValidatorFn;
|
|
388
|
+
|
|
389
|
+
interface User {
|
|
390
|
+
name: string;
|
|
391
|
+
surname: string;
|
|
392
|
+
|
|
393
|
+
luckyNumber?: number;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
const validator1 = createValidation<User>('name', value => value.length < 25);
|
|
397
|
+
const validator2 = createValidation<User>('surname', value => value.length < 25);
|
|
398
|
+
```
|
|
399
|
+
|
|
400
|
+
@group type-fest
|
|
401
|
+
*/
|
|
402
|
+
type RequiredKeysOf<BaseType extends object> = BaseType extends unknown ? Exclude<keyof BaseType, OptionalKeysOf<BaseType>> : never;
|
|
403
|
+
//#endregion
|
|
404
|
+
//#region ../core/src/types/type-fest/omit-index-signature.d.ts
|
|
405
|
+
/**
|
|
406
|
+
Omit any index signatures from the given object type, leaving only explicitly defined properties.
|
|
407
|
+
|
|
408
|
+
This is the counterpart of `PickIndexSignature`.
|
|
409
|
+
|
|
410
|
+
Use-cases:
|
|
411
|
+
- Remove overly permissive signatures from third-party types.
|
|
412
|
+
|
|
413
|
+
This type was taken from this [StackOverflow answer](https://stackoverflow.com/a/68261113/420747).
|
|
414
|
+
|
|
415
|
+
It relies on the fact that an empty object (`{}`) is assignable to an object with just an index signature, like `Record<string, unknown>`, but not to an object with explicitly defined keys, like `Record<'foo' | 'bar', unknown>`.
|
|
416
|
+
|
|
417
|
+
(The actual value type, `unknown`, is irrelevant and could be any type. Only the key type matters.)
|
|
418
|
+
|
|
419
|
+
```
|
|
420
|
+
const indexed: Record<string, unknown> = {}; // Allowed
|
|
421
|
+
|
|
422
|
+
const keyed: Record<'foo', unknown> = {}; // Error
|
|
423
|
+
// => TS2739: Type '{}' is missing the following properties from type 'Record<"foo" | "bar", unknown>': foo, bar
|
|
424
|
+
```
|
|
425
|
+
|
|
426
|
+
Instead of causing a type error like the above, you can also use a [conditional type](https://www.typescriptlang.org/docs/handbook/2/conditional-types.html) to test whether a type is assignable to another:
|
|
427
|
+
|
|
428
|
+
```
|
|
429
|
+
type Indexed = {} extends Record<string, unknown>
|
|
430
|
+
? '✅ `{}` is assignable to `Record<string, unknown>`'
|
|
431
|
+
: '❌ `{}` is NOT assignable to `Record<string, unknown>`';
|
|
432
|
+
// => '✅ `{}` is assignable to `Record<string, unknown>`'
|
|
433
|
+
|
|
434
|
+
type Keyed = {} extends Record<'foo' | 'bar', unknown>
|
|
435
|
+
? "✅ `{}` is assignable to `Record<'foo' | 'bar', unknown>`"
|
|
436
|
+
: "❌ `{}` is NOT assignable to `Record<'foo' | 'bar', unknown>`";
|
|
437
|
+
// => "❌ `{}` is NOT assignable to `Record<'foo' | 'bar', unknown>`"
|
|
438
|
+
```
|
|
439
|
+
|
|
440
|
+
Using a [mapped type](https://www.typescriptlang.org/docs/handbook/2/mapped-types.html#further-exploration), you can then check for each `KeyType` of `ObjectType`...
|
|
441
|
+
|
|
442
|
+
```
|
|
443
|
+
import type {OmitIndexSignature} from 'type-fest';
|
|
444
|
+
|
|
445
|
+
type OmitIndexSignature<ObjectType> = {
|
|
446
|
+
[KeyType in keyof ObjectType // Map each key of `ObjectType`...
|
|
447
|
+
]: ObjectType[KeyType]; // ...to its original value, i.e. `OmitIndexSignature<Foo> == Foo`.
|
|
448
|
+
};
|
|
449
|
+
```
|
|
450
|
+
|
|
451
|
+
...whether an empty object (`{}`) would be assignable to an object with that `KeyType` (`Record<KeyType, unknown>`)...
|
|
452
|
+
|
|
453
|
+
```
|
|
454
|
+
import type {OmitIndexSignature} from 'type-fest';
|
|
455
|
+
|
|
456
|
+
type OmitIndexSignature<ObjectType> = {
|
|
457
|
+
[KeyType in keyof ObjectType
|
|
458
|
+
// Is `{}` assignable to `Record<KeyType, unknown>`?
|
|
459
|
+
as {} extends Record<KeyType, unknown>
|
|
460
|
+
? ... // ✅ `{}` is assignable to `Record<KeyType, unknown>`
|
|
461
|
+
: ... // ❌ `{}` is NOT assignable to `Record<KeyType, unknown>`
|
|
462
|
+
]: ObjectType[KeyType];
|
|
463
|
+
};
|
|
464
|
+
```
|
|
465
|
+
|
|
466
|
+
If `{}` is assignable, it means that `KeyType` is an index signature and we want to remove it. If it is not assignable, `KeyType` is a "real" key and we want to keep it.
|
|
467
|
+
|
|
468
|
+
@example
|
|
469
|
+
```
|
|
470
|
+
import type {OmitIndexSignature} from 'type-fest';
|
|
471
|
+
|
|
472
|
+
interface Example {
|
|
473
|
+
// These index signatures will be removed.
|
|
474
|
+
[x: string]: any
|
|
475
|
+
[x: number]: any
|
|
476
|
+
[x: symbol]: any
|
|
477
|
+
[x: `head-${string}`]: string
|
|
478
|
+
[x: `${string}-tail`]: string
|
|
479
|
+
[x: `head-${string}-tail`]: string
|
|
480
|
+
[x: `${bigint}`]: string
|
|
481
|
+
[x: `embedded-${number}`]: string
|
|
482
|
+
|
|
483
|
+
// These explicitly defined keys will remain.
|
|
484
|
+
foo: 'bar';
|
|
485
|
+
qux?: 'baz';
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
type ExampleWithoutIndexSignatures = OmitIndexSignature<Example>;
|
|
489
|
+
// => { foo: 'bar'; qux?: 'baz' | undefined; }
|
|
490
|
+
```
|
|
491
|
+
|
|
492
|
+
@see PickIndexSignature
|
|
493
|
+
|
|
494
|
+
@group type-fest
|
|
495
|
+
*/
|
|
496
|
+
type OmitIndexSignature<ObjectType> = { [KeyType in keyof ObjectType as {} extends Record<KeyType, unknown> ? never : KeyType]: ObjectType[KeyType] };
|
|
497
|
+
//#endregion
|
|
498
|
+
//#region ../core/src/types/type-fest/pick-index-signature.d.ts
|
|
499
|
+
/**
|
|
500
|
+
Pick only index signatures from the given object type, leaving out all explicitly defined properties.
|
|
501
|
+
|
|
502
|
+
This is the counterpart of `OmitIndexSignature`.
|
|
503
|
+
|
|
504
|
+
@example
|
|
505
|
+
```
|
|
506
|
+
import type {PickIndexSignature} from 'type-fest';
|
|
507
|
+
|
|
508
|
+
declare const symbolKey: unique symbol;
|
|
509
|
+
|
|
510
|
+
type Example = {
|
|
511
|
+
// These index signatures will remain.
|
|
512
|
+
[x: string]: unknown;
|
|
513
|
+
[x: number]: unknown;
|
|
514
|
+
[x: symbol]: unknown;
|
|
515
|
+
[x: `head-${string}`]: string;
|
|
516
|
+
[x: `${string}-tail`]: string;
|
|
517
|
+
[x: `head-${string}-tail`]: string;
|
|
518
|
+
[x: `${bigint}`]: string;
|
|
519
|
+
[x: `embedded-${number}`]: string;
|
|
520
|
+
|
|
521
|
+
// These explicitly defined keys will be removed.
|
|
522
|
+
['kebab-case-key']: string;
|
|
523
|
+
[symbolKey]: string;
|
|
524
|
+
foo: 'bar';
|
|
525
|
+
qux?: 'baz';
|
|
526
|
+
};
|
|
527
|
+
|
|
528
|
+
type ExampleIndexSignature = PickIndexSignature<Example>;
|
|
529
|
+
// {
|
|
530
|
+
// [x: string]: unknown;
|
|
531
|
+
// [x: number]: unknown;
|
|
532
|
+
// [x: symbol]: unknown;
|
|
533
|
+
// [x: `head-${string}`]: string;
|
|
534
|
+
// [x: `${string}-tail`]: string;
|
|
535
|
+
// [x: `head-${string}-tail`]: string;
|
|
536
|
+
// [x: `${bigint}`]: string;
|
|
537
|
+
// [x: `embedded-${number}`]: string;
|
|
538
|
+
// }
|
|
539
|
+
```
|
|
540
|
+
|
|
541
|
+
@see OmitIndexSignature
|
|
542
|
+
|
|
543
|
+
@group type-fest
|
|
544
|
+
*/
|
|
545
|
+
type PickIndexSignature<ObjectType> = { [KeyType in keyof ObjectType as {} extends Record<KeyType, unknown> ? KeyType : never]: ObjectType[KeyType] };
|
|
546
|
+
//#endregion
|
|
547
|
+
//#region ../core/src/types/type-fest/merge.d.ts
|
|
548
|
+
type SimpleMerge<Destination, Source> = { [Key in keyof Destination as Key extends keyof Source ? never : Key]: Destination[Key] } & Source;
|
|
549
|
+
/**
|
|
550
|
+
Merge two types into a new type. Keys of the second type overrides keys of the first type.
|
|
551
|
+
|
|
552
|
+
@example
|
|
553
|
+
```
|
|
554
|
+
import type {Merge} from 'type-fest';
|
|
555
|
+
|
|
556
|
+
interface Foo {
|
|
557
|
+
[x: string]: unknown;
|
|
558
|
+
[x: number]: unknown;
|
|
559
|
+
foo: string;
|
|
560
|
+
bar: symbol;
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
type Bar = {
|
|
564
|
+
[x: number]: number;
|
|
565
|
+
[x: symbol]: unknown;
|
|
566
|
+
bar: Date;
|
|
567
|
+
baz: boolean;
|
|
568
|
+
};
|
|
569
|
+
|
|
570
|
+
export type FooBar = Merge<Foo, Bar>;
|
|
571
|
+
// => {
|
|
572
|
+
// [x: string]: unknown;
|
|
573
|
+
// [x: number]: number;
|
|
574
|
+
// [x: symbol]: unknown;
|
|
575
|
+
// foo: string;
|
|
576
|
+
// bar: Date;
|
|
577
|
+
// baz: boolean;
|
|
578
|
+
// }
|
|
579
|
+
```
|
|
580
|
+
|
|
581
|
+
@group type-fest
|
|
582
|
+
*/
|
|
583
|
+
type Merge<Destination, Source> = Simplify<SimpleMerge<PickIndexSignature<Destination>, PickIndexSignature<Source>> & SimpleMerge<OmitIndexSignature<Destination>, OmitIndexSignature<Source>>>;
|
|
584
|
+
//#endregion
|
|
585
|
+
//#region ../core/src/types/type-fest/if-any.d.ts
|
|
586
|
+
/**
|
|
587
|
+
An if-else-like type that resolves depending on whether the given type is `any`.
|
|
588
|
+
|
|
589
|
+
@see {@link IsAny}
|
|
590
|
+
|
|
591
|
+
@example
|
|
592
|
+
```
|
|
593
|
+
import type {IfAny} from 'type-fest';
|
|
594
|
+
|
|
595
|
+
type ShouldBeTrue = IfAny<any>;
|
|
596
|
+
//=> true
|
|
597
|
+
|
|
598
|
+
type ShouldBeBar = IfAny<'not any', 'foo', 'bar'>;
|
|
599
|
+
//=> 'bar'
|
|
600
|
+
```
|
|
601
|
+
|
|
602
|
+
@group type-fest
|
|
603
|
+
*/
|
|
604
|
+
type IfAny$1<T, TypeIfAny = true, TypeIfNotAny = false> = (IsAny<T> extends true ? TypeIfAny : TypeIfNotAny);
|
|
605
|
+
//#endregion
|
|
606
|
+
//#region ../core/src/types/type-fest/internal/type.d.ts
|
|
607
|
+
/**
|
|
608
|
+
An if-else-like type that resolves depending on whether the given type is `any` or `never`.
|
|
609
|
+
|
|
610
|
+
@example
|
|
611
|
+
```
|
|
612
|
+
// When `T` is a NOT `any` or `never` (like `string`) => Returns `IfNotAnyOrNever` branch
|
|
613
|
+
type A = IfNotAnyOrNever<string, 'VALID', 'IS_ANY', 'IS_NEVER'>;
|
|
614
|
+
//=> 'VALID'
|
|
615
|
+
|
|
616
|
+
// When `T` is `any` => Returns `IfAny` branch
|
|
617
|
+
type B = IfNotAnyOrNever<any, 'VALID', 'IS_ANY', 'IS_NEVER'>;
|
|
618
|
+
//=> 'IS_ANY'
|
|
619
|
+
|
|
620
|
+
// When `T` is `never` => Returns `IfNever` branch
|
|
621
|
+
type C = IfNotAnyOrNever<never, 'VALID', 'IS_ANY', 'IS_NEVER'>;
|
|
622
|
+
//=> 'IS_NEVER'
|
|
623
|
+
```
|
|
624
|
+
|
|
625
|
+
@group type-fest
|
|
626
|
+
*/
|
|
627
|
+
type IfNotAnyOrNever<T, IfNotAnyOrNever, IfAny = any, IfNever = never> = IsAny<T> extends true ? IfAny : IsNever<T> extends true ? IfNever : IfNotAnyOrNever;
|
|
628
|
+
//#endregion
|
|
629
|
+
//#region ../core/src/types/type-fest/internal/object.d.ts
|
|
630
|
+
/**
|
|
631
|
+
Works similar to the built-in `Pick` utility type, except for the following differences:
|
|
632
|
+
- Distributes over union types and allows picking keys from any member of the union type.
|
|
633
|
+
- Primitives types are returned as-is.
|
|
634
|
+
- Picks all keys if `Keys` is `any`.
|
|
635
|
+
- Doesn't pick `number` from a `string` index signature.
|
|
636
|
+
|
|
637
|
+
@example
|
|
638
|
+
```
|
|
639
|
+
type ImageUpload = {
|
|
640
|
+
url: string;
|
|
641
|
+
size: number;
|
|
642
|
+
thumbnailUrl: string;
|
|
643
|
+
};
|
|
644
|
+
|
|
645
|
+
type VideoUpload = {
|
|
646
|
+
url: string;
|
|
647
|
+
duration: number;
|
|
648
|
+
encodingFormat: string;
|
|
649
|
+
};
|
|
650
|
+
|
|
651
|
+
// Distributes over union types and allows picking keys from any member of the union type
|
|
652
|
+
type MediaDisplay = HomomorphicPick<ImageUpload | VideoUpload, "url" | "size" | "duration">;
|
|
653
|
+
//=> {url: string; size: number} | {url: string; duration: number}
|
|
654
|
+
|
|
655
|
+
// Primitive types are returned as-is
|
|
656
|
+
type Primitive = HomomorphicPick<string | number, 'toUpperCase' | 'toString'>;
|
|
657
|
+
//=> string | number
|
|
658
|
+
|
|
659
|
+
// Picks all keys if `Keys` is `any`
|
|
660
|
+
type Any = HomomorphicPick<{a: 1; b: 2} | {c: 3}, any>;
|
|
661
|
+
//=> {a: 1; b: 2} | {c: 3}
|
|
662
|
+
|
|
663
|
+
// Doesn't pick `number` from a `string` index signature
|
|
664
|
+
type IndexSignature = HomomorphicPick<{[k: string]: unknown}, number>;
|
|
665
|
+
//=> {}
|
|
666
|
+
|
|
667
|
+
@group type-fest
|
|
668
|
+
*/
|
|
669
|
+
type HomomorphicPick<T, Keys extends KeysOfUnion<T>> = { [P in keyof T as Extract<P, Keys>]: T[P] };
|
|
670
|
+
/**
|
|
671
|
+
Merges user specified options with default options.
|
|
672
|
+
|
|
673
|
+
@example
|
|
674
|
+
```
|
|
675
|
+
type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
|
|
676
|
+
type DefaultPathsOptions = {maxRecursionDepth: 10; leavesOnly: false};
|
|
677
|
+
type SpecifiedOptions = {leavesOnly: true};
|
|
678
|
+
|
|
679
|
+
type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
|
|
680
|
+
//=> {maxRecursionDepth: 10; leavesOnly: true}
|
|
681
|
+
```
|
|
682
|
+
|
|
683
|
+
@example
|
|
684
|
+
```
|
|
685
|
+
// Complains if default values are not provided for optional options
|
|
686
|
+
|
|
687
|
+
type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
|
|
688
|
+
type DefaultPathsOptions = {maxRecursionDepth: 10};
|
|
689
|
+
type SpecifiedOptions = {};
|
|
690
|
+
|
|
691
|
+
type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
|
|
692
|
+
// ~~~~~~~~~~~~~~~~~~~
|
|
693
|
+
// Property 'leavesOnly' is missing in type 'DefaultPathsOptions' but required in type '{ maxRecursionDepth: number; leavesOnly: boolean; }'.
|
|
694
|
+
```
|
|
695
|
+
|
|
696
|
+
@example
|
|
697
|
+
```
|
|
698
|
+
// Complains if an option's default type does not conform to the expected type
|
|
699
|
+
|
|
700
|
+
type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
|
|
701
|
+
type DefaultPathsOptions = {maxRecursionDepth: 10; leavesOnly: 'no'};
|
|
702
|
+
type SpecifiedOptions = {};
|
|
703
|
+
|
|
704
|
+
type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
|
|
705
|
+
// ~~~~~~~~~~~~~~~~~~~
|
|
706
|
+
// Types of property 'leavesOnly' are incompatible. Type 'string' is not assignable to type 'boolean'.
|
|
707
|
+
```
|
|
708
|
+
|
|
709
|
+
@example
|
|
710
|
+
```
|
|
711
|
+
// Complains if an option's specified type does not conform to the expected type
|
|
712
|
+
|
|
713
|
+
type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
|
|
714
|
+
type DefaultPathsOptions = {maxRecursionDepth: 10; leavesOnly: false};
|
|
715
|
+
type SpecifiedOptions = {leavesOnly: 'yes'};
|
|
716
|
+
|
|
717
|
+
type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
|
|
718
|
+
// ~~~~~~~~~~~~~~~~
|
|
719
|
+
// Types of property 'leavesOnly' are incompatible. Type 'string' is not assignable to type 'boolean'.
|
|
720
|
+
```
|
|
721
|
+
|
|
722
|
+
@group type-fest
|
|
723
|
+
*/
|
|
724
|
+
type ApplyDefaultOptions<Options extends object, Defaults extends Simplify<Omit<Required<Options>, RequiredKeysOf<Options>> & Partial<Record<RequiredKeysOf<Options>, never>>>, SpecifiedOptions extends Options> = IfAny$1<SpecifiedOptions, Defaults, IfNever$1<SpecifiedOptions, Defaults, Simplify<Merge<Defaults, { [Key in keyof SpecifiedOptions as Key extends OptionalKeysOf<Options> ? Extract<SpecifiedOptions[Key], undefined> extends never ? Key : never : Key]: SpecifiedOptions[Key] }> & Required<Options>>>>;
|
|
725
|
+
//#endregion
|
|
726
|
+
//#region ../core/src/types/type-fest/except.d.ts
|
|
727
|
+
/**
|
|
728
|
+
Filter out keys from an object.
|
|
729
|
+
|
|
730
|
+
Returns `never` if `Exclude` is strictly equal to `Key`.
|
|
731
|
+
Returns `never` if `Key` extends `Exclude`.
|
|
732
|
+
Returns `Key` otherwise.
|
|
733
|
+
|
|
734
|
+
@example
|
|
735
|
+
```
|
|
736
|
+
type Filtered = Filter<'foo', 'foo'>;
|
|
737
|
+
//=> never
|
|
738
|
+
```
|
|
739
|
+
|
|
740
|
+
@example
|
|
741
|
+
```
|
|
742
|
+
type Filtered = Filter<'bar', string>;
|
|
743
|
+
//=> never
|
|
744
|
+
```
|
|
745
|
+
|
|
746
|
+
@example
|
|
747
|
+
```
|
|
748
|
+
type Filtered = Filter<'bar', 'foo'>;
|
|
749
|
+
//=> 'bar'
|
|
750
|
+
```
|
|
751
|
+
|
|
752
|
+
@see {Except}
|
|
753
|
+
*/
|
|
754
|
+
type Filter<KeyType, ExcludeType> = IsEqual<KeyType, ExcludeType> extends true ? never : (KeyType extends ExcludeType ? never : KeyType);
|
|
755
|
+
type ExceptOptions = {
|
|
756
|
+
/**
|
|
757
|
+
Disallow assigning non-specified properties.
|
|
758
|
+
|
|
759
|
+
Note that any omitted properties in the resulting type will be present in autocomplete as `undefined`.
|
|
760
|
+
|
|
761
|
+
@default false
|
|
762
|
+
*/
|
|
763
|
+
requireExactProps?: boolean;
|
|
764
|
+
};
|
|
765
|
+
type DefaultExceptOptions = {
|
|
766
|
+
requireExactProps: false;
|
|
767
|
+
};
|
|
768
|
+
/**
|
|
769
|
+
Create a type from an object type without certain keys.
|
|
770
|
+
|
|
771
|
+
We recommend setting the `requireExactProps` option to `true`.
|
|
772
|
+
|
|
773
|
+
This type is a stricter version of [`Omit`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-5.html#the-omit-helper-type). The `Omit` type does not restrict the omitted keys to be keys present on the given type, while `Except` does. The benefits of a stricter type are avoiding typos and allowing the compiler to pick up on rename refactors automatically.
|
|
774
|
+
|
|
775
|
+
This type was proposed to the TypeScript team, which declined it, saying they prefer that libraries implement stricter versions of the built-in types ([microsoft/TypeScript#30825](https://github.com/microsoft/TypeScript/issues/30825#issuecomment-523668235)).
|
|
776
|
+
|
|
777
|
+
@example
|
|
778
|
+
```
|
|
779
|
+
import type {Except} from 'type-fest';
|
|
780
|
+
|
|
781
|
+
type Foo = {
|
|
782
|
+
a: number;
|
|
783
|
+
b: string;
|
|
784
|
+
};
|
|
785
|
+
|
|
786
|
+
type FooWithoutA = Except<Foo, 'a'>;
|
|
787
|
+
//=> {b: string}
|
|
788
|
+
|
|
789
|
+
const fooWithoutA: FooWithoutA = {a: 1, b: '2'};
|
|
790
|
+
//=> errors: 'a' does not exist in type '{ b: string; }'
|
|
791
|
+
|
|
792
|
+
type FooWithoutB = Except<Foo, 'b', {requireExactProps: true}>;
|
|
793
|
+
//=> {a: number} & Partial<Record<"b", never>>
|
|
794
|
+
|
|
795
|
+
const fooWithoutB: FooWithoutB = {a: 1, b: '2'};
|
|
796
|
+
//=> errors at 'b': Type 'string' is not assignable to type 'undefined'.
|
|
797
|
+
|
|
798
|
+
// The `Omit` utility type doesn't work when omitting specific keys from objects containing index signatures.
|
|
799
|
+
|
|
800
|
+
// Consider the following example:
|
|
801
|
+
|
|
802
|
+
type UserData = {
|
|
803
|
+
[metadata: string]: string;
|
|
804
|
+
email: string;
|
|
805
|
+
name: string;
|
|
806
|
+
role: 'admin' | 'user';
|
|
807
|
+
};
|
|
808
|
+
|
|
809
|
+
// `Omit` clearly doesn't behave as expected in this case:
|
|
810
|
+
type PostPayload = Omit<UserData, 'email'>;
|
|
811
|
+
//=> type PostPayload = { [x: string]: string; [x: number]: string; }
|
|
812
|
+
|
|
813
|
+
// In situations like this, `Except` works better.
|
|
814
|
+
// It simply removes the `email` key while preserving all the other keys.
|
|
815
|
+
type PostPayload = Except<UserData, 'email'>;
|
|
816
|
+
//=> type PostPayload = { [x: string]: string; name: string; role: 'admin' | 'user'; }
|
|
817
|
+
```
|
|
818
|
+
|
|
819
|
+
@group type-fest
|
|
820
|
+
*/
|
|
821
|
+
type Except<ObjectType, KeysType extends keyof ObjectType, Options extends ExceptOptions = {}> = _Except<ObjectType, KeysType, ApplyDefaultOptions<ExceptOptions, DefaultExceptOptions, Options>>;
|
|
822
|
+
type _Except<ObjectType, KeysType extends keyof ObjectType, Options extends Required<ExceptOptions>> = { [KeyType in keyof ObjectType as Filter<KeyType, KeysType>]: ObjectType[KeyType] } & (Options["requireExactProps"] extends true ? Partial<Record<KeysType, never>> : {});
|
|
823
|
+
//#endregion
|
|
824
|
+
//#region ../core/src/types/type-fest/require-at-least-one.d.ts
|
|
825
|
+
/**
|
|
826
|
+
Create a type that requires at least one of the given keys. The remaining keys are kept as is.
|
|
827
|
+
|
|
828
|
+
@example
|
|
829
|
+
```
|
|
830
|
+
import type {RequireAtLeastOne} from 'type-fest';
|
|
831
|
+
|
|
832
|
+
type Responder = {
|
|
833
|
+
text?: () => string;
|
|
834
|
+
json?: () => string;
|
|
835
|
+
secure?: boolean;
|
|
836
|
+
};
|
|
837
|
+
|
|
838
|
+
const responder: RequireAtLeastOne<Responder, 'text' | 'json'> = {
|
|
839
|
+
json: () => '{"message": "ok"}',
|
|
840
|
+
secure: true
|
|
841
|
+
};
|
|
842
|
+
```
|
|
843
|
+
|
|
844
|
+
@group type-fest
|
|
845
|
+
*/
|
|
846
|
+
type RequireAtLeastOne<ObjectType, KeysType extends keyof ObjectType = keyof ObjectType> = IfNotAnyOrNever<ObjectType, IfNever$1<KeysType, never, _RequireAtLeastOne<ObjectType, IfAny$1<KeysType, keyof ObjectType, KeysType>>>>;
|
|
847
|
+
type _RequireAtLeastOne<ObjectType, KeysType extends keyof ObjectType> = { [Key in KeysType]-?: Required<Pick<ObjectType, Key>> & Partial<Pick<ObjectType, Exclude<KeysType, Key>>> }[KeysType] & Except<ObjectType, KeysType>;
|
|
848
|
+
//#endregion
|
|
849
|
+
//#region ../core/src/types/type-fest/set-required.d.ts
|
|
850
|
+
/**
|
|
851
|
+
Create a type that makes the given keys required. The remaining keys are kept as is. The sister of the `SetOptional` type.
|
|
852
|
+
|
|
853
|
+
Use-case: You want to define a single model where the only thing that changes is whether or not some of the keys are required.
|
|
854
|
+
|
|
855
|
+
@example
|
|
856
|
+
```
|
|
857
|
+
import type {SetRequired} from 'type-fest';
|
|
858
|
+
|
|
859
|
+
type Foo = {
|
|
860
|
+
a?: number;
|
|
861
|
+
b: string;
|
|
862
|
+
c?: boolean;
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
type SomeRequired = SetRequired<Foo, 'b' | 'c'>;
|
|
866
|
+
// type SomeRequired = {
|
|
867
|
+
// a?: number;
|
|
868
|
+
// b: string; // Was already required and still is.
|
|
869
|
+
// c: boolean; // Is now required.
|
|
870
|
+
// }
|
|
871
|
+
|
|
872
|
+
// Set specific indices in an array to be required.
|
|
873
|
+
type ArrayExample = SetRequired<[number?, number?, number?], 0 | 1>;
|
|
874
|
+
//=> [number, number, number?]
|
|
875
|
+
```
|
|
876
|
+
|
|
877
|
+
@group type-fest
|
|
878
|
+
*/
|
|
879
|
+
type SetRequired<BaseType, Keys extends keyof BaseType> = BaseType extends UnknownArray ? SetArrayRequired<BaseType, Keys> extends infer ResultantArray ? IfArrayReadonly<BaseType, Readonly<ResultantArray>, ResultantArray> : never : Simplify<Except<BaseType, Keys> & Required<HomomorphicPick<BaseType, Keys>>>;
|
|
880
|
+
/**
|
|
881
|
+
Remove the optional modifier from the specified keys in an array.
|
|
882
|
+
*/
|
|
883
|
+
type SetArrayRequired<TArray extends UnknownArray, Keys, Counter extends any[] = [], Accumulator extends UnknownArray = []> = TArray extends unknown ? keyof TArray & `${number}` extends never ? [...Accumulator, ...TArray] : TArray extends readonly [(infer First)?, ...infer Rest] ? "0" extends OptionalKeysOf<TArray> ? `${Counter["length"]}` extends `${Keys & (string | number)}` ? SetArrayRequired<Rest, Keys, [...Counter, any], [...Accumulator, First]> : [...Accumulator, ...TArray] : SetArrayRequired<Rest, Keys, [...Counter, any], [...Accumulator, TArray[0]]> : never : never;
|
|
884
|
+
//#endregion
|
|
885
|
+
//#region ../core/src/types/options.d.ts
|
|
886
|
+
/**
|
|
887
|
+
* Adds an `unknown` index property to an interface.
|
|
888
|
+
*/
|
|
889
|
+
type WithUnknownIndex<T> = T & {
|
|
890
|
+
[key: string]: unknown;
|
|
891
|
+
};
|
|
892
|
+
/**
|
|
893
|
+
* Do not use this type directly; use {@link Option}, {@link ValueOption},
|
|
894
|
+
* or {@link FullOption} instead. For specific option types, you can use
|
|
895
|
+
* {@link FullField}, {@link FullOperator}, or {@link FullCombinator},
|
|
896
|
+
* all of which extend {@link FullOption}.
|
|
897
|
+
*
|
|
898
|
+
* @group Option Lists
|
|
899
|
+
*/
|
|
900
|
+
interface BaseOption<N extends string = string> {
|
|
901
|
+
name?: N;
|
|
902
|
+
value?: N;
|
|
903
|
+
label: string;
|
|
904
|
+
disabled?: boolean;
|
|
905
|
+
}
|
|
906
|
+
/**
|
|
907
|
+
* A generic {@link Option} with either a `name` or `value` as its primary identifier.
|
|
908
|
+
* {@link OptionList}-type props on the {@link QueryBuilder} component accept this type,
|
|
909
|
+
* but corresponding props passed down to subcomponents will always be translated
|
|
910
|
+
* to {@link FullOption} first.
|
|
911
|
+
*
|
|
912
|
+
* @group Option Lists
|
|
913
|
+
*/
|
|
914
|
+
type FlexibleOption<N extends string = string> = Simplify<WithUnknownIndex<RequireAtLeastOne<BaseOption<N>, "name" | "value">>>;
|
|
915
|
+
/**
|
|
916
|
+
* Utility type to turn an {@link Option}, {@link ValueOption}, or {@link BaseOption}
|
|
917
|
+
* into a {@link FlexibleOption}.
|
|
918
|
+
*
|
|
919
|
+
* @group Option Lists
|
|
920
|
+
*/
|
|
921
|
+
type ToFlexibleOption<Opt extends BaseOption | string> = WithUnknownIndex<RequireAtLeastOne<Opt extends string ? FlexibleOption<Opt> : Opt, "name" | "value">>;
|
|
922
|
+
/**
|
|
923
|
+
* A generic {@link Option} requiring both `name` _and_ `value` properties.
|
|
924
|
+
* Props that extend {@link OptionList} accept {@link BaseOption}, but
|
|
925
|
+
* corresponding props sent to subcomponents will always be translated to this
|
|
926
|
+
* type first to ensure both `name` and `value` are available.
|
|
927
|
+
*
|
|
928
|
+
* NOTE: Do not extend from this type directly. Use {@link BaseFullOption}
|
|
929
|
+
* (optionally wrapped in {@link WithUnknownIndex}) instead, otherwise
|
|
930
|
+
* the `unknown` index property will cause issues. See {@link Option} and
|
|
931
|
+
* {@link ValueOption} for examples.
|
|
932
|
+
*
|
|
933
|
+
* @group Option Lists
|
|
934
|
+
*/
|
|
935
|
+
type FullOption<N extends string = string> = Simplify<WithUnknownIndex<SetRequired<BaseOption<N>, "name" | "value">>>;
|
|
936
|
+
/**
|
|
937
|
+
* This type is identical to {@link FullOption} but without the `unknown` index
|
|
938
|
+
* property. Extend from this type instead of {@link FullOption} directly.
|
|
939
|
+
*
|
|
940
|
+
* @group Option Lists
|
|
941
|
+
*/
|
|
942
|
+
type BaseFullOption<N extends string = string> = Simplify<SetRequired<BaseOption<N>, "name" | "value">>;
|
|
943
|
+
/**
|
|
944
|
+
* Utility type to turn an {@link Option}, {@link ValueOption} or
|
|
945
|
+
* {@link BaseOption} into a {@link FullOption}.
|
|
946
|
+
*
|
|
947
|
+
* @group Option Lists
|
|
948
|
+
*/
|
|
949
|
+
type ToFullOption<Opt extends BaseOption> = Opt extends BaseFullOption ? Opt : Opt extends BaseOption<infer IdentifierType> ? WithUnknownIndex<Opt & FullOption<IdentifierType>> : never;
|
|
950
|
+
/**
|
|
951
|
+
* A group of {@link Option}s, usually within an {@link OptionList}.
|
|
952
|
+
*
|
|
953
|
+
* @group Option Lists
|
|
954
|
+
*/
|
|
955
|
+
interface OptionGroup<Opt extends BaseOption = FlexibleOption> {
|
|
956
|
+
label: string;
|
|
957
|
+
options: WithUnknownIndex<Opt>[];
|
|
958
|
+
}
|
|
959
|
+
/**
|
|
960
|
+
* A group of {@link BaseOption}s, usually within a {@link FlexibleOptionList}.
|
|
961
|
+
*
|
|
962
|
+
* @group Option Lists
|
|
963
|
+
*/
|
|
964
|
+
type FlexibleOptionGroup<Opt extends BaseOption | string = BaseOption> = {
|
|
965
|
+
label: string;
|
|
966
|
+
options: (Opt extends BaseFullOption ? Opt : ToFlexibleOption<Opt>)[];
|
|
967
|
+
};
|
|
968
|
+
/**
|
|
969
|
+
* An array of options or option groups, like {@link OptionList} but the option type
|
|
970
|
+
* may use either `name` or `value` as the primary identifier.
|
|
971
|
+
*
|
|
972
|
+
* @group Option Lists
|
|
973
|
+
*/
|
|
974
|
+
type FlexibleOptionList<Opt extends BaseOption> = ToFlexibleOption<Opt>[] | FlexibleOptionGroup<ToFlexibleOption<Opt>>[];
|
|
975
|
+
/**
|
|
976
|
+
* An array of options or option groups, like {@link OptionList}, but using
|
|
977
|
+
* {@link FullOption} instead of {@link Option}. This means that every member is
|
|
978
|
+
* guaranteed to have both `name` and `value`.
|
|
979
|
+
*
|
|
980
|
+
* @group Option Lists
|
|
981
|
+
*/
|
|
982
|
+
type FullOptionList<Opt extends BaseOption> = Opt extends BaseFullOption ? Opt[] | OptionGroup<Opt>[] : ToFullOption<Opt>[] | OptionGroup<ToFullOption<Opt>>[];
|
|
983
|
+
//#endregion
|
|
984
|
+
//#region ../core/src/types/ruleGroups.d.ts
|
|
985
|
+
/**
|
|
986
|
+
* Properties common to both rules and groups.
|
|
987
|
+
*/
|
|
988
|
+
interface CommonRuleAndGroupProperties {
|
|
989
|
+
path?: Path;
|
|
990
|
+
id?: string;
|
|
991
|
+
disabled?: boolean;
|
|
992
|
+
}
|
|
993
|
+
/**
|
|
994
|
+
* The main rule type. The `field`, `operator`, and `value` properties
|
|
995
|
+
* can be narrowed with generics.
|
|
996
|
+
*/
|
|
997
|
+
interface RuleType<F extends string = string, O extends string = string, V = any, C extends string = string> extends CommonRuleAndGroupProperties {
|
|
998
|
+
field: F;
|
|
999
|
+
operator: O;
|
|
1000
|
+
value: V;
|
|
1001
|
+
valueSource?: ValueSource;
|
|
1002
|
+
match?: MatchConfig;
|
|
1003
|
+
/**
|
|
1004
|
+
* Only used when adding a rule to a query that uses independent combinators.
|
|
1005
|
+
*/
|
|
1006
|
+
combinatorPreceding?: C;
|
|
1007
|
+
}
|
|
1008
|
+
/**
|
|
1009
|
+
* The main rule group type. This type is used for query definitions as well as
|
|
1010
|
+
* all sub-groups of queries.
|
|
1011
|
+
*/
|
|
1012
|
+
interface RuleGroupType<R extends RuleType = RuleType, C extends string = string> extends CommonRuleAndGroupProperties {
|
|
1013
|
+
combinator: C;
|
|
1014
|
+
rules: RuleGroupArray<RuleGroupType<R, C>, R>;
|
|
1015
|
+
not?: boolean;
|
|
1016
|
+
}
|
|
1017
|
+
/**
|
|
1018
|
+
* The type of the `rules` array in a {@link RuleGroupType}.
|
|
1019
|
+
*/
|
|
1020
|
+
type RuleGroupArray<RG extends RuleGroupType = RuleGroupType, R extends RuleType = RuleType> = (R | RG)[];
|
|
1021
|
+
/**
|
|
1022
|
+
* Default values for the `operator` property.
|
|
1023
|
+
*
|
|
1024
|
+
* @group Option Lists
|
|
1025
|
+
*/
|
|
1026
|
+
type DefaultOperatorName = "=" | "!=" | "<" | ">" | "<=" | ">=" | "contains" | "beginsWith" | "endsWith" | "doesNotContain" | "doesNotBeginWith" | "doesNotEndWith" | "null" | "notNull" | "in" | "notIn" | "between" | "notBetween";
|
|
1027
|
+
//#endregion
|
|
1028
|
+
//#region ../core/src/types/ruleGroupsIC.utils.d.ts
|
|
1029
|
+
type MAXIMUM_ALLOWED_BOUNDARY = 80;
|
|
1030
|
+
type MappedTuple<Tuple extends Array<unknown>, Result extends Array<unknown> = [], Count extends ReadonlyArray<number> = []> = Count["length"] extends MAXIMUM_ALLOWED_BOUNDARY ? Result : Tuple extends [] ? [] : Result extends [] ? MappedTuple<Tuple, Tuple, [...Count, 1]> : MappedTuple<Tuple, Result | [...Result, ...Tuple], [...Count, 1]>;
|
|
1031
|
+
//#endregion
|
|
1032
|
+
//#region ../core/src/types/ruleGroupsIC.d.ts
|
|
1033
|
+
/**
|
|
1034
|
+
* The main rule group interface when using independent combinators. This type is used
|
|
1035
|
+
* for query definitions as well as all sub-groups of queries.
|
|
1036
|
+
*/
|
|
1037
|
+
interface RuleGroupTypeIC<R extends RuleType = RuleType, C extends string = string> extends Except<RuleGroupType<R, C>, "combinator" | "rules"> {
|
|
1038
|
+
combinator?: undefined;
|
|
1039
|
+
rules: RuleGroupICArray<RuleGroupTypeIC<R, C>, R, C>;
|
|
1040
|
+
/**
|
|
1041
|
+
* Only used when adding a rule to a query that uses independent combinators
|
|
1042
|
+
*/
|
|
1043
|
+
combinatorPreceding?: C;
|
|
1044
|
+
}
|
|
1045
|
+
/**
|
|
1046
|
+
* Shorthand for "either {@link RuleGroupType} or {@link RuleGroupTypeIC}".
|
|
1047
|
+
*/
|
|
1048
|
+
type RuleGroupTypeAny<R extends RuleType = RuleType, C extends string = string> = RuleGroupType<R, C> | RuleGroupTypeIC<R, C>;
|
|
1049
|
+
/**
|
|
1050
|
+
* The type of the `rules` array in a {@link RuleGroupTypeIC}.
|
|
1051
|
+
*/
|
|
1052
|
+
type RuleGroupICArray<RG extends RuleGroupTypeIC = RuleGroupTypeIC, R extends RuleType = RuleType, C extends string = string> = [R | RG] | [R | RG, ...MappedTuple<[C, R | RG]>] | ((R | RG)[] & {
|
|
1053
|
+
length: 0;
|
|
1054
|
+
});
|
|
1055
|
+
//#endregion
|
|
1056
|
+
//#region ../core/src/types/validation.d.ts
|
|
1057
|
+
/**
|
|
1058
|
+
* Object with a `valid` boolean value and optional `reasons`.
|
|
1059
|
+
*/
|
|
1060
|
+
interface ValidationResult {
|
|
1061
|
+
valid: boolean;
|
|
1062
|
+
reasons?: any[];
|
|
1063
|
+
}
|
|
1064
|
+
/**
|
|
1065
|
+
* Map of rule/group `id` to its respective {@link ValidationResult}.
|
|
1066
|
+
*/
|
|
1067
|
+
type ValidationMap = Record<string, boolean | ValidationResult>;
|
|
1068
|
+
/**
|
|
1069
|
+
* Function that validates a query.
|
|
1070
|
+
*/
|
|
1071
|
+
type QueryValidator = (query: RuleGroupTypeAny) => boolean | ValidationMap;
|
|
1072
|
+
/**
|
|
1073
|
+
* Function that validates a rule.
|
|
1074
|
+
*/
|
|
1075
|
+
type RuleValidator = (rule: RuleType) => boolean | ValidationResult;
|
|
1076
|
+
//#endregion
|
|
1077
|
+
//#region ../core/src/types/basic.d.ts
|
|
1078
|
+
/**
|
|
1079
|
+
* @see https://react-querybuilder.js.org/docs/tips/path
|
|
1080
|
+
*/
|
|
1081
|
+
type Path = number[];
|
|
1082
|
+
/**
|
|
1083
|
+
* String of classnames, array of classname strings, or object where the
|
|
1084
|
+
* keys are classnames and those with truthy values will be included.
|
|
1085
|
+
* Suitable for passing to the `clsx` package.
|
|
1086
|
+
*/
|
|
1087
|
+
type Classname = string | string[] | Record<string, any>;
|
|
1088
|
+
/**
|
|
1089
|
+
* A source for the `value` property of a rule.
|
|
1090
|
+
*/
|
|
1091
|
+
type ValueSource = "value" | "field";
|
|
1092
|
+
/**
|
|
1093
|
+
* Type of {@link ValueEditor} that will be displayed.
|
|
1094
|
+
*/
|
|
1095
|
+
type ValueEditorType = "text" | "select" | "checkbox" | "radio" | "textarea" | "switch" | "multiselect" | null;
|
|
1096
|
+
/**
|
|
1097
|
+
* A valid array of potential value sources.
|
|
1098
|
+
*
|
|
1099
|
+
* @see {@link ValueSource}
|
|
1100
|
+
*/
|
|
1101
|
+
type ValueSources = ["value"] | ["value", "field"] | ["field", "value"] | ["field"];
|
|
1102
|
+
type ValueSourceFlexibleOptions = ToFlexibleOptionArrays<ValueSources>;
|
|
1103
|
+
type ToFlexibleOptionArrays<Sources extends readonly string[]> = Sources extends unknown ? { [K in keyof Sources]: FlexibleOption<Sources[K]> } : never;
|
|
1104
|
+
type WithOptionalClassName<T> = T & {
|
|
1105
|
+
className?: Classname;
|
|
1106
|
+
};
|
|
1107
|
+
/**
|
|
1108
|
+
* HTML5 input types
|
|
1109
|
+
*/
|
|
1110
|
+
type InputType = "button" | "checkbox" | "color" | "date" | "datetime-local" | "email" | "file" | "hidden" | "image" | "month" | "number" | "password" | "radio" | "range" | "reset" | "search" | "submit" | "tel" | "text" | "time" | "url" | "week" | "bigint" | (string & {});
|
|
1111
|
+
/**
|
|
1112
|
+
* Quantification mode describing how many elements of the value array must pass
|
|
1113
|
+
* the filter for the rule itself to pass.
|
|
1114
|
+
*
|
|
1115
|
+
* For "atLeast", "atMost", and "exactly", the threshold value will be converted to
|
|
1116
|
+
* a percentage if the number is less than 1. Non-numeric values and numbers less
|
|
1117
|
+
* than 0 will be ignored.
|
|
1118
|
+
*/
|
|
1119
|
+
interface MatchConfig {
|
|
1120
|
+
mode: MatchMode;
|
|
1121
|
+
threshold?: number | null | undefined;
|
|
1122
|
+
}
|
|
1123
|
+
type MatchMode = "all" | "some" | "none" | "atLeast" | "atMost" | "exactly";
|
|
1124
|
+
/**
|
|
1125
|
+
* Base for all Field types/interfaces.
|
|
1126
|
+
*/
|
|
1127
|
+
interface BaseFullField<FieldName extends string = string, OperatorName extends string = string, ValueName extends string = string, OperatorObj extends FullOption = FullOption<OperatorName>, ValueObj extends FullOption = FullOption<ValueName>> extends WithOptionalClassName<BaseFullOption<FieldName>> {
|
|
1128
|
+
id?: string;
|
|
1129
|
+
operators?: FlexibleOptionList<OperatorObj> | OperatorName[] | FlexibleOption<OperatorName>[] | (OperatorName | FlexibleOption<OperatorName>)[];
|
|
1130
|
+
valueEditorType?: ValueEditorType | ((operator: OperatorName) => ValueEditorType);
|
|
1131
|
+
valueSources?: ValueSources | ValueSourceFlexibleOptions | ((operator: OperatorName) => ValueSources | ValueSourceFlexibleOptions);
|
|
1132
|
+
inputType?: InputType | null;
|
|
1133
|
+
values?: FlexibleOptionList<ValueObj>;
|
|
1134
|
+
matchModes?: boolean | MatchMode[] | FlexibleOption<MatchMode>[];
|
|
1135
|
+
/** Properties of items in the value. */
|
|
1136
|
+
subproperties?: FlexibleOptionList<FullField>;
|
|
1137
|
+
defaultOperator?: OperatorName;
|
|
1138
|
+
defaultValue?: any;
|
|
1139
|
+
placeholder?: string;
|
|
1140
|
+
validator?: RuleValidator;
|
|
1141
|
+
comparator?: string | ((f: FullField, operator: string) => boolean);
|
|
1142
|
+
}
|
|
1143
|
+
/**
|
|
1144
|
+
* Full field definition used in the `fields` prop of {@link QueryBuilder}.
|
|
1145
|
+
* This type requires both `name` and `value`, but the `fields` prop itself
|
|
1146
|
+
* can use a {@link FlexibleOption} where only one of `name` or `value` is
|
|
1147
|
+
* required (along with `label`), or {@link Field} where only `name` and
|
|
1148
|
+
* `label` are required.
|
|
1149
|
+
*
|
|
1150
|
+
* The `name`/`value`, `operators`, and `values` properties of this interface
|
|
1151
|
+
* can be narrowed with generics.
|
|
1152
|
+
*
|
|
1153
|
+
* @group Option Lists
|
|
1154
|
+
*/
|
|
1155
|
+
type FullField<FieldName extends string = string, OperatorName extends string = string, ValueName extends string = string, OperatorObj extends FullOption = FullOption<OperatorName>, ValueObj extends FullOption = FullOption<ValueName>> = Simplify<FullOption<FieldName> & BaseFullField<FieldName, OperatorName, ValueName, OperatorObj, ValueObj>>;
|
|
1156
|
+
/**
|
|
1157
|
+
* Allowed values of the {@link FullOperator} property `arity`. A value of `"unary"` or
|
|
1158
|
+
* a number less than two will cause the default {@link ValueEditor} to render `null`.
|
|
1159
|
+
*/
|
|
1160
|
+
type Arity = number | "unary" | "binary" | "ternary";
|
|
1161
|
+
/**
|
|
1162
|
+
* Full operator definition used in the `operators`/`getOperators` props of
|
|
1163
|
+
* {@link QueryBuilder}. This type requires both `name` and `value`, but the
|
|
1164
|
+
* `operators`/`getOperators` props themselves can use a {@link FlexibleOption}
|
|
1165
|
+
* where only one of `name` or `value` is required, or {@link FullOperator} where
|
|
1166
|
+
* only `name` is required.
|
|
1167
|
+
*
|
|
1168
|
+
* The `name`/`value` properties of this interface can be narrowed with generics.
|
|
1169
|
+
*
|
|
1170
|
+
* @group Option Lists
|
|
1171
|
+
*/
|
|
1172
|
+
interface FullOperator<N extends string = string> extends WithOptionalClassName<FullOption<N>> {
|
|
1173
|
+
arity?: Arity;
|
|
1174
|
+
}
|
|
1175
|
+
type ParseNumberMethodName = "enhanced" | "native" | "strict";
|
|
1176
|
+
/**
|
|
1177
|
+
* Parsing algorithms used by {@link parseNumber}.
|
|
1178
|
+
*/
|
|
1179
|
+
|
|
1180
|
+
type ParseNumbersModerationLevel = "-limited" | "";
|
|
1181
|
+
/**
|
|
1182
|
+
* Options for the `parseNumbers` prop of {@link QueryBuilder}.
|
|
1183
|
+
*/
|
|
1184
|
+
type ParseNumbersPropConfig = boolean | `${ParseNumberMethodName}${ParseNumbersModerationLevel}`;
|
|
1185
|
+
//#endregion
|
|
1186
|
+
//#region ../core/src/types/export.d.ts
|
|
1187
|
+
/**
|
|
1188
|
+
* Available export formats for {@link formatQuery}.
|
|
1189
|
+
*
|
|
1190
|
+
* @group Export
|
|
1191
|
+
*/
|
|
1192
|
+
type ExportFormat = "json" | "sql" | "json_without_ids" | "parameterized" | "parameterized_named" | "mongodb" | "mongodb_query" | "cel" | "jsonlogic" | "spel" | "elasticsearch" | "jsonata" | "natural_language" | "ldap" | "drizzle" | "prisma" | "sequelize";
|
|
1193
|
+
/**
|
|
1194
|
+
* Available presets for the "sql" export format.
|
|
1195
|
+
*
|
|
1196
|
+
* @group Export
|
|
1197
|
+
*/
|
|
1198
|
+
type SQLPreset = "ansi" | "sqlite" | "postgresql" | "mysql" | "mssql" | "oracle";
|
|
1199
|
+
/**
|
|
1200
|
+
* A map of operators to strings to be used in the output of {@link formatQuery}. If the
|
|
1201
|
+
* result can differ based on the `valueSource`, the key should map to an array where the
|
|
1202
|
+
* second element represents the string to be used when `valueSource` is "field". The first
|
|
1203
|
+
* element will be used in all other cases.
|
|
1204
|
+
*
|
|
1205
|
+
* @group Export
|
|
1206
|
+
*/
|
|
1207
|
+
type ExportOperatorMap = Partial<Record<Lowercase<DefaultOperatorName> | DefaultOperatorName, string | [string, string]>>;
|
|
1208
|
+
/**
|
|
1209
|
+
* Options object shape for {@link formatQuery}.
|
|
1210
|
+
*
|
|
1211
|
+
* @group Export
|
|
1212
|
+
*/
|
|
1213
|
+
interface FormatQueryOptions {
|
|
1214
|
+
/**
|
|
1215
|
+
* The {@link ExportFormat}.
|
|
1216
|
+
*/
|
|
1217
|
+
format?: ExportFormat;
|
|
1218
|
+
/**
|
|
1219
|
+
* This function will be used to process the `operator` from each rule
|
|
1220
|
+
* for query language formats. If not defined, the appropriate
|
|
1221
|
+
* `defaultOperatorProcessor*` for the format will be used.
|
|
1222
|
+
*/
|
|
1223
|
+
operatorProcessor?: RuleProcessor;
|
|
1224
|
+
/**
|
|
1225
|
+
* This function will be used to process the `value` from each rule
|
|
1226
|
+
* for query language formats. If not defined, the appropriate
|
|
1227
|
+
* `defaultValueProcessor*` for the format will be used.
|
|
1228
|
+
*/
|
|
1229
|
+
valueProcessor?: ValueProcessorLegacy | ValueProcessorByRule;
|
|
1230
|
+
/**
|
|
1231
|
+
* This function will be used to process each rule. If not defined, the appropriate
|
|
1232
|
+
* `defaultRuleProcessor*` for the given format will be used.
|
|
1233
|
+
*/
|
|
1234
|
+
ruleProcessor?: RuleProcessor;
|
|
1235
|
+
/**
|
|
1236
|
+
* This function will be used to process each rule group. If not defined, the appropriate
|
|
1237
|
+
* `defaultRuleGroupProcessor*` for the format will be used.
|
|
1238
|
+
*
|
|
1239
|
+
* If this function is defined, it will override the `format` option. This also allows
|
|
1240
|
+
* `formatQuery` to produce completely custom output formats.
|
|
1241
|
+
*/
|
|
1242
|
+
ruleGroupProcessor?: RuleGroupProcessor;
|
|
1243
|
+
/**
|
|
1244
|
+
* In the "sql", "parameterized", "parameterized_named", and "jsonata" export
|
|
1245
|
+
* formats, field names will be bracketed by this string. If an array of strings
|
|
1246
|
+
* is passed, field names will be preceded by the first element and
|
|
1247
|
+
* succeeded by the second element.
|
|
1248
|
+
*
|
|
1249
|
+
* Tip: Use `fieldIdentifierSeparator` to bracket identifiers individually within field names.
|
|
1250
|
+
*
|
|
1251
|
+
* @default '' // the empty string
|
|
1252
|
+
*
|
|
1253
|
+
* @example
|
|
1254
|
+
* formatQuery(query, { format: 'sql', quoteFieldNamesWith: '"' })
|
|
1255
|
+
* // `"First name" = 'Steve'`
|
|
1256
|
+
*
|
|
1257
|
+
* @example
|
|
1258
|
+
* formatQuery(query, { format: 'sql', quoteFieldNamesWith: ['[', ']'] })
|
|
1259
|
+
* // "[First name] = 'Steve'"
|
|
1260
|
+
*/
|
|
1261
|
+
quoteFieldNamesWith?: string | [string, string];
|
|
1262
|
+
/**
|
|
1263
|
+
* When used in conjunction with the `quoteFieldNamesWith` option, field names will
|
|
1264
|
+
* be split by this string, each part being individually processed as per the rules
|
|
1265
|
+
* of the `quoteFieldNamesWith` configuration. The parts will then be re-joined
|
|
1266
|
+
* by the same string.
|
|
1267
|
+
*
|
|
1268
|
+
* A common value for this option is `'.'`.
|
|
1269
|
+
*
|
|
1270
|
+
* A value of `''` (the empty string) will disable splitting/rejoining.
|
|
1271
|
+
*
|
|
1272
|
+
* @default ''
|
|
1273
|
+
*
|
|
1274
|
+
* @example
|
|
1275
|
+
* formatQuery(query, {
|
|
1276
|
+
* format: 'sql',
|
|
1277
|
+
* quoteFieldNamesWith: ['[', ']'],
|
|
1278
|
+
* fieldIdentifierSeparator: '.',
|
|
1279
|
+
* })
|
|
1280
|
+
* // "[dbo].[Musicians].[First name] = 'Steve'"
|
|
1281
|
+
*/
|
|
1282
|
+
fieldIdentifierSeparator?: string;
|
|
1283
|
+
/**
|
|
1284
|
+
* Character to use for quoting string values in the SQL format.
|
|
1285
|
+
* @default `'`
|
|
1286
|
+
*/
|
|
1287
|
+
quoteValuesWith?: string;
|
|
1288
|
+
/**
|
|
1289
|
+
* Validator function for the entire query. Can be the same function passed
|
|
1290
|
+
* as `validator` prop to {@link QueryBuilder}.
|
|
1291
|
+
*/
|
|
1292
|
+
validator?: QueryValidator;
|
|
1293
|
+
/**
|
|
1294
|
+
* This can be the same {@link FullField} array passed to {@link QueryBuilder}, but
|
|
1295
|
+
* really all you need to provide is the `name` and `validator` for each field.
|
|
1296
|
+
*
|
|
1297
|
+
* The full field object from this array, where the field's identifying property
|
|
1298
|
+
* matches the rule's `field`, will be passed to the rule processor.
|
|
1299
|
+
*/
|
|
1300
|
+
fields?: FlexibleOptionList<FullField>;
|
|
1301
|
+
/**
|
|
1302
|
+
* This can be the same `getOperators` function passed to {@link QueryBuilder}.
|
|
1303
|
+
*
|
|
1304
|
+
* The full operator object from this array, where the operator's identifying property
|
|
1305
|
+
* matches the rule's `operator`, will be passed to the rule processor.
|
|
1306
|
+
*/
|
|
1307
|
+
getOperators?(field: string, misc: {
|
|
1308
|
+
fieldData: FullField;
|
|
1309
|
+
}): FlexibleOptionList<FullOperator> | null;
|
|
1310
|
+
/**
|
|
1311
|
+
* This string will be inserted in place of invalid groups for non-JSON formats.
|
|
1312
|
+
* Defaults to `'(1 = 1)'` for "sql"/"parameterized"/"parameterized_named" and
|
|
1313
|
+
* `'$and:[{$expr:true}]'` for "mongodb".
|
|
1314
|
+
*/
|
|
1315
|
+
fallbackExpression?: string;
|
|
1316
|
+
/**
|
|
1317
|
+
* This string will be placed in front of named parameters (aka bind variables)
|
|
1318
|
+
* when using the "parameterized_named" export format.
|
|
1319
|
+
*
|
|
1320
|
+
* @default ":"
|
|
1321
|
+
*/
|
|
1322
|
+
paramPrefix?: string;
|
|
1323
|
+
/**
|
|
1324
|
+
* Maintains the parameter prefix in the `params` object keys when using the
|
|
1325
|
+
* "parameterized_named" export format. Recommended when using SQLite.
|
|
1326
|
+
*
|
|
1327
|
+
* @default false
|
|
1328
|
+
*
|
|
1329
|
+
* @example
|
|
1330
|
+
* console.log(formatQuery(query, {
|
|
1331
|
+
* format: "parameterized_named",
|
|
1332
|
+
* paramPrefix: "$",
|
|
1333
|
+
* paramsKeepPrefix: true
|
|
1334
|
+
* }).params)
|
|
1335
|
+
* // { $firstName: "Stev" }
|
|
1336
|
+
* // Default (`paramsKeepPrefix` is `false`):
|
|
1337
|
+
* // { firstName: "Stev" }
|
|
1338
|
+
*/
|
|
1339
|
+
paramsKeepPrefix?: boolean;
|
|
1340
|
+
/**
|
|
1341
|
+
* Renders parameter placeholders as a series of sequential numbers
|
|
1342
|
+
* instead of '?' like the default. This option will respect the
|
|
1343
|
+
* `paramPrefix` option like the 'parameterized_named' format.
|
|
1344
|
+
*
|
|
1345
|
+
* @default false
|
|
1346
|
+
*/
|
|
1347
|
+
numberedParams?: boolean;
|
|
1348
|
+
/**
|
|
1349
|
+
* Preserves the order of values for "between" and "notBetween" rules, even if a larger
|
|
1350
|
+
* value comes before a smaller value (which will always evaluate to false).
|
|
1351
|
+
*/
|
|
1352
|
+
preserveValueOrder?: boolean;
|
|
1353
|
+
/**
|
|
1354
|
+
* Renders values as either `number`-types or unquoted strings, as
|
|
1355
|
+
* appropriate and when possible. Each `string`-type value is evaluated
|
|
1356
|
+
* against {@link numericRegex} to determine if it can be represented as a
|
|
1357
|
+
* plain numeric value. If so, `parseFloat` is used to convert it to a number.
|
|
1358
|
+
*/
|
|
1359
|
+
parseNumbers?: ParseNumbersPropConfig;
|
|
1360
|
+
/**
|
|
1361
|
+
* Any rules where the field is equal to this value will be ignored.
|
|
1362
|
+
*
|
|
1363
|
+
* @default '~'
|
|
1364
|
+
*/
|
|
1365
|
+
placeholderFieldName?: string;
|
|
1366
|
+
/**
|
|
1367
|
+
* Any rules where the operator is equal to this value will be ignored.
|
|
1368
|
+
*
|
|
1369
|
+
* @default '~'
|
|
1370
|
+
*/
|
|
1371
|
+
placeholderOperatorName?: string;
|
|
1372
|
+
/**
|
|
1373
|
+
* Any rules where the value is equal to this value will be ignored.
|
|
1374
|
+
*
|
|
1375
|
+
* @default '~'
|
|
1376
|
+
*/
|
|
1377
|
+
placeholderValueName?: string;
|
|
1378
|
+
/**
|
|
1379
|
+
* Operator to use when concatenating wildcard characters and field names in "sql" format.
|
|
1380
|
+
* The ANSI standard is `||`, while SQL Server uses `+`. MySQL does not implement a concatenation
|
|
1381
|
+
* operator by default, and therefore requires use of the `CONCAT` function.
|
|
1382
|
+
*
|
|
1383
|
+
* If `concatOperator` is set to `"CONCAT"` (case-insensitive), the `CONCAT` function will be
|
|
1384
|
+
* used. Note that Oracle SQL does not support more than two values in the `CONCAT` function,
|
|
1385
|
+
* so this option should not be used in that context. The default setting (`"||"`) is already
|
|
1386
|
+
* compatible with Oracle SQL.
|
|
1387
|
+
*
|
|
1388
|
+
* @default '||'
|
|
1389
|
+
*/
|
|
1390
|
+
concatOperator?: "||" | "+" | "CONCAT" | (string & {});
|
|
1391
|
+
/**
|
|
1392
|
+
* Option presets to maximize compatibility with various SQL dialects.
|
|
1393
|
+
*/
|
|
1394
|
+
preset?: SQLPreset;
|
|
1395
|
+
/**
|
|
1396
|
+
* Map of operators to their translations for the "natural_language" format. If the
|
|
1397
|
+
* result can differ based on the `valueSource`, the key should map to an array where the
|
|
1398
|
+
* second element represents the string to be used when `valueSource` is "field". The first
|
|
1399
|
+
* element will be used in all other cases.
|
|
1400
|
+
*/
|
|
1401
|
+
operatorMap?: ExportOperatorMap;
|
|
1402
|
+
/**
|
|
1403
|
+
* [Constituent word order](https://en.wikipedia.org/wiki/Word_order#Constituent_word_orders)
|
|
1404
|
+
* for the "natural_language" format. Can be abbreviated like "SVO" or spelled out like
|
|
1405
|
+
* "subject-verb-object".
|
|
1406
|
+
*
|
|
1407
|
+
* - Subject = field
|
|
1408
|
+
* - Verb = operator
|
|
1409
|
+
* - Object = value
|
|
1410
|
+
*/
|
|
1411
|
+
wordOrder?: ConstituentWordOrderString | Lowercase<ConstituentWordOrderString> | ({} & string);
|
|
1412
|
+
/**
|
|
1413
|
+
* Translatable strings used by the "natural_language" format.
|
|
1414
|
+
*/
|
|
1415
|
+
translations?: Partial<Record<NLTranslationKey, string>>;
|
|
1416
|
+
context?: Record<string, any>;
|
|
1417
|
+
}
|
|
1418
|
+
/**
|
|
1419
|
+
* Options object for {@link ValueProcessorByRule} functions.
|
|
1420
|
+
*
|
|
1421
|
+
* @group Export
|
|
1422
|
+
*/
|
|
1423
|
+
interface ValueProcessorOptions extends FormatQueryOptions {
|
|
1424
|
+
valueProcessor?: ValueProcessorByRule;
|
|
1425
|
+
escapeQuotes?: boolean;
|
|
1426
|
+
/**
|
|
1427
|
+
* The full field object, if `fields` was provided in the
|
|
1428
|
+
* {@link formatQuery} options parameter.
|
|
1429
|
+
*/
|
|
1430
|
+
fieldData?: FullField;
|
|
1431
|
+
/**
|
|
1432
|
+
* Included for the "parameterized_named" format only. Keys of this object represent
|
|
1433
|
+
* field names and values represent the current list of parameter names for that
|
|
1434
|
+
* field based on the query rules processed up to that point. Use this list to
|
|
1435
|
+
* ensure that parameter names generated by the custom rule processor are unique.
|
|
1436
|
+
*/
|
|
1437
|
+
fieldParamNames?: Record<string, string[]>;
|
|
1438
|
+
/**
|
|
1439
|
+
* Included for the "parameterized_named" format only. Call this function with a
|
|
1440
|
+
* field name to get a unique parameter name, as yet unused during query processing.
|
|
1441
|
+
*/
|
|
1442
|
+
getNextNamedParam?: (field: string) => string;
|
|
1443
|
+
/**
|
|
1444
|
+
* Additional prefix and suffix characters to wrap the value in. Useful for augmenting
|
|
1445
|
+
* the default value processor results with special syntax (e.g., for dates or function
|
|
1446
|
+
* calls).
|
|
1447
|
+
*/
|
|
1448
|
+
wrapValueWith?: [string, string];
|
|
1449
|
+
/**
|
|
1450
|
+
* Parse numbers in the rule value.
|
|
1451
|
+
*
|
|
1452
|
+
* @default false
|
|
1453
|
+
*/
|
|
1454
|
+
parseNumbers?: boolean;
|
|
1455
|
+
}
|
|
1456
|
+
/**
|
|
1457
|
+
* Options object curated by {@link formatQuery} and passed to a {@link RuleGroupProcessor}.
|
|
1458
|
+
*
|
|
1459
|
+
* @group Export
|
|
1460
|
+
*/
|
|
1461
|
+
interface FormatQueryFinalOptions extends Required<Except<FormatQueryOptions, "context" | "valueProcessor" | "validator" | "placeholderValueName" | "ruleGroupProcessor" | "parseNumbers">> {
|
|
1462
|
+
fields: FullOptionList<FullField>;
|
|
1463
|
+
getParseNumberBoolean: (inputType?: InputType | null) => boolean | undefined;
|
|
1464
|
+
parseNumbers?: ParseNumbersPropConfig | undefined;
|
|
1465
|
+
placeholderValueName?: string | undefined;
|
|
1466
|
+
valueProcessor: ValueProcessorByRule;
|
|
1467
|
+
validator?: QueryValidator;
|
|
1468
|
+
validateRule: FormatQueryValidateRule;
|
|
1469
|
+
validationMap: ValidationMap;
|
|
1470
|
+
context?: Record<string, unknown>;
|
|
1471
|
+
}
|
|
1472
|
+
/**
|
|
1473
|
+
* Function that produces a processed value for a given {@link RuleType}.
|
|
1474
|
+
*
|
|
1475
|
+
* @group Export
|
|
1476
|
+
*/
|
|
1477
|
+
type ValueProcessorByRule = (rule: RuleType, options?: ValueProcessorOptions) => string;
|
|
1478
|
+
/**
|
|
1479
|
+
* Function that produces a processed value for a given `field`, `operator`, `value`,
|
|
1480
|
+
* and `valueSource`.
|
|
1481
|
+
*
|
|
1482
|
+
* @group Export
|
|
1483
|
+
*/
|
|
1484
|
+
type ValueProcessorLegacy = (field: string, operator: string, value: any, valueSource?: ValueSource) => string;
|
|
1485
|
+
/**
|
|
1486
|
+
* Function to produce a result that {@link formatQuery} uses when processing a
|
|
1487
|
+
* {@link RuleType} object.
|
|
1488
|
+
*
|
|
1489
|
+
* See the default rule processor for each format to know what type to return.
|
|
1490
|
+
* | Format | Default rule processor |
|
|
1491
|
+
* | ------------------------ | ----------------------------------------- |
|
|
1492
|
+
* | `sql` | {@link defaultRuleProcessorSQL} |
|
|
1493
|
+
* | `parameterized` | {@link defaultRuleProcessorParameterized} |
|
|
1494
|
+
* | `parameterized_named` | {@link defaultRuleProcessorParameterized} |
|
|
1495
|
+
* | `mongodb` _(deprecated)_ | {@link defaultRuleProcessorMongoDB} |
|
|
1496
|
+
* | `mongodb_query` | {@link defaultRuleProcessorMongoDBQuery} |
|
|
1497
|
+
* | `cel` | {@link defaultRuleProcessorCEL} |
|
|
1498
|
+
* | `spel` | {@link defaultRuleProcessorSpEL} |
|
|
1499
|
+
* | `jsonlogic` | {@link defaultRuleProcessorJsonLogic} |
|
|
1500
|
+
* | `elasticsearch` | {@link defaultRuleProcessorElasticSearch} |
|
|
1501
|
+
* | `jsonata` | {@link defaultRuleProcessorJSONata} |
|
|
1502
|
+
*
|
|
1503
|
+
* @group Export
|
|
1504
|
+
*/
|
|
1505
|
+
type RuleProcessor = (rule: RuleType, options?: ValueProcessorOptions, meta?: {
|
|
1506
|
+
processedParams?: Record<string, any> | any[];
|
|
1507
|
+
context?: Record<string, any>;
|
|
1508
|
+
}) => any;
|
|
1509
|
+
/**
|
|
1510
|
+
* Function to produce a result that {@link formatQuery} uses when processing a
|
|
1511
|
+
* {@link RuleGroupType} or {@link RuleGroupTypeIC} object.
|
|
1512
|
+
*
|
|
1513
|
+
* See the default rule group processor for each format to know what type to return.
|
|
1514
|
+
* | Format | Default rule group processor |
|
|
1515
|
+
* | ------------------------ | ---------------------------------------------- |
|
|
1516
|
+
* | `sql` | {@link defaultRuleGroupProcessorSQL} |
|
|
1517
|
+
* | `parameterized` | {@link defaultRuleGroupProcessorParameterized} |
|
|
1518
|
+
* | `parameterized_named` | {@link defaultRuleGroupProcessorParameterized} |
|
|
1519
|
+
* | `mongodb` _(deprecated)_ | {@link defaultRuleGroupProcessorMongoDB} |
|
|
1520
|
+
* | `mongodb_query` | {@link defaultRuleGroupProcessorMongoDBQuery} |
|
|
1521
|
+
* | `cel` | {@link defaultRuleGroupProcessorCEL} |
|
|
1522
|
+
* | `spel` | {@link defaultRuleGroupProcessorSpEL} |
|
|
1523
|
+
* | `jsonlogic` | {@link defaultRuleGroupProcessorJsonLogic} |
|
|
1524
|
+
* | `elasticsearch` | {@link defaultRuleGroupProcessorElasticSearch} |
|
|
1525
|
+
* | `jsonata` | {@link defaultRuleGroupProcessorJSONata} |
|
|
1526
|
+
*
|
|
1527
|
+
* @group Export
|
|
1528
|
+
*/
|
|
1529
|
+
type RuleGroupProcessor<TResult = unknown> = (ruleGroup: RuleGroupTypeAny, options: FormatQueryFinalOptions, meta?: {
|
|
1530
|
+
processedParams?: Record<string, any> | any[];
|
|
1531
|
+
context?: Record<string, any>;
|
|
1532
|
+
}) => TResult;
|
|
1533
|
+
/**
|
|
1534
|
+
* Rule validator for {@link formatQuery}.
|
|
1535
|
+
*
|
|
1536
|
+
* @group Export
|
|
1537
|
+
*/
|
|
1538
|
+
type FormatQueryValidateRule = (rule: RuleType) => readonly [boolean | ValidationResult | undefined, RuleValidator | undefined];
|
|
1539
|
+
/**
|
|
1540
|
+
* @group Export
|
|
1541
|
+
*/
|
|
1542
|
+
interface RQBJsonLogicStartsWith {
|
|
1543
|
+
startsWith: [RQBJsonLogic, RQBJsonLogic, ...RQBJsonLogic[]];
|
|
1544
|
+
}
|
|
1545
|
+
/**
|
|
1546
|
+
* @group Export
|
|
1547
|
+
*/
|
|
1548
|
+
interface RQBJsonLogicEndsWith {
|
|
1549
|
+
endsWith: [RQBJsonLogic, RQBJsonLogic, ...RQBJsonLogic[]];
|
|
1550
|
+
}
|
|
1551
|
+
/**
|
|
1552
|
+
* JsonLogic rule object with additional operators generated by {@link formatQuery}
|
|
1553
|
+
* and accepted by {@link parseJsonLogic!parseJsonLogic}.
|
|
1554
|
+
*
|
|
1555
|
+
* @group Export
|
|
1556
|
+
*/
|
|
1557
|
+
type RQBJsonLogic = RulesLogic<RQBJsonLogicStartsWith | RQBJsonLogicEndsWith>;
|
|
1558
|
+
/**
|
|
1559
|
+
* Constituent word order (as string) for the "natural_language" format.
|
|
1560
|
+
*
|
|
1561
|
+
* - S (subject) = field
|
|
1562
|
+
* - V (verb) = operator
|
|
1563
|
+
* - O (object) = value
|
|
1564
|
+
*
|
|
1565
|
+
* @group Export
|
|
1566
|
+
*/
|
|
1567
|
+
type ConstituentWordOrderString = "SVO" | "SOV" | "OSV" | "OVS" | "VSO" | "VOS";
|
|
1568
|
+
type RepeatStrings<S extends string[], Depth extends number[] = []> = Depth["length"] extends 2 ? "" : "" | `_${S[number]}${RepeatStrings<S, [...Depth, 1]>}`;
|
|
1569
|
+
type ZeroOrMoreGroupVariants = RepeatStrings<["xor", "not"]>;
|
|
1570
|
+
/**
|
|
1571
|
+
* Rule group condition identifier for the "natural_language" format.
|
|
1572
|
+
*
|
|
1573
|
+
* @group Export
|
|
1574
|
+
*/
|
|
1575
|
+
|
|
1576
|
+
/**
|
|
1577
|
+
* Keys for the `translations` config object used by the "natural_language" format.
|
|
1578
|
+
*
|
|
1579
|
+
* @group Export
|
|
1580
|
+
*/
|
|
1581
|
+
type NLTranslationKey = "and" | "or" | "true" | "false" | `groupPrefix${ZeroOrMoreGroupVariants}` | `groupSuffix${ZeroOrMoreGroupVariants}`;
|
|
1582
|
+
//#endregion
|
|
1583
|
+
//#region src/types.d.ts
|
|
1584
|
+
interface RQBJsonLogicDateBetween {
|
|
1585
|
+
dateBetween: [RQBJsonLogic, RQBJsonLogic, RQBJsonLogic];
|
|
1586
|
+
}
|
|
1587
|
+
interface RQBJsonLogicDateNotBetween {
|
|
1588
|
+
dateNotBetween: [RQBJsonLogic, RQBJsonLogic, RQBJsonLogic];
|
|
1589
|
+
}
|
|
1590
|
+
interface RQBJsonLogicDateIn {
|
|
1591
|
+
dateIn: [RQBJsonLogic, RQBJsonLogic[]];
|
|
1592
|
+
}
|
|
1593
|
+
interface RQBJsonLogicDateNotIn {
|
|
1594
|
+
dateNotIn: [RQBJsonLogic, RQBJsonLogic[]];
|
|
1595
|
+
}
|
|
1596
|
+
interface RQBJsonLogicDateBefore {
|
|
1597
|
+
dateBefore: [RQBJsonLogic, RQBJsonLogic, ...RQBJsonLogic[]];
|
|
1598
|
+
}
|
|
1599
|
+
interface RQBJsonLogicDateOnOrBefore {
|
|
1600
|
+
dateOnOrBefore: [RQBJsonLogic, RQBJsonLogic, ...RQBJsonLogic[]];
|
|
1601
|
+
}
|
|
1602
|
+
interface RQBJsonLogicDateOn {
|
|
1603
|
+
dateOn: [RQBJsonLogic, RQBJsonLogic, ...RQBJsonLogic[]];
|
|
1604
|
+
}
|
|
1605
|
+
interface RQBJsonLogicDateNotOn {
|
|
1606
|
+
dateNotOn: [RQBJsonLogic, RQBJsonLogic, ...RQBJsonLogic[]];
|
|
1607
|
+
}
|
|
1608
|
+
interface RQBJsonLogicDateAfter {
|
|
1609
|
+
dateAfter: [RQBJsonLogic, RQBJsonLogic, ...RQBJsonLogic[]];
|
|
1610
|
+
}
|
|
1611
|
+
interface RQBJsonLogicDateOnOrAfter {
|
|
1612
|
+
dateOnOrAfter: [RQBJsonLogic, RQBJsonLogic, ...RQBJsonLogic[]];
|
|
1613
|
+
}
|
|
1614
|
+
/**
|
|
1615
|
+
* JsonLogic rule object with additional operators generated by
|
|
1616
|
+
* {@link @react-querybuilder/core!formatQuery formatQuery} and accepted by
|
|
1617
|
+
* {@link @react-querybuilder/core!parseJsonLogic parseJsonLogic}.
|
|
1618
|
+
*/
|
|
1619
|
+
type RQBDateTimeJsonLogic = RQBJsonLogic | RulesLogic<RQBJsonLogicDateAfter | RQBJsonLogicDateBefore | RQBJsonLogicDateBetween | RQBJsonLogicDateIn | RQBJsonLogicDateNotBetween | RQBJsonLogicDateNotIn | RQBJsonLogicDateNotOn | RQBJsonLogicDateOn | RQBJsonLogicDateOnOrAfter | RQBJsonLogicDateOnOrBefore>;
|
|
1620
|
+
type DateOrString = string | Date;
|
|
1621
|
+
interface RQBDateTimeLibraryAPI {
|
|
1622
|
+
/**
|
|
1623
|
+
* Formats a date as a string.
|
|
1624
|
+
*/
|
|
1625
|
+
format: (d: DateOrString, fmt: string) => string;
|
|
1626
|
+
/**
|
|
1627
|
+
* Formats a date as an ISO 8601 string, only including
|
|
1628
|
+
* the year, month, and day ('YYYY-MM-DD').
|
|
1629
|
+
*/
|
|
1630
|
+
toISOStringDateOnly: (d: DateOrString) => string;
|
|
1631
|
+
/**
|
|
1632
|
+
* Determines whether the first date is after the second date.
|
|
1633
|
+
*/
|
|
1634
|
+
isAfter: (a: DateOrString, b: DateOrString) => boolean;
|
|
1635
|
+
/**
|
|
1636
|
+
* Determines whether the first date is before the second date.
|
|
1637
|
+
*/
|
|
1638
|
+
isBefore: (a: DateOrString, b: DateOrString) => boolean;
|
|
1639
|
+
/**
|
|
1640
|
+
* Determines whether the first date is the same as the second date.
|
|
1641
|
+
*
|
|
1642
|
+
* If either the first date or the second date is a string in the
|
|
1643
|
+
* format 'YYYY-MM-DD', then the dates will only be compared at
|
|
1644
|
+
* the date level (no time component). Otherwise, they will be
|
|
1645
|
+
* compared to the millisecond.
|
|
1646
|
+
*/
|
|
1647
|
+
isSame: (a: DateOrString, b: DateOrString) => boolean;
|
|
1648
|
+
/**
|
|
1649
|
+
* Determines whether the date is either a valid `Date` object or an
|
|
1650
|
+
* ISO 8601-formatted string representing a valid date.
|
|
1651
|
+
*/
|
|
1652
|
+
isValid: (d: DateOrString) => boolean;
|
|
1653
|
+
/**
|
|
1654
|
+
* Converts a string to a `Date` object or returns a valid `Date` as is.
|
|
1655
|
+
*/
|
|
1656
|
+
toDate: (d: DateOrString) => Date;
|
|
1657
|
+
/**
|
|
1658
|
+
* Converts a `Date` or ISO 8601-formatted string into a full ISO 8601 UTC
|
|
1659
|
+
* string, in the format 'YYYY-MM-DDTHH:mm:ss.SSSZ' (the "T" and "Z" are
|
|
1660
|
+
* static; other parts represent standard time units, increasing in precision
|
|
1661
|
+
* from left to right).
|
|
1662
|
+
*/
|
|
1663
|
+
toISOString: (d: DateOrString) => string;
|
|
1664
|
+
}
|
|
1665
|
+
type FnDateDate = (a: DateOrString, b: DateOrString) => boolean;
|
|
1666
|
+
type FnDateArrayOfDates = (a: DateOrString, b: DateOrString[]) => boolean;
|
|
1667
|
+
type FnDateDateDate = (a: DateOrString, b: DateOrString, c: DateOrString) => boolean;
|
|
1668
|
+
type RQBJsonLogicDateTimeOperations = {
|
|
1669
|
+
/** Determines if the first date is after the second date. */
|
|
1670
|
+
dateAfter: FnDateDate;
|
|
1671
|
+
/** Determines if the first date is before the second date. */
|
|
1672
|
+
dateBefore: FnDateDate;
|
|
1673
|
+
/** Determines if the second date is between the first and third dates (inclusive). */
|
|
1674
|
+
dateBetween: FnDateDateDate;
|
|
1675
|
+
/** Determines if the first date is one of the values in the array of dates. */
|
|
1676
|
+
dateIn: FnDateArrayOfDates;
|
|
1677
|
+
/** Determines if the second date is not between the first and third dates. */
|
|
1678
|
+
dateNotBetween: FnDateDateDate;
|
|
1679
|
+
/** Determines if the first date is not one of the values in the array of dates. */
|
|
1680
|
+
dateNotIn: FnDateArrayOfDates;
|
|
1681
|
+
/** Determines if the first date is not the same as the second date. */
|
|
1682
|
+
dateNotOn: FnDateDate;
|
|
1683
|
+
/** Determines if the first date is the same as the second date (at the level of least precision between the two). */
|
|
1684
|
+
dateOn: FnDateDate;
|
|
1685
|
+
/** Determines if the first date is either the same as the second date or after it. */
|
|
1686
|
+
dateOnOrAfter: FnDateDate;
|
|
1687
|
+
/** Determines if the first date is either the same as the second date or before it. */
|
|
1688
|
+
dateOnOrBefore: FnDateDate;
|
|
1689
|
+
};
|
|
1690
|
+
type IsDateFieldFunction = (...[rule, opts]: Parameters<RuleProcessor>) => boolean;
|
|
1691
|
+
type IsDateField = IsDateFieldFunction | Record<string, unknown> | Record<string, unknown>[] | boolean | undefined;
|
|
1692
|
+
//#endregion
|
|
1693
|
+
export { ExportOperatorMap, IsDateField, IsDateFieldFunction, RQBDateTimeJsonLogic, RQBDateTimeLibraryAPI, RQBJsonLogicDateAfter, RQBJsonLogicDateBefore, RQBJsonLogicDateBetween, RQBJsonLogicDateIn, RQBJsonLogicDateNotBetween, RQBJsonLogicDateNotIn, RQBJsonLogicDateNotOn, RQBJsonLogicDateOn, RQBJsonLogicDateOnOrAfter, RQBJsonLogicDateOnOrBefore, RQBJsonLogicDateTimeOperations, RuleProcessor, ValueProcessorByRule };
|
|
1694
|
+
//# sourceMappingURL=types-EgfSemtR.d.ts.map
|