signal-desktop/ts/util/parseIntOrThrow.ts

25 lines
469 B
TypeScript
Raw Normal View History

2021-03-03 20:09:58 +00:00
// 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;
}