-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRideParamsHelper.js
More file actions
78 lines (70 loc) · 2.43 KB
/
Copy pathRideParamsHelper.js
File metadata and controls
78 lines (70 loc) · 2.43 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
import { config } from '../config.js';
import { t } from '../i18n/index.js';
export class RideParamsHelper {
/**
* Valid ride parameters and their descriptions
* @type {Object.<string, string>}
*/
static VALID_PARAMS = RideParamsHelper.getValidParams();
static getValidParams(language = config.i18n.defaultLanguage) {
const translate = (key) => t(language, key, {}, {
fallbackLanguage: config.i18n.fallbackLanguage,
withMissingMarker: config.isDev
});
return {
title: translate('params.title'),
category: translate('params.category'),
organizer: translate('params.organizer'),
when: translate('params.when'),
meet: translate('params.meet'),
route: translate('params.route'),
dist: translate('params.dist'),
duration: translate('params.duration'),
speed: translate('params.speed'),
cruisingSpeed: translate('params.cruisingSpeed'),
info: translate('params.info'),
'settings.notifyParticipation': translate('params.settingsNotifyParticipation'),
'settings.allowReposts': translate('params.settingsAllowReposts'),
id: translate('params.id')
};
}
static normalizeParamKey(rawKey) {
const normalizedKey = rawKey.trim().toLowerCase();
return Object.keys(RideParamsHelper.VALID_PARAMS).find(
key => key.toLowerCase() === normalizedKey
) || null;
}
/**
* Parse ride parameters from text
* @param {string} text - Text to parse
* @returns {{params: Object, unknownParams: Array<string>}} - Parsed parameters and any unknown parameters
*/
static parseRideParams(text) {
const lines = text.split('\n').slice(1); // Skip command line
const params = {};
const unknownParams = [];
for (const line of lines) {
const match = line.match(/^\s*([\w.]+)\s*:\s*(.+)$/);
if (match) {
const [_, key, value] = match;
const canonicalKey = RideParamsHelper.normalizeParamKey(key);
if (canonicalKey) {
const trimmedValue = value.trim();
if (canonicalKey === 'route') {
if (!params.route) {
params.route = [];
}
params.route.push(trimmedValue);
} else {
params[canonicalKey] = trimmedValue;
}
} else {
unknownParams.push(key.trim());
}
} else {
unknownParams.push(line.trim());
}
}
return { params, unknownParams };
}
}