# Quarc Pipes Zestaw podstawowych pipes dla frameworka Quarc, inspirowanych pipes z Angulara. ## Instalacja Pipes są dostępne w `@quarc/core`: ```typescript import { UpperCasePipe, DatePipe, JsonPipe } from '@quarc/core'; ``` ## Użycie w komponentach ### 1. Zaimportuj pipe w komponencie ```typescript import { Component } from '@quarc/core'; import { UpperCasePipe } from '@quarc/core'; @Component({ selector: 'app-example', template: '
{{ name | uppercase }}
', imports: [UpperCasePipe], }) export class ExampleComponent { name = 'hello world'; } ``` ### 2. Użyj w template ```html
{{ value | uppercase }}
{{ text | substr:0:10 }}
{{ name | lowercase | camelcase }}
{{ value || 'default' | uppercase }}
``` ## Dostępne Pipes ### UpperCasePipe Konwertuje tekst na wielkie litery. ```typescript @Pipe({ name: 'uppercase' }) ``` **Przykłady:** ```html {{ 'hello' | uppercase }} {{ name | uppercase }} ``` --- ### LowerCasePipe Konwertuje tekst na małe litery. ```typescript @Pipe({ name: 'lowercase' }) ``` **Przykłady:** ```html {{ 'HELLO' | lowercase }} {{ name | lowercase }} ``` --- ### JsonPipe Serializuje obiekt do formatu JSON z wcięciami. ```typescript @Pipe({ name: 'json' }) ``` **Przykłady:** ```html {{ user | json }} {{ items | json }} ``` --- ### CamelCasePipe Konwertuje tekst do camelCase. ```typescript @Pipe({ name: 'camelcase' }) ``` **Przykłady:** ```html {{ 'hello-world' | camelcase }} {{ 'hello_world' | camelcase }} {{ 'hello world' | camelcase }} {{ 'HelloWorld' | camelcase }} ``` --- ### PascalCasePipe Konwertuje tekst do PascalCase. ```typescript @Pipe({ name: 'pascalcase' }) ``` **Przykłady:** ```html {{ 'hello-world' | pascalcase }} {{ 'hello_world' | pascalcase }} {{ 'hello world' | pascalcase }} {{ 'helloWorld' | pascalcase }} ``` --- ### SnakeCasePipe Konwertuje tekst do snake_case. ```typescript @Pipe({ name: 'snakecase' }) ``` **Przykłady:** ```html {{ 'helloWorld' | snakecase }} {{ 'HelloWorld' | snakecase }} {{ 'hello-world' | snakecase }} {{ 'hello world' | snakecase }} ``` --- ### KebabCasePipe Konwertuje tekst do kebab-case. ```typescript @Pipe({ name: 'kebabcase' }) ``` **Przykłady:** ```html {{ 'helloWorld' | kebabcase }} {{ 'HelloWorld' | kebabcase }} {{ 'hello_world' | kebabcase }} {{ 'hello world' | kebabcase }} ``` --- ### SubstrPipe Zwraca fragment tekstu. ```typescript @Pipe({ name: 'substr' }) ``` **Parametry:** - `start: number` - pozycja początkowa - `length?: number` - długość fragmentu (opcjonalne) **Przykłady:** ```html {{ 'hello world' | substr:0:5 }} {{ 'hello world' | substr:6 }} {{ text | substr:0:10 }} ``` --- ### DatePipe Formatuje daty. ```typescript @Pipe({ name: 'date' }) ``` **Parametry:** - `format: string` - format daty (domyślnie: 'medium') **Predefiniowane formaty:** | Format | Przykład | |--------|----------| | `short` | 1/15/24, 2:30 PM | | `medium` | Jan 15, 2024, 2:30:45 PM | | `long` | January 15, 2024 at 2:30:45 PM | | `full` | Monday, January 15, 2024 at 2:30:45 PM | | `shortDate` | 1/15/24 | | `mediumDate` | Jan 15, 2024 | | `longDate` | January 15, 2024 | | `fullDate` | Monday, January 15, 2024 | | `shortTime` | 2:30 PM | | `mediumTime` | 2:30:45 PM | **Własne formaty:** | Symbol | Znaczenie | Przykład | |--------|-----------|----------| | `yyyy` | Rok (4 cyfry) | 2024 | | `yy` | Rok (2 cyfry) | 24 | | `MM` | Miesiąc (2 cyfry) | 01 | | `M` | Miesiąc (1-2 cyfry) | 1 | | `dd` | Dzień (2 cyfry) | 15 | | `d` | Dzień (1-2 cyfry) | 15 | | `HH` | Godzina 24h (2 cyfry) | 14 | | `H` | Godzina 24h (1-2 cyfry) | 14 | | `hh` | Godzina 12h (2 cyfry) | 02 | | `h` | Godzina 12h (1-2 cyfry) | 2 | | `mm` | Minuty (2 cyfry) | 30 | | `m` | Minuty (1-2 cyfry) | 30 | | `ss` | Sekundy (2 cyfry) | 45 | | `s` | Sekundy (1-2 cyfry) | 45 | | `a` | AM/PM | PM | **Przykłady:** ```html {{ date | date }} {{ date | date:'short' }} {{ date | date:'yyyy-MM-dd' }} {{ date | date:'HH:mm:ss' }} {{ date | date:'dd/MM/yyyy' }} {{ date | date:'h:mm a' }} ``` ## Łańcuchowanie Pipes Możesz łączyć wiele pipes w łańcuch: ```html {{ name | lowercase | camelcase }} {{ text | substr:0:20 | uppercase }} {{ value | json | lowercase }} ``` ## Kombinacja z operatorami Pipes działają poprawnie z operatorami logicznymi: ```html {{ value || 'default' | uppercase }} {{ (name || 'Unknown') | pascalcase }} {{ condition && value | lowercase }} ``` ## Tworzenie własnych Pipes ```typescript import { Pipe, PipeTransform } from '@quarc/core'; @Pipe({ name: 'reverse' }) export class ReversePipe implements PipeTransform { transform(value: string | null | undefined): string { if (value == null) return ''; return String(value).split('').reverse().join(''); } } ``` Użycie: ```typescript @Component({ selector: 'app-example', template: '
{{ text | reverse }}
', imports: [ReversePipe], }) export class ExampleComponent { text = 'hello'; // Wyświetli: olleh } ``` ## Testy Wszystkie pipes są przetestowane. Uruchom testy: ```bash cd /web/quarc/tests/unit npx ts-node test-pipes.ts ``` ## Uwagi - Wszystkie pipes obsługują wartości `null` i `undefined` zwracając pusty string - DatePipe obsługuje obiekty `Date`, stringi i liczby (timestamp) - Pipes są transformowane w czasie kompilacji na wywołania metod dla minimalnego rozmiaru bundle - Pipes są pure (czyste) - wynik zależy tylko od argumentów wejściowych