-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFieldProcessor.js
More file actions
230 lines (208 loc) · 7.87 KB
/
Copy pathFieldProcessor.js
File metadata and controls
230 lines (208 loc) · 7.87 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
import { parseDateTimeInput } from './date-input-parser.js';
import { parseDuration } from './duration-parser.js';
import { normalizeCategory, DEFAULT_CATEGORY } from './category-utils.js';
import { parseSpeedInput } from './speed-utils.js';
import { parseRouteEntries } from './route-links.js';
import { config } from '../config.js';
import { t } from '../i18n/index.js';
/**
* Utility class for processing ride field parameters
* Centralizes field processing logic to eliminate duplication between create and update operations
*/
export class FieldProcessor {
/**
* Process ride fields from parameters
* @param {Object} params - Input parameters
* @param {boolean} isUpdate - Whether this is an update operation (affects how '-' is handled)
* @param {{language?: string}} options - Localization options
* @returns {Object} - { data, error }
*/
static processRideFields(params, isUpdate = false, options = {}) {
const language = options.language;
const result = { data: {}, error: null };
// Process date
if (params.when) {
const dateResult = parseDateTimeInput(params.when, { language });
if (!dateResult.date) {
return { data: null, error: dateResult.error };
}
result.data.date = dateResult.date;
}
// Process distance
if (params.dist !== undefined) {
result.data.distance = this.processNumericField(params.dist, isUpdate);
}
// Process duration
if (params.duration !== undefined) {
const durationResult = this.processDurationField(params.duration, isUpdate, { language });
if (durationResult.error) {
return { data: null, error: durationResult.error };
}
result.data.duration = durationResult.value;
}
for (const [paramName, prefix] of [['speed', 'speed'], ['cruisingSpeed', 'cruisingSpeed']]) {
if (params[paramName] === undefined) continue;
const speedResult = this.processSpeedField(params[paramName], isUpdate, prefix);
if (speedResult === null) {
return { data: null, error: this.translateSpeedError(language, paramName) };
}
Object.assign(result.data, speedResult);
}
// Process route
const routeInput = params.routes !== undefined ? params.routes : params.route;
if (routeInput !== undefined) {
const routeValues = Array.isArray(routeInput) ? routeInput : [routeInput];
if (isUpdate && routeValues.length === 1 && routeValues[0] === '-') {
// Clear route for updates
result.data.routes = [];
result.data.routeLink = '';
} else {
const parsedRoutes = parseRouteEntries(routeValues, { validateUrl: false });
if (parsedRoutes.error) {
return { data: null, error: this.translateRouteError(language) };
}
result.data.routes = parsedRoutes.routes;
if (parsedRoutes.routes.length === 0) {
result.data.routeLink = '';
}
result.data._requiresRouteProcessing = parsedRoutes.routes.length > 0;
}
}
// Process simple text fields
this.processTextFields(params, result.data, isUpdate);
this.processBooleanSettings(params, result.data);
return result;
}
/**
* Process supported boolean ride settings from dotted and structured inputs.
*
* @param {Object} params
* @param {Object} data
*/
static processBooleanSettings(params, data) {
['notifyParticipation', 'allowReposts'].forEach(settingName => {
const value = params.settings?.[settingName] ?? params[`settings.${settingName}`];
if (value !== undefined) {
data.settings = {
...(data.settings || {}),
[settingName]: this.parseBooleanSetting(value)
};
}
});
}
/**
* Parse boolean-like setting inputs from text or structured values.
*
* @param {string|boolean|number} value
* @returns {boolean}
*/
static parseBooleanSetting(value) {
if (typeof value === 'boolean') {
return value;
}
const normalized = String(value).toLowerCase().trim();
return normalized === 'yes' || normalized === 'true' || normalized === '1';
}
/**
* Process numeric field (distance)
* @param {string} value - Field value
* @param {boolean} isUpdate - Whether this is an update operation
* @returns {number|null} - Parsed value or null if cleared
*/
static processNumericField(value, isUpdate) {
if (isUpdate && value === '-') return null;
return parseFloat(value);
}
/**
* Process duration field
* @param {string} value - Field value
* @param {boolean} isUpdate - Whether this is an update operation
* @param {{language?: string}} options - Localization options
* @returns {Object} - { value, error }
*/
static processDurationField(value, isUpdate, options = {}) {
if (isUpdate && value === '-') {
return { value: null, error: null };
}
const result = parseDuration(value, { language: options.language });
return { value: result.duration, error: result.error };
}
/**
* Process speed field supporting 4 input forms:
* "25-28" → range (speedMin=25, speedMax=28)
* "25+" or "25-"→ minimum (speedMin=25, speedMax=null)
* "-28" → maximum (speedMin=null, speedMax=28)
* "25" or "~25" → average (speedMin=25, speedMax=25)
*
* @param {string} value - Field value
* @param {boolean} isUpdate - Whether this is an update operation
* @returns {Object} - Object with speedMin and/or speedMax properties
*/
static processSpeedField(value, isUpdate, prefix = 'speed') {
const minKey = `${prefix}Min`;
const maxKey = `${prefix}Max`;
if (isUpdate && value === '-') {
return { [minKey]: null, [maxKey]: null };
}
const parsed = parseSpeedInput(value);
if (!parsed) return null;
const result = {};
if ('speedMin' in parsed) result[minKey] = parsed.speedMin;
if ('speedMax' in parsed) result[maxKey] = parsed.speedMax;
// On update, explicitly null out whichever bound was not specified,
// so switching forms (e.g. range → average) clears the old value.
if (isUpdate) {
if (!(minKey in result)) result[minKey] = null;
if (!(maxKey in result)) result[maxKey] = null;
}
return result;
}
/**
* Process simple text fields
* @param {Object} params - Input parameters
* @param {Object} data - Data object to populate
* @param {boolean} isUpdate - Whether this is an update operation
*/
static processTextFields(params, data, isUpdate) {
const textFields = ['title', 'meet', 'info', 'organizer', 'category'];
textFields.forEach(field => {
if (params[field] !== undefined) {
if (isUpdate && params[field] === '-') {
// Clear field value for updates
if (field === 'category') {
data[field] = DEFAULT_CATEGORY;
} else if (field === 'meet') {
data.meetingPoint = '';
} else if (field === 'info') {
data.additionalInfo = '';
} else {
data[field] = '';
}
} else {
// Set field value
if (field === 'category') {
data[field] = normalizeCategory(params[field]);
} else if (field === 'meet') {
data.meetingPoint = params[field];
} else if (field === 'info') {
data.additionalInfo = params[field];
} else {
data[field] = params[field];
}
}
}
});
}
static translateRouteError(language) {
return t(language || config.i18n.defaultLanguage, 'utils.routeParser.invalidUrl', {}, {
fallbackLanguage: config.i18n.fallbackLanguage,
withMissingMarker: config.isDev
});
}
static translateSpeedError(language, paramName) {
return t(language || config.i18n.defaultLanguage, `params.validation.${paramName}Invalid`, {}, {
fallbackLanguage: config.i18n.fallbackLanguage,
withMissingMarker: config.isDev
});
}
}