Skip to content

Commit

Permalink
feat(utils): 新增 defaultIndexTo 设置索引的默认值
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Sep 24, 2021
1 parent 0dddb40 commit 7b2f359
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/utils/defaultIndexTo.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { defaultIndexTo } from './defaultIndexTo'

describe('defaultIndexTo', () => {
test('ok', () => {
expect(defaultIndexTo(-1, 1)).toBe(1)
expect(defaultIndexTo(-1, 0)).toBe(0)
expect(defaultIndexTo(-2, 0)).toBe(0)
expect(defaultIndexTo(NaN, 1)).toBe(1)
expect(defaultIndexTo(NaN, 0)).toBe(0)
expect(defaultIndexTo(1, 0)).toBe(1)
expect(defaultIndexTo(2, 1)).toBe(2)
})
})
9 changes: 9 additions & 0 deletions src/utils/defaultIndexTo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* 设置默认索引。当前索引为 `-1` 或 `NaN` 时会使用默认索引。
*
* @param index 当前索引
* @param defaultIndex 默认索引
*/
export function defaultIndexTo(index: number, defaultIndex: number): number {
return index < 0 || index !== index ? defaultIndex : index
}
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export * from './createSubmit'
export * from './createUrlQueryString'
export * from './DataPacker'
export * from './dedent'
export * from './defaultIndexTo'
export * from './EventBus'
export * from './formatBytes'
export * from './formatNumber'
Expand Down

0 comments on commit 7b2f359

Please sign in to comment.