2022-09-06 19:09:25 +02:00
|
|
|
/**
|
|
|
|
* Utility functions all around strings.
|
|
|
|
*/
|
2022-09-14 16:33:43 +02:00
|
|
|
import { isInteger, type MAC } from "../types";
|
2020-04-08 21:25:33 +02:00
|
|
|
|
2022-09-06 19:09:25 +02:00
|
|
|
/**
|
|
|
|
* Trims the given `string` and replaces multiple whitespaces by one space each.
|
|
|
|
*
|
|
|
|
* Can be used to make sure user input has a canonical form.
|
|
|
|
*
|
|
|
|
* @param str - `string` to normalize.
|
|
|
|
* @returns The normalized `string`.
|
|
|
|
*/
|
2022-07-22 17:10:39 +02:00
|
|
|
export function normalizeString(str: string): string {
|
2022-09-06 19:09:25 +02:00
|
|
|
return str.trim().replace(/\s+/g, " ");
|
2020-04-08 21:25:33 +02:00
|
|
|
}
|
|
|
|
|
2022-09-06 19:09:25 +02:00
|
|
|
/**
|
|
|
|
* Normalizes a {@link MAC} address so that it has a canonical format:
|
|
|
|
*
|
|
|
|
* The `MAC` address will be converted so that it is all uppercase with colon as the delimiter, e.g.:
|
|
|
|
* `12:34:56:78:9A:BC`.
|
|
|
|
*
|
|
|
|
* @param mac - `MAC` address to normalize.
|
|
|
|
* @returns The normalized `MAC` address.
|
|
|
|
*/
|
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-08-23 20:08:53 +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-08-23 20:08:53 +02:00
|
|
|
return macParts.join(":") as MAC;
|
2020-04-08 21:25:33 +02:00
|
|
|
}
|
|
|
|
|
2022-09-06 19:09:25 +02:00
|
|
|
/**
|
|
|
|
* Parses the given `string` and converts it into an integer.
|
|
|
|
*
|
|
|
|
* For a `string` to be considered a valid representation of an integer `number` it has to satisfy the
|
|
|
|
* following criteria:
|
|
|
|
*
|
|
|
|
* * The integer is base `10`.
|
|
|
|
* * The `string` starts with an optional `+` or `-` sign followed by one or more digits.
|
|
|
|
* * The first digit must not be `0`.
|
|
|
|
* * The `string` does not contain any other characters.
|
|
|
|
*
|
|
|
|
* @param str - `string` to parse.
|
|
|
|
* @returns The parsed integer `number`.
|
|
|
|
* @throws {@link SyntaxError} - If the given `string` does not represent a valid integer.
|
|
|
|
*/
|
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-09-06 19:09:25 +02:00
|
|
|
const original = str.startsWith("+") ? str.slice(1) : str;
|
|
|
|
|
|
|
|
if (isInteger(parsed) && parsed.toString() === original) {
|
2022-07-18 17:49:42 +02:00
|
|
|
return parsed;
|
|
|
|
} else {
|
2022-08-23 20:08:53 +02:00
|
|
|
throw new SyntaxError(
|
|
|
|
`String does not represent a valid integer: "${str}"`
|
|
|
|
);
|
2022-07-18 17:49:42 +02:00
|
|
|
}
|
2020-04-08 21:25:33 +02:00
|
|
|
}
|