Last active
March 11, 2021 02:21
-
-
Save nuintun/8b666793747100f1de1e8898982ce87e to your computer and use it in GitHub Desktop.
Java 中字符串 hashCode 方法的 JavaScript 实现
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
/** | |
* @function hashCode | |
* @param {string} string | |
* @returns {number} | |
*/ | |
function hashCode(string: string): number { | |
let hash: number = 0; | |
const { length }: string = string; | |
for (let i: number = 0; i < length; i++) { | |
const code: number = string.codePointAt(i) as number; | |
hash = (hash << 5) - hash + code; | |
hash |= 0; // Convert to 32bit integer | |
} | |
return hash; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment