Skip to content

Commit e1405dd

Browse files
committed
v42 - added string hash code
1 parent afea60e commit e1405dd

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

Diff for: js/string.js

+21
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,24 @@ String.prototype.hasPrefix = function (prefix) {
5656
const hasPrefix = this.indexOf(prefix) === 0;
5757
return hasPrefix;
5858
};
59+
60+
/**
61+
* @function hashCode
62+
* @memberof String
63+
* @description Return a hash code for a string.
64+
* @returns number
65+
*/
66+
String.prototype.hashCode = function () {
67+
let hash = 0;
68+
let i;
69+
let chr;
70+
if (this.length === 0) return hash;
71+
for (i = 0; i < this.length; i += 1) {
72+
chr = this.charCodeAt(i);
73+
// eslint-disable-next-line no-bitwise
74+
hash = ((hash << 5) - hash) + chr;
75+
// eslint-disable-next-line no-bitwise
76+
hash |= 0; // Convert to 32bit integer
77+
}
78+
return Math.abs(hash);
79+
};

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@djthorpe/js-framework",
3-
"version": "0.0.41",
3+
"version": "0.0.42",
44
"description": "Javascript Framework",
55
"main": "dist/index.js",
66
"scripts": {

0 commit comments

Comments
 (0)