Extract encode_operand/decode_operand in hashchange.js.

These are just one-liners for now, but they will set us up
to do different encodings for different narrows.
This commit is contained in:
Steve Howell
2017-01-05 14:36:02 -08:00
committed by Tim Abbott
parent 030c6649ae
commit 3ee733eb51

View File

@@ -15,10 +15,18 @@ exports.encodeHashComponent = function (str) {
.replace(/%/g, '.'); .replace(/%/g, '.');
}; };
exports.encode_operand = function (operator, operand) {
return exports.encodeHashComponent(operand);
};
function decodeHashComponent(str) { function decodeHashComponent(str) {
return decodeURIComponent(str.replace(/\./g, '%')); return decodeURIComponent(str.replace(/\./g, '%'));
} }
exports.decode_operand = function (operator, operand) {
return decodeHashComponent(operand);
};
function set_hash(hash) { function set_hash(hash) {
var location = window.location; var location = window.location;
@@ -67,7 +75,7 @@ exports.operators_to_hash = function (operators) {
var sign = elem.negated ? '-' : ''; var sign = elem.negated ? '-' : '';
hash += '/' + sign + hashchange.encodeHashComponent(operator) hash += '/' + sign + hashchange.encodeHashComponent(operator)
+ '/' + hashchange.encodeHashComponent(operand); + '/' + hashchange.encode_operand(operator, operand);
}); });
} }
@@ -90,7 +98,7 @@ exports.parse_narrow = function (hash) {
// but the user might write one. // but the user might write one.
try { try {
var operator = decodeHashComponent(hash[i]); var operator = decodeHashComponent(hash[i]);
var operand = decodeHashComponent(hash[i+1] || ''); var operand = exports.decode_operand(operator, hash[i+1] || '');
var negated = false; var negated = false;
if (operator[0] === '-') { if (operator[0] === '-') {
negated = true; negated = true;