Created
October 8, 2018 21:03
-
-
Save shifatul-i/f1eb212042f43ea6e2054394c1341f95 to your computer and use it in GitHub Desktop.
Angular - short domain pipe (URL to domain - https://github.com/ThunderRoid → github.com)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Pipe, PipeTransform } from '@angular/core'; | |
@Pipe({ | |
name: 'shortDomain' | |
}) | |
export class ShortDomainPipe implements PipeTransform { | |
transform(url: string, args?: any): any { | |
if (url) { | |
if (url.length > 3) { | |
let result; | |
let match; | |
if (match = url.match(/^(?:https?:\/\/)?(?:www\.)?([^:\/\n?=]+)/im)) { | |
result = match[1]; | |
if (match = result.match(/^[^.]+\.(.+\..+)$/)) | |
result = match[1]; | |
} | |
return result; | |
} | |
return url; | |
} | |
return url; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment