From 94884510802906cdbfdd3490fac5fc9401937da7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jovan=20Kruni=C4=87?= Date: Mon, 29 Mar 2021 18:06:50 +0200 Subject: [PATCH] fix: wrong way alias names are generated Closes #79 --- src/storage/elasticsearch/elasticsearch.ts | 9 ++++++--- test/storage/elasticsearch/elasticsearch.spec.ts | 10 +++++++++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/storage/elasticsearch/elasticsearch.ts b/src/storage/elasticsearch/elasticsearch.ts index d36ba4d3..50c882c3 100644 --- a/src/storage/elasticsearch/elasticsearch.ts +++ b/src/storage/elasticsearch/elasticsearch.ts @@ -141,10 +141,13 @@ export class Elasticsearch implements Database { * @param uid The UID of the current bulk (for debugging purposes) */ static removeAliasChars(alias: string, uid: string | undefined): string { - // spaces are included in some types, so throwing an error in this case would clutter up the log unnecessarily let formattedAlias = alias; - while (formattedAlias.includes(' ')) { - formattedAlias = formattedAlias.replace(' ', ''); + + // spaces are included in some types, replace them with underscores + if (formattedAlias.includes(' ')) { + formattedAlias = formattedAlias.trim(); + formattedAlias = formattedAlias.split(' ') + .join('_'); } // List of invalid characters: https://www.elastic.co/guide/en/elasticsearch/reference/6.6/indices-create-index.html ['\\', '/', '*', '?', '"', '<', '>', '|', ',', '#'].forEach((value) => { diff --git a/test/storage/elasticsearch/elasticsearch.spec.ts b/test/storage/elasticsearch/elasticsearch.spec.ts index a8b46b20..d77e9455 100644 --- a/test/storage/elasticsearch/elasticsearch.spec.ts +++ b/test/storage/elasticsearch/elasticsearch.spec.ts @@ -101,8 +101,16 @@ describe('Elasticsearch', function () { }); describe('removeAliasChars', function () { + it('should remove spaces from both ends', function () { + expect(Elasticsearch.removeAliasChars(' foobaralias ', 'bulk-uid')).to.be.equal('foobaralias'); + }); + + it('should replace inner spaces with underscores', function () { + expect(Elasticsearch.removeAliasChars('foo bar alias', 'bulk-uid')).to.be.equal('foo_bar_alias'); + }); + it('should remove invalid characters', function () { - expect(Elasticsearch.removeAliasChars('f,o#o\\ b|ar/ * ', 'bulk-uid')).to.be.equal('foobaralias'); + expect(Elasticsearch.removeAliasChars('f,o#o\\b|ar/* ', 'bulk-uid')).to.be.equal('foobaralias'); }); it('should remove invalid starting characters', function () {