Skip to content

Instantly share code, notes, and snippets.

@ziuniecki
Last active July 20, 2020 11:26
Show Gist options
  • Save ziuniecki/56d3767dcebab59e33542839bd05e409 to your computer and use it in GitHub Desktop.
Save ziuniecki/56d3767dcebab59e33542839bd05e409 to your computer and use it in GitHub Desktop.
HowMuchPipe
import {Pipe, PipeTransform} from '@angular/core';
/**
* Pipe do określania słów jakie mają się pojawić po cyfrze np: 1 marka; 2 marki; 12 marek
* @example
* {{ 20 | howMuch:['marka', 'marki', 'marek']}}
*/
@Pipe({name: 'howMuch'})
export class HowMuchPipe implements PipeTransform {
/**
* Funkcja wykorzystywana przez Pipe
* @param count - liczba do której słowo się dostosowuje.
* @param words - np ['marka', 'marki', 'marek'].
*/
transform(count: number, words: [string, string, string]) {
if (count === 1) {
return 1 ${words[0]};
} else if ((count % 100 !== 12 && count % 10 === 2) || (count % 100 !== 13 && count % 10 === 3) ||
(count % 100 !== 14 && count % 10 === 4)) {
return ${count} ${words[1]};
} else {
return ${count} ${words[2]};
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment