This repository was archived by the owner on Feb 23, 2023. It is now read-only.
File tree Expand file tree Collapse file tree 6 files changed +89
-3
lines changed Expand file tree Collapse file tree 6 files changed +89
-3
lines changed Original file line number Diff line number Diff line change @@ -85,6 +85,7 @@ You can find the documentations in the [`docs`](./docs) folder or on [`GitBook`]
8585* [ ` random ` ] ( ./docs/math.md#random )
8686* [ ` pow ` ] ( ./docs/math.md#pow )
8787* [ ` sqrt ` ] ( ./docs/math.md#sqrt )
88+ * [ ` ordinal ` ] ( ./docs/math.md#ordinal )
8889
8990### Aggregate
9091
Original file line number Diff line number Diff line change @@ -205,3 +205,21 @@ import { AbsPipe } from 'angular-pipes';
205205``` html
206206{{ -2 | abs }} <!-- 2 -->
207207```
208+
209+ #### ordinal
210+
211+ Returns the number with a suffix indicating the ordinal.
212+
213+ ##### File
214+
215+ ``` typescript
216+ import { OrdinalPipe } from ' angular-pipes' ;
217+ ```
218+
219+ ##### Usage
220+
221+ ``` html
222+ {{ 1 | ordinal }} <!-- 1st -->
223+ {{ 523 | ordinal }} <!-- 523rd -->
224+ {{ 15 | ordinal }} <!-- 15th -->
225+ ```
Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ import { RandomPipe } from './random.pipe';
1010import { SqrtPipe } from './sqrt.pipe' ;
1111import { PowPipe } from './pow.pipe' ;
1212import { AbsPipe } from './abs.pipe' ;
13+ import { OrdinalPipe } from './ordinal.pipe' ;
1314
1415@NgModule ( {
1516 declarations : [
@@ -22,7 +23,8 @@ import { AbsPipe } from './abs.pipe';
2223 RandomPipe ,
2324 SqrtPipe ,
2425 PowPipe ,
25- AbsPipe
26+ AbsPipe ,
27+ OrdinalPipe
2628 ] ,
2729 exports : [
2830 BytesPipe ,
@@ -34,7 +36,8 @@ import { AbsPipe } from './abs.pipe';
3436 RandomPipe ,
3537 SqrtPipe ,
3638 PowPipe ,
37- AbsPipe
39+ AbsPipe ,
40+ OrdinalPipe
3841 ]
3942} )
4043export class NgMathPipesModule { }
Original file line number Diff line number Diff line change 1+ import { OrdinalPipe } from './ordinal.pipe' ;
2+
3+ describe ( 'OrdinalPipe' , ( ) => {
4+
5+ let pipe : OrdinalPipe ;
6+
7+ beforeEach ( ( ) => {
8+ pipe = new OrdinalPipe ( ) ;
9+ } ) ;
10+
11+ it ( 'Should return 123rd' , ( ) => {
12+
13+ expect ( pipe . transform ( 123 ) ) . toEqual ( '123rd' ) ;
14+ } ) ;
15+
16+ it ( 'Should return 221st' , ( ) => {
17+
18+ expect ( pipe . transform ( 221 ) ) . toEqual ( '221st' ) ;
19+ } ) ;
20+
21+ it ( 'Should return 2nd' , ( ) => {
22+
23+ expect ( pipe . transform ( 2 ) ) . toEqual ( '2nd' ) ;
24+ } ) ;
25+
26+ it ( 'Should return 15th' , ( ) => {
27+
28+ expect ( pipe . transform ( 15 ) ) . toEqual ( '15th' ) ;
29+ } ) ;
30+
31+ it ( 'Should return NaN' , ( ) => {
32+ expect ( pipe . transform ( 'a' ) ) . toEqual ( 'NaN' ) ;
33+ } ) ;
34+
35+ } ) ;
Original file line number Diff line number Diff line change 1+ import { Pipe , PipeTransform } from '@angular/core' ;
2+ import { isNumberFinite } from '../utils/utils' ;
3+
4+ @Pipe ( {
5+ name : 'ordinal'
6+ } )
7+ export class OrdinalPipe implements PipeTransform {
8+
9+ transform ( input : any ) : any {
10+
11+ if ( ! isNumberFinite ( input ) ) {
12+ return 'NaN' ;
13+ }
14+
15+ const cardinal = input . toString ( ) . charAt ( input . toString ( ) . length - 1 ) ;
16+
17+ switch ( cardinal ) {
18+ case '1' :
19+ return input + 'st' ;
20+ case '2' :
21+ return input + 'nd' ;
22+ case '3' :
23+ return input + 'rd' ;
24+ default :
25+ return input + 'th' ;
26+ }
27+
28+ }
29+ }
Original file line number Diff line number Diff line change @@ -79,7 +79,7 @@ export { RandomPipe } from './math/random.pipe';
7979export { SqrtPipe } from './math/sqrt.pipe' ;
8080export { PowPipe } from './math/pow.pipe' ;
8181export { AbsPipe } from './math/abs.pipe' ;
82-
82+ export { OrdinalPipe } from './math/ordinal.pipe' ;
8383export { KeysPipe } from './object/keys.pipe' ;
8484export { ToArrayPipe } from './object/to-array.pipe' ;
8585export { DefaultsPipe } from './object/defaults.pipe' ;
You can’t perform that action at this time.
0 commit comments