All files / app/classes image.class.ts

96.22% Statements 51/53
90% Branches 9/10
100% Functions 3/3
96.22% Lines 51/53

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 541x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 5x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x     4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x  
import { ImageInterface } from '../interfaces/image.interface';
 
export class ImageClass {
	displayName: string;
	position: number;
	url: string;
 
	constructor(data: ImageInterface) {
		this.displayName = data['display-name'];
		this.position = data.position;
		this.url = data.url;
	}
 
	fromJson(json: ImageInterface): void {
		this.displayName = json['display-name'];
		this.position = json.position;
		this.url = json.url;
	}
 
	getNewUrl(): string {
		if (!this.url || typeof this.url !== 'string') return '';
 
		const [baseUrl, params] = this.url.split('/upload/');
		const parts = params.split('/');
		const transformationsIndex = parts.findIndex((part) => part.match(/v[0-9]+/));
 
		let transformations = parts.slice(0, transformationsIndex).join(',');
		let restOfUrl = parts.slice(transformationsIndex).join('/');
 
		const transformationArray = transformations.split(',');
 
		const newTransformations = [`f_auto`, `q_50`];
 
		const updatedTransformations = transformationArray
			.map((trans) => {
				if (trans.startsWith('w_') || trans.startsWith('h_')) {
					return null;
				}
				return trans;
			})
			.filter((trans) => trans !== null);
 
		const mergedTransformations = new Set([...updatedTransformations, ...newTransformations]);
 
		transformations = Array.from(mergedTransformations).join(',');
 
		const supportedFormatsRegex = /\.(png|jpg|jpeg)$/i;
 
		restOfUrl = restOfUrl.replace(supportedFormatsRegex, '.webp');
 
		return `${baseUrl}/upload/${transformations}/${restOfUrl}`;
	}
}