Implement isNumber pipe

This commit is contained in:
shamoon committed 2023-08-12 08:31:42 -07:00
1 parent 0b4e8141b0
commit 3dda02660c
8 files changed
+33 -3

No files matched your search

@@ -0,0 +1,13 @@
import { IsNumberPipe } from './is-number.pipe'
describe('IsNumberPipe', () => {
it('should detect numbers', () => {
const pipe = new IsNumberPipe()
expect(pipe.transform(0)).toBeTruthy()
expect(pipe.transform(123)).toBeTruthy()
expect(pipe.transform('123')).toBeFalsy()
expect(pipe.transform(null)).toBeFalsy()
expect(pipe.transform(undefined)).toBeFalsy()
expect(pipe.transform(NaN)).toBeFalsy()
})
})
+10
View File
@@ -0,0 +1,10 @@
import { Pipe, PipeTransform } from '@angular/core'
@Pipe({
name: 'isNumber',
})
export class IsNumberPipe implements PipeTransform {
transform(value: any): boolean {
return typeof value === 'number' && !isNaN(value)
}
}