add truncate pipe for recipe-mini description

pull/5/head
remrem 12 months ago
parent b2c05f1440
commit 8e49b63a99

@ -5,7 +5,7 @@
</div>
<div class="recipe-details">
<h2>{{ recipe.name }}</h2>
<p>{{recipe.description}}</p>
<p>{{ recipe.description | truncate:50:false }}</p>
<a href="/recipe/{{ recipe.id }}">Voir la recette</a>
</div>
</div>

@ -1,12 +1,12 @@
import { Component } from '@angular/core';
import { Recipe } from '../../model/recipe.model';
import { Input } from '@angular/core';
import { TruncatePipe } from '../../pipes/truncate.pipe';
@Component({
selector: 'app-recipe-mini',
standalone: true,
imports: [],
imports: [TruncatePipe],
templateUrl: './recipe-mini.component.html',
styleUrl: './recipe-mini.component.css'
})

@ -0,0 +1,8 @@
import { TruncatePipe } from './truncate.pipe';
describe('TruncatePipe', () => {
it('create an instance', () => {
const pipe = new TruncatePipe();
expect(pipe).toBeTruthy();
});
});

@ -0,0 +1,20 @@
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'truncate',
standalone: true,
})
export class TruncatePipe implements PipeTransform {
transform(value: string, limit: number = 50, completeWords: boolean = false, ellipsis: string = '...'): string {
if (!value) return '';
if (value.length <= limit) return value;
if (completeWords) {
limit = value.substring(0, limit).lastIndexOf(' ');
}
return `${value.substring(0, limit)}${ellipsis}`;
}
}
Loading…
Cancel
Save