Skip to content

Commit

Permalink
feat: add times, sample
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Nov 9, 2018
1 parent 5cc58d9 commit 6d70aee
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,14 @@ export { default as reduce } from './reduce'
export { default as repeat } from './repeat'
export { default as request } from './request'
export { default as result } from './result'
export { default as sample } from './sample'
export { default as set } from './set'
export { default as shuffle } from './shuffle'
export { default as startsWith } from './startsWith'
export { default as stopEventPropagation } from './stopEventPropagation'
export { default as storage } from './storage'
export { default as supportPassiveEventListener } from './supportPassiveEventListener'
export { default as times } from './times'
export { default as toDate } from './toDate'
export { default as toPath } from './toPath'
export { default as upperCaseFirst } from './upperCaseFirst'
Expand Down
12 changes: 12 additions & 0 deletions src/sample.ts
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]]
}
14 changes: 14 additions & 0 deletions src/times.ts
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
}
23 changes: 23 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1280,3 +1280,26 @@ describe('range', () => {
expect(vtils.range(-3)).toEqual([-3, -2, -1])
})
})

describe('times', () => {
test('ok', () => {
expect(vtils.times(4, () => 1)).toEqual([1, 1, 1, 1])
expect(vtils.times(4, index => index)).toEqual([0, 1, 2, 3])
expect(vtils.times(0, index => index)).toEqual([])
})
})

describe('sample', () => {
test('数组', () => {
const arr: any[] = [1, '', false, null]
vtils.times(1000, () => {
expect(arr).toContainEqual(vtils.sample(arr))
})
})
test('对象', () => {
const obj: object = { 1: '3', x: null, '-': '@' }
vtils.times(1000, () => {
expect(Object.values(obj)).toContainEqual(vtils.sample(obj))
})
})
})
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
]
},
"include": [
"test",
"src",
"global.d.ts"
],
Expand Down

0 comments on commit 6d70aee

Please sign in to comment.