-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
31 lines (26 loc) · 853 Bytes
/
index.js
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
28
29
30
Object.defineProperty(Object.prototype, 'toItem', {
value: function (map) {
function toInt(key) {
key = toString(key);
let h = 0, i = key.length;
while (i > 0) {
h = (h << 5) - h + key.charCodeAt(--i) | 0;
}
return Math.abs(h)
}
function toString(obj) {
let str = '';
for (let property in obj) {
if (obj.hasOwnProperty(property)) {
str += `${property}: ${obj[property]}`;
}
}
return str || str.toString();
}
if (typeof map === "undefined") return this;
let int = toInt(toString(this));
let nodes = Array.from(map).sort((a, b) => a - b);
return nodes[int % nodes.length];
},
enumerable: false
});