libs/common/src/lib/normalize-url/normalize-url.pipe.ts
Normalizes the given url based on the DEPLOY_URL
injection token.
Usage Notes
<img [src]="myUrl | talyNormalizeUrl" data-testid="image2" />
If DEPLOY_URL
token is not provided, then the given value is returned as it is.
If DEPLOY_URL
token is provided, then the url is intelligently prefixed with the given
DEPLOY_URL
dependent on the input.
DEPLOY_URL | Input | Output |
---|---|---|
null |
'assets/image1.png' |
'assets/image1.png' |
'http://my-domain.com/my-prj' |
'assets/image1.png' |
'http://my-domain.com/my-prj/assets/image1.png' |
'http://my-domain.com/my-prj' |
'./assets/image1.png' |
'http://my-domain.com/my-prj/assets/image1.png' |
'http://my-domain.com/my-prj' |
'/assets/image1.png' |
'http://my-domain.com/assets/image1.png' |
'http://my-domain.com/my-prj' |
'http://cdn.com/assets/image1.png' |
'http://cdn.com/assets/image1.png' |
Name | talyNormalizeUrl |
transform | ||||||
transform(value: string)
|
||||||
Parameters :
Returns :
string
|
import { Inject, Optional, Pipe, PipeTransform } from '@angular/core';
import { DEPLOY_URL } from './token';
/**
* Normalizes the given url based on the `DEPLOY_URL` injection token.
*
* Usage Notes
*
* ```html
* <img [src]="myUrl | talyNormalizeUrl" data-testid="image2" />
* ```
*
* If `DEPLOY_URL` token is not provided, then the given value is returned as it is.
* If `DEPLOY_URL` token is provided, then the url is intelligently prefixed with the given
* `DEPLOY_URL` dependent on the input.
*
* | DEPLOY_URL |Input |Output |
* |----------------------------------|-------------------------------------|--------------------------------------------------|
* |`null` |`'assets/image1.png'` |`'assets/image1.png'` |
* |`'http://my-domain.com/my-prj'` |`'assets/image1.png'` |`'http://my-domain.com/my-prj/assets/image1.png'` |
* |`'http://my-domain.com/my-prj'` |`'./assets/image1.png'` |`'http://my-domain.com/my-prj/assets/image1.png'` |
* |`'http://my-domain.com/my-prj'` |`'/assets/image1.png'` |`'http://my-domain.com/assets/image1.png'` |
* |`'http://my-domain.com/my-prj'` |`'http://cdn.com/assets/image1.png'` |`'http://cdn.com/assets/image1.png'` |
*
*/
@Pipe({
name: 'talyNormalizeUrl'
})
export class NormalizeUrlPipe implements PipeTransform {
constructor(@Optional() @Inject(DEPLOY_URL) private deployUrl: string | null) {}
transform(value: string): string {
if (!value) return value;
return this.deployUrl ? new URL(value, this.deployUrl).toString() : value;
}
}