Created
April 9, 2020 13:46
-
-
Save Qarun-Qadir-Bissoondial/9fee700c7db95b82da904da621c054a0 to your computer and use it in GitHub Desktop.
Pipes Demo - Order Suffix Pipe (Part 3)
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
import { Pipe, PipeTransform } from '@angular/core'; | |
@Pipe({ | |
name: 'orderSuffix' | |
}) | |
export class OrderSuffixPipe implements PipeTransform { | |
suffixMap = { | |
1: 'st', | |
2: 'nd', | |
3: 'rd' | |
}; | |
edgeCases = [11, 12, 13]; | |
transform(value: number, args?: any): string { | |
const lastTwoDigits = value % 100; | |
if (this.edgeCases.includes(lastTwoDigits)) { | |
return `${value}th` | |
} | |
const lastDigit = value % 10; | |
const suffix = this.suffixMap[lastDigit]; | |
return !suffix | |
? `${value}th` | |
: `${value}${suffix}`; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment