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`]
85
85
* [ ` random ` ] ( ./docs/math.md#random )
86
86
* [ ` pow ` ] ( ./docs/math.md#pow )
87
87
* [ ` sqrt ` ] ( ./docs/math.md#sqrt )
88
+ * [ ` ordinal ` ] ( ./docs/math.md#ordinal )
88
89
89
90
### Aggregate
90
91
Original file line number Diff line number Diff line change @@ -205,3 +205,21 @@ import { AbsPipe } from 'angular-pipes';
205
205
``` html
206
206
{{ -2 | abs }} <!-- 2 -->
207
207
```
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';
10
10
import { SqrtPipe } from './sqrt.pipe' ;
11
11
import { PowPipe } from './pow.pipe' ;
12
12
import { AbsPipe } from './abs.pipe' ;
13
+ import { OrdinalPipe } from './ordinal.pipe' ;
13
14
14
15
@NgModule ( {
15
16
declarations : [
@@ -22,7 +23,8 @@ import { AbsPipe } from './abs.pipe';
22
23
RandomPipe ,
23
24
SqrtPipe ,
24
25
PowPipe ,
25
- AbsPipe
26
+ AbsPipe ,
27
+ OrdinalPipe
26
28
] ,
27
29
exports : [
28
30
BytesPipe ,
@@ -34,7 +36,8 @@ import { AbsPipe } from './abs.pipe';
34
36
RandomPipe ,
35
37
SqrtPipe ,
36
38
PowPipe ,
37
- AbsPipe
39
+ AbsPipe ,
40
+ OrdinalPipe
38
41
]
39
42
} )
40
43
export 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';
79
79
export { SqrtPipe } from './math/sqrt.pipe' ;
80
80
export { PowPipe } from './math/pow.pipe' ;
81
81
export { AbsPipe } from './math/abs.pipe' ;
82
-
82
+ export { OrdinalPipe } from './math/ordinal.pipe' ;
83
83
export { KeysPipe } from './object/keys.pipe' ;
84
84
export { ToArrayPipe } from './object/to-array.pipe' ;
85
85
export { DefaultsPipe } from './object/defaults.pipe' ;
You can’t perform that action at this time.
0 commit comments