-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(utils): 新增 defaultIndexTo 设置索引的默认值
- Loading branch information
Showing
3 changed files
with
23 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters