Skip to content

Commit

Permalink
feat: add isFinite, isInteger, isNaN
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Oct 11, 2018
1 parent 0a6b0cf commit 070d6cf
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ export { default as inBrowser } from './inBrowser'
export { default as isArray } from './isArray'
export { default as isBoolean } from './isBoolean'
export { default as isDate } from './isDate'
export { default as isFinite } from './isFinite'
export { default as isFunction } from './isFunction'
export { default as isInteger } from './isInteger'
export { default as isNaN } from './isNaN'
export { default as isNil } from './isNil'
export { default as isNull } from './isNull'
export { default as isNumber } from './isNumber'
Expand Down
9 changes: 9 additions & 0 deletions src/isFinite.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* 检查 value 是否是原始有限数值。
*
* @param value 要检查的值
* @returns 是(true)或否(false)
*/
export default function isFinite(value: any): boolean {
return Number.isFinite(value)
}
9 changes: 9 additions & 0 deletions src/isInteger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* 检查 value 是否是一个整数。
*
* @param value 要检查的值
* @returns 是(true)或否(false)
*/
export default function isInteger(value: any): boolean {
return Number.isInteger(value)
}
9 changes: 9 additions & 0 deletions src/isNaN.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* 检查 value 是否是 NaN。
*
* @param value 要检查的值
* @returns 是(true)或否(false)
*/
export default function isNaN(value: any): boolean {
return value !== value
}
41 changes: 41 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,47 @@ describe('isNumber', () => {
})
})

describe('isFinite', () => {
test('是', () => {
expect(vtils.isFinite(0)).toBeTruthy()
expect(vtils.isFinite(-99999999)).toBeTruthy()
expect(vtils.isFinite(6666666666)).toBeTruthy()
expect(vtils.isFinite(Number.MIN_VALUE)).toBeTruthy()
})
test('不是', () => {
expect(vtils.isFinite(Infinity)).toBeFalsy()
expect(vtils.isFinite(Number.NEGATIVE_INFINITY)).toBeFalsy()
expect(vtils.isFinite(NaN)).toBeFalsy()
})
})

describe('isInteger', () => {
test('是', () => {
expect(vtils.isInteger(0)).toBeTruthy()
expect(vtils.isInteger(-99999999)).toBeTruthy()
expect(vtils.isInteger(6666666666)).toBeTruthy()
})
test('不是', () => {
expect(vtils.isInteger(1.2)).toBeFalsy()
expect(vtils.isInteger(Infinity)).toBeFalsy()
expect(vtils.isInteger(Number.NEGATIVE_INFINITY)).toBeFalsy()
expect(vtils.isInteger(NaN)).toBeFalsy()
})
})

describe('isNaN', () => {
test('是', () => {
expect(vtils.isNaN(NaN)).toBeTruthy()
expect(vtils.isNaN(1 / ('x' as any))).toBeTruthy()
})
test('不是', () => {
expect(vtils.isNaN(0)).toBeFalsy()
expect(vtils.isNaN(1.2)).toBeFalsy()
expect(vtils.isNaN(Infinity)).toBeFalsy()
expect(vtils.isNaN(Number.NEGATIVE_INFINITY)).toBeFalsy()
})
})

describe('isBoolean', () => {
test('是', () => {
expect(vtils.isBoolean(true)).toBeTruthy()
Expand Down

0 comments on commit 070d6cf

Please sign in to comment.