Skip to content

Commit

Permalink
added support for .toJSON to the rison encoder. This allows us to put…
Browse files Browse the repository at this point in the history
… complex objects in the state and have them encoded sanely
  • Loading branch information
Spencer Alger committed Apr 24, 2014
1 parent 7c5df8d commit b57be6b
Showing 1 changed file with 18 additions and 24 deletions.
42 changes: 18 additions & 24 deletions src/kibana/utils/rison.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,21 +115,22 @@ rison.quote = function(x) {
var sq = { // url-ok but quoted in strings
"'": true, '!': true
},
enc = function (v) {
if (v && typeof v.toJSON === 'function') v = v.toJSON();
var fn = s[typeof v];
if (fn) return fn(v);
},
s = {
array: function (x) {
var a = ['!('], b, f, i, l = x.length, v;
for (i = 0; i < l; i += 1) {
v = x[i];
f = s[typeof v];
if (f) {
v = f(v);
if (typeof v == 'string') {
if (b) {
a[a.length] = ',';
}
a[a.length] = v;
b = true;
v = enc(x[i]);
if (typeof v == 'string') {
if (b) {
a[a.length] = ',';
}
a[a.length] = v;
b = true;
}
}
a[a.length] = ')';
Expand All @@ -154,27 +155,20 @@ rison.quote = function(x) {
if (x instanceof Array) {
return s.array(x);
}
// WILL: will this work on non-Firefox browsers?
if (typeof x.__prototype__ === 'object' && typeof x.__prototype__.encode_rison !== 'undefined')
return x.encode_rison();

var a = ['('], b, f, i, v, ki, ks=[];
for (i in x)
ks[ks.length] = i;
ks.sort();
for (ki = 0; ki < ks.length; ki++) {
i = ks[ki];
v = x[i];
f = s[typeof v];
if (f) {
v = f(v);
if (typeof v == 'string') {
if (b) {
a[a.length] = ',';
}
a.push(s.string(i), ':', v);
b = true;
v = enc(x[i]);
if (typeof v == 'string') {
if (b) {
a[a.length] = ',';
}
a.push(s.string(i), ':', v);
b = true;
}
}
a[a.length] = ')';
Expand Down Expand Up @@ -209,7 +203,7 @@ rison.quote = function(x) {
*
*/
rison.encode = function (v) {
return s[typeof v](v);
return enc(v);
};

/**
Expand Down

0 comments on commit b57be6b

Please sign in to comment.