Repository navigation
Expand file tree
/
Copy pathoperator.ts
More file actions
306 lines (281 loc) · 8.6 KB
/
Copy pathoperator.ts
File metadata and controls
306 lines (281 loc) · 8.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
type OperatorValuesSingle = string | number | boolean;
export type OperatorValuesList = string[] | number[] | boolean[] | any[];
export type OperatorValues = OperatorValuesSingle | OperatorValuesList;
export enum Condition {
Equal = 'equal',
NotEqual = 'notEqual',
GreaterThan = 'greaterThan',
GreaterThanEqual = 'greaterThanEqual',
LessThan = 'lessThan',
LessThanEqual = 'lessThanEqual',
Contains = 'contains',
IsNull = 'isNull',
IsNotNull = 'isNotNull',
}
/**
* Helper class to generate operator strings for atomic operations.
*/
export class Operator {
method: string;
values: OperatorValuesList | undefined;
/**
* Constructor for Operator class.
*
* @param {string} method
* @param {OperatorValues} values
*/
constructor(method: string, values?: OperatorValues) {
this.method = method;
if (values !== undefined) {
if (Array.isArray(values)) {
this.values = values;
} else {
this.values = [values] as OperatorValuesList;
}
}
}
/**
* Convert the operator object to a JSON string.
*
* @returns {string}
*/
toString(): string {
return JSON.stringify({
method: this.method,
values: this.values,
});
}
/**
* Increment a numeric attribute by a specified value.
*
* @param {number} value
* @param {number} max
* @returns {string}
*/
static increment = (value: number = 1, max?: number): string => {
if (isNaN(value) || !isFinite(value)) {
throw new Error('Value cannot be NaN or Infinity');
}
if (max !== undefined && (isNaN(max) || !isFinite(max))) {
throw new Error('Max cannot be NaN or Infinity');
}
const values: any[] = [value];
if (max !== undefined) {
values.push(max);
}
return new Operator('increment', values).toString();
};
/**
* Decrement a numeric attribute by a specified value.
*
* @param {number} value
* @param {number} min
* @returns {string}
*/
static decrement = (value: number = 1, min?: number): string => {
if (isNaN(value) || !isFinite(value)) {
throw new Error('Value cannot be NaN or Infinity');
}
if (min !== undefined && (isNaN(min) || !isFinite(min))) {
throw new Error('Min cannot be NaN or Infinity');
}
const values: any[] = [value];
if (min !== undefined) {
values.push(min);
}
return new Operator('decrement', values).toString();
};
/**
* Multiply a numeric attribute by a specified factor.
*
* @param {number} factor
* @param {number} max
* @returns {string}
*/
static multiply = (factor: number, max?: number): string => {
if (isNaN(factor) || !isFinite(factor)) {
throw new Error('Factor cannot be NaN or Infinity');
}
if (max !== undefined && (isNaN(max) || !isFinite(max))) {
throw new Error('Max cannot be NaN or Infinity');
}
const values: any[] = [factor];
if (max !== undefined) {
values.push(max);
}
return new Operator('multiply', values).toString();
};
/**
* Divide a numeric attribute by a specified divisor.
*
* @param {number} divisor
* @param {number} min
* @returns {string}
*/
static divide = (divisor: number, min?: number): string => {
if (isNaN(divisor) || !isFinite(divisor)) {
throw new Error('Divisor cannot be NaN or Infinity');
}
if (min !== undefined && (isNaN(min) || !isFinite(min))) {
throw new Error('Min cannot be NaN or Infinity');
}
if (divisor === 0) {
throw new Error('Divisor cannot be zero');
}
const values: any[] = [divisor];
if (min !== undefined) {
values.push(min);
}
return new Operator('divide', values).toString();
};
/**
* Apply modulo operation on a numeric attribute.
*
* @param {number} divisor
* @returns {string}
*/
static modulo = (divisor: number): string => {
if (isNaN(divisor) || !isFinite(divisor)) {
throw new Error('Divisor cannot be NaN or Infinity');
}
if (divisor === 0) {
throw new Error('Divisor cannot be zero');
}
return new Operator('modulo', [divisor]).toString();
};
/**
* Raise a numeric attribute to a specified power.
*
* @param {number} exponent
* @param {number} max
* @returns {string}
*/
static power = (exponent: number, max?: number): string => {
if (isNaN(exponent) || !isFinite(exponent)) {
throw new Error('Exponent cannot be NaN or Infinity');
}
if (max !== undefined && (isNaN(max) || !isFinite(max))) {
throw new Error('Max cannot be NaN or Infinity');
}
const values: any[] = [exponent];
if (max !== undefined) {
values.push(max);
}
return new Operator('power', values).toString();
};
/**
* Append values to an array attribute.
*
* @param {any[]} values
* @returns {string}
*/
static arrayAppend = (values: any[]): string =>
new Operator('arrayAppend', values).toString();
/**
* Prepend values to an array attribute.
*
* @param {any[]} values
* @returns {string}
*/
static arrayPrepend = (values: any[]): string =>
new Operator('arrayPrepend', values).toString();
/**
* Insert a value at a specific index in an array attribute.
*
* @param {number} index
* @param {any} value
* @returns {string}
*/
static arrayInsert = (index: number, value: any): string =>
new Operator('arrayInsert', [index, value]).toString();
/**
* Remove a value from an array attribute.
*
* @param {any} value
* @returns {string}
*/
static arrayRemove = (value: any): string =>
new Operator('arrayRemove', [value]).toString();
/**
* Remove duplicate values from an array attribute.
*
* @returns {string}
*/
static arrayUnique = (): string =>
new Operator('arrayUnique', []).toString();
/**
* Keep only values that exist in both the current array and the provided array.
*
* @param {any[]} values
* @returns {string}
*/
static arrayIntersect = (values: any[]): string =>
new Operator('arrayIntersect', values).toString();
/**
* Remove values from the array that exist in the provided array.
*
* @param {any[]} values
* @returns {string}
*/
static arrayDiff = (values: any[]): string =>
new Operator('arrayDiff', values).toString();
/**
* Filter array values based on a condition.
*
* @param {Condition} condition
* @param {any} value
* @returns {string}
*/
static arrayFilter = (condition: Condition, value?: any): string => {
const values: any[] = [
condition as string,
value === undefined ? null : value,
];
return new Operator('arrayFilter', values).toString();
};
/**
* Concatenate a value to a string or array attribute.
*
* @param {any} value
* @returns {string}
*/
static stringConcat = (value: any): string =>
new Operator('stringConcat', [value]).toString();
/**
* Replace occurrences of a search string with a replacement string.
*
* @param {string} search
* @param {string} replace
* @returns {string}
*/
static stringReplace = (search: string, replace: string): string =>
new Operator('stringReplace', [search, replace]).toString();
/**
* Toggle a boolean attribute.
*
* @returns {string}
*/
static toggle = (): string => new Operator('toggle', []).toString();
/**
* Add days to a date attribute.
*
* @param {number} days
* @returns {string}
*/
static dateAddDays = (days: number): string =>
new Operator('dateAddDays', [days]).toString();
/**
* Subtract days from a date attribute.
*
* @param {number} days
* @returns {string}
*/
static dateSubDays = (days: number): string =>
new Operator('dateSubDays', [days]).toString();
/**
* Set a date attribute to the current date and time.
*
* @returns {string}
*/
static dateSetNow = (): string => new Operator('dateSetNow', []).toString();
}