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

Commit b2f5d37

Browse files
thelebdevfknop
authored andcommitted
Fix tenth ordinal (#84)
* chore: yarn file * refactor: spacing refactor * chore: helper function to check if the number ends with tenth * chore: add test cases for numbers ending with a 10th * chore: add functionality to check for 10th number before going into switch cases * add code from existing PR with updated versions * add missing link to math.md doc * test case for switch case default
1 parent 549d277 commit b2f5d37

File tree

4 files changed

+4627
-28
lines changed

4 files changed

+4627
-28
lines changed

docs/math.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* [`pow`](#pow)
1111
* [`sqrt`](#sqrt)
1212
* [`abs`](#abs)
13+
* [`ordinal`](#ordinal)
1314

1415
You can check the module import [`here`](./modules.md).
1516

src/math/ordinal.pipe.spec.ts

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,54 @@
11
import { OrdinalPipe } from './ordinal.pipe';
22

33
describe('OrdinalPipe', () => {
4-
4+
55
let pipe: OrdinalPipe;
6-
6+
77
beforeEach(() => {
8-
pipe = new OrdinalPipe();
8+
pipe = new OrdinalPipe();
99
});
10-
10+
1111
it('Should return 123rd', () => {
12-
12+
1313
expect(pipe.transform(123)).toEqual('123rd');
1414
});
1515

1616
it('Should return 221st', () => {
17-
17+
1818
expect(pipe.transform(221)).toEqual('221st');
1919
});
20-
20+
2121
it('Should return 2nd', () => {
22-
23-
expect(pipe.transform(2)).toEqual('2nd');
22+
23+
expect(pipe.transform(2)).toEqual('2nd');
2424
});
2525

2626
it('Should return 15th', () => {
27-
28-
expect(pipe.transform(15)).toEqual('15th');
27+
28+
expect(pipe.transform(15)).toEqual('15th');
29+
});
30+
31+
it('Should return 11th', () => {
32+
33+
expect(pipe.transform(11)).toEqual('11th');
34+
});
35+
36+
it('Should return 12th', () => {
37+
38+
expect(pipe.transform(12)).toEqual('12th');
39+
});
40+
41+
it('Should return 133313th', () => {
42+
43+
expect(pipe.transform(133313)).toEqual('133313th');
2944
});
3045

3146
it('Should return NaN', () => {
3247
expect(pipe.transform('a')).toEqual('NaN');
3348
});
34-
49+
50+
it('Should return 35th', () => {
51+
expect(pipe.transform(35)).toEqual('35th');
52+
});
53+
3554
});

src/math/ordinal.pipe.ts

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,38 @@
1-
import { Pipe, PipeTransform } from '@angular/core';
1+
import { Pipe, PipeTransform } from '@angular/core';
22
import { isNumberFinite } from '../utils/utils';
33

44
@Pipe({
5-
name: 'ordinal'
5+
name: 'ordinal'
66
})
77
export class OrdinalPipe implements PipeTransform {
8-
9-
transform (input: any): any {
8+
9+
transform(input: any): any {
1010

1111
if (!isNumberFinite(input)) {
1212
return 'NaN';
1313
}
1414

15-
const cardinal = input.toString().charAt(input.toString().length - 1);
15+
if (this.endsWithTenth(input)) {
16+
return input + 'th';
17+
} else {
18+
const cardinal = input.toString().charAt(input.toString().length - 1);
1619

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-
20+
switch (cardinal) {
21+
case '1':
22+
return input + 'st';
23+
case '2':
24+
return input + 'nd';
25+
case '3':
26+
return input + 'rd';
27+
default:
28+
return input + 'th';
29+
}
30+
}
31+
}
32+
33+
private endsWithTenth(input: any): boolean {
34+
const beforeLastDigit = input.toString().charAt(input.toString().length - 2);
35+
36+
return beforeLastDigit === '1';
2837
}
2938
}

0 commit comments

Comments
 (0)