-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
43 lines (39 loc) · 976 Bytes
/
index.js
File metadata and controls
43 lines (39 loc) · 976 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
* - index.js
* @author luckydrq(drqzju@gmail.com)
*/
'use strict';
var assert = require('assert');
var util = require('core-util-is');
/**
* return a hash code of this string
* @ref http://blog.csdn.net/exceptional_derek/article/details/9074137
* @param {String} str
* @return {Number}
* @api public
*/
module.exports = function hashCode(str) {
assert(util.isString(str), 'string.hashCode require String');
if (util.isFunction(str.hashCode)) {
return str.hashCode();
}
str = new String(str);
str.value = str.split('').map(function(c) { return c.charCodeAt(0); });
str.offset = 0;
str.hash = 0;
str.hashCode = function() {
var count = this.length;
var h = this.hash;
if (h === 0 && count > 0) {
var off = this.offset;
var val = this.value;
var len = count;
for (var i = 0; i < len; i++) {
h = 31 * h + val[off++];
}
this.hash = h;
}
return h;
};
return str.hashCode();
};