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

Commit d41f56d

Browse files
baso10fknop
authored andcommitted
feat: implement Math.abs pipe (#64)
1 parent 13a09fe commit d41f56d

File tree

5 files changed

+67
-2
lines changed

5 files changed

+67
-2
lines changed

docs/math.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* [`random`](#random)
1010
* [`pow`](#pow)
1111
* [`sqrt`](#sqrt)
12+
* [`abs`](#abs)
1213

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

@@ -188,3 +189,19 @@ import { PowPipe } from 'angular-pipes';
188189
{{ 2 | pow }} <!-- 4 -->
189190
{{ 2 | pow: 3 }} <!-- 8 -->
190191
```
192+
193+
#### abs
194+
195+
Returns the absolute of a number.
196+
197+
##### File
198+
199+
```typescript
200+
import { AbsPipe } from 'angular-pipes';
201+
```
202+
203+
##### Usage
204+
205+
```html
206+
{{ -2 | abs }} <!-- 2 -->
207+
```

src/math/abs.pipe.spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { AbsPipe } from './abs.pipe';
2+
3+
4+
5+
describe('AbsPipe', () => {
6+
7+
let pipe: AbsPipe;
8+
9+
beforeEach(() => {
10+
pipe = new AbsPipe();
11+
});
12+
13+
it('Should return 2', () => {
14+
15+
expect(pipe.transform(2)).toEqual(2);
16+
});
17+
18+
it('Should return 2', () => {
19+
20+
expect(pipe.transform(-2)).toEqual(2);
21+
});
22+
23+
it('Should return NaN', () => {
24+
expect(pipe.transform('a')).toEqual('NaN');
25+
});
26+
27+
});

src/math/abs.pipe.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Pipe, PipeTransform } from '@angular/core';
2+
import { isNumberFinite } from '../utils/utils';
3+
4+
@Pipe({
5+
name: 'abs'
6+
})
7+
export class AbsPipe implements PipeTransform {
8+
9+
transform (input: any): any {
10+
11+
if (!isNumberFinite(input)) {
12+
return 'NaN';
13+
}
14+
15+
return Math.abs(input);
16+
}
17+
}

src/math/math.module.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { RadiansPipe } from './radians.pipe';
99
import { RandomPipe } from './random.pipe';
1010
import { SqrtPipe } from './sqrt.pipe';
1111
import { PowPipe } from './pow.pipe';
12+
import { AbsPipe } from './abs.pipe';
1213

1314
@NgModule({
1415
declarations: [
@@ -20,7 +21,8 @@ import { PowPipe } from './pow.pipe';
2021
RadiansPipe,
2122
RandomPipe,
2223
SqrtPipe,
23-
PowPipe
24+
PowPipe,
25+
AbsPipe
2426
],
2527
exports: [
2628
BytesPipe,
@@ -31,7 +33,8 @@ import { PowPipe } from './pow.pipe';
3133
RadiansPipe,
3234
RandomPipe,
3335
SqrtPipe,
34-
PowPipe
36+
PowPipe,
37+
AbsPipe
3538
]
3639
})
3740
export class NgMathPipesModule {}

src/public_api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ export { RadiansPipe } from './math/radians.pipe';
7878
export { RandomPipe } from './math/random.pipe';
7979
export { SqrtPipe } from './math/sqrt.pipe';
8080
export { PowPipe } from './math/pow.pipe';
81+
export { AbsPipe } from './math/abs.pipe';
8182

8283
export { KeysPipe } from './object/keys.pipe';
8384
export { ToArrayPipe } from './object/to-array.pipe';

0 commit comments

Comments
 (0)