import rept from "./rept";
import { ERRORTYPES as error } from "./error";
import isNaN from "./isnan";
Copyright 2015-2021 JC Fisher
import rept from "./rept";
import { ERRORTYPES as error } from "./error";
import isNaN from "./isnan";
export default function dec2bin(input, places) {
exit if input is an error
if (input instanceof Error) {
return number;
}
cast input to number
var number = parseInt(input);
if (!/^-?[0-9]{1,3}$/.test(number) || isNaN(number)) {
return error.value;
}
Return error.if number is not decimal, is lower than -512, or is greater than 511
if (number < -512 || number > 511) {
return error.num;
}
Ignore places and return a 10-character binary number if number is negative
if (number < 0) {
return (
"1" +
rept("0", 9 - (512 + number).toString(2).length) +
(512 + number).toString(2)
);
}
Convert decimal number to binary
var result = parseInt(number, 10).toString(2);
Return binary number using the minimum number of characters necessary if places is undefined
if (typeof places === "undefined") {
return result;
} else {
Return error.if places is nonnumeric
if (!/^-?[0-9]{1,3}$/.test(places) || isNaN(places)) {
return error.value;
}
Return error.if places is negative
if (places < 0) {
return error.num;
}
Truncate places in case it is not an integer
places = Math.floor(places);
Pad return value with leading 0s (zeros) if necessary (using Underscore.string)
return places >= result.length
? rept("0", places - result.length) + result
: error.num;
}
}