2020-04-08 21:25:33 +02:00
|
|
|
import _ from "lodash"
|
|
|
|
|
|
|
|
export function normalizeString (str: string): string {
|
|
|
|
return _.isString(str) ? str.trim().replace(/\s+/g, ' ') : str;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function normalizeMac (mac: string): string {
|
|
|
|
// parts only contains values at odd indexes
|
|
|
|
const parts = mac.toUpperCase().replace(/:/g, '').split(/([A-F0-9]{2})/);
|
|
|
|
|
|
|
|
const macParts = [];
|
|
|
|
|
|
|
|
for (let i = 1; i < parts.length; i += 2) {
|
|
|
|
macParts.push(parts[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return macParts.join(':');
|
|
|
|
}
|
|
|
|
|
2022-07-18 17:49:42 +02:00
|
|
|
export function parseInteger (str: string): number {
|
2020-04-08 21:25:33 +02:00
|
|
|
const parsed = _.parseInt(str, 10);
|
2022-07-18 17:49:42 +02:00
|
|
|
if (parsed.toString() === str) {
|
|
|
|
return parsed;
|
|
|
|
} else {
|
|
|
|
throw new SyntaxError(`String does not represent a valid integer: "${str}"`);
|
|
|
|
}
|
2020-04-08 21:25:33 +02:00
|
|
|
}
|