@e22m4u/js-repository 0.0.31
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/.c8rc +9 -0
- package/.commitlintrc +5 -0
- package/.editorconfig +13 -0
- package/.eslintignore +1 -0
- package/.eslintrc.cjs +27 -0
- package/.husky/commit-msg +4 -0
- package/.husky/pre-commit +9 -0
- package/.mocharc.cjs +7 -0
- package/.prettierrc +7 -0
- package/LICENSE +21 -0
- package/README.md +523 -0
- package/mocha.setup.js +10 -0
- package/package.json +57 -0
- package/src/adapter/adapter-loader.d.ts +16 -0
- package/src/adapter/adapter-loader.js +63 -0
- package/src/adapter/adapter-loader.spec.js +31 -0
- package/src/adapter/adapter-registry.d.ts +14 -0
- package/src/adapter/adapter-registry.js +36 -0
- package/src/adapter/adapter-registry.spec.js +36 -0
- package/src/adapter/adapter.d.ts +118 -0
- package/src/adapter/adapter.js +181 -0
- package/src/adapter/adapter.spec.js +144 -0
- package/src/adapter/builtin/memory-adapter.d.ts +118 -0
- package/src/adapter/builtin/memory-adapter.js +342 -0
- package/src/adapter/builtin/memory-adapter.spec.js +2925 -0
- package/src/adapter/decorator/data-sanitizing-decorator.d.ts +13 -0
- package/src/adapter/decorator/data-sanitizing-decorator.js +44 -0
- package/src/adapter/decorator/data-sanitizing-decorator.spec.js +59 -0
- package/src/adapter/decorator/data-validation-decorator.d.ts +13 -0
- package/src/adapter/decorator/data-validation-decorator.js +41 -0
- package/src/adapter/decorator/data-validation-decorator.spec.js +59 -0
- package/src/adapter/decorator/default-values-decorator.d.ts +13 -0
- package/src/adapter/decorator/default-values-decorator.js +57 -0
- package/src/adapter/decorator/default-values-decorator.spec.js +141 -0
- package/src/adapter/decorator/fields-filtering-decorator.d.ts +13 -0
- package/src/adapter/decorator/fields-filtering-decorator.js +72 -0
- package/src/adapter/decorator/fields-filtering-decorator.spec.js +119 -0
- package/src/adapter/decorator/inclusion-decorator.d.ts +13 -0
- package/src/adapter/decorator/inclusion-decorator.js +78 -0
- package/src/adapter/decorator/inclusion-decorator.spec.js +117 -0
- package/src/adapter/decorator/index.d.ts +5 -0
- package/src/adapter/decorator/index.js +5 -0
- package/src/adapter/index.d.ts +3 -0
- package/src/adapter/index.js +3 -0
- package/src/definition/datasource/datasource-definition-validator.d.ts +14 -0
- package/src/definition/datasource/datasource-definition-validator.js +33 -0
- package/src/definition/datasource/datasource-definition-validator.spec.js +63 -0
- package/src/definition/datasource/datasource-definition.d.ts +7 -0
- package/src/definition/datasource/index.d.ts +2 -0
- package/src/definition/datasource/index.js +1 -0
- package/src/definition/definition-registry.d.ts +50 -0
- package/src/definition/definition-registry.js +98 -0
- package/src/definition/definition-registry.spec.js +78 -0
- package/src/definition/index.d.ts +3 -0
- package/src/definition/index.js +3 -0
- package/src/definition/model/index.d.ts +7 -0
- package/src/definition/model/index.js +6 -0
- package/src/definition/model/model-data-sanitizer.d.ts +15 -0
- package/src/definition/model/model-data-sanitizer.js +33 -0
- package/src/definition/model/model-data-validator.d.ts +32 -0
- package/src/definition/model/model-data-validator.js +144 -0
- package/src/definition/model/model-data-validator.spec.js +1889 -0
- package/src/definition/model/model-definition-utils.d.ts +161 -0
- package/src/definition/model/model-definition-utils.js +371 -0
- package/src/definition/model/model-definition-utils.spec.js +1474 -0
- package/src/definition/model/model-definition-validator.d.ts +14 -0
- package/src/definition/model/model-definition-validator.js +83 -0
- package/src/definition/model/model-definition-validator.spec.js +143 -0
- package/src/definition/model/model-definition.d.ts +28 -0
- package/src/definition/model/properties/data-type.d.ts +11 -0
- package/src/definition/model/properties/data-type.js +11 -0
- package/src/definition/model/properties/default-values-definition-validator.d.ts +15 -0
- package/src/definition/model/properties/default-values-definition-validator.js +53 -0
- package/src/definition/model/properties/default-values-definition-validator.spec.js +136 -0
- package/src/definition/model/properties/index.d.ts +5 -0
- package/src/definition/model/properties/index.js +4 -0
- package/src/definition/model/properties/primary-keys-definition-validator.d.ts +15 -0
- package/src/definition/model/properties/primary-keys-definition-validator.js +55 -0
- package/src/definition/model/properties/primary-keys-definition-validator.spec.js +145 -0
- package/src/definition/model/properties/properties-definition-validator.d.ts +15 -0
- package/src/definition/model/properties/properties-definition-validator.js +194 -0
- package/src/definition/model/properties/properties-definition-validator.spec.js +373 -0
- package/src/definition/model/properties/property-definition.d.ts +20 -0
- package/src/definition/model/relations/index.d.ts +3 -0
- package/src/definition/model/relations/index.js +2 -0
- package/src/definition/model/relations/relation-definition.d.ts +254 -0
- package/src/definition/model/relations/relation-type.d.ts +9 -0
- package/src/definition/model/relations/relation-type.js +9 -0
- package/src/definition/model/relations/relations-definition-validator.d.ts +15 -0
- package/src/definition/model/relations/relations-definition-validator.js +449 -0
- package/src/definition/model/relations/relations-definition-validator.spec.js +772 -0
- package/src/errors/index.d.ts +3 -0
- package/src/errors/index.js +3 -0
- package/src/errors/invalid-argument-error.d.ts +6 -0
- package/src/errors/invalid-argument-error.js +6 -0
- package/src/errors/invalid-argument-error.spec.js +33 -0
- package/src/errors/invalid-operator-value-error.d.ts +13 -0
- package/src/errors/invalid-operator-value-error.js +24 -0
- package/src/errors/invalid-operator-value-error.spec.js +11 -0
- package/src/errors/not-implemented-error.d.ts +6 -0
- package/src/errors/not-implemented-error.js +6 -0
- package/src/errors/not-implemented-error.spec.js +33 -0
- package/src/filter/fields-clause-tool.d.ts +38 -0
- package/src/filter/fields-clause-tool.js +88 -0
- package/src/filter/fields-clause-tool.spec.js +133 -0
- package/src/filter/filter.d.ts +335 -0
- package/src/filter/include-clause-tool.d.ts +53 -0
- package/src/filter/include-clause-tool.js +364 -0
- package/src/filter/include-clause-tool.spec.js +653 -0
- package/src/filter/index.d.ts +7 -0
- package/src/filter/index.js +6 -0
- package/src/filter/operator-clause-tool.d.ts +223 -0
- package/src/filter/operator-clause-tool.js +515 -0
- package/src/filter/operator-clause-tool.spec.js +1064 -0
- package/src/filter/order-clause-tool.d.ts +32 -0
- package/src/filter/order-clause-tool.js +97 -0
- package/src/filter/order-clause-tool.spec.js +438 -0
- package/src/filter/slice-clause-tool.d.ts +30 -0
- package/src/filter/slice-clause-tool.js +65 -0
- package/src/filter/slice-clause-tool.spec.js +117 -0
- package/src/filter/where-clause-tool.d.ts +23 -0
- package/src/filter/where-clause-tool.js +165 -0
- package/src/filter/where-clause-tool.spec.js +280 -0
- package/src/index.d.ts +9 -0
- package/src/index.js +8 -0
- package/src/relations/belongs-to-resolver.d.ts +46 -0
- package/src/relations/belongs-to-resolver.js +242 -0
- package/src/relations/belongs-to-resolver.spec.js +1047 -0
- package/src/relations/has-many-resolver.d.ts +67 -0
- package/src/relations/has-many-resolver.js +317 -0
- package/src/relations/has-many-resolver.spec.js +2911 -0
- package/src/relations/has-one-resolver.d.ts +67 -0
- package/src/relations/has-one-resolver.js +311 -0
- package/src/relations/has-one-resolver.spec.js +2274 -0
- package/src/relations/index.d.ts +4 -0
- package/src/relations/index.js +4 -0
- package/src/relations/references-many-resolver.d.ts +27 -0
- package/src/relations/references-many-resolver.js +113 -0
- package/src/relations/references-many-resolver.spec.js +631 -0
- package/src/repository/index.d.ts +2 -0
- package/src/repository/index.js +2 -0
- package/src/repository/repository-registry.d.ts +29 -0
- package/src/repository/repository-registry.js +57 -0
- package/src/repository/repository-registry.spec.js +38 -0
- package/src/repository/repository.d.ts +164 -0
- package/src/repository/repository.js +207 -0
- package/src/repository/repository.spec.js +202 -0
- package/src/schema.d.ts +37 -0
- package/src/schema.js +41 -0
- package/src/types.d.ts +30 -0
- package/src/utils/capitalize.d.ts +6 -0
- package/src/utils/capitalize.js +10 -0
- package/src/utils/capitalize.spec.js +14 -0
- package/src/utils/clone-deep.d.ts +6 -0
- package/src/utils/clone-deep.js +61 -0
- package/src/utils/clone-deep.spec.js +28 -0
- package/src/utils/exclude-object-keys.d.ts +10 -0
- package/src/utils/exclude-object-keys.js +20 -0
- package/src/utils/exclude-object-keys.spec.js +49 -0
- package/src/utils/get-ctor-name.d.ts +6 -0
- package/src/utils/get-ctor-name.js +11 -0
- package/src/utils/get-ctor-name.spec.js +17 -0
- package/src/utils/get-value-by-path.d.ts +12 -0
- package/src/utils/get-value-by-path.js +23 -0
- package/src/utils/get-value-by-path.spec.js +36 -0
- package/src/utils/index.d.ts +10 -0
- package/src/utils/index.js +10 -0
- package/src/utils/is-ctor.d.ts +7 -0
- package/src/utils/is-ctor.js +10 -0
- package/src/utils/is-ctor.spec.js +26 -0
- package/src/utils/is-pure-object.d.ts +6 -0
- package/src/utils/is-pure-object.js +15 -0
- package/src/utils/is-pure-object.spec.js +25 -0
- package/src/utils/select-object-keys.d.ts +10 -0
- package/src/utils/select-object-keys.js +37 -0
- package/src/utils/select-object-keys.spec.js +40 -0
- package/src/utils/singularize.d.ts +6 -0
- package/src/utils/singularize.js +22 -0
- package/src/utils/singularize.spec.js +23 -0
- package/src/utils/string-to-regexp.d.ts +10 -0
- package/src/utils/string-to-regexp.js +22 -0
- package/src/utils/string-to-regexp.spec.js +35 -0
- package/tsconfig.json +9 -0
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import {Service} from '@e22m4u/js-service';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Operator clause tool.
|
|
5
|
+
*/
|
|
6
|
+
export declare class OperatorClauseTool extends Service {
|
|
7
|
+
/**
|
|
8
|
+
* Compare.
|
|
9
|
+
*
|
|
10
|
+
* @param val1
|
|
11
|
+
* @param val2
|
|
12
|
+
*/
|
|
13
|
+
compare(val1: unknown, val2: unknown): number;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Test all operators.
|
|
17
|
+
*
|
|
18
|
+
* @param clause
|
|
19
|
+
* @param value
|
|
20
|
+
*/
|
|
21
|
+
testAll(clause: object, value: unknown): boolean | undefined;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Test eq/neq operator.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* {
|
|
29
|
+
* eq: 'foo',
|
|
30
|
+
* }
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* {
|
|
36
|
+
* neq: 'foo',
|
|
37
|
+
* }
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* @param clause
|
|
41
|
+
* @param value
|
|
42
|
+
*/
|
|
43
|
+
testEqNeq(clause: object, value: unknown): boolean | undefined;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Test lt/gt/lte/gte operator.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```ts
|
|
50
|
+
* {
|
|
51
|
+
* lt: 10,
|
|
52
|
+
* }
|
|
53
|
+
* ```
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```ts
|
|
57
|
+
* {
|
|
58
|
+
* lte: 10,
|
|
59
|
+
* }
|
|
60
|
+
* ```
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```ts
|
|
64
|
+
* {
|
|
65
|
+
* gt: 10,
|
|
66
|
+
* }
|
|
67
|
+
* ```
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```ts
|
|
71
|
+
* {
|
|
72
|
+
* gte: 10,
|
|
73
|
+
* }
|
|
74
|
+
* ```
|
|
75
|
+
*
|
|
76
|
+
* @param clause
|
|
77
|
+
* @param value
|
|
78
|
+
*/
|
|
79
|
+
testGtLt(clause: object, value: unknown): boolean | undefined;
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Test inc operator.
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* ```ts
|
|
86
|
+
* {
|
|
87
|
+
* inc: ['foo', 'bar'],
|
|
88
|
+
* }
|
|
89
|
+
* ```
|
|
90
|
+
*
|
|
91
|
+
* @param clause
|
|
92
|
+
* @param value
|
|
93
|
+
*/
|
|
94
|
+
testInq(clause: object, value: unknown): boolean | undefined;
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Test nin operator.
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* ```ts
|
|
101
|
+
* {
|
|
102
|
+
* nin: ['foo', 'bar'],
|
|
103
|
+
* }
|
|
104
|
+
* ```
|
|
105
|
+
*
|
|
106
|
+
* @param clause
|
|
107
|
+
* @param value
|
|
108
|
+
*/
|
|
109
|
+
testNin(clause: object, value: unknown): boolean | undefined;
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Test between operator.
|
|
113
|
+
*
|
|
114
|
+
* @example
|
|
115
|
+
* ```ts
|
|
116
|
+
* {
|
|
117
|
+
* between: [10, 20],
|
|
118
|
+
* }
|
|
119
|
+
* ```
|
|
120
|
+
*
|
|
121
|
+
* @param clause
|
|
122
|
+
* @param value
|
|
123
|
+
*/
|
|
124
|
+
testBetween(clause: object, value: unknown): boolean | undefined;
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Test exists operator.
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* ```ts
|
|
131
|
+
* {
|
|
132
|
+
* exists: true,
|
|
133
|
+
* }
|
|
134
|
+
* ```
|
|
135
|
+
*
|
|
136
|
+
* @param clause
|
|
137
|
+
* @param value
|
|
138
|
+
*/
|
|
139
|
+
testExists(clause: object, value: unknown): boolean | undefined;
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Test like operator.
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* ```ts
|
|
146
|
+
* {
|
|
147
|
+
* like: 'foo',
|
|
148
|
+
* }
|
|
149
|
+
* ```
|
|
150
|
+
*
|
|
151
|
+
* @param clause
|
|
152
|
+
* @param value
|
|
153
|
+
*/
|
|
154
|
+
testLike(clause: object, value: unknown): boolean | undefined;
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Test nlike operator.
|
|
158
|
+
*
|
|
159
|
+
* @example
|
|
160
|
+
* ```ts
|
|
161
|
+
* {
|
|
162
|
+
* nlike: 'foo',
|
|
163
|
+
* }
|
|
164
|
+
* ```
|
|
165
|
+
*
|
|
166
|
+
* @param clause
|
|
167
|
+
* @param value
|
|
168
|
+
*/
|
|
169
|
+
testNlike(clause: object, value: unknown): boolean | undefined;
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Test ilike operator.
|
|
173
|
+
*
|
|
174
|
+
* @example
|
|
175
|
+
* ```ts
|
|
176
|
+
* {
|
|
177
|
+
* ilike: 'foo',
|
|
178
|
+
* }
|
|
179
|
+
* ```
|
|
180
|
+
*
|
|
181
|
+
* @param clause
|
|
182
|
+
* @param value
|
|
183
|
+
*/
|
|
184
|
+
testIlike(clause: object, value: unknown): boolean | undefined;
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Test nilike operator.
|
|
188
|
+
*
|
|
189
|
+
* @example
|
|
190
|
+
* ```ts
|
|
191
|
+
* {
|
|
192
|
+
* nilike: 'foo',
|
|
193
|
+
* }
|
|
194
|
+
* ```
|
|
195
|
+
*
|
|
196
|
+
* @param clause
|
|
197
|
+
* @param value
|
|
198
|
+
*/
|
|
199
|
+
testNilike(clause: object, value: unknown): boolean | undefined;
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Test regexp.
|
|
203
|
+
*
|
|
204
|
+
* @example
|
|
205
|
+
* ```ts
|
|
206
|
+
* {
|
|
207
|
+
* regexp: 'foo.*',
|
|
208
|
+
* }
|
|
209
|
+
* ```
|
|
210
|
+
*
|
|
211
|
+
* @example
|
|
212
|
+
* ```ts
|
|
213
|
+
* {
|
|
214
|
+
* regexp: 'foo.*',
|
|
215
|
+
* flags: 'i',
|
|
216
|
+
* }
|
|
217
|
+
* ```
|
|
218
|
+
*
|
|
219
|
+
* @param clause
|
|
220
|
+
* @param value
|
|
221
|
+
*/
|
|
222
|
+
testRegexp(clause: object, value: unknown): boolean | undefined;
|
|
223
|
+
}
|
|
@@ -0,0 +1,515 @@
|
|
|
1
|
+
import {Service} from '@e22m4u/js-service';
|
|
2
|
+
import {stringToRegexp} from '../utils/index.js';
|
|
3
|
+
import {InvalidArgumentError} from '../errors/index.js';
|
|
4
|
+
import {InvalidOperatorValueError} from '../errors/index.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Operator clause tool.
|
|
8
|
+
*/
|
|
9
|
+
export class OperatorClauseTool extends Service {
|
|
10
|
+
/**
|
|
11
|
+
* Compare.
|
|
12
|
+
*
|
|
13
|
+
* @param {*} val1 The 1st value
|
|
14
|
+
* @param {*} val2 The 2nd value
|
|
15
|
+
* @returns {number} 0: =, positive: >, negative <
|
|
16
|
+
*/
|
|
17
|
+
compare(val1, val2) {
|
|
18
|
+
if (val1 == null || val2 == null) {
|
|
19
|
+
return val1 == val2 ? 0 : NaN;
|
|
20
|
+
}
|
|
21
|
+
if (typeof val1 === 'number') {
|
|
22
|
+
if (
|
|
23
|
+
typeof val2 === 'number' ||
|
|
24
|
+
typeof val2 === 'string' ||
|
|
25
|
+
typeof val2 === 'boolean'
|
|
26
|
+
) {
|
|
27
|
+
if (val1 === val2) return 0;
|
|
28
|
+
return val1 - Number(val2);
|
|
29
|
+
}
|
|
30
|
+
return NaN;
|
|
31
|
+
}
|
|
32
|
+
if (typeof val1 === 'string') {
|
|
33
|
+
const isDigits = /^\d+$/.test(val1);
|
|
34
|
+
if (isDigits) return this.compare(Number(val1), val2);
|
|
35
|
+
try {
|
|
36
|
+
if (val1 > val2) return 1;
|
|
37
|
+
if (val1 < val2) return -1;
|
|
38
|
+
if (val1 == val2) return 0;
|
|
39
|
+
} catch (e) {
|
|
40
|
+
/**/
|
|
41
|
+
}
|
|
42
|
+
return NaN;
|
|
43
|
+
}
|
|
44
|
+
if (typeof val1 === 'boolean') {
|
|
45
|
+
return Number(val1) - Number(val2);
|
|
46
|
+
}
|
|
47
|
+
// Return NaN if we don't know how to compare.
|
|
48
|
+
return val1 === val2 ? 0 : NaN;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Test all operators.
|
|
53
|
+
*
|
|
54
|
+
* @param {object} clause
|
|
55
|
+
* @param {*} value
|
|
56
|
+
* @returns {boolean|undefined}
|
|
57
|
+
*/
|
|
58
|
+
testAll(clause, value) {
|
|
59
|
+
if (!clause || typeof clause !== 'object' || Array.isArray(clause))
|
|
60
|
+
throw new InvalidArgumentError(
|
|
61
|
+
'A first argument of OperatorUtils.testAll ' +
|
|
62
|
+
'should be an Object, but %v given.',
|
|
63
|
+
clause,
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
// {eq: ...}
|
|
67
|
+
// {neq: ...}
|
|
68
|
+
const eqNeqTest = this.testEqNeq(clause, value);
|
|
69
|
+
if (eqNeqTest !== undefined) return eqNeqTest;
|
|
70
|
+
|
|
71
|
+
// {gt: ...}
|
|
72
|
+
// {gte: ...}
|
|
73
|
+
// {lt: ...}
|
|
74
|
+
// {lte: ...}
|
|
75
|
+
const gtLtTest = this.testGtLt(clause, value);
|
|
76
|
+
if (gtLtTest !== undefined) return gtLtTest;
|
|
77
|
+
|
|
78
|
+
// {inc: ...}
|
|
79
|
+
const incTest = this.testInq(clause, value);
|
|
80
|
+
if (incTest !== undefined) return incTest;
|
|
81
|
+
|
|
82
|
+
// {nin: ...}
|
|
83
|
+
const ninTest = this.testNin(clause, value);
|
|
84
|
+
if (ninTest !== undefined) return ninTest;
|
|
85
|
+
|
|
86
|
+
// {between: ...}
|
|
87
|
+
const betweenTest = this.testBetween(clause, value);
|
|
88
|
+
if (betweenTest !== undefined) return betweenTest;
|
|
89
|
+
|
|
90
|
+
// {exists: ...}
|
|
91
|
+
const existsTest = this.testExists(clause, value);
|
|
92
|
+
if (existsTest !== undefined) return existsTest;
|
|
93
|
+
|
|
94
|
+
// {like: ...}
|
|
95
|
+
const likeTest = this.testLike(clause, value);
|
|
96
|
+
if (likeTest !== undefined) return likeTest;
|
|
97
|
+
|
|
98
|
+
// {nlike: ...}
|
|
99
|
+
const nlikeTest = this.testNlike(clause, value);
|
|
100
|
+
if (nlikeTest !== undefined) return nlikeTest;
|
|
101
|
+
|
|
102
|
+
// {ilike: ...}
|
|
103
|
+
const ilikeTest = this.testIlike(clause, value);
|
|
104
|
+
if (ilikeTest !== undefined) return ilikeTest;
|
|
105
|
+
|
|
106
|
+
// {nilike: ...}
|
|
107
|
+
const nilikeTest = this.testNilike(clause, value);
|
|
108
|
+
if (nilikeTest !== undefined) return nilikeTest;
|
|
109
|
+
|
|
110
|
+
// {regexp: ...}
|
|
111
|
+
const regExpTest = this.testRegexp(clause, value);
|
|
112
|
+
if (regExpTest !== undefined) return regExpTest;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Test eq/neq operator.
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* ```ts
|
|
120
|
+
* {
|
|
121
|
+
* eq: 'foo',
|
|
122
|
+
* }
|
|
123
|
+
* ```
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* ```ts
|
|
127
|
+
* {
|
|
128
|
+
* neq: 'foo',
|
|
129
|
+
* }
|
|
130
|
+
* ```
|
|
131
|
+
*
|
|
132
|
+
* @param {object} clause
|
|
133
|
+
* @param {*} value
|
|
134
|
+
* @returns {boolean|undefined}
|
|
135
|
+
*/
|
|
136
|
+
testEqNeq(clause, value) {
|
|
137
|
+
if (!clause || typeof clause !== 'object')
|
|
138
|
+
throw new InvalidArgumentError(
|
|
139
|
+
'A first argument of OperatorUtils.testEqNeq ' +
|
|
140
|
+
'should be an Object, but %v given.',
|
|
141
|
+
clause,
|
|
142
|
+
);
|
|
143
|
+
if ('eq' in clause) return this.compare(clause.eq, value) === 0;
|
|
144
|
+
if ('neq' in clause) return this.compare(clause.neq, value) !== 0;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Test lt/gt/lte/gte operator.
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
* ```ts
|
|
152
|
+
* {
|
|
153
|
+
* lt: 10,
|
|
154
|
+
* }
|
|
155
|
+
* ```
|
|
156
|
+
*
|
|
157
|
+
* @example
|
|
158
|
+
* ```ts
|
|
159
|
+
* {
|
|
160
|
+
* lte: 10,
|
|
161
|
+
* }
|
|
162
|
+
* ```
|
|
163
|
+
*
|
|
164
|
+
* @example
|
|
165
|
+
* ```ts
|
|
166
|
+
* {
|
|
167
|
+
* gt: 10,
|
|
168
|
+
* }
|
|
169
|
+
* ```
|
|
170
|
+
*
|
|
171
|
+
* @example
|
|
172
|
+
* ```ts
|
|
173
|
+
* {
|
|
174
|
+
* gte: 10,
|
|
175
|
+
* }
|
|
176
|
+
* ```
|
|
177
|
+
*
|
|
178
|
+
* @param {object} clause
|
|
179
|
+
* @param {*} value
|
|
180
|
+
* @returns {boolean|undefined}
|
|
181
|
+
*/
|
|
182
|
+
testGtLt(clause, value) {
|
|
183
|
+
if (!clause || typeof clause !== 'object')
|
|
184
|
+
throw new InvalidArgumentError(
|
|
185
|
+
'A first argument of OperatorUtils.testGtLt ' +
|
|
186
|
+
'should be an Object, but %v given.',
|
|
187
|
+
clause,
|
|
188
|
+
);
|
|
189
|
+
if ('gt' in clause) return this.compare(value, clause.gt) > 0;
|
|
190
|
+
if ('gte' in clause) return this.compare(value, clause.gte) >= 0;
|
|
191
|
+
if ('lt' in clause) return this.compare(value, clause.lt) < 0;
|
|
192
|
+
if ('lte' in clause) return this.compare(value, clause.lte) <= 0;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Test inc operator.
|
|
197
|
+
*
|
|
198
|
+
* @example
|
|
199
|
+
* ```ts
|
|
200
|
+
* {
|
|
201
|
+
* inc: ['foo', 'bar'],
|
|
202
|
+
* }
|
|
203
|
+
* ```
|
|
204
|
+
*
|
|
205
|
+
* @param {object} clause
|
|
206
|
+
* @param {*} value
|
|
207
|
+
* @returns {boolean|undefined}
|
|
208
|
+
*/
|
|
209
|
+
testInq(clause, value) {
|
|
210
|
+
if (!clause || typeof clause !== 'object')
|
|
211
|
+
throw new InvalidArgumentError(
|
|
212
|
+
'A first argument of OperatorUtils.testInq ' +
|
|
213
|
+
'should be an Object, but %v given.',
|
|
214
|
+
clause,
|
|
215
|
+
);
|
|
216
|
+
if ('inq' in clause && clause.inq !== undefined) {
|
|
217
|
+
if (!clause.inq || !Array.isArray(clause.inq)) {
|
|
218
|
+
throw new InvalidOperatorValueError(
|
|
219
|
+
'inq',
|
|
220
|
+
'an Array of possible values',
|
|
221
|
+
clause.inq,
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
for (let i = 0; i < clause.inq.length; i++) {
|
|
225
|
+
if (clause.inq[i] == value) return true;
|
|
226
|
+
}
|
|
227
|
+
return false;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Test nin operator.
|
|
233
|
+
*
|
|
234
|
+
* @example
|
|
235
|
+
* ```ts
|
|
236
|
+
* {
|
|
237
|
+
* nin: ['foo', 'bar'],
|
|
238
|
+
* }
|
|
239
|
+
* ```
|
|
240
|
+
*
|
|
241
|
+
* @param {object} clause
|
|
242
|
+
* @param {*} value
|
|
243
|
+
* @returns {boolean|undefined}
|
|
244
|
+
*/
|
|
245
|
+
testNin(clause, value) {
|
|
246
|
+
if (!clause || typeof clause !== 'object')
|
|
247
|
+
throw new InvalidArgumentError(
|
|
248
|
+
'A first argument of OperatorUtils.testNin ' +
|
|
249
|
+
'should be an Object, but %v given.',
|
|
250
|
+
clause,
|
|
251
|
+
);
|
|
252
|
+
if ('nin' in clause && clause.nin !== undefined) {
|
|
253
|
+
if (!clause.nin || !Array.isArray(clause.nin)) {
|
|
254
|
+
throw new InvalidOperatorValueError(
|
|
255
|
+
'nin',
|
|
256
|
+
'an Array of possible values',
|
|
257
|
+
clause.nin,
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
for (let i = 0; i < clause.nin.length; i++) {
|
|
261
|
+
if (clause.nin[i] == value) return false;
|
|
262
|
+
}
|
|
263
|
+
return true;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Test between operator.
|
|
269
|
+
*
|
|
270
|
+
* @example
|
|
271
|
+
* ```ts
|
|
272
|
+
* {
|
|
273
|
+
* between: [10, 20],
|
|
274
|
+
* }
|
|
275
|
+
* ```
|
|
276
|
+
*
|
|
277
|
+
* @param {object} clause
|
|
278
|
+
* @param {*} value
|
|
279
|
+
* @returns {boolean|undefined}
|
|
280
|
+
*/
|
|
281
|
+
testBetween(clause, value) {
|
|
282
|
+
if (!clause || typeof clause !== 'object')
|
|
283
|
+
throw new InvalidArgumentError(
|
|
284
|
+
'A first argument of OperatorUtils.testBetween ' +
|
|
285
|
+
'should be an Object, but %v given.',
|
|
286
|
+
clause,
|
|
287
|
+
);
|
|
288
|
+
if ('between' in clause && clause.between !== undefined) {
|
|
289
|
+
if (!Array.isArray(clause.between) || clause.between.length !== 2) {
|
|
290
|
+
throw new InvalidOperatorValueError(
|
|
291
|
+
'between',
|
|
292
|
+
'an Array of 2 elements',
|
|
293
|
+
clause.between,
|
|
294
|
+
);
|
|
295
|
+
}
|
|
296
|
+
return (
|
|
297
|
+
this.testGtLt({gte: clause.between[0]}, value) &&
|
|
298
|
+
this.testGtLt({lte: clause.between[1]}, value)
|
|
299
|
+
);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Test exists operator.
|
|
305
|
+
*
|
|
306
|
+
* @example
|
|
307
|
+
* ```ts
|
|
308
|
+
* {
|
|
309
|
+
* exists: true,
|
|
310
|
+
* }
|
|
311
|
+
* ```
|
|
312
|
+
*
|
|
313
|
+
* @param {object} clause
|
|
314
|
+
* @param {*} value
|
|
315
|
+
* @returns {boolean|undefined}
|
|
316
|
+
*/
|
|
317
|
+
testExists(clause, value) {
|
|
318
|
+
if (!clause || typeof clause !== 'object')
|
|
319
|
+
throw new InvalidArgumentError(
|
|
320
|
+
'A first argument of OperatorUtils.testExists ' +
|
|
321
|
+
'should be an Object, but %v given.',
|
|
322
|
+
clause,
|
|
323
|
+
);
|
|
324
|
+
if ('exists' in clause && clause.exists !== undefined) {
|
|
325
|
+
if (typeof clause.exists !== 'boolean') {
|
|
326
|
+
throw new InvalidOperatorValueError(
|
|
327
|
+
'exists',
|
|
328
|
+
'a Boolean',
|
|
329
|
+
clause.exists,
|
|
330
|
+
);
|
|
331
|
+
}
|
|
332
|
+
return clause.exists ? value !== undefined : value === undefined;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* Test like operator.
|
|
338
|
+
*
|
|
339
|
+
* @example
|
|
340
|
+
* ```ts
|
|
341
|
+
* {
|
|
342
|
+
* like: 'foo',
|
|
343
|
+
* }
|
|
344
|
+
* ```
|
|
345
|
+
*
|
|
346
|
+
* @param {object} clause
|
|
347
|
+
* @param {*} value
|
|
348
|
+
* @returns {boolean|undefined}
|
|
349
|
+
*/
|
|
350
|
+
testLike(clause, value) {
|
|
351
|
+
if (!clause || typeof clause !== 'object')
|
|
352
|
+
throw new InvalidArgumentError(
|
|
353
|
+
'A first argument of OperatorUtils.testLike ' +
|
|
354
|
+
'should be an Object, but %v given.',
|
|
355
|
+
clause,
|
|
356
|
+
);
|
|
357
|
+
if ('like' in clause && clause.like !== undefined) {
|
|
358
|
+
if (typeof clause.like !== 'string' && !(clause.like instanceof RegExp))
|
|
359
|
+
throw new InvalidOperatorValueError('like', 'a String', clause.like);
|
|
360
|
+
return stringToRegexp(clause.like).test(value);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* Test nlike operator.
|
|
366
|
+
*
|
|
367
|
+
* @example
|
|
368
|
+
* ```ts
|
|
369
|
+
* {
|
|
370
|
+
* nlike: 'foo',
|
|
371
|
+
* }
|
|
372
|
+
* ```
|
|
373
|
+
*
|
|
374
|
+
* @param {object} clause
|
|
375
|
+
* @param {*} value
|
|
376
|
+
* @returns {boolean|undefined}
|
|
377
|
+
*/
|
|
378
|
+
testNlike(clause, value) {
|
|
379
|
+
if (!clause || typeof clause !== 'object')
|
|
380
|
+
throw new InvalidArgumentError(
|
|
381
|
+
'A first argument of OperatorUtils.testNlike ' +
|
|
382
|
+
'should be an Object, but %v given.',
|
|
383
|
+
clause,
|
|
384
|
+
);
|
|
385
|
+
if ('nlike' in clause && clause.nlike !== undefined) {
|
|
386
|
+
if (
|
|
387
|
+
typeof clause.nlike !== 'string' &&
|
|
388
|
+
!(clause.nlike instanceof RegExp)
|
|
389
|
+
) {
|
|
390
|
+
throw new InvalidOperatorValueError('nlike', 'a String', clause.nlike);
|
|
391
|
+
}
|
|
392
|
+
return !stringToRegexp(clause.nlike).test(value);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/**
|
|
397
|
+
* Test ilike operator.
|
|
398
|
+
*
|
|
399
|
+
* @example
|
|
400
|
+
* ```ts
|
|
401
|
+
* {
|
|
402
|
+
* ilike: 'foo',
|
|
403
|
+
* }
|
|
404
|
+
* ```
|
|
405
|
+
*
|
|
406
|
+
* @param {object} clause
|
|
407
|
+
* @param {*} value
|
|
408
|
+
* @returns {boolean|undefined}
|
|
409
|
+
*/
|
|
410
|
+
testIlike(clause, value) {
|
|
411
|
+
if (!clause || typeof clause !== 'object')
|
|
412
|
+
throw new InvalidArgumentError(
|
|
413
|
+
'A first argument of OperatorUtils.testIlike ' +
|
|
414
|
+
'should be an Object, but %v given.',
|
|
415
|
+
clause,
|
|
416
|
+
);
|
|
417
|
+
if ('ilike' in clause && clause.ilike !== undefined) {
|
|
418
|
+
if (
|
|
419
|
+
typeof clause.ilike !== 'string' &&
|
|
420
|
+
!(clause.ilike instanceof RegExp)
|
|
421
|
+
) {
|
|
422
|
+
throw new InvalidOperatorValueError('ilike', 'a String', clause.ilike);
|
|
423
|
+
}
|
|
424
|
+
return stringToRegexp(clause.ilike, 'i').test(value);
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/**
|
|
429
|
+
* Test nilike operator.
|
|
430
|
+
*
|
|
431
|
+
* @example
|
|
432
|
+
* ```ts
|
|
433
|
+
* {
|
|
434
|
+
* nilike: 'foo',
|
|
435
|
+
* }
|
|
436
|
+
* ```
|
|
437
|
+
*
|
|
438
|
+
* @param {object} clause
|
|
439
|
+
* @param {*} value
|
|
440
|
+
* @returns {boolean|undefined}
|
|
441
|
+
*/
|
|
442
|
+
testNilike(clause, value) {
|
|
443
|
+
if (!clause || typeof clause !== 'object')
|
|
444
|
+
throw new InvalidArgumentError(
|
|
445
|
+
'A first argument of OperatorUtils.testNilike ' +
|
|
446
|
+
'should be an Object, but %v given.',
|
|
447
|
+
clause,
|
|
448
|
+
);
|
|
449
|
+
if ('nilike' in clause && clause.nilike !== undefined) {
|
|
450
|
+
if (
|
|
451
|
+
typeof clause.nilike !== 'string' &&
|
|
452
|
+
!(clause.nilike instanceof RegExp)
|
|
453
|
+
) {
|
|
454
|
+
throw new InvalidOperatorValueError(
|
|
455
|
+
'nilike',
|
|
456
|
+
'a String',
|
|
457
|
+
clause.nilike,
|
|
458
|
+
);
|
|
459
|
+
}
|
|
460
|
+
return !stringToRegexp(clause.nilike, 'i').test(value);
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
/**
|
|
465
|
+
* Test regexp.
|
|
466
|
+
*
|
|
467
|
+
* @example
|
|
468
|
+
* ```ts
|
|
469
|
+
* {
|
|
470
|
+
* regexp: 'foo.*',
|
|
471
|
+
* }
|
|
472
|
+
* ```
|
|
473
|
+
*
|
|
474
|
+
* @example
|
|
475
|
+
* ```ts
|
|
476
|
+
* {
|
|
477
|
+
* regexp: 'foo.*',
|
|
478
|
+
* flags: 'i',
|
|
479
|
+
* }
|
|
480
|
+
* ```
|
|
481
|
+
*
|
|
482
|
+
* @param {object} clause
|
|
483
|
+
* @param {*} value
|
|
484
|
+
* @returns {boolean|undefined}
|
|
485
|
+
*/
|
|
486
|
+
testRegexp(clause, value) {
|
|
487
|
+
if (!clause || typeof clause !== 'object')
|
|
488
|
+
throw new InvalidArgumentError(
|
|
489
|
+
'A first argument of OperatorUtils.testRegexp ' +
|
|
490
|
+
'should be an Object, but %v given.',
|
|
491
|
+
clause,
|
|
492
|
+
);
|
|
493
|
+
if ('regexp' in clause && clause.regexp !== undefined) {
|
|
494
|
+
if (
|
|
495
|
+
typeof clause.regexp !== 'string' &&
|
|
496
|
+
!(clause.regexp instanceof RegExp)
|
|
497
|
+
) {
|
|
498
|
+
throw new InvalidOperatorValueError(
|
|
499
|
+
'regexp',
|
|
500
|
+
'a String',
|
|
501
|
+
clause.regexp,
|
|
502
|
+
);
|
|
503
|
+
}
|
|
504
|
+
const flags = clause.flags || undefined;
|
|
505
|
+
if (flags && typeof flags !== 'string')
|
|
506
|
+
throw new InvalidArgumentError(
|
|
507
|
+
'RegExp flags must be a String, but %v given.',
|
|
508
|
+
clause.flags,
|
|
509
|
+
);
|
|
510
|
+
if (!value || typeof value !== 'string') return false;
|
|
511
|
+
const regExp = stringToRegexp(clause.regexp, flags);
|
|
512
|
+
return !!value.match(regExp);
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
}
|