Comparing version 2.0.3 to 2.1.0
98
index.js
@@ -7,2 +7,46 @@ 'use strict'; | ||
Hash.decodeUnescape = | ||
function decodeUnescape(str) { | ||
var keyvals = []; | ||
var parts = str.split('&'); | ||
for (var i = 0; i < parts.length; i++) { | ||
var keystr = parts[i].split('='); | ||
var key = unescape(keystr[0]); | ||
var val = unescape(keystr[1]) || ''; | ||
keyvals.push([key, val]); | ||
} | ||
return keyvals; | ||
}; | ||
Hash.encodeMinEscape = | ||
function encodeMinEscape(keyvals) { | ||
var parts = []; | ||
for (var i = 0; i < keyvals.length; i++) { | ||
var key = keyvals[i][0]; | ||
var val = keyvals[i][1]; | ||
var part = '' + minEscape(key); | ||
if (val !== undefined && val !== '') { | ||
part += '=' + minEscape(val); | ||
} | ||
parts.push(part); | ||
} | ||
return parts.join('&'); | ||
}; | ||
Hash.encodeMaxEscape = | ||
function encodeMaxEscape(keyvals) { | ||
var parts = []; | ||
for (var i = 0; i < keyvals.length; i++) { | ||
var key = keyvals[i][0]; | ||
var val = keyvals[i][1]; | ||
var part = '' + escape(key); | ||
if (val !== undefined && val !== '') { | ||
part += '=' + escape(val); | ||
} | ||
parts.push(part); | ||
} | ||
return parts.join('&'); | ||
}; | ||
function Hash(window, options) { | ||
@@ -15,3 +59,2 @@ var self = this; | ||
this.window = window; | ||
this.window.addEventListener('hashchange', onHashChange); | ||
this.last = ''; | ||
@@ -21,8 +64,11 @@ this.cache = {}; | ||
this.bound = {}; | ||
this.load(); | ||
// TODO: do we ever need to escape? | ||
this.fullEscape = | ||
options.escape === undefined | ||
? true : !!options.escape; | ||
this.decode = options.decode || Hash.decodeUnescape; | ||
this.encode = options.encode || (options.escape | ||
? Hash.encodeMaxEscape | ||
: Hash.encodeMinEscape); | ||
this.window.addEventListener('hashchange', onHashChange); | ||
this.load(); | ||
function onHashChange(e) { | ||
@@ -40,8 +86,8 @@ self.load(); | ||
this.last = this.window.location.hash; | ||
var parts = this.last.slice(1).split('&'); | ||
var keystrs = this.decode(this.last.slice(1)); | ||
var seen = {}; | ||
for (var i = 0; i < parts.length; i++) { | ||
var keyval = parts[i].split('='); | ||
var key = unescape(keyval[0]); | ||
var str = unescape(keyval[1]) || ''; | ||
for (var i = 0; i < keystrs.length; i++) { | ||
var key = keystrs[i][0]; | ||
var str = keystrs[i][1]; | ||
if (this.cache[key] !== str) { | ||
@@ -85,3 +131,3 @@ this.cache[key] = str; | ||
function save() { | ||
var parts = []; | ||
var keystrs = []; | ||
var keys = Object.keys(this.cache); | ||
@@ -94,30 +140,12 @@ for (var i = 0; i < keys.length; i++) { | ||
var str = this.cache[key]; | ||
var part = '' + this.escapePart(key); | ||
if (str === undefined) { | ||
continue; | ||
} | ||
if (str !== '') { | ||
part += '=' + this.escapePart(str); | ||
} | ||
parts.push(part); | ||
keystrs.push([key, str]); | ||
} | ||
var hash = parts.join('&'); | ||
var hash = this.encode(keystrs); | ||
if (hash) { | ||
hash = '#' + hash; | ||
} | ||
this.window.location.hash = this.last = hash; | ||
}; | ||
Hash.prototype.escapePart = | ||
function escapePart(str) { | ||
if (this.fullEscape) { | ||
return escape(str); | ||
} | ||
return str.replace(/[#=&]/g, function each(part) { | ||
return escape(part); | ||
}); | ||
}; | ||
Hash.prototype.bind = | ||
@@ -333,1 +361,9 @@ function bind(key) { | ||
} | ||
function minEscape(str) { | ||
return str.replace(/[#=&]/g, escapeMatch); | ||
} | ||
function escapeMatch(part) { | ||
return escape(part); | ||
} |
{ | ||
"name": "hashbind", | ||
"version": "2.0.3", | ||
"version": "2.1.0", | ||
"description": "Dual Binding Library For window.location.hash", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
10256
320