forked from processing-js/processing-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvirtHashCode.js
More file actions
executable file
·27 lines (27 loc) · 862 Bytes
/
Copy pathvirtHashCode.js
File metadata and controls
executable file
·27 lines (27 loc) · 862 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/**
* Returns Java hashCode() result for the object. If the object has the "hashCode" function,
* it preforms the call of this function. Otherwise it uses/creates the "$id" property,
* which is used as the hashCode.
*
* @param {Object} obj The object.
* @returns {int} The object's hash code.
*/
module.exports = function virtHashCode(obj, undef) {
if (typeof(obj) === "string") {
var hash = 0;
for (var i = 0; i < obj.length; ++i) {
hash = (hash * 31 + obj.charCodeAt(i)) & 0xFFFFFFFF;
}
return hash;
}
if (typeof(obj) !== "object") {
return obj & 0xFFFFFFFF;
}
if (obj.hashCode instanceof Function) {
return obj.hashCode();
}
if (obj.$id === undef) {
obj.$id = ((Math.floor(Math.random() * 0x10000) - 0x8000) << 16) | Math.floor(Math.random() * 0x10000);
}
return obj.$id;
};