Skip to content

Commit

Permalink
feat: add isNumeric
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Oct 30, 2018
1 parent 957efc4 commit 533bc2f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export { default as Disposer } from './Disposer'
export { default as base64Decode } from './base64Decode'
export { default as base64Encode } from './base64Encode'
export { default as base64UrlDecode } from './base64UrlDecode'
Expand All @@ -8,6 +7,7 @@ export { default as castArray } from './castArray'
export { default as clamp } from './clamp'
export { default as cssTransform } from './cssTransform'
export { default as currencyFormat } from './currencyFormat'
export { default as Disposer } from './Disposer'
export { default as fill } from './fill'
export { default as forOwn } from './forOwn'
export { default as getType } from './getType'
Expand All @@ -26,6 +26,7 @@ export { default as isNaN } from './isNaN'
export { default as isNil } from './isNil'
export { default as isNull } from './isNull'
export { default as isNumber } from './isNumber'
export { default as isNumeric } from './isNumeric'
export { default as isObject } from './isObject'
export { default as isPlainObject } from './isPlainObject'
export { default as isPromise } from './isPromise'
Expand Down
2 changes: 1 addition & 1 deletion src/isNumber.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* 检查 value 是否是一个数值
* 检查 value 是否是一个数字
*
* @param value 要检查的值
* @returns 是(true)或否(false)
Expand Down
9 changes: 9 additions & 0 deletions src/isNumeric.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* 检查 value 是否是一个数值。
*
* @param value 要检查的值
* @returns 是(true)或否(false)
*/
export default function isNumeric(value: any): boolean {
return value != null && !isNaN(value - parseFloat(value))
}
13 changes: 13 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -792,3 +792,16 @@ describe('currencyFormat', () => {
expect(vtils.currencyFormat(840224, { decimal: false })).toBe('840,224')
})
})

describe('isNumeric', () => {
test('是', () => {
[123, -22, '223', '-244.3', 0xFF, '0xFF', '8e5', 0o144, '0144'].forEach(item => {
expect(vtils.isNumeric(item)).toBeTruthy()
})
})
test('不是', () => {
[undefined, Infinity, true, null, NaN, {}, '', '7.2acdgs', '-0x42'].forEach(item => {
expect(vtils.isNumeric(item)).toBeFalsy()
})
})
})

0 comments on commit 533bc2f

Please sign in to comment.