-
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.
- Loading branch information
Showing
5 changed files
with
52 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
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,12 @@ | ||
import isArray from './isArray' | ||
import shuffle from './shuffle' | ||
|
||
/** | ||
* 从数组或对象 `value` 中随机获取一个元素。 | ||
* | ||
* @param value 要取样的值 | ||
* @returns 随机元素 | ||
*/ | ||
export default function sample<T>(value: { [key: string]: T } | T[]): T { | ||
return isArray(value) ? shuffle(value)[0] : value[shuffle(Object.keys(value))[0]] | ||
} |
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,14 @@ | ||
/** | ||
* 调用 `fn` n 次,将每次的调用结果存进数组并返回。 | ||
* | ||
* @param n 调用次数 | ||
* @param fn 调用函数 | ||
* @returns 调用结果的数组 | ||
*/ | ||
export default function times<T extends (index: number) => any>(n: number, fn: T): Array<ReturnType<T>> { | ||
const result: any[] = [] | ||
for (let i = 0; i < n; i++) { | ||
result.push(fn(i)) | ||
} | ||
return result | ||
} |
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
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 |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
] | ||
}, | ||
"include": [ | ||
"test", | ||
"src", | ||
"global.d.ts" | ||
], | ||
|