signal-desktop/ts/util/parseIntOrThrow.ts
2021-03-19 16:53:52 -04:00

24 lines
469 B
TypeScript

// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
export function parseIntOrThrow(value: unknown, message: string): number {
let result: number;
switch (typeof value) {
case 'number':
result = value;
break;
case 'string':
result = parseInt(value, 10);
break;
default:
result = NaN;
break;
}
if (!Number.isInteger(result)) {
throw new Error(message);
}
return result;
}