From 0d1046e6dbbee38cac0924f8fb9248f06d7ed60b Mon Sep 17 00:00:00 2001 From: Chris Cowan Date: Tue, 4 Nov 2014 15:06:40 -0700 Subject: [PATCH] Closes #1550 - Slugify IDs with the most minimal scheme --- .../courier/saved_object/saved_object.js | 5 +++ src/kibana/utils/slugify_id.js | 20 +++++++++++ test/unit/index.html | 3 +- test/unit/specs/utils/slugify_id.js | 35 +++++++++++++++++++ 4 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 src/kibana/utils/slugify_id.js create mode 100644 test/unit/specs/utils/slugify_id.js diff --git a/src/kibana/components/courier/saved_object/saved_object.js b/src/kibana/components/courier/saved_object/saved_object.js index 443d6712030a1..9d2dc30aa06c0 100644 --- a/src/kibana/components/courier/saved_object/saved_object.js +++ b/src/kibana/components/courier/saved_object/saved_object.js @@ -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); diff --git a/src/kibana/utils/slugify_id.js b/src/kibana/utils/slugify_id.js new file mode 100644 index 0000000000000..ecd626fbee895 --- /dev/null +++ b/src/kibana/utils/slugify_id.js @@ -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; + }; +}); diff --git a/test/unit/index.html b/test/unit/index.html index f5ac1be523206..ccd725cd9f5bf 100644 --- a/test/unit/index.html +++ b/test/unit/index.html @@ -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); }); diff --git a/test/unit/specs/utils/slugify_id.js b/test/unit/specs/utils/slugify_id.js new file mode 100644 index 0000000000000..03693bc80590b --- /dev/null +++ b/test/unit/specs/utils/slugify_id.js @@ -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); + }); + + }); +});