Created
March 5, 2015 23:31
-
-
Save pv8/647c1f3f1d48e3b45555 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
/** | |
* Pad the left-side of this string with a 'padStr' | |
* | |
* @param padStr: string that will be padded | |
* @param length: number of characters to return | |
* @returns {String} | |
*/ | |
String.prototype.lpad = function(padStr, length) { | |
var str = this; | |
while (str.length < length) | |
str = padStr + str; | |
return str; | |
} | |
/** | |
* Pad the right-side of this string with a 'padStr' | |
* | |
* @param padStr: string that will be padded | |
* @param length: number of characters to return | |
* @returns {String} | |
*/ | |
String.prototype.rpad = function(padStr, length) { | |
var str = this; | |
while (str.length < length) | |
str = str + padStr; | |
return str; | |
} | |
/** | |
* Convert this number to a binary string with a | |
* | |
* @param minLength: minimum length for the binary string (default 8) | |
* @returns {String} | |
*/ | |
Number.prototype.toBinary = function(minLength) { | |
minLength = minLength == null? 8: minLength; // default | |
return parseInt(this).toString(2).lpad("0", minLength); | |
}; | |
/** | |
* Convert this number to binary array | |
* | |
* @param minLength: minimum length for the binary array (default 8) | |
* @returns {Array[int]} | |
*/ | |
Number.prototype.toBinaryArray = function(minLength) { | |
return this.toBinary(minLength).split("").map(function(str){ | |
return parseInt(str); | |
}); | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment