-
Notifications
You must be signed in to change notification settings - Fork 471
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
163 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import {Pipe, type PipeTransform} from '@angular/core'; | ||
import type {TuiIterationRange} from '@taiga-ui/cdk/types'; | ||
|
||
type Range<From extends number, To extends number> = TuiIterationRange<From, To, 1>; | ||
|
||
@Pipe({ | ||
standalone: true, | ||
name: 'tuiRepeatTimes', | ||
}) | ||
export class TuiRepeatTimesPipe implements PipeTransform { | ||
public transform<N extends number>(length: N): Range<1, N> { | ||
return Array.from({length}, (_, i) => ++i) as unknown as Range<1, N>; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"lib": { | ||
"entryFile": "index.ts" | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
projects/cdk/pipes/repeat-times/test/repeat-times.pipe.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import {TuiRepeatTimesPipe} from '@taiga-ui/cdk'; | ||
|
||
describe('TuiRepeatTimes pipe', () => { | ||
const pipe = new TuiRepeatTimesPipe(); | ||
|
||
it('repeat works', () => { | ||
const arr = pipe.transform(5); | ||
|
||
expect(arr).toEqual([1, 2, 3, 4, 5]); | ||
|
||
expect(pipe.transform(0)).toEqual([]); | ||
expect(pipe.transform(1)).toEqual([1]); | ||
expect(pipe.transform(2)).toEqual([1, 2]); | ||
expect(pipe.transform(3)).toEqual([1, 2, 3]); | ||
}); | ||
|
||
it('repeat pipe should not compile and cannot be less than minimum value', () => { | ||
// @ts-expect-error | ||
expect(pipe.transform(-1)).toEqual([]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
type ComputeRange< | ||
N extends number, | ||
Result extends unknown[] = [], | ||
> = Result['length'] extends N ? Result : ComputeRange<N, [...Result, Result['length']]>; | ||
|
||
type Add<A extends number, B extends number> = [ | ||
...ComputeRange<A>, | ||
...ComputeRange<B>, | ||
]['length']; | ||
|
||
type IsGreater<A extends number, B extends number> = | ||
IsLiteralNumber<[...ComputeRange<B>][Last<[...ComputeRange<A>]>]> extends true | ||
? false | ||
: true; | ||
|
||
type Last<T extends any[]> = T extends [ | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
...infer _never, | ||
infer Last, | ||
] | ||
? Last extends number | ||
? Last | ||
: never | ||
: never; | ||
|
||
type RemoveLast<T extends any[]> = T extends [ | ||
...infer Rest, | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
infer _never, | ||
] | ||
? Rest | ||
: never; | ||
|
||
type IsLiteralNumber<N> = N extends number ? (number extends N ? false : true) : false; | ||
|
||
export type TuiIterationRange< | ||
Min extends number, | ||
Max extends number, | ||
ScaleBy extends number, | ||
Result extends unknown[] = [Min], | ||
> = | ||
IsGreater<Last<Result>, Max> extends true | ||
? RemoveLast<Result> | ||
: TuiIterationRange<Min, Max, ScaleBy, [...Result, Add<Last<Result>, ScaleBy>]>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
projects/demo/src/modules/pipes/repeat-times/examples/1/index.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<ng-container *ngFor="let item of 5 | tuiRepeatTimes"> | ||
{{ item }} | ||
</ng-container> |
14 changes: 14 additions & 0 deletions
14
projects/demo/src/modules/pipes/repeat-times/examples/1/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
15 changes: 15 additions & 0 deletions
15
projects/demo/src/modules/pipes/repeat-times/examples/import/import.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} | ||
``` |
5 changes: 5 additions & 0 deletions
5
projects/demo/src/modules/pipes/repeat-times/examples/import/template.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
```html | ||
@for (item of 3 | tuiRepeatTimes; track item) { | ||
<div class="t-cell"></div> | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |