Skip to content

Instantly share code, notes, and snippets.

@vsDizzy
Last active October 28, 2022 19:17
Show Gist options
  • Save vsDizzy/713993f1ea5d26cee594a261053ce6b6 to your computer and use it in GitHub Desktop.
Save vsDizzy/713993f1ea5d26cee594a261053ce6b6 to your computer and use it in GitHub Desktop.
export function hash(s: string) {
return [].reduce.call(s, (p, c) => (p << 5) - p + c.charCodeAt(0), 0)
}
@DarrenSem
Copy link

DarrenSem commented Sep 23, 2022

// so... in other words the following
export hash = s => [].reduce.call(s, (p, c) => (p << 5) - p + c.charCodeAt(0), 0);
String.prototype.hash = function() { return [].reduce.call(this, (acc, el) => (acc << 5) - acc + el.charCodeAt(0), 0); };
AKA String.prototype.hash = function() { return Array.from(this).reduce((acc, el) => (acc << 5) - acc + el.charCodeAt(0), 0); };
AKA String.prototype.hash = function() { return Array.from(this, c => c.charCodeAt(0)).reduce((acc, el) => (acc << 5) - acc + el, 0); };
AKA String.prototype.hash = function() { return [...this].reduce((n, c) => (n << 5) - n + c.charCodeAt(0), 0); };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment