Created
October 8, 2018 21:03
-
-
Save shifatul-i/b99c7c484d663a0a802d2c4ec0e0e9b0 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