Skip to content

Commit fa1784b

Browse files
cesperianatscott
authored andcommitted
docs: improve examples, description of PipeTransform (#40863)
Fixes #37321 PR Close #40863
1 parent c6e2234 commit fa1784b

File tree

4 files changed

+57
-11
lines changed

4 files changed

+57
-11
lines changed

packages/core/src/change_detection/pipe_transform.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,19 @@
1313
*
1414
* @usageNotes
1515
*
16-
* In the following example, `RepeatPipe` repeats a given value a given number of times.
16+
* In the following example, `TruncatePipe` returns the shortened value with an added ellipses.
1717
*
18-
* ```ts
19-
* import {Pipe, PipeTransform} from '@angular/core';
18+
* <code-example path="core/ts/pipes/simple_truncate.ts" header="simple_truncate.ts"></code-example>
2019
*
21-
* @Pipe({name: 'repeat'})
22-
* export class RepeatPipe implements PipeTransform {
23-
* transform(value: any, times: number) {
24-
* return value.repeat(times);
25-
* }
26-
* }
27-
* ```
20+
* Invoking `{{ 'It was the best of times' | truncate }}` in a template will produce `It was...`.
2821
*
29-
* Invoking `{{ 'ok' | repeat:3 }}` in a template produces `okokok`.
22+
* In the following example, `TruncatePipe` takes parameters that sets the truncated length and the
23+
* string to append with.
24+
*
25+
* <code-example path="core/ts/pipes/truncate.ts" header="truncate.ts"></code-example>
26+
*
27+
* Invoking `{{ 'It was the best of times' | truncate:4:'....' }}` in a template will produce `It
28+
* was the best....`.
3029
*
3130
* @publicApi
3231
*/
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {NgModule} from '@angular/core';
10+
import {TruncatePipe as SimpleTruncatePipe} from './simple_truncate';
11+
import {TruncatePipe} from './truncate';
12+
13+
@NgModule({declarations: [SimpleTruncatePipe, TruncatePipe]})
14+
export class TruncateModule {
15+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {Pipe, PipeTransform} from '@angular/core';
10+
11+
@Pipe({name: 'truncate'})
12+
export class TruncatePipe implements PipeTransform {
13+
transform(value: any) {
14+
return value.split(' ').slice(0, 2).join(' ') + '...';
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {Pipe, PipeTransform} from '@angular/core';
10+
11+
@Pipe({name: 'truncate'})
12+
export class TruncatePipe implements PipeTransform {
13+
transform(value: any, length: number, symbol: string) {
14+
return value.split(' ').slice(0, length).join(' ') + symbol;
15+
}
16+
}

0 commit comments

Comments
 (0)