Closes #1550 - Slugify IDs with the most minimal scheme

This commit is contained in:
Chris Cowan 2014-11-04 15:06:40 -07:00
parent b5d1533009
commit 0d1046e6db
4 changed files with 62 additions and 1 deletions

View file

@ -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'));
@ -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);

View file

@ -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;
};
});

View file

@ -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);
});

View file

@ -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);
});
});
});