├── .gitignore ├── README.md ├── bower.json ├── build ├── hash-string.js └── hash-string.min.js ├── example └── example.html ├── gulpfile.js ├── package.json └── source └── hash-string.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | bower_components 3 | npm-debug.log -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Hash String 2 | 3 | [![NPM](https://nodei.co/npm/hash-string.png)](https://nodei.co/npm/hash-string/) 4 | 5 | A string hashing function based on Daniel J. Bernstein's popular 'times 33' hash algorithm. 6 | 7 | ## Example 8 | 9 | ```javascript 10 | console.log(hash('{ test: true }')); 11 | ``` -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hash-string", 3 | "version": "1.0.0", 4 | "authors": [ 5 | "MatthewBarker " 6 | ], 7 | "description": "A string hashing function based on Daniel J. Bernstein's popular 'times 33' hash algorithm.", 8 | "main": "build/hash-string.js", 9 | "moduleType": [ 10 | "amd", 11 | "globals", 12 | "node" 13 | ], 14 | "keywords": [ 15 | "hash", 16 | "string", 17 | "algorithm" 18 | ], 19 | "license": "ISC", 20 | "homepage": "https://github.com/MatthewBarker/hash-string", 21 | "ignore": [ 22 | "**/.*", 23 | "node_modules", 24 | "bower_components", 25 | "example", 26 | "source", 27 | "gulpfile.js" 28 | ] 29 | } 30 | -------------------------------------------------------------------------------- /build/hash-string.js: -------------------------------------------------------------------------------- 1 | (function(root, factory) { 2 | if (typeof define === 'function' && define.amd) { 3 | define([], factory); 4 | } else if (typeof exports === 'object') { 5 | module.exports = factory(); 6 | } else { 7 | root.hash = factory(); 8 | } 9 | }(this, function() { 10 | /** 11 | A string hashing function based on Daniel J. Bernstein's popular 'times 33' hash algorithm. 12 | @param {string} text - String to hash 13 | @return {number} Resulting number. 14 | */ 15 | function hash(text) { 16 | 'use strict'; 17 | 18 | var hash = 5381, 19 | index = text.length; 20 | 21 | while (index) { 22 | hash = (hash * 33) ^ text.charCodeAt(--index); 23 | } 24 | 25 | return hash >>> 0; 26 | } 27 | return hash; 28 | })); 29 | -------------------------------------------------------------------------------- /build/hash-string.min.js: -------------------------------------------------------------------------------- 1 | !function(e,t){"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?module.exports=t():e.hash=t()}(this,function(){function e(e){"use strict";for(var t=5381,n=e.length;n;)t=33*t^e.charCodeAt(--n);return t>>>0}return e}); -------------------------------------------------------------------------------- /example/example.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Hash String Example 5 | 6 | 7 | 8 | 11 | 12 | -------------------------------------------------------------------------------- /gulpfile.js: -------------------------------------------------------------------------------- 1 | var gulp = require('gulp'), 2 | rename = require('gulp-rename'), 3 | uglify = require('gulp-uglify'), 4 | umd = require('gulp-umd'); 5 | 6 | gulp.task('default', function() { 7 | return gulp.src('source/*.js') 8 | .pipe(umd({ 9 | exports: function () { 10 | return 'hash'; 11 | }, 12 | namespace: function () { 13 | return 'hash'; 14 | } 15 | })) 16 | .pipe(gulp.dest('build')) 17 | .pipe(rename({ 18 | suffix: '.min' 19 | })) 20 | .pipe(uglify()) 21 | .pipe(gulp.dest('build')); 22 | }); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hash-string", 3 | "version": "1.0.0", 4 | "description": "A string hashing function based on Daniel J. Bernstein's popular 'times 33' hash algorithm.", 5 | "main": "build/hash-string.js", 6 | "repository": { 7 | "type": "git", 8 | "url": "https://github.com/MatthewBarker/hash-string" 9 | }, 10 | "keywords": [ 11 | "hash", 12 | "string", 13 | "algorithm" 14 | ], 15 | "author": "Matt Barker", 16 | "license": "ISC", 17 | "bugs": { 18 | "url": "https://github.com/MatthewBarker/hash-string/issues" 19 | }, 20 | "homepage": "https://github.com/MatthewBarker/hash-string", 21 | "devDependencies": { 22 | "gulp": "^3.8.11", 23 | "gulp-rename": "^1.2.2", 24 | "gulp-uglify": "^1.2.0", 25 | "gulp-umd": "^0.1.3" 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /source/hash-string.js: -------------------------------------------------------------------------------- 1 | /** 2 | A string hashing function based on Daniel J. Bernstein's popular 'times 33' hash algorithm. 3 | @param {string} text - String to hash 4 | @return {number} Resulting number. 5 | */ 6 | function hash(text) { 7 | 'use strict'; 8 | 9 | var hash = 5381, 10 | index = text.length; 11 | 12 | while (index) { 13 | hash = (hash * 33) ^ text.charCodeAt(--index); 14 | } 15 | 16 | return hash >>> 0; 17 | } --------------------------------------------------------------------------------