Last active
August 29, 2015 14:01
-
-
Save jmillerdesign/43e17b40d1e439012dc5 to your computer and use it in GitHub Desktop.
Convert any string into an integer
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
| /** | |
| * Convert any string into an integer | |
| * | |
| * @param {string} str Input string | |
| * @return {integer} Integer | |
| */ | |
| var strToInt = (function () { | |
| var map = {}; | |
| var size = 0; | |
| var hash; | |
| return function (str) { | |
| hash = crypto.createHash('md5').update(str).digest('hex'); | |
| if (!map.hasOwnProperty(hash)) { | |
| // Add new hash to the map | |
| map[hash] = ++size; | |
| } | |
| return map[hash]; | |
| }; | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment