All files / app/classes sites.class.ts

97.18% Statements 69/71
90% Branches 9/10
100% Functions 3/3
97.18% Lines 69/71

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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 721x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x     3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x  
import { SiteInterface } from '../interfaces/site.interface';
 
export class SiteClass {
	altText: string;
	altText2: string;
	description: string;
	image2: string;
	imageUrl: string;
	name: string;
	position: number;
	tags: string[];
	thumbnail: string;
 
	constructor(data: SiteInterface) {
		this.altText = data['alt-text'];
		this.altText2 = data['alt-text-2'];
		this.description = data.description;
		this.image2 = data['image-2'];
		this.imageUrl = data['image-url'];
		this.name = data.name;
		this.position = data.position;
		this.tags = data.tags;
		this.thumbnail = data.thumbnail;
	}
 
	fromJson(json: SiteInterface): void {
		this.altText = json['alt-text'];
		this.altText2 = json['alt-text-2'];
		this.description = json.description;
		this.image2 = json['image-2'];
		this.imageUrl = json['image-url'];
		this.name = json.name;
		this.position = json.position;
		this.tags = json.tags;
		this.thumbnail = json.thumbnail;
	}
 
	getMainImage(): string {
		if (!this.imageUrl || typeof this.imageUrl !== 'string') return '';
 
		const [baseUrl, params] = this.imageUrl.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 supportedFormatsRegex = /\.(png|jpg|jpeg)$/i;
 
		restOfUrl = restOfUrl.replace(supportedFormatsRegex, '.webp');
 
		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(',');
 
		return `${baseUrl}/upload/${transformations}/${restOfUrl}`;
	}
}