Skip to content
This repository was archived by the owner on Feb 23, 2023. It is now read-only.

Commit 53bf3ad

Browse files
tanayvfknop
authored andcommitted
feat: ordinal pipe (#68)
1 parent 06df035 commit 53bf3ad

File tree

6 files changed

+89
-3
lines changed

6 files changed

+89
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff 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

docs/math.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
```

src/math/math.module.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { RandomPipe } from './random.pipe';
1010
import { SqrtPipe } from './sqrt.pipe';
1111
import { PowPipe } from './pow.pipe';
1212
import { 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
})
4043
export class NgMathPipesModule {}

src/math/ordinal.pipe.spec.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
});

src/math/ordinal.pipe.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
}

src/public_api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export { RandomPipe } from './math/random.pipe';
7979
export { SqrtPipe } from './math/sqrt.pipe';
8080
export { PowPipe } from './math/pow.pipe';
8181
export { AbsPipe } from './math/abs.pipe';
82-
82+
export { OrdinalPipe } from './math/ordinal.pipe';
8383
export { KeysPipe } from './object/keys.pipe';
8484
export { ToArrayPipe } from './object/to-array.pipe';
8585
export { DefaultsPipe } from './object/defaults.pipe';

0 commit comments

Comments
 (0)