Skip to content

Commit

Permalink
feat(utils): 新增 sampleBy, sampleIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Feb 19, 2021
1 parent c88ffa3 commit 7df6ac8
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/date/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ export {
lastDayOfYear,
lightFormat,
max,
milliseconds,
min,
parse,
parseISO,
Expand Down Expand Up @@ -235,6 +236,7 @@ export {
enIN,
enNZ,
enUS,
enZA,
eo,
es,
et,
Expand Down
2 changes: 2 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ export * from './readFile'
export * from './RichUrl'
export * from './rot13'
export * from './run'
export * from './sampleBy'
export * from './sampleIndex'
export * from './swap'
export * from './traverse'
export * from './TreeData'
Expand Down
12 changes: 12 additions & 0 deletions src/utils/sampleBy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { nthArg } from 'lodash-uni'
import { sampleBy } from './sampleBy'

describe('sampleBy', () => {
test('ok', () => {
expect(sampleBy([1, 2], (_, i) => i)).toBeOneOf([0, 1])
expect(sampleBy([1, 2], nthArg(1))).toBeOneOf([0, 1])
expect(sampleBy([], (_, i) => i)).toBeUndefined()
expect(sampleBy({ x: 1, y: 'dd' }, (_, k) => k)).toBeOneOf(['x', 'y'])
expect(sampleBy({}, (_, k) => k)).toBeUndefined()
})
})
26 changes: 26 additions & 0 deletions src/utils/sampleBy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { forOwn, isArray, isObject, sample } from 'lodash-uni'

/**
* 从集合中随机获得一个元素的迭代值。
*
* @param collection 集合
* @param iteratee 迭代函数
*/
export function sampleBy<T, X>(
collection: T[],
iteratee: (element: T, index: number) => X,
): X | undefined
export function sampleBy<T extends Record<any, any>, X>(
collection: T,
iteratee: <K extends keyof T>(value: T[K], key: K) => X,
): X | undefined
export function sampleBy(collection: any, iteratee: any): any {
if (isArray(collection)) {
return sample(collection.map(iteratee))
} else if (isObject(collection)) {
const values: any[] = []
forOwn(collection, (v, k) => values.push(iteratee(v, k)))
return sample(values)
}
return undefined
}
10 changes: 10 additions & 0 deletions src/utils/sampleIndex.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { sampleIndex } from './sampleIndex'

describe('sampleIndex', () => {
test('ok', () => {
expect(sampleIndex([1, 2])).toBeOneOf([0, 1])
expect(sampleIndex([])).toBeUndefined()
expect(sampleIndex({ x: 1, y: 'o' })).toBeOneOf(['x', 'y'])
expect(sampleIndex({})).toBeUndefined()
})
})
15 changes: 15 additions & 0 deletions src/utils/sampleIndex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { nthArg } from 'lodash-uni'
import { sampleBy } from './sampleBy'

/**
* 从集合中随机获得一个元素的索引(数组)或键(对象)。
*
* @param collection 集合
*/
export function sampleIndex<T>(collection: T[]): number | undefined
export function sampleIndex<T extends Record<any, any>>(
collection: T,
): keyof T | undefined
export function sampleIndex(collection: any): any {
return sampleBy(collection, nthArg(1))
}

0 comments on commit 7df6ac8

Please sign in to comment.