Skip to content

Commit

Permalink
Add a try/catch in query.encodeValue
Browse files Browse the repository at this point in the history
This change enables converting values containing special characters, such as the percent sign (%), to strings, by always encoding values for which the check if encoding is actually necessary has failed.
For values containing URI escape characters, the check throws an error (because they are not decodable), but we still need to encode them.

Fixes persvr#75
  • Loading branch information
rkaw92 committed Aug 24, 2016
1 parent 86b76eb commit 7db4e49
Showing 1 changed file with 25 additions and 21 deletions.
46 changes: 25 additions & 21 deletions query.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,28 +79,32 @@ function encodeString(s) {
exports.encodeValue = function(val) {
var encoded;
if (val === null) val = 'null';
if (val !== parser.converters["default"]('' + (
val.toISOString && val.toISOString() || val.toString()
))) {
var type = typeof val;
if(val instanceof RegExp){
// TODO: control whether to we want simpler glob() style
val = val.toString();
var i = val.lastIndexOf('/');
type = val.substring(i).indexOf('i') >= 0 ? "re" : "RE";
val = encodeString(val.substring(1, i));
encoded = true;
}
if(type === "object"){
type = "epoch";
val = val.getTime();
encoded = true;
}
if(type === "string") {
val = encodeString(val);
try {
if (val !== parser.converters["default"]('' + (
val.toISOString && val.toISOString() || val.toString()
))) {
var type = typeof val;
if(val instanceof RegExp){
// TODO: control whether to we want simpler glob() style
val = val.toString();
var i = val.lastIndexOf('/');
type = val.substring(i).indexOf('i') >= 0 ? "re" : "RE";
val = encodeString(val.substring(1, i));
encoded = true;
}
val = [type, val].join(":");
}
if(type === "object"){
type = "epoch";
val = val.getTime();
encoded = true;
}
if(type === "string") {
val = encodeString(val);
encoded = true;
}
val = [type, val].join(":");
}
} catch (conversionError) {
// The conversion necessity check has failed and we have not encoded the value. The next conditional block will take care of it.
}
if (!encoded && typeof val === "string") val = encodeString(val);
return val;
Expand Down

0 comments on commit 7db4e49

Please sign in to comment.