Skip to content

Commit

Permalink
Closes #1550 - Slugify IDs with the most minimal scheme
Browse files Browse the repository at this point in the history
  • Loading branch information
simianhacker committed Nov 4, 2014
1 parent b5d1533 commit 0d1046e
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/kibana/components/courier/saved_object/saved_object.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ define(function (require) {
var errors = require('errors');
var angular = require('angular');
var _ = require('lodash');
var slugifyId = require('utils/slugify_id');

var DocSource = Private(require('components/courier/data_source/doc_source'));
var SearchSource = Private(require('components/courier/data_source/search_source'));
Expand Down Expand Up @@ -198,6 +199,10 @@ define(function (require) {
};
}


// Slugify the object id
obj.id = slugifyId(obj.id);

// ensure that the docSource has the current obj.id
docSource.id(obj.id);

Expand Down
20 changes: 20 additions & 0 deletions src/kibana/utils/slugify_id.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
define(function (require) {
var _ = require('lodash');
return function (id) {
if (id == null) return;

var trans = {
'/' : '-slash-',
'\\?' : '-questionmark-',
'\\&' : '-ampersand-',
'=' : '-equal-'
};
_.each(trans, function (val, key) {
var regex = new RegExp(key);
id = id.replace(regex, val);
});
id = id.replace(/[\s]+/g, '_');
id = id.replace(/[_]+/g, '_');
return id;
};
});
3 changes: 2 additions & 1 deletion test/unit/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@
'specs/visualize/_transform_aggregation',
'specs/visualize/_create_raw_data',
'specs/visualize/_array_to_linked_list',
'specs/visualize/_collect_branch'
'specs/visualize/_collect_branch',
'specs/utils/slugify_id'
], function () {
bootstrap(kibana, sinon);
});
Expand Down
35 changes: 35 additions & 0 deletions test/unit/specs/utils/slugify_id.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
define(function (require) {
var _ = require('lodash');
var slugifyId = require('utils/slugify_id');

describe('slugifyId()', function () {

var fixtures = [
['test/test', 'test-slash-test'],
['test?test', 'test-questionmark-test'],
['test=test', 'test-equal-test'],
['test&test', 'test-ampersand-test'],
['test / test', 'test_-slash-_test'],
['test ? test', 'test_-questionmark-_test'],
['test = test', 'test_-equal-_test'],
['test & test', 'test_-ampersand-_test'],
['test / ^test', 'test_-slash-_^test'],
['test ? test', 'test_-questionmark-_test'],
['test = test', 'test_-equal-_test'],
['test & test', 'test_-ampersand-_test']
];

_.each(fixtures, function (fixture) {
var msg = 'should convert ' + fixture[0] + ' to ' + fixture[1];
it(msg, function () {
var results = slugifyId(fixture[0]);
expect(results).to.be(fixture[1]);
});
});

it('should do nothing if the id is undefined', function () {
expect(slugifyId(undefined)).to.be(undefined);
});

});
});

0 comments on commit 0d1046e

Please sign in to comment.