mirror of
https://github.com/angular/angular
synced 2026-05-24 09:28:37 +00:00
refactor(common): move loader util functions to a common location (#47082)
This commit refactors the code to avoid an extra utils file and instead, all extra helpers are moved to a common location. PR Close #47082
This commit is contained in:
parent
dde5b9bff0
commit
3836ebbbdf
6 changed files with 28 additions and 27 deletions
|
|
@ -10,9 +10,9 @@ import {Provider, ɵRuntimeError as RuntimeError} from '@angular/core';
|
|||
|
||||
import {RuntimeErrorCode} from '../../../errors';
|
||||
import {PRECONNECT_CHECK_BLOCKLIST} from '../preconnect_link_checker';
|
||||
import {isValidPath, normalizePath, normalizeSrc} from '../util';
|
||||
|
||||
import {IMAGE_LOADER, ImageLoaderConfig} from './image_loader';
|
||||
import {isValidPath, normalizePath, normalizeSrc} from './loader_utils';
|
||||
|
||||
/**
|
||||
* Function that generates a built-in ImageLoader for Cloudinary
|
||||
|
|
|
|||
|
|
@ -10,9 +10,9 @@ import {Provider, ɵRuntimeError as RuntimeError} from '@angular/core';
|
|||
|
||||
import {RuntimeErrorCode} from '../../../errors';
|
||||
import {PRECONNECT_CHECK_BLOCKLIST} from '../preconnect_link_checker';
|
||||
import {isValidPath, normalizePath, normalizeSrc} from '../util';
|
||||
|
||||
import {IMAGE_LOADER, ImageLoaderConfig} from './image_loader';
|
||||
import {isValidPath, normalizePath, normalizeSrc} from './loader_utils';
|
||||
|
||||
/**
|
||||
* Function that generates a built-in ImageLoader for ImageKit
|
||||
|
|
@ -44,7 +44,7 @@ export function provideImageKitLoader(path: string, options: {ensurePreconnect?:
|
|||
let params = `tr:q-auto`; // applies the "auto quality" transformation
|
||||
if (config.width) {
|
||||
params += `,w-${config.width?.toString()}`;
|
||||
};
|
||||
}
|
||||
const url = `${path}/${params}/${normalizeSrc(config.src)}`;
|
||||
return url;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,9 +10,9 @@ import {Provider, ɵRuntimeError as RuntimeError} from '@angular/core';
|
|||
|
||||
import {RuntimeErrorCode} from '../../../errors';
|
||||
import {PRECONNECT_CHECK_BLOCKLIST} from '../preconnect_link_checker';
|
||||
import {isValidPath, normalizePath, normalizeSrc} from '../util';
|
||||
|
||||
import {IMAGE_LOADER, ImageLoaderConfig} from './image_loader';
|
||||
import {isValidPath, normalizePath, normalizeSrc} from './loader_utils';
|
||||
|
||||
/**
|
||||
* Function that generates a built-in ImageLoader for Imgix and turns it
|
||||
|
|
|
|||
|
|
@ -1,22 +0,0 @@
|
|||
export function isValidPath(path: unknown) {
|
||||
const isString = typeof path === 'string';
|
||||
|
||||
if (!isString || path.trim() === '') {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
const url = new URL(path);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export function normalizePath(path: string) {
|
||||
return path.endsWith('/') ? path.slice(0, -1) : path;
|
||||
}
|
||||
|
||||
export function normalizeSrc(src: string) {
|
||||
return src.startsWith('/') ? src.slice(1) : src;
|
||||
}
|
||||
|
|
@ -38,3 +38,26 @@ export function extractHostname(url: string): string {
|
|||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
export function isValidPath(path: unknown): boolean {
|
||||
const isString = typeof path === 'string';
|
||||
|
||||
if (!isString || path.trim() === '') {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
const url = new URL(path);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export function normalizePath(path: string): string {
|
||||
return path.endsWith('/') ? path.slice(0, -1) : path;
|
||||
}
|
||||
|
||||
export function normalizeSrc(src: string): string {
|
||||
return src.startsWith('/') ? src.slice(1) : src;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ import {IMAGE_LOADER, ImageLoader, PRECONNECT_CHECK_BLOCKLIST} from '@angular/co
|
|||
import {provideCloudinaryLoader} from '@angular/common/src/directives/ng_optimized_image/image_loaders/cloudinary_loader';
|
||||
import {provideImageKitLoader} from '@angular/common/src/directives/ng_optimized_image/image_loaders/imagekit_loader';
|
||||
import {provideImgixLoader} from '@angular/common/src/directives/ng_optimized_image/image_loaders/imgix_loader';
|
||||
import {isValidPath} from '@angular/common/src/directives/ng_optimized_image/image_loaders/loader_utils';
|
||||
import {isValidPath} from '@angular/common/src/directives/ng_optimized_image/util';
|
||||
import {createEnvironmentInjector, ValueProvider} from '@angular/core';
|
||||
|
||||
describe('Built-in image directive loaders', () => {
|
||||
|
|
|
|||
Loading…
Reference in a new issue