Skip to content

Commit

Permalink
Add transform for decoding base64 strings. Closes elastic#4358
Browse files Browse the repository at this point in the history
  • Loading branch information
jbudz committed Jul 1, 2015
1 parent fa35ba8 commit 90e4fb8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/kibana/components/stringify/types/String.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,31 @@ define(function (require) {
{ id: false, name: '- none -' },
{ id: 'lower', name: 'Lower Case' },
{ id: 'upper', name: 'Upper Case' },
{ id: 'short', name: 'Short Dots' }
{ id: 'short', name: 'Short Dots' },
{ id: 'binary', name: 'Base64 Decode'}
];

_String.sampleInputs = [
'A Quick Brown Fox.',
'com.organizations.project.ClassName',
'hostname.net'
'hostname.net',
'SGVsbG8gd29ybGQ='
];

_String.prototype._base64Decode = function (val) {
try {
return window.atob(val);
} catch (e) {
return _.asPrettyString(val);
}
};

_String.prototype._convert = function (val) {
switch (this.param('transform')) {
case 'lower': return String(val).toLowerCase();
case 'upper': return String(val).toUpperCase();
case 'short': return _.shortenDottedString(val);
case 'binary': return this._base64Decode(val);
default: return _.asPrettyString(val);
}
};
Expand Down
19 changes: 19 additions & 0 deletions test/unit/specs/components/stringify/_string.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
define(function (require) {
return ['String Format', function () {
var fieldFormats;

beforeEach(module('kibana'));
beforeEach(inject(function (Private) {
fieldFormats = Private(require('registry/field_formats'));
}));

it('decode a base64 string', function () {
var StringFormat = fieldFormats.getType('string');
var string = new StringFormat({
transform: 'binary'
});
expect(string.convert('Zm9vYmFy')).to.be('foobar');
});

}];
});
1 change: 1 addition & 0 deletions test/unit/specs/components/stringify/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ define(function (require) {
describe(require('specs/components/stringify/_conformance'));
describe(require('specs/components/stringify/_ip'));
describe(require('specs/components/stringify/_source'));
describe(require('specs/components/stringify/_string'));
describe(require('specs/components/stringify/_url'));
});
});

0 comments on commit 90e4fb8

Please sign in to comment.