Skip to content

Commit

Permalink
feat(cdk): add TuiRepeatTimes pipe (#9262)
Browse files Browse the repository at this point in the history
  • Loading branch information
splincode authored Sep 30, 2024
1 parent 91dab47 commit 712c5b3
Show file tree
Hide file tree
Showing 13 changed files with 111 additions and 0 deletions.
1 change: 1 addition & 0 deletions projects/cdk/pipes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ export * from '@taiga-ui/cdk/pipes/filter';
export * from '@taiga-ui/cdk/pipes/is-present';
export * from '@taiga-ui/cdk/pipes/keys';
export * from '@taiga-ui/cdk/pipes/mapper';
export * from '@taiga-ui/cdk/pipes/repeat-times';
export * from '@taiga-ui/cdk/pipes/replace';
export * from '@taiga-ui/cdk/pipes/to-array';
12 changes: 12 additions & 0 deletions projects/cdk/pipes/repeat-times/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {Pipe, type PipeTransform} from '@angular/core';
import {tuiClamp} from '@taiga-ui/cdk/utils';

@Pipe({
standalone: true,
name: 'tuiRepeatTimes',
})
export class TuiRepeatTimesPipe implements PipeTransform {
public transform(n: number): number[] {
return Array.from({length: tuiClamp(n, 0, n)}, (_, i) => i);
}
}
5 changes: 5 additions & 0 deletions projects/cdk/pipes/repeat-times/ng-package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"lib": {
"entryFile": "index.ts"
}
}
16 changes: 16 additions & 0 deletions projects/cdk/pipes/repeat-times/test/repeat-times.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {TuiRepeatTimesPipe} from '@taiga-ui/cdk';

describe('TuiRepeatTimes pipe', () => {
const pipe = new TuiRepeatTimesPipe();

it('works', () => {
expect(pipe.transform(-2)).toEqual([]);
expect(pipe.transform(-1)).toEqual([]);
expect(pipe.transform(0)).toEqual([]);
expect(pipe.transform(1)).toEqual([0]);
expect(pipe.transform(2)).toEqual([0, 1]);
expect(pipe.transform(3)).toEqual([0, 1, 2]);
expect(pipe.transform(4)).toEqual([0, 1, 2, 3]);
expect(pipe.transform(5)).toEqual([0, 1, 2, 3, 4]);
});
});
5 changes: 5 additions & 0 deletions projects/demo/src/modules/app/app.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,11 @@ export const ROUTES: Routes = [
loadComponent: async () => import('../pipes/mapper'),
title: 'Mapper',
}),
route({
path: DemoRoute.RepeatTimes,
loadComponent: async () => import('../pipes/repeat-times'),
title: 'RepeatTimes',
}),
route({
path: DemoRoute.Stringify,
loadComponent: async () => import('../pipes/stringify'),
Expand Down
1 change: 1 addition & 0 deletions projects/demo/src/modules/app/demo-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ export const DemoRoute = {
FormatNumber: '/pipes/format-number',
IsPresent: '/pipes/is-present',
Mapper: '/pipes/mapper',
RepeatTimes: '/pipes/repeat-times',
Stringify: '/pipes/stringify',
StringifyContent: '/pipes/stringify-content',
Alert: '/components/alert',
Expand Down
6 changes: 6 additions & 0 deletions projects/demo/src/modules/app/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1368,6 +1368,12 @@ export const pages: TuiDocRoutePages = [
keywords: 'mapper, мап, преобразование, пайп, pipe',
route: DemoRoute.Mapper,
},
{
section: 'Tools',
title: 'RepeatTimes',
keywords: 'повторение, repeat, пайп, pipe',
route: DemoRoute.RepeatTimes,
},
{
section: 'Tools',
title: 'FieldError',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<ng-container *ngFor="let index of 5 | tuiRepeatTimes">
{{ index }}
</ng-container>
14 changes: 14 additions & 0 deletions projects/demo/src/modules/pipes/repeat-times/examples/1/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {NgForOf} from '@angular/common';
import {Component} from '@angular/core';
import {changeDetection} from '@demo/emulate/change-detection';
import {encapsulation} from '@demo/emulate/encapsulation';
import {TuiRepeatTimesPipe} from '@taiga-ui/cdk';

@Component({
standalone: true,
imports: [NgForOf, TuiRepeatTimesPipe],
templateUrl: './index.html',
encapsulation,
changeDetection,
})
export default class Example {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
```ts
import {TuiRepeatTimesPipe} from '@taiga-ui/cdk';

//...

@Component({
standalone: true,
imports: [
// ...
TuiRepeatTimesPipe,
],
// ...
})
export class Example {}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
```html
@for (index of 3 | tuiRepeatTimes; track index) {
<div class="t-cell"></div>
}
```
17 changes: 17 additions & 0 deletions projects/demo/src/modules/pipes/repeat-times/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<tui-doc-page
header="RepeatTimes"
package="CDK"
type="pipes"
>
<ng-template pageTab>
<tui-doc-example
id="base"
description="Pipe for making an array of elements by length"
heading="Base"
[component]="1 | tuiComponent"
[content]="1 | tuiExample"
/>
</ng-template>

<tui-setup *pageTab="'Setup'" />
</tui-doc-page>
11 changes: 11 additions & 0 deletions projects/demo/src/modules/pipes/repeat-times/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {Component} from '@angular/core';
import {changeDetection} from '@demo/emulate/change-detection';
import {TuiDemo} from '@demo/utils';

@Component({
standalone: true,
imports: [TuiDemo],
templateUrl: './index.html',
changeDetection,
})
export default class Page {}

0 comments on commit 712c5b3

Please sign in to comment.