Skip to content

Commit

Permalink
feat: add getType, isArray, isBoolean, isDate, isNull, isNumber, isOb…
Browse files Browse the repository at this point in the history
…ject, isRegExp, isString, isUndefined
  • Loading branch information
fjc0k committed Oct 11, 2018
1 parent 40f7182 commit 73e517e
Show file tree
Hide file tree
Showing 12 changed files with 243 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/getType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* 检测 value 值的类型。
*
* @export
* @param value 要检测的值
* @returns 检测值的类型
*/
export default function getType(value: any): string {
return Object.prototype.toString.call(value).split(' ')[1].replace(']', '')
}
10 changes: 10 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,19 @@ export { default as castArray } from './castArray'
export { default as clamp } from './clamp'
export { default as Disposer } from './Disposer'
export { default as forOwn } from './forOwn'
export { default as getType } from './getType'
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 isFunction } from './isFunction'
export { default as isNil } from './isNil'
export { default as isNull } from './isNull'
export { default as isNumber } from './isNumber'
export { default as isObject } from './isObject'
export { default as isRegExp } from './isRegExp'
export { default as isString } from './isString'
export { default as isUndefined } from './isUndefined'
export { default as noop } from './noop'
export { default as reduce } from './reduce'
export { default as repeat } from './repeat'
9 changes: 9 additions & 0 deletions src/isArray.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* 检查 value 是否是一个数组。
*
* @param value 要检查的值
* @returns 是(true)或否(false)
*/
export default function isArray(value: any): value is any[] {
return Array.isArray(value)
}
9 changes: 9 additions & 0 deletions src/isBoolean.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* 检查 value 是否是一个布尔值。
*
* @param value 要检查的值
* @returns 是(true)或否(false)
*/
export default function isBoolean(value: any): value is boolean {
return typeof value === 'boolean'
}
11 changes: 11 additions & 0 deletions src/isDate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import getType from './getType'

/**
* 检查 value 是否是一个日期。
*
* @param value 要检查的值
* @returns 是(true)或否(false)
*/
export default function isDate(value: any): value is Date {
return getType(value) === 'Date'
}
9 changes: 9 additions & 0 deletions src/isNull.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* 检查 value 是否等于 null。
*
* @param value 要检查的值
* @returns 是(true)或否(false)
*/
export default function isNull(value: any): value is null {
return value === null
}
9 changes: 9 additions & 0 deletions src/isNumber.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* 检查 value 是否是一个数值。
*
* @param value 要检查的值
* @returns 是(true)或否(false)
*/
export default function isNumber(value: any): value is number {
return typeof value === 'number'
}
10 changes: 10 additions & 0 deletions src/isObject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* 检查 value 是否是一个对象。
*
* @param value 要检查的值
* @returns 是(true)或否(false)
*/
export default function isObject(value: any): value is object {
const type = typeof value
return value != null && (type === 'object' || type === 'function')
}
11 changes: 11 additions & 0 deletions src/isRegExp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import getType from './getType'

/**
* 检查 value 是否是一个正则表达式。
*
* @param value 要检查的值
* @returns 是(true)或否(false)
*/
export default function isRegExp(value: any): value is RegExp {
return getType(value) === 'RegExp'
}
9 changes: 9 additions & 0 deletions src/isString.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* 检查 value 是否是一个字符串。
*
* @param value 要检查的值
* @returns 是(true)或否(false)
*/
export default function isString(value: any): value is string {
return typeof value === 'string'
}
9 changes: 9 additions & 0 deletions src/isUndefined.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* 检查 value 是否等于 undefined。
*
* @param value 要检查的值
* @returns 是(true)或否(false)
*/
export default function isUndefined(value: any): value is undefined {
return value === void 0
}
137 changes: 137 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,74 @@ describe('inBrowser', () => {
})
})

describe('getType', () => {
test('正确返回类型', () => {
expect(vtils.getType('')).toBe('String')
expect(vtils.getType(1)).toBe('Number')
expect(vtils.getType({})).toBe('Object')
expect(vtils.getType(Object.create(null))).toBe('Object')
expect(vtils.getType(new Date())).toBe('Date')
expect(vtils.getType(/X/)).toBe('RegExp')
expect(vtils.getType(false)).toBe('Boolean')
expect(vtils.getType(null)).toBe('Null')
expect(vtils.getType(undefined)).toBe('Undefined')
})
})

describe('isString', () => {
test('是', () => {
expect(vtils.isString('')).toBeTruthy()
expect(vtils.isString('hello')).toBeTruthy()
expect(vtils.isString(String(1))).toBeTruthy()
})
test('不是', () => {
expect(vtils.isString({})).toBeFalsy()
expect(vtils.isString(2)).toBeFalsy()
expect(vtils.isString(/.+/)).toBeFalsy()
expect(vtils.isString(null)).toBeFalsy()
})
})

describe('isNumber', () => {
test('是', () => {
expect(vtils.isNumber(0)).toBeTruthy()
expect(vtils.isNumber(Infinity)).toBeTruthy()
expect(vtils.isNumber(NaN)).toBeTruthy()
})
test('不是', () => {
expect(vtils.isNumber('3')).toBeFalsy()
expect(vtils.isNumber({})).toBeFalsy()
expect(vtils.isNumber(/.+/)).toBeFalsy()
expect(vtils.isNumber(null)).toBeFalsy()
})
})

describe('isBoolean', () => {
test('是', () => {
expect(vtils.isBoolean(true)).toBeTruthy()
expect(vtils.isBoolean(false)).toBeTruthy()
})
test('不是', () => {
expect(vtils.isBoolean('3')).toBeFalsy()
expect(vtils.isBoolean({})).toBeFalsy()
expect(vtils.isBoolean(/.+/)).toBeFalsy()
expect(vtils.isBoolean(null)).toBeFalsy()
})
})

describe('isArray', () => {
test('是', () => {
expect(vtils.isArray([])).toBeTruthy()
expect(vtils.isArray(Array(1))).toBeTruthy()
})
test('不是', () => {
expect(vtils.isArray({})).toBeFalsy()
expect(vtils.isArray(2)).toBeFalsy()
expect(vtils.isArray(/.+/)).toBeFalsy()
expect(vtils.isArray(null)).toBeFalsy()
})
})

describe('isFunction', () => {
test('是', () => {
expect(vtils.isFunction(() => ({}))).toBeTruthy()
Expand All @@ -211,6 +279,75 @@ describe('isFunction', () => {
})
})

describe('isObject', () => {
test('是', () => {
expect(vtils.isObject({})).toBeTruthy()
expect(vtils.isObject(() => ({}))).toBeTruthy()
expect(vtils.isObject(Date)).toBeTruthy()
expect(vtils.isObject(/X/)).toBeTruthy()
})
test('不是', () => {
expect(vtils.isObject('str')).toBeFalsy()
expect(vtils.isObject(2)).toBeFalsy()
expect(vtils.isObject(null)).toBeFalsy()
})
})

describe('isDate', () => {
test('是', () => {
expect(vtils.isDate(now)).toBeTruthy()
expect(vtils.isDate(new Date())).toBeTruthy()
})
test('不是', () => {
expect(vtils.isDate('str')).toBeFalsy()
expect(vtils.isDate(2)).toBeFalsy()
expect(vtils.isDate(null)).toBeFalsy()
expect(vtils.isDate({})).toBeFalsy()
})
})

describe('isRegExp', () => {
test('是', () => {
expect(vtils.isRegExp(/x/)).toBeTruthy()
expect(vtils.isRegExp(new RegExp('xxx'))).toBeTruthy()
})
test('不是', () => {
expect(vtils.isRegExp('str')).toBeFalsy()
expect(vtils.isRegExp(2)).toBeFalsy()
expect(vtils.isRegExp(null)).toBeFalsy()
expect(vtils.isRegExp({})).toBeFalsy()
})
})

describe('isNull', () => {
test('是', () => {
expect(vtils.isNull(null)).toBeTruthy()
})
test('不是', () => {
expect(vtils.isNull(undefined)).toBeFalsy()
expect(vtils.isNull('')).toBeFalsy()
expect(vtils.isNull(0)).toBeFalsy()
expect(vtils.isNull(false)).toBeFalsy()
expect(vtils.isNull({})).toBeFalsy()
expect(vtils.isNull(/X/)).toBeFalsy()
})
})

describe('isUndefined', () => {
test('是', () => {
expect(vtils.isUndefined(undefined)).toBeTruthy()
expect(vtils.isUndefined(void 0)).toBeTruthy()
})
test('不是', () => {
expect(vtils.isUndefined('')).toBeFalsy()
expect(vtils.isUndefined(0)).toBeFalsy()
expect(vtils.isUndefined(false)).toBeFalsy()
expect(vtils.isUndefined({})).toBeFalsy()
expect(vtils.isUndefined(/X/)).toBeFalsy()
expect(vtils.isUndefined(null)).toBeFalsy()
})
})

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

0 comments on commit 73e517e

Please sign in to comment.