mirror of
https://github.com/elastic/kibana.git
synced 2025-04-23 17:28:26 -04:00
Closes #1550 - Slugify IDs with the most minimal scheme
This commit is contained in:
parent
b5d1533009
commit
0d1046e6db
4 changed files with 62 additions and 1 deletions
|
@ -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);
|
||||
|
||||
|
|
20
src/kibana/utils/slugify_id.js
Normal file
20
src/kibana/utils/slugify_id.js
Normal 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;
|
||||
};
|
||||
});
|
|
@ -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);
|
||||
});
|
||||
|
|
35
test/unit/specs/utils/slugify_id.js
Normal file
35
test/unit/specs/utils/slugify_id.js
Normal 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);
|
||||
});
|
||||
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue