monad-ts
Version:
Monad-ts is a small library implements some of key monads and way to chain them in a pipe (flow) in JavaScript and TypeScript. Angular 2+ compatible.
24 lines (21 loc) • 763 B
text/typescript
/**
* Calculates a hash (32 bit).
* Based on FNV-1a algorithm, ref: http://isthe.com/chongo/tech/comp/fnv/
* @method hash
* @param {string} str - string to hash
* @param {number} [pHash = 2166136261] previous hash.
* @returns {number}
*/
export function hash( str: string, pHash: number = 2166136261): number {
if(str.length === 0 || !str) return null;
let n = str.length-1;
let hash = pHash;
while(n >= 0){
// xor the bottom with the current chunk.
hash ^= str.charCodeAt(n--);
// multiply with a expression of shifts.
hash += (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
}
return hash;
}
//Copyright (c) 2017 Alex Tranchenko. All rights reserved.