/** * some magic number */ const GOLDEN_RATIO_64 = 0xfd258f8f3210c68; /** initial value for the hash code */ const HASH_SEED_64 = 0x47da98c72c46f4a6; /** * Returns an initial value for the hash code * * @return the initial value */ function _longHash() { return HASH_SEED_64; } function _longCombine(oldHash: number, data: number): number { // tslint:disable-next-line:no-bitwise return oldHash ^ (data + GOLDEN_RATIO_64 + (oldHash << 6) + (oldHash >> 2)); } function _hashString(oldHash: number, s: string): number { let newHash = oldHash; let len = s.length; while (len-- > 0) { newHash = _longCombine(newHash, s.charCodeAt(len)); } return newHash; } // some character sets const _START = '$_'; const _DIGITS = '0123456789'; const _LOWERCASE = 'abcdefghijklmnopqrstuvwxyz'; const _UPPERCASE = _LOWERCASE.toUpperCase(); // valid first letters const _FIRST_LETTERS = _START + _LOWERCASE + _UPPERCASE; const _NEXT_LETTERS = _FIRST_LETTERS + _DIGITS; const _FIRST_LETTERS_LENGTH = _FIRST_LETTERS.length; const _NEXT_LETTERS_LENGTH = _NEXT_LETTERS.length; // shortcut to math functions const _abs = Math.abs; const _floor = Math.floor; /** * Converts a hash number into a javascript identifier * * @param aHash the hash number * @return a valid javascript identifier */ function _hashToIdentifier(aHash: number): string { // make sure the number is positive let num = _abs(_floor(aHash)); const res = [_FIRST_LETTERS[num % _FIRST_LETTERS_LENGTH]]; num = _floor(num / _FIRST_LETTERS_LENGTH); // next while (num > 0) { res.push(_NEXT_LETTERS[num % _NEXT_LETTERS_LENGTH]); num = _floor(num / _NEXT_LETTERS_LENGTH); } // ok return res.join(''); } export { _hashString as hashString, _longHash as longHash, _hashToIdentifier as hashToIdentifier };