add truncate pipe for recipe-mini description #5
Merged
remi.arnal
merged 1 commits from truncate-pipe
into master
12 months ago
@ -1,11 +1,11 @@
|
|||||||
<div class="recipe-card">
|
<div class="recipe-card">
|
||||||
<div class="recipe-image">
|
<div class="recipe-image">
|
||||||
<img [src]="recipe.image" onerror="this.onerror=null;this.src='https://placehold.co/100x100';"
|
<img [src]="recipe.image" onerror="this.onerror=null;this.src='https://placehold.co/100x100';"
|
||||||
alt="{{recipe.name}}">
|
alt="{{ recipe.name }}">
|
||||||
</div>
|
</div>
|
||||||
<div class="recipe-details">
|
<div class="recipe-details">
|
||||||
<h2>{{recipe.name}}</h2>
|
<h2>{{ recipe.name }}</h2>
|
||||||
<p>{{recipe.description}}</p>
|
<p>{{ recipe.description | truncate:50:false }}</p>
|
||||||
<a href="/recipe/{{ recipe.id }}">Voir la recette</a>
|
<a href="/recipe/{{ recipe.id }}">Voir la recette</a>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
@ -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…
Reference in new issue