20 lines
463 B
TypeScript
20 lines
463 B
TypeScript
import {Converter} from './converter';
|
|
|
|
export class BinToDecConverter implements Converter {
|
|
getDisplayname(): string {
|
|
return 'Convert binary to decimal';
|
|
}
|
|
|
|
getId(): string {
|
|
return 'bintodec';
|
|
}
|
|
|
|
convert(input: string): string {
|
|
const n: number = parseInt(input, 2);
|
|
if (isNaN(n) || !input.trim().match(/^([01]+)$/)) {
|
|
throw new Error('The input seems not to be a valid binary number.');
|
|
}
|
|
return n.toString(10);
|
|
}
|
|
}
|