Skip to content

Commit

Permalink
feat(react): 新增 isVisibleValue
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Jul 20, 2020
1 parent a9c95d7 commit a368e51
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/react/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export * from 'react-use'

// @index(['./**/*.ts', '!./**/*.{test,taro}.*', '!./{useToggle,createGlobalState,useTitle,useInterval,useSearchParam,useLocalStorage,useWindowSize}.*'], f => `export * from '${f.path}'`)
export * from './defineComponent'
export * from './isVisibleValue'
export * from './useClassName'
export * from './useLoadMore'
export * from './useReachBottom'
Expand Down
18 changes: 18 additions & 0 deletions src/react/isVisibleValue.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { isVisibleValue } from './isVisibleValue'

describe('isVisibleValue', () => {
test('不是', () => {
expect(isVisibleValue(undefined)).toBeFalse()
expect(isVisibleValue(null)).toBeFalse()
expect(isVisibleValue(false)).toBeFalse()
expect(isVisibleValue(true)).toBeFalse()
expect(isVisibleValue('')).toBeFalse()
})

test('是', () => {
expect(isVisibleValue(0)).toBeTrue()
expect(isVisibleValue('1')).toBeTrue()
expect(isVisibleValue({})).toBeTrue()
expect(isVisibleValue([])).toBeTrue()
})
})
15 changes: 15 additions & 0 deletions src/react/isVisibleValue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* 是否是渲染后可见的值。
* 渲染后不可见的值包括:`undefined`、`null`、`true`、`false`、空字符串。
*
* @param value 值
* @returns 返回结果
* ```typescript
* isVisibleValue(null) // => false
* isVisibleValue(0) // => true
* ```
*/
export function isVisibleValue(value: any): boolean {
// ref: https://reactjs.org/docs/jsx-in-depth.html#booleans-null-and-undefined-are-ignored
return value != null && value !== true && value !== false && value !== ''
}

0 comments on commit a368e51

Please sign in to comment.