Skip to content

Commit

Permalink
feat: add isNil
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Oct 10, 2018
1 parent 266b56e commit 7fe8199
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export { default as clamp } from './clamp'
export { default as Disposer } from './Disposer'
export { default as inBrowser } from './inBrowser'
export { default as isFunction } from './isFunction'
export { default as isNil } from './isNil'
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/isNil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* 检查 value 是否是 null 或 undefined。
*
* @param value 要检查的值
* @returns 是(true)或否(false)
*/
export default function isNil(value: any): boolean {
return value == null
}
41 changes: 28 additions & 13 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,6 @@ describe('reduce', () => {
})
})

describe('isFunction', () => {
test('是函数', () => {
expect(vtils.isFunction(() => ({}))).toBeTruthy()
expect(vtils.isFunction(now.getDate)).toBeTruthy()
})
test('不是函数', () => {
expect(vtils.isFunction({})).toBeFalsy()
expect(vtils.isFunction(2)).toBeFalsy()
expect(vtils.isFunction(/.+/)).toBeFalsy()
expect(vtils.isFunction(null)).toBeFalsy()
})
})

describe('repeat', () => {
test('空字符串', () => {
expect(vtils.repeat('')).toBe('')
Expand Down Expand Up @@ -200,3 +187,31 @@ describe('Disposer', () => {
expect((disposer as any).jar).toEqual({})
})
})

describe('isFunction', () => {
test('是', () => {
expect(vtils.isFunction(() => ({}))).toBeTruthy()
expect(vtils.isFunction(now.getDate)).toBeTruthy()
})
test('不是', () => {
expect(vtils.isFunction({})).toBeFalsy()
expect(vtils.isFunction(2)).toBeFalsy()
expect(vtils.isFunction(/.+/)).toBeFalsy()
expect(vtils.isFunction(null)).toBeFalsy()
})
})

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

0 comments on commit 7fe8199

Please sign in to comment.