All files / app/util/hash-string hash-string.ts

100% Statements 13/13
100% Branches 0/0
100% Functions 2/2
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24            1x 184x 184x 184x 811x 811x 811x   184x   184x   184x       1x  
/**
 * Generates a hash for the given string with the given seed.
 * @param str  - The string for which to generate the hash
 * @param seed - A seed which can change the output
 *  even if the string is the same
 */
const hashString = (str: string, seed: number): Promise<number> => {
  return new Promise((resolve) => {
    let h1 = 0xdeadbeef ^ seed; let h2 = 0x41c6ce57 ^ seed;
    for (let i = 0, ch; i < str.length; i++) {
      ch = str.charCodeAt(i);
      h1 = Math.imul(h1 ^ ch, 2654435761);
      h2 = Math.imul(h2 ^ ch, 1597334677);
    }
    h1 = Math.imul(h1 ^ h1>>>16, 2246822507) ^
      Math.imul(h2 ^ h2>>>13, 3266489909);
    h2 = Math.imul(h2 ^ h2>>>16, 2246822507) ^
      Math.imul(h1 ^ h1>>>13, 3266489909);
    resolve(4294967296 * (2097151 & h2) + (h1>>>0));
  });
};
 
export default hashString;