2022-08-23 15:14:52 +02:00
|
|
|
import {isString, type MAC} from "../types";
|
2020-04-08 21:25:33 +02:00
|
|
|
|
2022-07-22 17:10:39 +02:00
|
|
|
export function normalizeString(str: string): string {
|
2022-07-28 13:16:13 +02:00
|
|
|
return isString(str) ? str.trim().replace(/\s+/g, ' ') : str;
|
2020-04-08 21:25:33 +02:00
|
|
|
}
|
|
|
|
|
2022-07-22 17:10:39 +02:00
|
|
|
export function normalizeMac(mac: MAC): MAC {
|
2020-04-08 21:25:33 +02:00
|
|
|
// parts only contains values at odd indexes
|
2022-07-22 17:10:39 +02:00
|
|
|
const parts = mac.toUpperCase().replace(/[-:]/g, '').split(/([A-F0-9]{2})/);
|
2020-04-08 21:25:33 +02:00
|
|
|
|
|
|
|
const macParts = [];
|
|
|
|
|
|
|
|
for (let i = 1; i < parts.length; i += 2) {
|
|
|
|
macParts.push(parts[i]);
|
|
|
|
}
|
|
|
|
|
2022-07-22 17:10:39 +02:00
|
|
|
return macParts.join(':') as MAC;
|
2020-04-08 21:25:33 +02:00
|
|
|
}
|
|
|
|
|
2022-07-22 17:10:39 +02:00
|
|
|
export function parseInteger(str: string): number {
|
2022-08-04 18:40:38 +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
|
|
|
}
|