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

Commit 1f3c395

Browse files
committed
Merge branch 'master' of github.com:fknop/angular-pipes
2 parents f466bae + 619004e commit 1f3c395

File tree

6 files changed

+4636
-34
lines changed

6 files changed

+4636
-34
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
language: node_js
22
node_js:
3-
- 7
43
- 8
54
- 9
5+
- 10
6+
- 11
67

78
cache:
89
yarn: true

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/bytes.pipe.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,14 @@ export class BytesPipe implements PipeTransform {
4444
}
4545

4646
for (const key in BytesPipe.formats) {
47-
const format = BytesPipe.formats[key];
48-
if (bytes < format.max) {
47+
if (BytesPipe.formats.hasOwnProperty(key)) {
48+
const format = BytesPipe.formats[key];
49+
if (bytes < format.max) {
4950

50-
const result = toDecimal(BytesPipe.calculateResult(format, bytes), decimal);
51+
const result = toDecimal(BytesPipe.calculateResult(format, bytes), decimal);
5152

52-
return BytesPipe.formatResult(result, key);
53+
return BytesPipe.formatResult(result, key);
54+
}
5355
}
5456
}
5557
}
@@ -62,4 +64,4 @@ export class BytesPipe implements PipeTransform {
6264
const prev = format.prev ? BytesPipe.formats[format.prev] : undefined;
6365
return prev ? bytes / prev.max : bytes;
6466
}
65-
}
67+
}

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)