Last active
August 20, 2018 14:51
-
-
Save greyscaled/114df2339b896fa07885f4dbf0d1f055 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* A set of static methods governing string-to-int conversion | |
* in base26. In base26, only lowercase alpha chars [a-z] | |
* are valid and map to an integer range (0, 26] | |
*/ | |
module.exports = { | |
/** | |
* returns the base26 integer corresponding with the provided char. Note | |
* that by design uppercase chars may be supplied, and are | |
* treated as lowercase. If a string containing more than one char is | |
* supplied, only the first char (0 index) is considered. | |
* | |
* @param {string} char | |
* @returns {number} (0, 26] | |
*/ | |
getBase26Digit: (char) => { | |
let normalized = char.toLowerCase().charCodeAt(0) - 97 | |
if (normalized <= 25 && normalized >= 0) { return normalized } | |
else { throw new Error('not base 26') } | |
}, | |
/** | |
* returns a lowercase alpha char [a-z] corresponding with the provided integer. | |
* If the provided integer is outside the range of (0, 26], an error will be thrown. | |
* | |
* @param {number} digit - a number in the range (0, 26] | |
* @throws will throw an error if the digit param is outside (0, 26] | |
* @returns {string} [a-z] | |
*/ | |
getUTF16: (digit) => { | |
if (digit < 0 || digit > 25) { throw new Error('not base 26') } | |
return String.fromCharCode(digit + 97) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment