Last active
July 20, 2020 11:26
-
-
Save ziuniecki/56d3767dcebab59e33542839bd05e409 to your computer and use it in GitHub Desktop.
HowMuchPipe
This file contains hidden or 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
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