From 376cff7faaa59283aeb32fcaa96ea7ad9ac89ffd Mon Sep 17 00:00:00 2001 From: danrevah Date: Thu, 26 Jan 2017 10:17:20 +0200 Subject: [PATCH] fix(Slugify pipe): issue (#18) --- src/app/pipes/string/slugify.spec.ts | 5 +++++ src/app/pipes/string/slugify.ts | 4 +++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/app/pipes/string/slugify.spec.ts b/src/app/pipes/string/slugify.spec.ts index 5d2ff94c..4e598508 100644 --- a/src/app/pipes/string/slugify.spec.ts +++ b/src/app/pipes/string/slugify.spec.ts @@ -16,4 +16,9 @@ describe('SlugifyPipe Tests', () => { expect(pipe.transform('Foo Bar Baz')).toEqual('foo-bar-baz'); expect(pipe.transform('UPPER CASE TEXT')).toEqual('upper-case-text'); }); + + it('Should slugify special strings', () => { + expect(pipe.transform('http://example.com/foo')).toEqual('http-example-com-foo'); + expect(pipe.transform(' http://example.com/foo ')).toEqual('http-example-com-foo'); + }); }); diff --git a/src/app/pipes/string/slugify.ts b/src/app/pipes/string/slugify.ts index 6d0e72eb..cf96f54d 100644 --- a/src/app/pipes/string/slugify.ts +++ b/src/app/pipes/string/slugify.ts @@ -6,7 +6,9 @@ export class SlugifyPipe implements PipeTransform { transform(str: string): string { return GeneralHelper.isString(str) - ? str.toLowerCase().replace(/\s+/g, '-') + ? str.toLowerCase().trim() + .replace(/[^\w\-]+/g, ' ') + .replace(/\s+/g, '-') : str; } }